go get github.com/zikwall/geofind
package main
import (
"fmt"
"github.com/zikwall/geofind"
"io/ioutil"
"time"
)
func main() {
start := time.Now()
countryJson, err := ioutil.ReadFile("example/data/countries/RU.json")
if err != nil {
panic(err)
}
//Load countries database to memory, example Russian
countryPolygons, err := geofind.Init(countryJson)
if err != nil {
panic(err)
}
point := geofind.Point{53.671362646, 82.18341123}
found := false
in := geofind.Properties{}
// Loop all features of country
for _, feature := range countryPolygons.Features {
if found == true {
break
}
// read method comment for help
if feature.IsMultiPolygonal() {
for _, polygon := range feature.GetAllPolygons() {
finder := &geofind.Polygon{Coordinates: polygon, Itterations: 0}
found = finder.In(point)
if found == true {
in = feature.Properties
break
}
}
continue
}
finder := &geofind.Polygon{Coordinates: feature.GetSinglePolygon(), Itterations: 0}
found = finder.In(point)
if found == true {
in = feature.Properties
}
}
fmt.Println(found)
fmt.Println(in)
fmt.Println(time.Since(start).Seconds())
}
If the test point is on the border of the polygon, this algorithm will deliver unpredictable results; i.e. the result may be “inside” or “outside” depending on arbitrary factors such as how the polygon is oriented with respect to the coordinate system. (That is not generally a problem, since the edge of the polygon is infinitely thin anyway, and points that fall right on the edge can go either way without hurting the look of the polygon.)