Skip to content

Commit

Permalink
add geospatial coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
tsirysndr committed May 29, 2022
1 parent fb1b846 commit bf7127c
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 67 deletions.
19 changes: 11 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@ module github.com/tsirysndr/mada

go 1.18

require (
github.com/blevesearch/bleve/v2 v2.3.2
github.com/everystreet/go-shapefile v1.0.0
github.com/fatih/color v1.13.0
github.com/mattn/go-sqlite3 v2.0.3+incompatible
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v1.4.0
github.com/twpayne/go-geom v1.4.1
)

require (
github.com/RoaringBitmap/roaring v1.1.0 // indirect
github.com/bits-and-blooms/bitset v1.2.2 // indirect
github.com/blevesearch/bleve/v2 v2.3.2 // indirect
github.com/blevesearch/bleve_index_api v1.0.1 // indirect
github.com/blevesearch/go-porterstemmer v1.0.3 // indirect
github.com/blevesearch/gtreap v0.1.1 // indirect
Expand All @@ -22,8 +31,6 @@ require (
github.com/blevesearch/zapx/v15 v15.3.3 // indirect
github.com/everystreet/go-geojson v1.1.1 // indirect
github.com/everystreet/go-geojson/v2 v2.0.2 // indirect
github.com/everystreet/go-shapefile v1.0.0 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/golang/geo v0.0.0-20210211234256-740aa86cb551 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
Expand All @@ -33,22 +40,18 @@ require (
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mattn/go-sqlite3 v1.14.13 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mschoch/smat v0.2.0 // indirect
github.com/nats-io/nats.go v1.15.0 // indirect
github.com/nats-io/nkeys v0.3.0 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/cobra v1.4.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/twpayne/go-geom v1.4.1 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898 // indirect
golang.org/x/net v0.0.0-20220524220425-1d687d428aca // indirect
golang.org/x/net v0.0.0-20220526153639-5463443f8c37 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
Expand Down
16 changes: 4 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ github.com/huandu/xstrings v1.3.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq
github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=
github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand All @@ -112,7 +111,6 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lib/pq v0.0.0-20180327071824-d34b9ff171c2/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.8.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/lib/pq v1.10.6 h1:jbk+ZieJ0D7EVGJYpL9QTz7/YW6UHbmdnZWYyK5cdBs=
github.com/lib/pq v1.10.6/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
Expand All @@ -126,20 +124,17 @@ github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRC
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v1.14.13 h1:1tj15ngiFfcZzii7yd82foL+ks+ouQcj8j/TPq3fk1I=
github.com/mattn/go-sqlite3 v1.14.13/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v2.0.3+incompatible h1:gXHsfypPkaMZrKbD5209QV9jbUTJKjyR5WD3HYQSd+U=
github.com/mattn/go-sqlite3 v2.0.3+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/mschoch/smat v0.2.0 h1:8imxQsjDm8yFEAVBe7azKmKSgzSkZXDuKkSq9374khM=
github.com/mschoch/smat v0.2.0/go.mod h1:kc9mz7DoBKqDyiRL7VZN8KvXQMWeTaVnttLRXOlotKw=
github.com/nats-io/nats.go v1.15.0 h1:3IXNBolWrwIUf2soxh6Rla8gPzYWEZQBUBK6RV21s+o=
github.com/nats-io/nats.go v1.15.0/go.mod h1:BPko4oXsySz4aSWeFgOHLZs3G4Jq4ZAyE6/zMCxRT6w=
github.com/nats-io/nkeys v0.3.0 h1:cgM5tL53EvYRU+2YLXIK0G2mJtK12Ft9oeooSZMA2G8=
github.com/nats-io/nkeys v0.3.0/go.mod h1:gvUNGjVcM2IPr5rCsRsC6Wb3Hr2CQAm08dsxtV6A5y4=
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
Expand Down Expand Up @@ -186,9 +181,7 @@ github.com/twpayne/go-kml v1.5.2/go.mod h1:kz8jAiIz6FIdU2Zjce9qGlVtgFYES9vt7BTPB
github.com/twpayne/go-polyline v1.0.0/go.mod h1:ICh24bcLYBX8CknfvNPKqoTbe+eg+MX1NPyJmSBo7pU=
github.com/twpayne/go-waypoint v0.0.0-20200706203930-b263a7f6e4e8/go.mod h1:qj5pHncxKhu9gxtZEYWypA/z097sxhFlbTyOyt9gcnU=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU=
github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
go.etcd.io/bbolt v1.3.5/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ=
Expand All @@ -198,16 +191,15 @@ golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnf
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20210314154223-e6e6c4f2bb5b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898 h1:SLP7Q4Di66FONjDJbCYrCRrh97focO6sLogHO7/g8F0=
golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20191003171128-d98b1b443823/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210410081132-afb366fc7cd1/go.mod h1:9tjilg8BloeKEkVJvy7fQ90B1CfIiPueXVOjqfkSzI8=
golang.org/x/net v0.0.0-20220524220425-1d687d428aca h1:xTaFYiPROfpPhqrfTIDXj0ri1SpfueYT951s4bAuDO8=
golang.org/x/net v0.0.0-20220524220425-1d687d428aca/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220526153639-5463443f8c37 h1:lUkvobShwKsOesNfWWlCS5q7fnbG1MEliIzwu886fn8=
golang.org/x/net v0.0.0-20220526153639-5463443f8c37/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
36 changes: 22 additions & 14 deletions mada/commune.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ import (
"fmt"

"github.com/blevesearch/bleve/v2"
"github.com/twpayne/go-geom"
"github.com/twpayne/go-geom/encoding/wkt"
)

type Commune struct {
ID string `json:"id"`
Name string `json:"name"`
Region string `json:"region"`
District string `json:"district"`
Country string `json:"country"`
ID string `json:"id"`
Name string `json:"name"`
Region string `json:"region"`
District string `json:"district"`
Country string `json:"country"`
Coordinates [][]geom.Coord `json:"coordinates"`
}

type CommuneService struct {
Expand Down Expand Up @@ -57,19 +60,22 @@ func (c *CommuneService) List(outputInJSON bool, limit int) {
}

func (c *CommuneService) ShowCommune(id string, outputInJSON bool) {
rows, _ := c.db.Query("SELECT uid, name, region, district, country FROM commune WHERE uid = ?", id)
rows, _ := c.db.Query("SELECT uid, name, region, district, country, ST_AsText(geom) FROM commune WHERE uid = ?", id)
defer rows.Close()
var uid, name, region, district, country string
var uid, name, region, district, country, g string
rows.Next()
rows.Scan(&uid, &name, &region, &district, &country)
rows.Scan(&uid, &name, &region, &district, &country, &g)

p, _ := wkt.Unmarshal(g)

if outputInJSON {
b, _ := json.MarshalIndent(Commune{
ID: uid,
Name: name,
Region: region,
District: district,
Country: country,
ID: uid,
Name: name,
Region: region,
District: district,
Country: country,
Coordinates: p.(*geom.Polygon).Coords(),
}, "", " ")

fmt.Println(string(b))
Expand All @@ -90,5 +96,7 @@ func (c *CommuneService) ShowCommune(id string, outputInJSON bool) {
commune
country
Madagascar
`, uid, name, district, region)
geometry
%v
`, uid, name, district, region, p.(*geom.Polygon).Coords())
}
31 changes: 22 additions & 9 deletions mada/district.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import (
"fmt"

"github.com/blevesearch/bleve/v2"
"github.com/twpayne/go-geom"
"github.com/twpayne/go-geom/encoding/wkt"
)

type District struct {
ID string `json:"id"`
Name string `json:"name"`
Region string `json:"region"`
Country string `json:"country"`
ID string `json:"id"`
Name string `json:"name"`
Region string `json:"region"`
Country string `json:"country"`
Coordinates [][]geom.Coord `json:"coordinates"`
}

type DistrictService struct {
Expand Down Expand Up @@ -56,14 +59,22 @@ func (d *DistrictService) List(outputInJSON bool, limit int) {
}

func (d *DistrictService) ShowDistrict(id string, outputInJSON bool) {
rows, _ := d.db.Query("SELECT uid, name, region FROM district WHERE uid = ?", id)
rows, _ := d.db.Query("SELECT uid, name, region, ST_AsText(geom) FROM district WHERE uid = ?", id)
defer rows.Close()
var uid, name, region string
var uid, name, region, g string
rows.Next()
rows.Scan(&uid, &name, &region)
rows.Scan(&uid, &name, &region, &g)

p, _ := wkt.Unmarshal(g)

if outputInJSON {
b, _ := json.MarshalIndent(District{ID: uid, Name: name, Region: region, Country: "Madagascar"}, "", " ")
b, _ := json.MarshalIndent(District{
ID: uid,
Name: name,
Region: region,
Country: "Madagascar",
Coordinates: p.(*geom.Polygon).Coords(),
}, "", " ")
fmt.Println(string(b))
return
}
Expand All @@ -80,5 +91,7 @@ func (d *DistrictService) ShowDistrict(id string, outputInJSON bool) {
district
country
Madagascar
`, uid, name, region)
geometry
%v
`, uid, name, region, p.(*geom.Polygon).Coords())
}
40 changes: 24 additions & 16 deletions mada/fokontany.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ import (
"fmt"

"github.com/blevesearch/bleve/v2"
"github.com/twpayne/go-geom"
"github.com/twpayne/go-geom/encoding/wkt"
)

type Fokontany struct {
ID string `json:"id"`
Name string `json:"name"`
Commune string `json:"commune"`
Region string `json:"region"`
District string `json:"district"`
Country string `json:"country"`
ID string `json:"id"`
Name string `json:"name"`
Commune string `json:"commune"`
Region string `json:"region"`
District string `json:"district"`
Country string `json:"country"`
Coordinates [][]geom.Coord `json:"coordinates"`
}

type FokontanyService struct {
Expand Down Expand Up @@ -58,20 +61,23 @@ func (f *FokontanyService) List(outputInJSON bool, limit int) {
}

func (f *FokontanyService) ShowFokontany(id string, outputInJSON bool) {
rows, _ := f.db.Query("SELECT uid, name, commune, region, district, country FROM fokontany WHERE uid = ?", id)
rows, _ := f.db.Query("SELECT uid, name, commune, region, district, country, ST_AsText(geom) FROM fokontany WHERE uid = ?", id)
defer rows.Close()
var uid, name, commune, district, region, country string
var uid, name, commune, district, region, country, g string
rows.Next()
rows.Scan(&uid, &name, &commune, &region, &district, &country)
rows.Scan(&uid, &name, &commune, &region, &district, &country, &g)

p, _ := wkt.Unmarshal(g)

if outputInJSON {
b, _ := json.MarshalIndent(Fokontany{
ID: uid,
Name: name,
Commune: commune,
Region: region,
District: district,
Country: country,
ID: uid,
Name: name,
Commune: commune,
Region: region,
District: district,
Country: country,
Coordinates: p.(*geom.Polygon).Coords(),
}, "", " ")
fmt.Println(string(b))
return
Expand All @@ -93,5 +99,7 @@ func (f *FokontanyService) ShowFokontany(id string, outputInJSON bool) {
fokontany
country
Madagascar
`, uid, name, commune, region, district, country)
geometry
%v
`, uid, name, commune, region, district, country, p.(*geom.Polygon).Coords())
}
23 changes: 15 additions & 8 deletions mada/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import (
"fmt"

"github.com/blevesearch/bleve/v2"
"github.com/twpayne/go-geom"
"github.com/twpayne/go-geom/encoding/wkt"
)

type Region struct {
ID string `json:"id"`
Name string `json:"name"`
Country string `json:"country"`
ID string `json:"id"`
Name string `json:"name"`
Country string `json:"country"`
Coordinates [][]geom.Coord `json:"coordinates"`
}

type RegionService struct {
Expand Down Expand Up @@ -55,14 +58,16 @@ func (r *RegionService) List(outputInJSON bool, limit int) {
}

func (r *RegionService) ShowRegion(id string, outputInJSON bool) {
rows, _ := r.db.Query("SELECT uid, name FROM region WHERE uid = ?", id)
rows, _ := r.db.Query("SELECT uid, name, ST_AsText(geom) FROM region WHERE uid = ?", id)
defer rows.Close()
var uid, name string
var uid, name, g string
rows.Next()
rows.Scan(&uid, &name)
rows.Scan(&uid, &name, &g)

p, _ := wkt.Unmarshal(g)

if outputInJSON {
b, _ := json.MarshalIndent(Region{ID: uid, Name: name, Country: "Madagascar"}, "", " ")
b, _ := json.MarshalIndent(Region{ID: uid, Name: name, Country: "Madagascar", Coordinates: p.(*geom.Polygon).Coords()}, "", " ")
fmt.Println(string(b))
return
}
Expand All @@ -76,5 +81,7 @@ func (r *RegionService) ShowRegion(id string, outputInJSON bool) {
region
country
Madagascar
`, uid, name)
geometry
%v
`, uid, name, p.(*geom.Polygon).Coords())
}

0 comments on commit bf7127c

Please sign in to comment.