func (p WOFPointInPolygon) GetIntersectsByLatLon(lat float64, lon float64) []rtreego.Spatial { pt := rtreego.Point{lon, lat} bbox, _ := rtreego.NewRect(pt, []float64{0.0001, 0.0001}) // how small can I make this? results := p.Rtree.SearchIntersect(bbox) return results }
func (wof WOFFeature) EnSpatializeGeom() ([]*WOFSpatial, error) { id := wof.Id() name := wof.Name() placetype := wof.Placetype() deprecated := wof.Deprecated() superseded := wof.Superseded() spatial := make([]*WOFSpatial, 0) polygons := wof.GeomToPolygons() for offset, poly := range polygons { swlat := 0.0 swlon := 0.0 nelat := 0.0 nelon := 0.0 for _, pt := range poly.OuterRing.Points() { lat := pt.Lat() lon := pt.Lng() if swlat == 0.0 || swlat > lat { swlat = lat } if swlon == 0.0 || swlon > lon { swlon = lon } if nelat == 0.0 || nelat < lat { nelat = lat } if nelon == 0.0 || nelon < lon { nelon = lon } // fmt.Println(lat, lon, swlat, swlon, nelat, nelon) } llat := nelat - swlat llon := nelon - swlon pt := rtreego.Point{swlon, swlat} rect, err := rtreego.NewRect(pt, []float64{llon, llat}) if err != nil { return nil, err } sp := WOFSpatial{rect, id, name, placetype, offset, deprecated, superseded} spatial = append(spatial, &sp) } return spatial, nil }
// Parses /gj/<south>/<west>/<north>/<east>.json and returns a GeoJSON containing the object currently loaded func serveGeoJson(w http.ResponseWriter, r *http.Request) { url := r.URL.Path[len("/gj/"):] if strings.HasSuffix(url, ".json") { url = url[:len(url)-len(".json")] } params := strings.Split(url, "/") if len(params) != 4 { http.Error(w, "Invalid format for bbox", http.StatusInternalServerError) return } // String to float convertion paramsf := make([]float64, 4) for i, v := range params { f, err := strconv.ParseFloat(v, 64) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } paramsf[i] = f } bb, err := rtreego.NewRect(rtreego.Point{paramsf[1], paramsf[0]}, []float64{paramsf[3] - paramsf[1], paramsf[2] - paramsf[0]}) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } // results := rt_countries.SearchIntersect(bb) results := rt.SearchIntersect(bb) gjc := gjFeatureCollection{ Type: "FeatureCollection", Features: make([]gjFeature, len(results)), } for i, res := range results { obj, _ := res.(SpatialData) geod := obj.GetData() gjf, err := ToGjFeature(geod.Geom) if err != nil { continue } gjc.Features[i] = gjf gjc.Features[i].Properties = *geod } b, err := json.Marshal(gjc) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", "application/json") w.Write(b) }
func (p WOFPointInPolygon) GetIntersectsByLatLon(lat float64, lon float64) ([]rtreego.Spatial, time.Duration) { // Error checking on rect? pt := rtreego.Point{lon, lat} rect, _ := rtreego.NewRect(pt, []float64{0.0001, 0.0001}) // how small can I make this? return p.GetIntersectsByRect(rect) }
func (idx *WOFIndex) GetIntersectsByLatLon(lat float64, lon float64) []rtreego.Spatial { // Error checking on rect? pt := rtreego.Point{lon, lat} rect, _ := rtreego.NewRect(pt, []float64{0.0001, 0.0001}) // how small can I make this? return idx.GetIntersectsByRect(rect) }
//only two dim supported func NewNode(x, y, w, h float64, id int64, ext string) *Node { p := rtreego.Point{x, y} r, ok := rtreego.NewRect(p, []float64{w, h}) if ok != nil { return nil } return &Node{r, id, ext} }
func (p WOFPointInPolygon) GetIntersectsByBoundingBox(swlat float64, swlon float64, nelat float64, nelon float64) ([]rtreego.Spatial, time.Duration) { // Error checking on rect? llat := nelat - swlat llon := nelon - swlon pt := rtreego.Point{swlon, swlat} rect, _ := rtreego.NewRect(pt, []float64{llon, llat}) return p.GetIntersectsByRect(rect) }
func (idx *WOFIndex) GetIntersectsByBoundingBox(swlat float64, swlon float64, nelat float64, nelon float64) []rtreego.Spatial { // Error checking on rect? llat := nelat - swlat llon := nelon - swlon pt := rtreego.Point{swlon, swlat} rect, _ := rtreego.NewRect(pt, []float64{llon, llat}) return idx.GetIntersectsByRect(rect) }
// Returns the bounding box of the geos.Geometry as a rtreego.Rect func RtreeBboxg(g *geos.Geometry, tol float64) (*rtreego.Rect, error) { env, err := g.Envelope() if err != nil { return nil, err } shell, _ := env.Shell() if err != nil { return nil, err } c, err := shell.Coords() if err != nil { return nil, err } return rtreego.NewRect(rtreego.Point{c[0].X, c[0].Y}, []float64{math.Max(tol, c[2].X-c[0].X), math.Max(tol, c[2].Y-c[0].Y)}) }
func (wof WOFFeature) EnSpatialize() (*WOFSpatial, error) { id := wof.Id() name := wof.Name() placetype := wof.Placetype() deprecated := wof.Deprecated() superseded := wof.Superseded() body := wof.Body() var swlon float64 var swlat float64 var nelon float64 var nelat float64 children, _ := body.S("bbox").Children() if len(children) != 4 { return nil, errors.New("weird and freaky bounding box") } swlon = children[0].Data().(float64) swlat = children[1].Data().(float64) nelon = children[2].Data().(float64) nelat = children[3].Data().(float64) llat := nelat - swlat llon := nelon - swlon /* fmt.Printf("%f - %f = %f\n", nelat, swlat, llat) fmt.Printf("%f - %f = %f\n", nelon, swlon, llon) */ pt := rtreego.Point{swlon, swlat} rect, err := rtreego.NewRect(pt, []float64{llon, llat}) if err != nil { return nil, err } return &WOFSpatial{rect, id, name, placetype, -1, deprecated, superseded}, nil }
func (wof WOFFeature) EnSpatialize() (*WOFSpatial, error) { id := wof.WOFId() name := wof.WOFName() placetype := wof.WOFPlacetype() body := wof.Body() var swlon float64 var swlat float64 var nelon float64 var nelat float64 children, _ := body.S("bbox").Children() if len(children) != 4 { return nil, &WOFError{"weird and freaky bounding box"} } swlon = children[0].Data().(float64) swlat = children[1].Data().(float64) nelon = children[2].Data().(float64) nelat = children[3].Data().(float64) llat := nelat - swlat llon := nelon - swlon // fmt.Printf("%f - %f = %f\n", nelat, swlat, llat) // fmt.Printf("%f - %f = %f\n", nelon, swlon, llon) pt := rtreego.Point{swlon, swlat} rect, err := rtreego.NewRect(pt, []float64{llon, llat}) if err != nil { return nil, err } return &WOFSpatial{rect, id, name, placetype}, nil }
//search all nodes which intersect with [(x,y),(x+w,y+h)] //if you need rlock you should set need_lock to true, //it always works without lock in many case func (t *Tree) SearchIntersect(x, y, w, h float64, need_lock bool) []*Node { if need_lock { t.Mutex.RLock() defer t.Mutex.RUnlock() } p := rtreego.Point{x, y} r, ok := rtreego.NewRect(p, []float64{w, h}) if ok != nil { return nil } results := t.Rtree.SearchIntersect(r) nodes := []*Node{} for _, res := range results { if node, ok := res.(*Node); ok { ///TODO: filters here nodes = append(nodes, node) } else { panic("convert result to *Node fail") } } return nodes }