18

I would like to generalize a SpatialPolygonsDataFrame in R to make plotting more efficient. The polygons I have result in huge pdf plots that are slow to load and hard to integrate into a document.

I tried to use the gSimplify function from the rgeos package, but unfortunately it does not conserve the topology of the polygons.

Is there an alternative that I can use?

3
  • not really a solution, but could you plot to raster (jpg/png) instead? This stackoverflow question which discusses pdfsize reduction may be of use: stackoverflow.com/questions/8521299/…
    – djq
    Aug 5, 2012 at 1:37
  • plotting the maps as raster image is an option, but whenever possible I would like to use vector graphics. The image quality is better when using vector graphics, especially when looking at the digital version of documents.
    – yellowcap
    Aug 6, 2012 at 10:00
  • gSimplify() has an optional parameter topologyPreserve which defaults to FALSE. Have you tried setting that to TRUE?
    – krlmlr
    Jan 22, 2013 at 16:34

2 Answers 2

7

There is a discussion about this on r-sig-geo. For a definitive answer you should ask there, because there are people who know the insides of spatial R.

But, you can also do this in GIS desktop applications (export the shape using writeOGR command from rgdal or writePolyShape() from maptools) like QGIS, GRASS or SAGA.

For QGIS use Vector / Geometry Tools / Simplify geometries (I have tested and does not preserve the topology, but applied to Romania admin1 shapefile, looks fine with 2000 nodes).

For GRASS use v.generalize (read the manual for info about the algorithms, there are some).

For SAGA you must first convert the polygon to line (Shapes - Lines / Convert Polygons to Lines), then simplify lines (Shapes Lines / Line Simplification), and finally convert lines to polygons (Shapes - Polygons / Convert Lines to Polygons). I have tested this and the results has the topology preserved.

0
2

There is now the wonderful rmapshaper package by Andy Teucher that includes a simplify function that "performs topologically-aware multi-polygon simplification".

From their github repo, a usage example:

states_simp <- ms_simplify(states_sp)

where states_sp is a spatialPolygons* object.

See the package README for more information: https://github.com/ateucher/rmapshaper

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.