Created
October 31, 2020 20:51
-
-
Save LaPingvino/69217cb5a7f0e48d8a5de24f426b5169 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"os" | |
"fmt" | |
"bufio" | |
"encoding/json" | |
"strings" | |
"strconv" | |
) | |
type Name struct { | |
Name string | |
Names map[string]string | |
AdminLevel int | |
Place string | |
Lat float64 | |
Long float64 | |
} | |
func main() { | |
if len(os.Args) < 2 { | |
panic("need to invoke with planet.osm file") | |
} | |
file, err := os.Open(os.Args[1]) | |
if err != nil { | |
panic("problem opening file: " + err.Error()) | |
} | |
fb := bufio.NewReader(file) | |
var node, place bool | |
var line string | |
var name Name | |
var counter, old int64 | |
for err == nil { | |
line, err = fb.ReadString('\n') | |
counter += int64(len(line)) | |
if counter > old + 1000000000 { | |
old = counter | |
print(".") | |
} | |
if len(line) < 7 { | |
continue | |
} | |
switch { | |
case strings.Contains(line,"<node"): | |
node = true | |
lati := strings.Index(line, "lat=\"") | |
if lati == -1 { | |
node = false | |
continue | |
} | |
lats := line[lati+5:strings.Index(line[lati+5:], "\"")+lati+5] | |
loni := strings.Index(line, "lon=\"") | |
if loni == -1 { | |
node = false | |
continue | |
} | |
lons := line[loni+5:strings.Index(line[loni+5:], "\"")+loni+5] | |
lat, _ := strconv.ParseFloat(lats, 64) | |
lon, _ := strconv.ParseFloat(lons, 64) | |
name = Name{Lat: lat, Long: lon, Names: map[string]string{}} | |
case strings.Contains(line, "k=\"name\""): | |
if !node { | |
continue | |
} | |
namei := strings.Index(line, "v=\"") | |
if namei == -1 { | |
node = false | |
continue | |
} | |
name.Name = line[namei+3:strings.Index(line[namei+3:],"\"")+namei+3] | |
case strings.Contains(line, "k=\"name"): | |
if !node { | |
continue | |
} | |
langi := strings.Index(line, "k=\"name:") | |
if langi == -1 { | |
node = false | |
continue | |
} | |
namei := strings.Index(line, "v=\"") | |
if namei == -1 { | |
node = false | |
continue | |
} | |
lang := line[langi+8:strings.Index(line[langi+8:],"\"")+langi+8] | |
name.Names[lang] = line[namei+3:strings.Index(line[namei+3:],"\"")+namei+3] | |
case strings.Contains(line, "k=\"alt_name"): | |
if !node { | |
continue | |
} | |
langi := strings.Index(line, "k=\"") | |
if langi == -1 { | |
node = false | |
continue | |
} | |
namei := strings.Index(line, "v=\"") | |
if namei == -1 { | |
node = false | |
continue | |
} | |
lang := line[langi+3:strings.Index(line[langi+3:],"\"")+langi+3] | |
name.Names[lang] = line[namei+3:strings.Index(line[namei+3:],"\"")+namei+3] | |
case strings.Contains(line, "k=\"place\""): | |
if !node { | |
continue | |
} | |
placei := strings.Index(line, "v=\"") | |
if placei == -1 { | |
node = false | |
continue | |
} | |
name.Place = line[placei+3:strings.Index(line[placei+3:],"\"")+placei+3] | |
switch name.Place { | |
case "country", "state", "region", "province", "district", "county", "municipality": | |
fallthrough | |
case "city", "borough", "suburb", "quarter", "neighborhood", "city_block", "plot": | |
fallthrough | |
case "town", "isolated_dwelling", "farm", "allotments": | |
fallthrough | |
// case "village", "hamlet", "locality": // there are a LOT of these, | |
// fallthrough // and while they can be useful for navigation, | |
// they make the file way too big... | |
case "continent", "archipelago", "island", "islet", "square", "sea", "ocean": | |
place = true | |
} | |
case strings.Contains(line, "k=\"admin_level\""): | |
if !node { | |
continue | |
} | |
admi := strings.Index(line, "v=\"") | |
if admi == -1 { | |
node = false | |
continue | |
} | |
admlvl := line[admi+3:strings.Index(line[admi+3:],"\"")+admi+3] | |
name.AdminLevel, _ = strconv.Atoi(admlvl) | |
case strings.Contains(line, "</node>"): | |
if !node { | |
continue | |
} | |
if !place { | |
node = false | |
place = false | |
continue | |
} | |
if name.Name == "" { | |
node = false | |
continue | |
} | |
output, _ := json.Marshal(name) | |
fmt.Println(string(output)) | |
node = false | |
place = false | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment