Monday 29 July 2019

shapefile - merging 2 polygon adjacent to each other using R


I have several shp files which are the components (or tiles) of a map, I have joint all these tiles together to have a large shp file. Then I realized some of the polygon is now split into 2 halves, as the upper part belongs to a tile, while the lower part belongs to another tile.


This output disturbs me a lot as I need to fill some polygon, but not part of the "split polygon", so I need to find a way to merge these split polygon back to one (preferably mechanical way, as I have 10000+ polygons, not knowing which are split, which are not).



I need to know if the two or more polygons originate from the larger polygon. And I think I can somehow use union and spRbind to create an algorithm for this task. But I doubt if I am really the first one who encounters this problem, so I wonder if there is any existing tool in R that can solve this.


Thanks.



Answer



I can only provide a partial answer to your question.


There is a function in the maptools R package called unionSpatialPolygons, see here for a manual.


The function expects an ID field, and all polygons with the same ID value will be merged. If you have an ID that identifies your polygons to be dissolved, use it. Otherwise you could try something like


unifiedPolygons <- unionSpatialPolygons(myPolygons, rep(1, length(myPolygons))


but that will probably also dissolve all other polygons that touch within you area.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...