MCPcopy Create free account
hub / github.com/cockroachdb/cockroachdb-parser / SpatialObjectToGeoJSON

Function SpatialObjectToGeoJSON

pkg/geo/encode.go:111–156  ·  view source on GitHub ↗

SpatialObjectToGeoJSON transforms a given SpatialObject to GeoJSON.

(
	so geopb.SpatialObject, maxDecimalDigits int, flag SpatialObjectToGeoJSONFlag,
)

Source from the content-addressed store, hash-verified

109
110// SpatialObjectToGeoJSON transforms a given SpatialObject to GeoJSON.
111func SpatialObjectToGeoJSON(
112 so geopb.SpatialObject, maxDecimalDigits int, flag SpatialObjectToGeoJSONFlag,
113) ([]byte, error) {
114 t, err := ewkb.Unmarshal([]byte(so.EWKB))
115 if err != nil {
116 return nil, err
117 }
118 options := []geojson.EncodeGeometryOption{
119 geojson.EncodeGeometryWithMaxDecimalDigits(maxDecimalDigits),
120 }
121 if flag&SpatialObjectToGeoJSONFlagIncludeBBox != 0 {
122 // Do not encoding empty bounding boxes.
123 if so.BoundingBox != nil {
124 options = append(
125 options,
126 geojson.EncodeGeometryWithBBox(),
127 )
128 }
129 }
130 // Take CRS flag in order of precedence.
131 if t.SRID() != 0 {
132 if flag&SpatialObjectToGeoJSONFlagLongCRS != 0 {
133 crs, err := geomToGeoJSONCRS(t, true /* long */)
134 if err != nil {
135 return nil, err
136 }
137 options = append(options, geojson.EncodeGeometryWithCRS(crs))
138 } else if flag&SpatialObjectToGeoJSONFlagShortCRS != 0 {
139 crs, err := geomToGeoJSONCRS(t, false /* long */)
140 if err != nil {
141 return nil, err
142 }
143 options = append(options, geojson.EncodeGeometryWithCRS(crs))
144 } else if flag&SpatialObjectToGeoJSONFlagShortCRSIfNot4326 != 0 {
145 if t.SRID() != 4326 {
146 crs, err := geomToGeoJSONCRS(t, false /* long */)
147 if err != nil {
148 return nil, err
149 }
150 options = append(options, geojson.EncodeGeometryWithCRS(crs))
151 }
152 }
153 }
154
155 return geojson.Marshal(t, options...)
156}
157
158// SpatialObjectToWKBHex transforms a given SpatialObject to WKBHex.
159func SpatialObjectToWKBHex(so geopb.SpatialObject) (string, error) {

Callers 1

spatialObjectToJSONFunction · 0.92

Calls 4

geomToGeoJSONCRSFunction · 0.85
UnmarshalMethod · 0.65
SRIDMethod · 0.45
MarshalMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…