func SymmetricDifference(js geojson.GeoJSON) (*geojson.FeatureCollection, error) { polygons, err := geometryToPolygonList(js) if err != nil { return nil, err } var features []geojson.Feature for i := 0; i < len(polygons); i++ { a := polygons[i] for j := 0; j < len(polygons); j++ { if i == j { continue } var c s2.Polygon b := polygons[j] c.InitToDifference(a, b) a = &c } if a.NumLoops() > 0 { feat := geojson.Feature{ Typ: "Feature", Geometry: s2PolygonToGeometry(*a), } features = append(features, feat) } } fc := &geojson.FeatureCollection{ Typ: "FeatureCollection", Features: features, } return fc, nil }
func Union(js geojson.GeoJSON) (*geojson.FeatureCollection, error) { polygons, err := geometryToPolygonList(js) if err != nil { return nil, err } a := polygons[0] for i := 1; i < len(polygons); i++ { var c s2.Polygon b := polygons[i] c.InitToUnion(a, b) a = &c } return featureCollectionFromS2Polygon(a), nil }
func s2PolygonToGeometry(poly s2.Polygon) *geojson.Polygon { // Don't want nil rings for coordinates. rings := [][]geojson.Coordinate{} for i := 0; i < poly.NumLoops(); i++ { loop := poly.Loop(i) var ring []geojson.Coordinate for j := 0; j <= loop.NumVertices(); j++ { ll := s2.LatLngFromPoint(*loop.Vertex(j)) ring = append(ring, geojson.Coordinate{ll.Lng.Degrees(), ll.Lat.Degrees()}) } rings = append(rings, ring) } return &geojson.Polygon{Typ: "Polygon", Coordinates: rings} }
func Intersection(js geojson.GeoJSON) (*geojson.FeatureCollection, error) { polygons, err := geometryToPolygonList(js) if err != nil { return nil, err } var intersections []*s2.Polygon for i := 0; i < len(polygons); i++ { a := polygons[i] for j := i + 1; j < len(polygons); j++ { var c s2.Polygon b := polygons[j] c.InitToIntersection(a, b) intersections = append(intersections, &c) } } a := intersections[0] for i := 1; i < len(intersections); i++ { b := intersections[i] var c s2.Polygon c.InitToUnion(a, b) a = &c } return featureCollectionFromS2Polygon(a), nil }