3

I want to clip two SpatialPolygons on R without success, I followed the article in the disscution How to perform a true GIS clip of polygons layer using a polygon layer in R?

Polygone 01 :

> polygone1
class       : SpatialPolygons 
features    : 1 
extent      : -8.673868, 11.98891, 19.43771, 37.09514  (xmin, xmax, ymin, ymax)
coord. ref. : +init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 

Polygone 02 :

> Polygone2
class       : SpatialPolygons 
features    : 4 
extent      : 0.665381, 12.35598, 30.72105, 37.50386  (xmin, xmax, ymin, ymax)
coord. ref. : +init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 

Code :

clip2 <- gIntersection(Polygone1, Polygone2, byid=TRUE)

Erreur :

Error in RGEOSBinTopoFunc(spgeom1, spgeom2, byid, id, drop_not_poly, "rgeos_intersection") : 
  TopologyException: Input geom 0 is invalid: Self-intersection at or near point 8.1451386683704339 37.192197131616837 at 8.1451386683704339 37.192197131616837

Does anyone can help me?

zina_GIS
  • 763
  • 1
  • 14
  • 22
  • Do you intend to use Polygone1 for both arguments? What's the point of that? – Rich Wawrzonek Oct 23 '14 at 16:59
  • I edited my post, it was a mistake clip2 <- gIntersection(Polygone1, Polygone2, byid=TRUE) – zina_GIS Oct 23 '14 at 17:06
  • 2
    The error seems to say it all. The first polygon has a self-intersection and the command doesn't support that. You can check if a polygon is valid with gIsValid command. http://www.inside-r.org/packages/cran/rgeos/docs/RGEOSisValid – Rich Wawrzonek Oct 23 '14 at 17:12
  • thanks Rich ,how to correct it? – zina_GIS Oct 23 '14 at 17:29
  • I have ArcGIS so I would fix it there. I don't know if it can be done within R. Might be best to ask a new question and describe what file format you have and the software you have available. Search this site first, there might be an answer already. – Rich Wawrzonek Oct 23 '14 at 18:03

2 Answers2

7

Try:

polygone1 <- gBuffer(polygone1, byid=TRUE, width=0)
polygone2 <- gBuffer(polygone2, byid=TRUE, width=0)
clip2 <- gIntersection(Polygone1, Polygone2, byid=TRUE)

It is ugly, but it usually solves this kind of problem.

HTH

Mark Cupitt
  • 3,884
  • 4
  • 30
  • 54
alecamar
  • 106
  • 3
0

What we need is intersect function, not the gIntersection function. gIntersecion function provides a logical array, but intersect provides the actual intersect polygons.

Bharadwaj A K
  • 185
  • 1
  • 3