paulmach / go.geo

Geometry/geography library in Go, DEPRECATED, use ->
https://github.com/paulmach/orb
MIT License
330 stars 55 forks source link

Is a Point within a Hexagon? #63

Closed sid6mathur closed 6 years ago

sid6mathur commented 6 years ago

This library is beautiful work Paul, much gratitude.

I see no way to check if a Point is within a hexagon using either go.geo or using go.geojson. Could such a helper function be added?

I have a Polygon-type Geometry handy with [][][]float64 underlying data guaranteed to be closed and hexagonal, but there is no way to check whether a Point is in it.

The overall topic is segmenting a city into hundreds of hexagons and binning samples into each of them. Equivalent functionality in Turf.js is hexGrid() for generating hexagons in a bounding box and then assigning points to bins with pointsWithinPolygon().

Thank you! Siddharth

paulmach commented 6 years ago

This library does not support that, and because of its design can't really do that easily. However, I am working on a new library that tries to fix these problems (and other). https://github.com/paulmach/orb

Using orb you'd take your raw geojson data and do something like

package main

import (
    "fmt"

    "github.com/paulmach/orb"
    "github.com/paulmach/orb/geojson"
    "github.com/paulmach/orb/planar"
)

func main() {
    point := orb.Point{1, 2} // some point to test
    rawGeoJSONFeature := []byte{}

    feature, err := geojson.UnmarshalFeature(rawGeoJSONFeature)
    if err != nil {
        panic(err)
    }

    polygon, ok := feature.Geometry.(orb.Polygon)
    if !ok {
        panic(fmt.Sprintf("geometry not a polygon, it's a %s", feature.Geometry.GeoJSONType()))
    }

    if planar.PolygonContains(polygon, point) {
        fmt.Printf("YES")
    } else {
        fmt.Printf("NO")
    }
}

I hope that helps.

I'm going to close this since I'm since it's question that I think I answered.