Skip to content

Commit b93f315

Browse files
committed
Add -city flag
The -city flag provides a shortcut to calculate / lookup Lat/Long values from the World Cities Database as available via https://simplemaps.com/data/world-cities
1 parent 4185127 commit b93f315

File tree

6 files changed

+44866
-0
lines changed

6 files changed

+44866
-0
lines changed

cmd/astral/cities.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//go:generate go run ../../data/citycsv2go.go ../../data/worldcities.csv.gz cities_gen.go
2+
3+
package main
4+
5+
import (
6+
"fmt"
7+
"strings"
8+
)
9+
10+
func latLongFromCity(city string) (lat, long float64, err error) {
11+
12+
city = strings.TrimSpace(city)
13+
city = strings.ToLower(city)
14+
for _, c := range cities {
15+
ncc := strings.ToLower(c.NameCC)
16+
if strings.HasPrefix(ncc, city) {
17+
return c.Lat, c.Long, nil
18+
}
19+
// "new york" -> "newyork"
20+
ncc = strings.ReplaceAll(ncc, " ", "")
21+
if strings.HasPrefix(ncc, city) {
22+
return c.Lat, c.Long, nil
23+
}
24+
}
25+
26+
return 0, 0, fmt.Errorf("not found")
27+
}

0 commit comments

Comments
 (0)