Skip to content
Prev 23623 / 29559 Next

Exporting Multipolygon geojson with rgdal::writeOGR

Hi Andy,
On Mon, 26 Oct 2015, Andy Teucher wrote:

            
Your example was very helpful, the problem was that when writeOGR() and 
the underlying compiled code was written, the model used was that of the 
ESRI Shapefile driver, rather than OGC SFS, which was first published in 
2004. Most older formats (and their drivers in OGR) use a OGR geometry 
factory function to organize polygons on reading - but this is not present 
in shapelib (bundled with the R maptools package), so when designing 
polygon classes for sp, and writing rgdal::writeOGR() about 10 years ago, 
this was not known, and was not revisited subsequently, until you happily 
spotted the bug.

GEOS in rgeos uses OGC SFS definitions (the ones you are using below), but 
the SpatialPolygons/Polygons/Polygon structure was already established by 
then, and a fix (using comment attributes in Polygons objects) was used, 
rather than a major change to Polygons objects (in 2010).

The revision to rgdal::writeOGR() now branches on the input objects - if 
they have comments (applied in code, not user-facing), these are trusted, 
and the MultiPolygon objects constructed containing one or more Polygon 
objects with one exterior ring and zero or more interior rings on that 
basis. However, if the input objects do not have comment attributes, 
OGRGeometryFactory::organizePolygons() (C++) is used to create properly 
organized MultiPolygon objects for export.

This means that the pre-OGC SFS drivers continue to work, but that the OGC 
SFS drivers, like GeoJSON, now also work correctly when the second ring in 
an object is not assumed to be an interior ring (rgdal < 1.1-1). A test 
set for all the possible cases has been introduced.

The revision is now in source package form on CRAN:

https://cran.r-project.org/web/packages/rgdal/index.html

and a Windows binary package will be available later on Tuesday I hope. 
The OSX revision may take a little longer, but we are grateful to have 
what we have anyway.

Thanks for a clear and very helpful bug report.

Best wishes,

Roger