https://github.com/iwpnd/piper
yet another point in polygon package
https://github.com/iwpnd/piper
geometry geometry-processing geospatial geospatial-processing golang
Last synced: 10 months ago
JSON representation
yet another point in polygon package
- Host: GitHub
- URL: https://github.com/iwpnd/piper
- Owner: iwpnd
- License: mit
- Created: 2022-02-10T22:42:35.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2022-10-15T08:20:15.000Z (over 3 years ago)
- Last Synced: 2025-03-15T15:39:40.554Z (11 months ago)
- Topics: geometry, geometry-processing, geospatial, geospatial-processing, golang
- Language: Go
- Homepage:
- Size: 36.1 KB
- Stars: 13
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# piper
Yet another point in polygon package. Piper makes use of ray casting and does account for holes in polygons.
## Installation
```
go get -u github.com/iwpnd/piper
```
## Usage
Vanilla
```go
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](https://github.com/paulmach/go.geojson)
```go
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)
}
```
## Benchmark
```
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
```
## License
MIT
## Maintainer
Benjamin Ramser - [@iwpnd](https://github.com/iwpnd)
Project Link: [https://github.com/iwpnd/piper](https://github.com/iwpnd/piper)
## Acknowledgement
Phillip Lemons - [Ray Casting Algorithm](http://philliplemons.com/posts/ray-casting-algorithm)
Great introduction into the topic with good visualisations.