Last active
August 28, 2019 21:31
-
-
Save ear7h/80d233451e90a31ec63aeea435f296b2 to your computer and use it in GitHub Desktop.
golang "script" for converting wkt into github.com/go-spatial/goem types
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
/* | |
* can be run as like so: | |
* psql -d natural_earth -t -c "select 'CoastLine', ST_AsText(wkb_geometry) from ne_10m_coastline limit 1 offset 1;" | go run wkt_conv.go >> natural_earth.go | |
*/ | |
package main | |
import ( | |
"fmt" | |
"os" | |
"strings" | |
"io/ioutil" | |
) | |
func geomType(typ string) string { | |
if strings.HasPrefix(typ, "MULTI") { | |
return "Multi" + geomType(typ[len("MULTI"):]) | |
} | |
switch typ { | |
case "POINT": | |
return "Point" | |
case "LINESTRING": | |
return "LineString" | |
case "POLYGON": | |
return "Polygon" | |
case "GEOMETRYCOLLECTION": | |
return "Collection" | |
} | |
panic("not found " + typ) | |
} | |
func main() { | |
byt, _ := ioutil.ReadAll(os.Stdin) | |
str := string(byt) | |
n := strings.Index(str, "|") | |
name := strings.Replace(str[:n], " ", "", -1) | |
str = str[n+1:] | |
n = strings.Index(str, "(") | |
typ := strings.TrimSpace(str[:n]) | |
typ = geomType(typ) | |
str = str[n:] | |
str = strings.Replace(str, ",", "},{", -1) | |
str = strings.Replace(str, " ", ", ", -1) | |
str = strings.Replace(str, "(", "{", -1) | |
str = strings.Replace(str, ")", "}", -1) | |
str = strings.TrimSpace(str) | |
fmt.Printf("var %s = geom.%s{%s}\n", name, typ, str) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment