func PointInPolygon(ctx *gin.Context, conn *store.Connection, lat float64, lon float64) store.Response { chans := make([]<-chan int64, 0) search := geo.NewPoint(lat, lon) resp := conn.ListKeys(Constituency{}) keys := resp.Data.([]*datastore.Key) for _, k := range keys { if Cache[k.IntID()] == nil { con := &Constituency{Id: k.IntID()} resp = conn.Get(con) Cache[k.IntID()] = BuildPolygon(con) } chans = append(chans, Process(k.IntID(), search)) } resp.Data = new(map[string]string) for n := range merge(chans) { if n != 0 { con := &Constituency{Id: n} resp = conn.Get(con) con.Points = nil resp.Data = con } } return resp }
func BuildPolygon(con *Constituency) (polygon *geo.Polygon) { polygon = &geo.Polygon{} for _, p := range con.Points { gp := geo.NewPoint(p.Lat, p.Lng) polygon.Add(gp) } return polygon }
func (wof WOFFeature) DumpCoords(poly []interface{}) geo.Polygon { polygon := geo.Polygon{} for _, icoords := range poly { coords := icoords.([]interface{}) lon := coords[0].(float64) lat := coords[1].(float64) pt := geo.NewPoint(lat, lon) polygon.Add(pt) } return polygon }
func BenchmarkGeoKellyDunn(b *testing.B) { // Setup polygon := kdgeo.NewPolygon([]*kdgeo.Point{ kdgeo.NewPoint(0, 1), kdgeo.NewPoint(1, 2), kdgeo.NewPoint(2, 1), kdgeo.NewPoint(2, 0), kdgeo.NewPoint(1, -1), kdgeo.NewPoint(0, 0), kdgeo.NewPoint(0, 1), }) point := kdgeo.NewPoint(1, 0) b.ResetTimer() // Test for i := 0; i < b.N; i++ { if contains := polygon.Contains(point); !contains { b.Fatal("not contains") } } }
func (p *WOFPolygon) Contains(latitude float64, longitude float64) bool { pt := geo.NewPoint(latitude, longitude) contains := false if p.OuterRing.Contains(pt) { contains = true } if contains && len(p.InteriorRings) > 0 { wg := new(sync.WaitGroup) for _, r := range p.InteriorRings { wg.Add(1) go func(poly geo.Polygon, point *geo.Point) { defer wg.Done() /* File under yak-shaving: Some way to send an intercept to poly.Contains to stop the raycasting if any one of these goroutines gets the answer it needs independent the results of the others. Like I said... yaks. (20151028/thisisaaronland) */ if poly.Contains(point) { contains = false } }(r, pt) } wg.Wait() } return contains }