Esempio n. 1
0
func cellIdsToJSON(w http.ResponseWriter, ids []s2.CellID) {
	covering := []CellIDJSON{}
	for _, id := range ids {
		idJson := CellIDJSON{}
		cell := s2.CellFromCellID(id)
		center := s2.LatLngFromPoint(cell.Center())
		for i := 0; i < 4; i++ {
			ll := s2.LatLngFromPoint(cell.Vertex(i))
			idJson.Shape[i].Lat = ll.Lat.Degrees()
			idJson.Shape[i].Lng = ll.Lng.Degrees()
		}
		idJson.LL.Lat = center.Lat.Degrees()
		idJson.LL.Lng = center.Lng.Degrees()
		idJson.Id = strconv.FormatUint(uint64(cell.Id()), 10)
		idJson.IdSigned = strconv.FormatInt(int64(cell.Id()), 10)
		idJson.Token = cell.Id().ToToken()
		idJson.Pos = strconv.FormatUint(cell.Id().Pos(), 10)
		idJson.Face = cell.Id().Face()
		idJson.Level = cell.Id().Level()
		covering = append(covering, idJson)
	}
	enc := json.NewEncoder(w)
	if err := enc.Encode(covering); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}
Esempio n. 2
0
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}
}