Skip to content

Error using unionSpatialPolygons

5 messages · Karl Ove Hufthammer, Roger Bivand, Ariel Ortiz-Bobea

#
Hello,

I was recycling some code I used a few months ago that involved using the
unionSpatialPolygons() of the maptools package. Back then I didn't have a
problem, now I get the following error:
Loading required package: rgeos
Loading required package: stringr
rgeos: (SVN revision 330)
 GEOS runtime version: 3.3.3-CAPI-1.7.4 
 Polygon checking: TRUE 
 WARNING! if you turn polygon checking off, and polygons are
 not valid in GEOS, you risk losing data as your R session may crash! 

Error: TopologyException: found non-noded intersection between LINESTRING
(-76.0029 36.5547, -75.9398 36.5547) and LINESTRING (-75.9742 36.5261,
-75.9857 36.5547) at -75.985664369999995 36.55470657

I tried an approach in an older message to a similar problem but didn't work
(see
http://r-sig-geo.2731867.n2.nabble.com/Problem-with-aggregating-UScensus2000-FL-data-td6641334.html).

Any help would be great.

Ariel

----- Here is my reproducible code: --------------------------------------

# packages
library(maps)
library(maptools)

# Import NC counties in "map" format
   usa_counties   <- map('county', region = c('north carolina'), plot=FALSE,
fill=TRUE)

# extract data from map
   data  <- data.frame(id=1:length(usa_counties$names),
name=usa_counties$names, polyname=gsub("\\s","",
sapply(strsplit(usa_counties$names,":"), function(x) x[1]))  ) # create name
id for merge   

# create county id (akin a fips code)
   data$fips <- sapply(1:dim(data)[1], function(x)
match(as.character(data$polyname)[x],unique(as.character(data$polyname))) )

# This is the key county with several polygons
   data[27:29,]

# convert to SptialPolygons
   usa_counties   <- map2SpatialPolygons(usa_counties, data$id, proj4string
= CRS(as.character(NA)))

# Merge polygons belonging to same county
# This is where the error occurs:
   usa_counties <- unionSpatialPolygons(usa_counties, data$fips)

----- My Session info ---------------------------------------------
R version 2.15.0 (2012-03-30)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] rgeos_0.2-5     stringr_0.6     maptools_0.8-14 lattice_0.20-6 
sp_0.9-99       foreign_0.8-49 
[7] maps_2.2-5     

loaded via a namespace (and not attached):
[1] grid_2.15.0  plyr_1.7.1   tools_2.15.0



-----
Ariel Ortiz-Bobea
PhD Candidate in Agricultural & Resource Economics
University of Maryland - College Park
--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Error-using-unionSpatialPolygons-tp7580881.html
Sent from the R-sig-geo mailing list archive at Nabble.com.
#
Ariel Ortiz-Bobea skreiv:
The data in the ?maps? package are probably buggy. Use the vector data from
http://www.naturalearthdata.com/ instead. They are of great quality and 
completely free (public domain).
#
On Mon, 3 Sep 2012, Karl Ove Hufthammer wrote:

            
The state boundaries also have issues with dangles, despite passing the 
topology checks used when the maps package installs raw boundaries.

Another source of US boundary data is the US Census:

http://www.census.gov/geo/www/cob/

Roger

  
    
#
I think this will do it. I was looking for a quick way to do this without
involving a shape file but your suggestions is the clean/robust way. Thanks.
Ariel



-----
Ariel Ortiz-Bobea
PhD Candidate in Agricultural & Resource Economics
University of Maryland - College Park
--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Error-using-unionSpatialPolygons-tp7580881p7580886.html
Sent from the R-sig-geo mailing list archive at Nabble.com.