Skip to content

Commit e7f99de

Browse files
committed
implement reverse geocoding
1 parent ab4353b commit e7f99de

File tree

8 files changed

+349
-85
lines changed

8 files changed

+349
-85
lines changed

cmd/search.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,28 @@ to quickly create a Cobra application.`,
5151
return
5252
}
5353
outputInJSON, _ := cmd.Flags().GetBool("json")
54-
mada.Search(args[0], outputInJSON)
54+
searchForFokontany, _ := cmd.Flags().GetBool("fokontany")
55+
searchForCommune, _ := cmd.Flags().GetBool("commune")
56+
searchForDistrict, _ := cmd.Flags().GetBool("district")
57+
searchForRegion, _ := cmd.Flags().GetBool("region")
58+
options := mada.SearchOptions{
59+
OutputInJSON: outputInJSON,
60+
SearchForFokontany: searchForFokontany,
61+
SearchForCommune: searchForCommune,
62+
SearchForDistrict: searchForDistrict,
63+
SearchForRegion: searchForRegion,
64+
}
65+
mada.Search(args[0], options)
5566
},
5667
}
5768

5869
func init() {
5970
rootCmd.AddCommand(searchCmd)
6071
searchCmd.Flags().BoolP("json", "j", false, "Output in JSON format")
72+
searchCmd.Flags().BoolP("fokontany", "f", false, "Search for a fokontany")
73+
searchCmd.Flags().BoolP("commune", "c", false, "Search for a commune")
74+
searchCmd.Flags().BoolP("district", "d", false, "Search for a district")
75+
searchCmd.Flags().BoolP("region", "r", false, "Search for a region")
6176

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

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ require (
3333
github.com/golang/geo v0.0.0-20210211234256-740aa86cb551 // indirect
3434
github.com/golang/protobuf v1.5.2 // indirect
3535
github.com/golang/snappy v0.0.4 // indirect
36+
github.com/google/open-location-code/go v0.0.0-20220120191843-cafb35c0d74d // indirect
3637
github.com/inconshreveable/mousetrap v1.0.0 // indirect
3738
github.com/lib/pq v1.10.6 // indirect
3839
github.com/mattn/go-colorable v0.1.12 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
9191
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
9292
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
9393
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
94+
github.com/google/open-location-code/go v0.0.0-20220120191843-cafb35c0d74d h1:1/3RagbDc9Eow+XUawT2CUfm5XYLv8Nx2rhyRpakNS8=
95+
github.com/google/open-location-code/go v0.0.0-20220120191843-cafb35c0d74d/go.mod h1:eJfRN6aj+kR/rnua/rw9jAgYhqoMHldQkdTi+sePRKk=
9496
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
9597
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
9698
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=

mada/commune.go

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -72,26 +72,26 @@ func (c *CommuneService) ShowCommune(id string, outputInJSON bool) {
7272
rows, _ := c.db.Query("SELECT uid, name, region, district, country, ST_AsText(geom) FROM commune WHERE uid = $1", id)
7373
defer rows.Close()
7474
var uid, name, region, district, country, g string
75-
rows.Next()
76-
rows.Scan(&uid, &name, &region, &district, &country, &g)
77-
78-
p, _ := wkt.Unmarshal(g)
79-
80-
if outputInJSON {
81-
b, _ := json.MarshalIndent(Commune{
82-
ID: uid,
83-
Name: name,
84-
Region: region,
85-
District: district,
86-
Country: country,
87-
Coordinates: p.(*geom.Polygon).Coords(),
88-
}, "", " ")
89-
90-
fmt.Println(string(b))
91-
return
92-
}
75+
for rows.Next() {
76+
rows.Scan(&uid, &name, &region, &district, &country, &g)
77+
78+
p, _ := wkt.Unmarshal(g)
79+
80+
if outputInJSON {
81+
b, _ := json.MarshalIndent(Commune{
82+
ID: uid,
83+
Name: name,
84+
Region: region,
85+
District: district,
86+
Country: country,
87+
Coordinates: p.(*geom.Polygon).Coords(),
88+
}, "", " ")
89+
90+
fmt.Println(string(b))
91+
return
92+
}
9393

94-
fmt.Printf(`
94+
fmt.Printf(`
9595
id
9696
%s
9797
name
@@ -108,4 +108,5 @@ func (c *CommuneService) ShowCommune(id string, outputInJSON bool) {
108108
geometry
109109
%v
110110
`, uid, name, district, region, p.(*geom.Polygon).Coords())
111+
}
111112
}

