-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): add geocondig api to govpolygon
- Loading branch information
Showing
6 changed files
with
201 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package govpolygon | ||
|
||
import ( | ||
"github.com/JamesLMilner/quadtree-go" | ||
geojson "github.com/paulmach/go.geojson" | ||
) | ||
|
||
type Quadtree struct { | ||
qt *quadtree.Quadtree | ||
ft map[quadtree.Bounds]string | ||
} | ||
|
||
func NewQuadtree(f []*geojson.Feature) *Quadtree { | ||
ft := map[quadtree.Bounds]string{} | ||
qt := &quadtree.Quadtree{ | ||
MaxObjects: 10, | ||
MaxLevels: 100, | ||
Objects: make([]quadtree.Bounds, 0), | ||
Nodes: make([]quadtree.Quadtree, 0), | ||
} | ||
|
||
for _, f := range f { | ||
b, ok := bounds(f.Geometry) | ||
if !ok { | ||
continue | ||
} | ||
|
||
qt.Insert(b) | ||
ft[b] = f.Properties["code"].(string) | ||
} | ||
|
||
return &Quadtree{ | ||
qt: qt, | ||
ft: ft, | ||
} | ||
} | ||
|
||
func (q *Quadtree) Find(lng, lat float64) (string, bool) { | ||
res := q.qt.RetrieveIntersections(quadtree.Bounds{ | ||
X: lng, | ||
Y: lat, | ||
}) | ||
if len(res) == 0 { | ||
return "", false | ||
} | ||
|
||
r, ok := q.ft[res[0]] | ||
return r, ok | ||
} | ||
|
||
func bounds(g *geojson.Geometry) (b quadtree.Bounds, _ bool) { | ||
if !g.IsMultiPolygon() && !g.IsPolygon() { | ||
return | ||
} | ||
|
||
if g.IsPolygon() { | ||
g = &geojson.Geometry{ | ||
Type: "MultiPolygon", | ||
MultiPolygon: [][][][]float64{g.Polygon}, | ||
} | ||
} | ||
|
||
minlat := -1.0 | ||
minlng := -1.0 | ||
maxlat := -1.0 | ||
maxlng := -1.0 | ||
|
||
for _, polygon := range g.MultiPolygon { | ||
for _, ring := range polygon { | ||
for _, p := range ring { | ||
lng := p[0] | ||
lat := p[1] | ||
|
||
if minlat == -1 || lat < minlat { | ||
minlat = lat | ||
} | ||
if minlng == -1 || lng < minlng { | ||
minlng = lng | ||
} | ||
|
||
if maxlat == -1 || lat > maxlat { | ||
maxlat = lat | ||
} | ||
if maxlng == -1 || lng > maxlng { | ||
maxlng = lng | ||
} | ||
} | ||
} | ||
} | ||
|
||
return quadtree.Bounds{ | ||
X: minlng, | ||
Y: minlat, | ||
Width: maxlng - minlng, | ||
Height: maxlat - minlat, | ||
}, true | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package govpolygon | ||
|
||
import ( | ||
"context" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestQuadtree(t *testing.T) { | ||
p := NewProcessor(filepath.Join(dirpath, "japan_city.geojson")) | ||
ctx := context.Background() | ||
f, _, err := p.ComputeGeoJSON(ctx, nil) | ||
assert.NoError(t, err) | ||
|
||
q := NewQuadtree(f.Features) | ||
res, ok := q.Find(139.760296, 35.686067) | ||
assert.True(t, ok) | ||
assert.Equal(t, "13101", res) | ||
} | ||
|
||
func BenchmarkQuadtree(b *testing.B) { | ||
p := NewProcessor(filepath.Join(dirpath, "japan_city.geojson")) | ||
ctx := context.Background() | ||
f, _, _ := p.ComputeGeoJSON(ctx, nil) | ||
q := NewQuadtree(f.Features) | ||
|
||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
_, _ = q.Find(139.760296, 35.686067) | ||
} | ||
} |