Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Intersects with polygon #13

Closed
josebalius opened this issue Sep 28, 2016 · 7 comments
Closed

Question: Intersects with polygon #13

josebalius opened this issue Sep 28, 2016 · 7 comments

Comments

@josebalius
Copy link

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

Thanks!

@tidwall
Copy link
Owner

tidwall commented Sep 28, 2016

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
Copy link
Author

@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
Copy link
Owner

tidwall commented Sep 28, 2016

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
Copy link

eminden commented Mar 31, 2017

@tidwall any news regarding this development?

@tidwall
Copy link
Owner

tidwall commented Mar 31, 2017

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

@eminden
Copy link

eminden commented Apr 6, 2017

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

@tidwall
Copy link
Owner

tidwall commented Apr 30, 2018

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!

@tidwall tidwall closed this as completed Apr 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants