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?

  • 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 '12 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 '12 at 10:00
  • gSimplify() has an optional parameter topologyPreserve which defaults to FALSE. Have you tried setting that to TRUE? – krlmlr Jan 22 '13 at 16:34

There is a discussion about this on r-sig-geo. For a definitive answer you should ask there, cause there are peoples which know the insights 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 QuantumGIS, GRASS or SAGA.

For QuantumGIS 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.

Cheers and good luck,

  • In the meantime I have also found an online service that performs simplification while conserving topologies: mapshaper.org. I have used it as alternative, as it seems that it is not possible in R directly (QGis is not really topology conserving and I don't know the other software packages) – yellowcap Aug 6 '12 at 15:41
  • 1
    I tested your observation with Saga 2.0.8, and the result doesn't have topology preserved! – Fabian Zeindl Apr 5 '13 at 13:57

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


that should help:

  • gSimplify(..., topologyPreserve=T) from the GEOS package
  • 2
    unfortunately the topology preserve option in gSimplify does not really preserve the topology over several polygons. I think it relates more to the topology of single or multipolygons. For a shapefile with many polygons, the topology is not conserved. – yellowcap May 29 '13 at 14:46

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.