SymbolixAU / geojsonsf

Conversion between sf and geojson
Other
82 stars 7 forks source link

GEOMETRYCOLLECTIONS with ZM attributes are handled at the individual geometry #39

Closed SymbolixAU closed 6 years ago

SymbolixAU commented 6 years ago
js <- '{
  "type":"GeometryCollection",
  "geometries":[
  {
    "type":"Polygon",
    "coordinates":[[[0,0,5],[1,0,5],[1,1,5],[0,1,5],[0,0,5]]]
  },{
    "type":"Point",
    "coordinates":[3,3]
  }
  ]
}'

sf <- geojson_sf( js )
sf
# Simple feature collection with 1 feature and 1 field
# geometry type:  GEOMETRY
# dimension:      XY
# bbox:           xmin: 0 ymin: 0 xmax: 3 ymax: 3
# epsg (SRID):    4326
# proj4string:    +proj=longlat +datum=WGS84 +no_defs
#                         geometry id
# 1 GEOMETRYCOLLECTION (POLYGON...  1

i.e, the POLYGON has XYZ and the POINT has XY.

This is valid GeoJSON.

Should the GEOMETRYCOLLECTION also inherit a Z dimension?

_Originally posted by @SymbolixAU in https://github.com/SymbolixAU/geojsonsf/issue_comments#issuecomment-429577091_

SymbolixAU commented 6 years ago
js <- '{"type":"GeometryCollection","geometries":[{"type":"MultiPolygon","coordinates":[[[[0,0],[0,1],[1,1],[1,0],[0,0,0,0]]]]},{"type":"Point","coordinates":[0,0]}]}'
sf <- geojson_sfc( js )

Given

  1. this is valid GeoJSON
  2. sf prints collectly
  3. it converts to WKT
  4. sf gives a useful error when trying to plot
sf
# Geometry set for 1 feature 
# geometry type:  GEOMETRY
# dimension:      XY
# bbox:           xmin: 0 ymin: 0 xmax: 1 ymax: 1
# epsg (SRID):    4326
# proj4string:    +proj=longlat +datum=WGS84 +no_defs
# GEOMETRYCOLLECTION (MULTIPOLYGON ZM (((0 0 0 0,...
sf::st_as_text( sf )
# [1] "GEOMETRYCOLLECTION (MULTIPOLYGON ZM (((0 0 0 0, 0 1 0 0, 1 1 0 0, 1 0 0 0, 0 0 0 0))), POINT (0 0))"
plot( sf )
# Error in CPL_geos_is_empty(st_geometry(x)) : 
#   GEOS does not support XYM or XYZM geometries; use st_zm() to drop M

I'm going to keep this behaviour.