Skip to content

Instantly share code, notes, and snippets.

@ear7h
Last active August 28, 2019 21:31
Show Gist options
  • Save ear7h/80d233451e90a31ec63aeea435f296b2 to your computer and use it in GitHub Desktop.
Save ear7h/80d233451e90a31ec63aeea435f296b2 to your computer and use it in GitHub Desktop.
golang "script" for converting wkt into github.com/go-spatial/goem types
/*
* 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