mada/district.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,24 +71,24 @@ func (d *DistrictService) ShowDistrict(id string, outputInJSON bool) {
7171
rows, _ := d.db.Query("SELECT uid, name, region, ST_AsText(geom) FROM district WHERE uid = $1", id)
7272
defer rows.Close()
7373
var uid, name, region, g string
74-
rows.Next()
75-
rows.Scan(&uid, &name, &region, &g)
76-
77-
p, _ := wkt.Unmarshal(g)
78-
79-
if outputInJSON {
80-
b, _ := json.MarshalIndent(District{
81-
ID: uid,
82-
Name: name,
83-
Region: region,
84-
Country: "Madagascar",
85-
Coordinates: p.(*geom.Polygon).Coords(),
86-
}, "", " ")
87-
fmt.Println(string(b))
88-
return
89-
}
74+
for rows.Next() {
75+
rows.Scan(&uid, &name, &region, &g)
76+
77+
p, _ := wkt.Unmarshal(g)
78+
79+
if outputInJSON {
80+
b, _ := json.MarshalIndent(District{
81+
ID: uid,
82+
Name: name,
83+
Region: region,
84+
Country: "Madagascar",
85+
Coordinates: p.(*geom.Polygon).Coords(),
86+
}, "", " ")
87+
fmt.Println(string(b))
88+
return
89+
}
9090

91-
fmt.Printf(`
91+
fmt.Printf(`
9292
id
9393
%s
9494
name
@@ -103,4 +103,5 @@ func (d *DistrictService) ShowDistrict(id string, outputInJSON bool) {
103103
geometry
104104
%v
105105
`, uid, name, region, p.(*geom.Polygon).Coords())
106+
}
106107
}

mada/fokontany.go

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -76,43 +76,44 @@ func (f *FokontanyService) ShowFokontany(id string, outputInJSON bool) {
7676
log.Fatal(err)
7777
}
7878
defer rows.Close()
79+
7980
var uid, name, commune, district, region, country, g string
80-
rows.Next()
81-
rows.Scan(&uid, &name, &commune, &region, &district, &country, &g)
82-
83-
p, _ := wkt.Unmarshal(g)
84-
85-
if outputInJSON {
86-
b, _ := json.MarshalIndent(Fokontany{
87-
ID: uid,
88-
Name: name,
89-
Commune: commune,
90-
Region: region,
91-
District: district,
92-
Country: country,
93-
Coordinates: p.(*geom.Polygon).Coords(),
94-
}, "", " ")
95-
fmt.Println(string(b))
96-
return
81+
for rows.Next() {
82+
rows.Scan(&uid, &name, &commune, &region, &district, &country, &g)
83+
84+
p, _ := wkt.Unmarshal(g)
85+
86+
if outputInJSON {
87+
b, _ := json.MarshalIndent(Fokontany{
88+
ID: uid,
89+
Name: name,
90+
Commune: commune,
91+
Region: region,
92+
District: district,
93+
Country: country,
94+
Coordinates: p.(*geom.Polygon).Coords(),
95+
}, "", " ")
96+
fmt.Println(string(b))
97+
return
98+
}
99+
fmt.Printf(`
100+
id
101+
%s
102+
name
103+
%s
104+
commune
105+
%s
106+
district
107+
%s
108+
region
109+
%s
110+
country
111+
%s
112+
type
113+
fokontany
114+
geometry
115+
%v
116+
`, uid, name, commune, district, region, country, p.(*geom.Polygon).Coords())
117+
97118
}
98-
fmt.Printf(`
99-
id
100-
%s
101-
name
102-
%s
103-
commune
104-
%s
105-
district
106-
%s
107-
region
108-
%s
109-
country
110-
%s
111-
type
112-
fokontany
113-
country
114-
Madagascar
115-
geometry
116-
%v
117-
`, uid, name, commune, region, district, country, p.(*geom.Polygon).Coords())
118119
}

mada/region.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,18 @@ func (r *RegionService) ShowRegion(id string, outputInJSON bool) {
7070
rows, _ := r.db.Query("SELECT uid, name, ST_AsText(geom) FROM region WHERE uid = $1", id)
7171
defer rows.Close()
7272
var uid, name, g string
73-
rows.Next()
74-
rows.Scan(&uid, &name, &g)
73+
for rows.Next() {
74+
rows.Scan(&uid, &name, &g)
7575

76-
p, _ := wkt.Unmarshal(g)
76+
p, _ := wkt.Unmarshal(g)
7777

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

84-
fmt.Printf(`
84+
fmt.Printf(`
8585
id
8686
%s
8787
name
@@ -93,4 +93,5 @@ func (r *RegionService) ShowRegion(id string, outputInJSON bool) {
9393
geometry
9494
%v
9595
`, uid, name, p.(*geom.Polygon).Coords())
96+
}
9697
}

0 commit comments

Comments
 (0)