Quantcast
Channel: Question and Answer » gdal
Viewing all articles
Browse latest Browse all 397

writeOGR alters MultiPolygon holes

$
0
0

I’having trouble using writeOGR to write GeoJSON files, when there are MultiPolygon objects. Here is an example: I’m creating a SpatialPolygonsDataFrame object from GeoJSON, with a single feature made of two polygons. When I export to GeoJSON, the file that’s written has a single feature with one polygon and a hole.

Why is this, and how can I persuade writeOGR to write two polygons?

# Create SpatialPolygonsDataFrame with one feature, two polygons
spdf <- readOGR(layer='OGRGeoJSON', verbose=FALSE, '{
"type": "FeatureCollection",
"features": [{
 "type": "Feature",
 "id": 0,
 "properties": {"label":"CENTRAL"},
 "geometry": {
     "type": "MultiPolygon",
     "coordinates": [[[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]],
                     [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]]]
     } }] }')

# When I save and reload this object, coordinates has length 1,
# i.e. the feature is a polygon and a hole.
fn <- tempfile()
writeOGR(spdf, fn, layer='whatever', driver='GeoJSON')
length(fromJSON(file=fn)$features[[1]]$geometry$coordinates)

I’m using R 3.1.2 and rgdal 0.9.1 on OSX Yosemite.

UPDATE: Here is the GeoJSON tempfile that is created. As you can see, the coordinates array has been modified from the original.

{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
    { "type": "Feature",
      "id": 0,
      "properties": { "label": "CENTRAL" },
      "geometry": {
          "type": "MultiPolygon",
          "coordinates": [ [ [ [ 102.0, 2.0 ], [ 102.0, 3.0 ], [ 103.0, 3.0 ], [ 103.0, 2.0 ], [ 102.0, 2.0 ] ],
                             [ [ 100.0, 0.0 ], [ 100.0, 1.0 ], [ 101.0, 1.0 ], [ 101.0, 0.0 ], [ 100.0, 0.0 ] ] ] ] } }
    ]
}

Viewing all articles
Browse latest Browse all 397

Trending Articles