tidwall / buntdb

BuntDB is an embeddable, in-memory key/value database for Go with custom indexing and geospatial support
MIT License
4.57k stars 289 forks source link

Question: Intersects with polygon #13

Closed josebalius closed 6 years ago

josebalius commented 8 years ago

Is it somehow possible to intersect queries with polygons? Do i have to create a custom indexer for that?

Thanks!

tidwall commented 8 years ago

It's not available today but I plan on having this feature in the very near future.

For now you will need to create a custom indexer and then filter out the intersecting polygons from inside the Intersects call.

For example, you could use the GeoJSON library from the Tile38 project:


// custom index function
func IndexGeoJSON(a string) (min, max []float64) {
    o, err := geojson.ObjectJSON(a)
    if err != nil {
        return []float64{0, 0}, []float64{0, 0}
    }
    bbox := o.CalculatedBBox()
    return []float64{bbox.Min.X, bbox.Min.Y}, []float64{bbox.Max.X, bbox.Max.Y}
}

// create the spatial index and bind it to the custom function
db.CreateSpatialIndex("poly", "poly:*", IndexGeoJSON) 

// provide a polygon, or some valid GeoJSON object that you want to intersect against.
polyJSON := `{"type":"Polygon","coordinates":[...]}`

// do the query
var intersectingKeys []string
polyObj, err := geojson.ObjectJSON(poly)
if err == nil {
    db.View(func(tx *buntdb.Tx) error {
        tx.Intersects("poly", polyJSON, func(key, val string) bool {
            valObj, err := geojson.ObjectJSON(val)
            if err == nil && valObj.Intersects(polyObj) {
                intersectingKeys = append(intersectingKeys, key)
            }
            return true
        })
        return nil
    })
}
josebalius commented 8 years ago

@tidwall awesome, yeah i started going down this path, i am generating an envelope/bbox for the poly, querying the points, and then filtering points that fall outside of the poly shape. Perf seems fine but it would be awesome to support it out of the box.

Let me know if i can help.

tidwall commented 8 years ago

I'm looking at creating a spatial index interface that handles Intersects, Within, and Nearby. I would likely provide GeoJSON and Well-Known Text/Binary out of the box. But the interface would allow for any format such as Topo or KML.

I'll keep you posted on progress.

eminden commented 7 years ago

@tidwall any news regarding this development?

tidwall commented 7 years ago

@eminden I got nothing at this time. Sorry. :(

eminden commented 7 years ago

I need k-nearest queries, is it possible to do it somehow on the current implementation?

tidwall commented 6 years ago

This issue is pretty old so I'm closing it. I've wanted to add more geospatial functionality but I just haven't found the time to do so. Feel free to reopen if needed.

Thanks!