Yet another point in polygon package. Piper makes use of ray casting and does account for holes in polygons.
go get -u github.com/iwpnd/piper
Vanilla
package main
import (
"fmt"
"github.com/iwpnd/piper"
)
func main() {
p := []float64{0.5,0.5} // [longitude, latitude]
polygon := [][][]float64{{{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}}
pip := piper.Pip(p, polygon)
fmt.Printf("Point in Polygon: %+v\n", pip)
}
Or using github/paulmach/go.geojson
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"github.com/iwpnd/piper"
"github.com/paulmach/go.geojson"
)
func main() {
p := []float64{0.5, 0.5} // [longitude, latitude]
raw, err := ioutil.ReadFile("my_feature.geojson")
if err != nil {
panic("something went wrong")
}
var feature geojson.Feature
err = json.Unmarshal(raw, &feature)
if err != nil {
panic("something went wrong")
}
pip := piper.Pip(p, feature.Geometry.Polygon)
fmt.Printf("Point in Polygon: %+v\n", pip)
}
goos: darwin
goarch: amd64
pkg: github.com/iwpnd/piper
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkPipSimpleInside-12 43108360 27.73 ns/op
BenchmarkPipSimpleOutside-12 44025870 27.53 ns/op
BenchmarkPipSimpleInsideWithHoles-12 27355524 42.84 ns/op
BenchmarkPipSimpleOutsideWithHoles-12 42239286 28.22 ns/op
BenchmarkPipComplexInside-12 474601 2323 ns/op
MIT
Benjamin Ramser - @iwpnd
Project Link: https://github.com/iwpnd/piper
Phillip Lemons - Ray Casting Algorithm
Great introduction into the topic with good visualisations.