1

I want to plot a map of my region, with colour filled polygons using R. I have downloaded municipal (lines) and province (polygons) boundaries. I've used SAGA to merge municipal and province shapes to produce new boundaries (polygons) to read and plot with R and ggplot2.

I succesfully plotted the original province (polygons) but failed to plot new boundaries (polygons) with this error message:

Error en createPolygonsComment(p) : IllegalArgumentException: Invalid number of points in LinearRing found 3 - must be 0 or >= 4

Maybe I made some mistake when merging original polygons and lines to create the new boundaries. Shoud I check shp attributes?

You can find original boundaries here and newly created ones here

EDIT: Added link to data file lineas_limites_municipales_etrs89.dbf

And the original R code I used to plot original boundaries, not working for new ones (see error message above):

require("rgdal")
require("maptools")
require("ggplot2")
require("plyr")

# Reading municipal boundaries

 esp = readOGR(dsn=".", layer="lineas_limite_municipales_etrs89")

 muni=subset(esp, esp$PROV1 == "46" | esp$PROV1 == "12" | esp$PROV1 == "3")
 muni@data$id = rownames(muni@data)
 muni.points = fortify(muni,region="id")
 muni.df = join(muni.points, muni@data, by="id")

# Reading province boundaries

prov = readOGR(dsn=".", layer="poligonos_provincia_etrs89")

pr=subset(prov, prov$CODINE == "46" | prov$CODINE == "12" | prov$CODINE == "03" )
pr@data$id = rownames(pr@data)
pr.points = fortify(pr, region="id")
pr.df = join(pr.points, pr@data, by="id")

ggplot(muni.df) + aes(long,lat,group=group) + geom_path(color="blue") +
coord_equal()+ geom_path(data=pr.df,   aes(x=long,y=lat,group=group),color="red", size=0.5)
  • There is a problem in boundaries.tar: lineas_limite_municipales_etrs89.dbf.gz is not a gzip file (it seems to be overwritten by a tar file which contain .prj, .shp, .shx files) – rcs Nov 7 '13 at 10:40
  • Hi @rcs Newly created tar data uploaded, hope it works now. Sorry for the inconvenience. – pacomet Nov 7 '13 at 10:50
  • doesn't work, lineas_limite_municipales_etrs89.dbf.gz is still a tar archive (in boundaries.tar) ... see here: pastebin.com/WZr9UfKg – rcs Nov 7 '13 at 15:30
  • @rcs Thanks for your patience. Added link just to the dbf file. I've extracted again from original downloaded zip file. – pacomet Nov 7 '13 at 15:47
  • Your code works perfectly fine for me with the data you provide. Generally, your error does point to a problem with the GEOS library not being able to deal with the geometry, but I cannot reproduce it. I have rgeos version 0.2-16 and GEOS runtime version: 3.3.3-CAPI-1.7.4. – cengel Nov 8 '13 at 18:22
1

The error message is telling that there is at least one corrupt polygon among your data, i.e., a polygon with more than 0 and less than 4 points (0 < points < 4). See Tidying feature geometries with sf for instructions on how to inspect and fix the data, if it is the case.

However, I could not download "original boundaries" (it seems the link is broken), but this is what I got (I think the same as @cengel) with data provided on second link (+ the .dbf file link):

require(rgdal)
require(maptools)
require(ggplot2)
require(plyr)

# Reading municipal boundaries

esp = readOGR(dsn="C:\\...\\boundaries", layer="lineas_limite_municipales_etrs89")

muni=subset(esp, esp$PROV1 == "46" | esp$PROV1 == "12" | esp$PROV1 == "3")
muni@data$id = rownames(muni@data)
muni.points = fortify(muni,region="id")
muni.df = join(muni.points, muni@data, by="id")

#Adapted ggplot function to existing data
ggplot(muni.df) + aes(long,lat,group=group) + geom_path(color="blue") +
coord_equal() + theme_bw()

enter image description here

  • Hi @andre-silva I have also plotted municipal boundaries but what I wanted was to create polygons by merging municipal lines and province polygons. When I have time I will restart creating the polygons and then post here again the right ones to try to find a solution. – pacomet Nov 20 '13 at 10:35

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.