SpatialObjectToGeoJSON transforms a given SpatialObject to GeoJSON.
( so geopb.SpatialObject, maxDecimalDigits int, flag SpatialObjectToGeoJSONFlag, )
| 126 | |
| 127 | // SpatialObjectToGeoJSON transforms a given SpatialObject to GeoJSON. |
| 128 | func SpatialObjectToGeoJSON( |
| 129 | so geopb.SpatialObject, maxDecimalDigits int, flag SpatialObjectToGeoJSONFlag, |
| 130 | ) ([]byte, error) { |
| 131 | t, err := ewkb.Unmarshal([]byte(so.EWKB)) |
| 132 | if err != nil { |
| 133 | return nil, err |
| 134 | } |
| 135 | options := []geojson.EncodeGeometryOption{ |
| 136 | geojson.EncodeGeometryWithMaxDecimalDigits(maxDecimalDigits), |
| 137 | } |
| 138 | if flag&SpatialObjectToGeoJSONFlagIncludeBBox != 0 { |
| 139 | // Do not encoding empty bounding boxes. |
| 140 | if so.BoundingBox != nil { |
| 141 | options = append( |
| 142 | options, |
| 143 | geojson.EncodeGeometryWithBBox(), |
| 144 | ) |
| 145 | } |
| 146 | } |
| 147 | // Take CRS flag in order of precedence. |
| 148 | if t.SRID() != 0 { |
| 149 | if flag&SpatialObjectToGeoJSONFlagLongCRS != 0 { |
| 150 | crs, err := geomToGeoJSONCRS(t, true /* long */) |
| 151 | if err != nil { |
| 152 | return nil, err |
| 153 | } |
| 154 | options = append(options, geojson.EncodeGeometryWithCRS(crs)) |
| 155 | } else if flag&SpatialObjectToGeoJSONFlagShortCRS != 0 { |
| 156 | crs, err := geomToGeoJSONCRS(t, false /* long */) |
| 157 | if err != nil { |
| 158 | return nil, err |
| 159 | } |
| 160 | options = append(options, geojson.EncodeGeometryWithCRS(crs)) |
| 161 | } else if flag&SpatialObjectToGeoJSONFlagShortCRSIfNot4326 != 0 { |
| 162 | if t.SRID() != 4326 { |
| 163 | crs, err := geomToGeoJSONCRS(t, false /* long */) |
| 164 | if err != nil { |
| 165 | return nil, err |
| 166 | } |
| 167 | options = append(options, geojson.EncodeGeometryWithCRS(crs)) |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | return geojson.Marshal(t, options...) |
| 173 | } |
| 174 | |
| 175 | // SpatialObjectToWKBHex transforms a given SpatialObject to WKBHex. |
| 176 | func SpatialObjectToWKBHex(so geopb.SpatialObject) (string, error) { |
no test coverage detected