Skip to content

Commit

Permalink
implement reverse geocoding
Browse files Browse the repository at this point in the history
  • Loading branch information
tsirysndr committed May 31, 2022
1 parent ab4353b commit e7f99de
Show file tree
Hide file tree
Showing 8 changed files with 349 additions and 85 deletions.
17 changes: 16 additions & 1 deletion cmd/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,28 @@ to quickly create a Cobra application.`,
return
}
outputInJSON, _ := cmd.Flags().GetBool("json")
mada.Search(args[0], outputInJSON)
searchForFokontany, _ := cmd.Flags().GetBool("fokontany")
searchForCommune, _ := cmd.Flags().GetBool("commune")
searchForDistrict, _ := cmd.Flags().GetBool("district")
searchForRegion, _ := cmd.Flags().GetBool("region")
options := mada.SearchOptions{
OutputInJSON: outputInJSON,
SearchForFokontany: searchForFokontany,
SearchForCommune: searchForCommune,
SearchForDistrict: searchForDistrict,
SearchForRegion: searchForRegion,
}
mada.Search(args[0], options)
},
}

func init() {
rootCmd.AddCommand(searchCmd)
searchCmd.Flags().BoolP("json", "j", false, "Output in JSON format")
searchCmd.Flags().BoolP("fokontany", "f", false, "Search for a fokontany")
searchCmd.Flags().BoolP("commune", "c", false, "Search for a commune")
searchCmd.Flags().BoolP("district", "d", false, "Search for a district")
searchCmd.Flags().BoolP("region", "r", false, "Search for a region")

// Here you will define your flags and configuration settings.

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ require (
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
github.com/google/open-location-code/go v0.0.0-20220120191843-cafb35c0d74d // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/lib/pq v1.10.6 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/open-location-code/go v0.0.0-20220120191843-cafb35c0d74d h1:1/3RagbDc9Eow+XUawT2CUfm5XYLv8Nx2rhyRpakNS8=
github.com/google/open-location-code/go v0.0.0-20220120191843-cafb35c0d74d/go.mod h1:eJfRN6aj+kR/rnua/rw9jAgYhqoMHldQkdTi+sePRKk=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
Expand Down
39 changes: 20 additions & 19 deletions mada/commune.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,26 @@ func (c *CommuneService) ShowCommune(id string, outputInJSON bool) {
rows, _ := c.db.Query("SELECT uid, name, region, district, country, ST_AsText(geom) FROM commune WHERE uid = $1", id)
defer rows.Close()
var uid, name, region, district, country, g string
rows.Next()
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,
Coordinates: p.(*geom.Polygon).Coords(),
}, "", " ")

fmt.Println(string(b))
return
}
for rows.Next() {
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,
Coordinates: p.(*geom.Polygon).Coords(),
}, "", " ")

fmt.Println(string(b))
return
}

fmt.Printf(`
fmt.Printf(`
id
%s
name
Expand All @@ -108,4 +108,5 @@ func (c *CommuneService) ShowCommune(id string, outputInJSON bool) {
geometry
%v
`, uid, name, district, region, p.(*geom.Polygon).Coords())
}
}
35 changes: 18 additions & 17 deletions mada/district.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,24 @@ func (d *DistrictService) ShowDistrict(id string, outputInJSON bool) {
rows, _ := d.db.Query("SELECT uid, name, region, ST_AsText(geom) FROM district WHERE uid = $1", id)
defer rows.Close()
var uid, name, region, g string
rows.Next()
rows.Scan(&uid, &name, &region, &g)

p, _ := wkt.Unmarshal(g)

if outputInJSON {
b, _ := json.MarshalIndent(District{
ID: uid,
Name: name,
Region: region,
Country: "Madagascar",
Coordinates: p.(*geom.Polygon).Coords(),
}, "", " ")
fmt.Println(string(b))
return
}
for rows.Next() {
rows.Scan(&uid, &name, &region, &g)

p, _ := wkt.Unmarshal(g)

if outputInJSON {
b, _ := json.MarshalIndent(District{
ID: uid,
Name: name,
Region: region,
Country: "Madagascar",
Coordinates: p.(*geom.Polygon).Coords(),
}, "", " ")
fmt.Println(string(b))
return
}

fmt.Printf(`
fmt.Printf(`
id
%s
name
Expand All @@ -103,4 +103,5 @@ func (d *DistrictService) ShowDistrict(id string, outputInJSON bool) {
geometry
%v
`, uid, name, region, p.(*geom.Polygon).Coords())
}
}
75 changes: 38 additions & 37 deletions mada/fokontany.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,43 +76,44 @@ func (f *FokontanyService) ShowFokontany(id string, outputInJSON bool) {
log.Fatal(err)
}
defer rows.Close()

var uid, name, commune, district, region, country, g string
rows.Next()
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,
Coordinates: p.(*geom.Polygon).Coords(),
}, "", " ")
fmt.Println(string(b))
return
for rows.Next() {
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,
Coordinates: p.(*geom.Polygon).Coords(),
}, "", " ")
fmt.Println(string(b))
return
}
fmt.Printf(`
id
%s
name
%s
commune
%s
district
%s
region
%s
country
%s
type
fokontany
geometry
%v
`, uid, name, commune, district, region, country, p.(*geom.Polygon).Coords())

}
fmt.Printf(`
id
%s
name
%s
commune
%s
district
%s
region
%s
country
%s
type
fokontany
country
Madagascar
geometry
%v
`, uid, name, commune, region, district, country, p.(*geom.Polygon).Coords())
}
19 changes: 10 additions & 9 deletions mada/region.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,18 @@ func (r *RegionService) ShowRegion(id string, outputInJSON bool) {
rows, _ := r.db.Query("SELECT uid, name, ST_AsText(geom) FROM region WHERE uid = $1", id)
defer rows.Close()
var uid, name, g string
rows.Next()
rows.Scan(&uid, &name, &g)
for rows.Next() {
rows.Scan(&uid, &name, &g)

p, _ := wkt.Unmarshal(g)
p, _ := wkt.Unmarshal(g)

if outputInJSON {
b, _ := json.MarshalIndent(Region{ID: uid, Name: name, Country: "Madagascar", Coordinates: p.(*geom.Polygon).Coords()}, "", " ")
fmt.Println(string(b))
return
}
if outputInJSON {
b, _ := json.MarshalIndent(Region{ID: uid, Name: name, Country: "Madagascar", Coordinates: p.(*geom.Polygon).Coords()}, "", " ")
fmt.Println(string(b))
return
}

fmt.Printf(`
fmt.Printf(`
id
%s
name
Expand All @@ -93,4 +93,5 @@ func (r *RegionService) ShowRegion(id string, outputInJSON bool) {
geometry
%v
`, uid, name, p.(*geom.Polygon).Coords())
}
}
Loading

0 comments on commit e7f99de

Please sign in to comment.