Merging two spatial polygon objects
On Thu, 5 Nov 2009, Roger Bivand wrote:
On Thu, 5 Nov 2009, BRWIN338 at aol.com wrote:
Good Evening I have two spatial polygon objects. The first "StMaps50" contains state level map data for the 50 US states. The second "CAMaps" contains provincial and territorial polygons for Canada. I would like to merge these two objects into a single Canada-US spatial polygon object that I could then write to a shapefile. The data fields in the @data component have been constructed with identical fields. I have tried using the maptools "spRbind" function but get the following error message:
With recent versions of sp, you can say: rownames(StMaps50) rownames(CAMaps)
Sorry, on checking: row.names(StMaps50) row.names(CAMaps) (rownames() is a method for matrices, row.names() for data.frames and friends). Roger
By default, the IDs for each Polygons object, corresponding to each row in the data.frame object in the "data" slot, are the feature IDs read in form file. In the case of shapefiles, these usually run from "0" to say n-1. So they will be the same. Use the spChFIDs() method in maptools to change the IDs of at least one of your objects to disambiguate them (say paste "US" in front of the US ones and "CAN" in front of the Canadian ones). I'm also assuming the the coordinate reference systems are identical. Hope this helps, Roger
spRbind(StMaps50,CAMaps)
Error in spRbind(as(obj, "SpatialPolygons"), as(x, "SpatialPolygons")) : non-unique polygon IDs I have attached additional information below. Any suggestions would be appreciated. Joe ##################################################
class(StMaps50)
[1] "SpatialPolygonsDataFrame" attr(,"package") [1] "sp"
_StMaps50 at data[1:5_ (mailto:StMaps50 at data[1:5) ,]
ctry sname sabbr sfips pfips 1 US Washington WA 53 NA 2 US Montana MT 30 NA 3 US Maine ME 23 NA 4 US North Dakota ND 38 NA 5 US South Dakota SD 46 NA
class(CAMaps)
[1] "SpatialPolygonsDataFrame" attr(,"package") [1] "sp"
_CAMaps at data_ (mailto:CAMaps at data)
ctry sname sabbr sfips pfips 0 CA Alberta ALB 61 CA01 1 CA British Columbia BCL 62 CA02 2 CA Manitoba MNT 63 CA03 3 CA New Brunswick NBR 64 CA04 4 CA Nova Scotia NSC 65 CA07 5 CA Northwest Territories NTR 66 CA06 6 CA Newfoundland NWF 67 CA05 7 CA Ontario ONT 68 CA08 8 CA Prince Edward Island PEI 69 CA09 9 CA Quebec QUE 70 CA10 10 CA Saskatchewan SSK 71 CA11 11 CA Yukon Territory YTR 72 CA12
################################################ [[alternative HTML version deleted]]
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at stat.math.ethz.ch https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: Roger.Bivand at nhh.no