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?

mgri
  • 16,159
  • 6
  • 47
  • 80
yellowcap
  • 3,013
  • 22
  • 36
  • not really a solution, but could you plot to raster (jpg/png) instead? This stackoverflow question which discusses pdfsize reduction may be of use: http://stackoverflow.com/questions/8521299/reduce-pdf-file-size-of-plot-in-r/8524610#8524610 – djq Aug 05 '12 at 01: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 06 '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

2 Answers2

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.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Niculita Mihai
  • 670
  • 3
  • 11
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

Phil
  • 1,129
  • 2
  • 11
  • 15