Skip to content

Commit

Permalink
rename cmds (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
ringsaturn authored Jun 12, 2024
1 parent 86793ac commit a5a9dbd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Output:
`FromGeo`

```bash
placekey FromGeo -lat 40.71237820442784 -long -74.0056425771711
placekey FromGeo -lat 40.71237820442784 -lng -74.0056425771711
```

Output:
Expand Down
8 changes: 4 additions & 4 deletions cmd/placekey/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
)

const (
FromGeoFlag = "FromGeo"
ToGeoFlag = "ToGeo"
FromGeoFlag = "from-geo"
ToGeoFlag = "to-geo"
)

func showErr() {
Expand All @@ -28,11 +28,11 @@ func main() {
case FromGeoFlag:
FromGeoCmd := flag.NewFlagSet(FromGeoFlag, flag.ExitOnError)
lat := FromGeoCmd.Float64("lat", 0, "latitude")
long := FromGeoCmd.Float64("long", 0, "longitude")
lng := FromGeoCmd.Float64("lng", 0, "longitude")
if err := FromGeoCmd.Parse(os.Args[2:]); err != nil {
panic(err)
}
placeKey, err := pk.GeoToPlacekey(*lat, *long)
placeKey, err := pk.GeoToPlacekey(*lat, *lng)
if err != nil {
panic(err)
}
Expand Down
24 changes: 12 additions & 12 deletions pk.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ func radians(degree float64) float64 {
return degree * math.Pi / 180
}

func geoDistance(lat1, long1, lat2, long2 float64) float64 {
func geoDistance(lat1, lng1, lat2, lng2 float64) float64 {
lat1 = radians(lat1)
long1 = radians(long1)
lng1 = radians(lng1)
lat2 = radians(lat2)
long2 = radians(long2)
lng2 = radians(lng2)

hav_lat := 0.5 * (1 - math.Cos(lat1-lat2))
hav_long := 0.5 * (1 - math.Cos(long1-long2))
radical := math.Sqrt(hav_lat + math.Cos(lat1)*math.Cos(lat2)*hav_long)
hav_lng := 0.5 * (1 - math.Cos(lng1-lng2))
radical := math.Sqrt(hav_lat + math.Cos(lat1)*math.Cos(lat2)*hav_lng)
return 2 * _EARTH_RADIUS * math.Asin(radical) * 1000
}

Expand Down Expand Up @@ -188,11 +188,11 @@ func cleanString(s string) string {
return s
}

func GeoToPlacekey(lat, long float64) (string, error) {
if lat < -90 || lat > 90 || long < -180 || long > 180 {
return "", errors.New("invalid lat/long range")
func GeoToPlacekey(lat, lng float64) (string, error) {
if lat < -90 || lat > 90 || lng < -180 || lng > 180 {
return "", errors.New("invalid lat/lng range")
}
return encodeH3Int(int64(h3.NewLatLng(lat, long).Cell(_RESOLUTION))), nil
return encodeH3Int(int64(h3.NewLatLng(lat, lng).Cell(_RESOLUTION))), nil
}

func parsePlacekey(placekey string) (string, string, error) {
Expand Down Expand Up @@ -264,16 +264,16 @@ func PlacekeyToGeo(placekey string) (float64, float64, error) {
}

func PlacekeyDistance(pk1 string, pk2 string) (float64, error) {
lat1, long1, err := PlacekeyToGeo(pk1)
lat1, lng1, err := PlacekeyToGeo(pk1)
if err != nil {
return 0, err
}

lat2, long2, err := PlacekeyToGeo(pk2)
lat2, lng2, err := PlacekeyToGeo(pk2)
if err != nil {
return 0, err
}
return geoDistance(lat1, long1, lat2, long2), nil
return geoDistance(lat1, lng1, lat2, lng2), nil
}

func validateWhat(what string) bool {
Expand Down
6 changes: 3 additions & 3 deletions pk_dataset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ func TestPlaceKeyDataset(t *testing.T) {
continue
}
lat_str := rawparts[0]
long_str := rawparts[1]
lng_str := rawparts[1]
placekey_str := rawparts[4]

lat_float, err := strconv.ParseFloat(lat_str, 64)
if err != nil {
panic(err)
}
long_float, err := strconv.ParseFloat(long_str, 64)
lng_float, err := strconv.ParseFloat(lng_str, 64)
if err != nil {
panic(err)
}
placeKey, err := pk.GeoToPlacekey(lat_float, long_float)
placeKey, err := pk.GeoToPlacekey(lat_float, lng_float)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit a5a9dbd

Please sign in to comment.