[GIS] R Spatial: Trouble with disjointed polygon that has self-intersections

polygonr

I have a shapefile which represents the areas of a city which have higher risks of flooding than others. This is a spatial polygon object however the polygon is disjoint and not connected. I also have a database of house locations in the city, and I want to develop a dummy variable to determine whether a house is located in any one of the disjointed parts of the polygon.

I've used the gContains(polygon, points) however I get the error

Error in RGEOSBinPredFunc(spgeom1, spgeom2, byid, func) : 
  TopologyException: side location conflict at -9.2199117665562582 38.693948116552484

Which I imagine has to do with the fact that it is not one single polygon but multiple fractions of the same shapefile polygon spread out over the city. Here is a picture of the problem I'm having. The blue areas representing the disjoint polygon and the points being those which I would like to determine if they are located in any of the shaded blue region. For completeness, I've also included the underlying city map.

enter image description here

Maybe the gContains is not the best function to use, but I haven't been able to come across anything to help me out. Happy to share the shapefiles and selection of the points database if that helps.


After running the "Check Geometries" in ArcGIS I see there are several self-intersections of the polygon. I ran the "Repair Geometries" and exported the resulting repaired shapefile to upload into R, however still get the same error message as before. Since these shapefiles were obtained from an external source, it may be that the files are not built in such a way to facilitate the type of analysis I am working on.


I was able to solve the problem with a better understanding of what was occuring with the help of Self intersections for polygons, how to solve?

Exporting the shapefile after running the Union code in ArcGIS removed the errors I was having in R.

Best Answer

I also had this same error using gContains(). I didn't realise I had anything internal in my polygon. I dissolved the internal structure like this:

spgeom1$rowNo = 1

spgeom1= unionSpatialPolygons(spgeom1, spgeom1$rowNo)

and went on to use gContains() with no problem.