0

I have a list contains lot of polygon, points, and lines. Now I want to examine the area of the intersection between every polygon. I know i can use gIntersect, but my data is in data.frame and not in spatialpolygon file. Is there a smart way to do that?

 lon      lat  ID  
127.0167 29.14449   1
127.0148 29.14507   1
123.8638 17.63341   2
123.8593 17.62754   2
123.8568 17.63601   2
123.8528 17.65023   2
127.6790 49.01934   3
127.2719 49.12513   6
127.0249 49.14633   7
127.6763 49.02139   8
127.6710 49.02426   8
127.6684 49.02668   8
127.4648 49.07208  13
127.3757 49.08205  13
127.4198 30.04310  14
127.4259 40.04974  14
127.3136 39.09050  15
127.3197 29.09516  15
127.2360 39.16492  16
127.2099 49.16787  16
127.2266 29.01800  17
128.2771 48.36411  18
128.1930 28.44411  18
128.1925 18.44530  18
128.1928 28.44553  18
128.1932 48.44598  18 
128.1953 38.44774  18
128.1978 28.44532  18
125.7947 28.71272  19
125.7982 28.72078  19
125.8402 18.74029  19
125.8572 18.74141  19

Here is the list. It contains more data points in original file. Coordinates with same ID belongs together to the same polygon,points or line. I need to examine for every polygon the intersection with all other polygon in the list.

quallenjäger
  • 119
  • 1
  • 7
  • Have you considered this example here: https://stat.ethz.ch/R-manual/R-devel/library/mgcv/html/in.out.html – Tonio Liebrand Nov 21 '15 at 10:57
  • yes, my problem is converting this list to spatial data, so i can use gIntersection. Can I convert it with polygon(f,ID=ID)? with f the list above. I dont know how to identify each matrix with each other. – quallenjäger Nov 21 '15 at 11:23
  • Have you thought about using the sf package? https://stackoverflow.com/questions/57010155/intersection-of-polygons-in-r-using-sf – damo Jul 19 '19 at 10:38

1 Answers1

1

You may convert your data.frame to a SpatialPolygons.

library(sp)
polys <- lapply(unique(coords$ID), function(i) {
    Polygons(list(Polygon(coords[coords$ID==i, 1:2])), ID=i)
})
spa_polys <- SpatialPolygons(polys)

Also, I have found that gIntersects doesn't work for a point (one-point spatial polygon) or a line (two-point spatial polygon).

library(rgeos)
> gIntersects(spa_polys[13], spa_polys[1])
Error in RGEOSBinPredFunc(spgeom1, spgeom2, byid, func) : 
  IllegalArgumentException: Invalid number of points in LinearRing found 3 - must be 0 or >= 4
> gIntersects(spa_polys[13], spa_polys[2])
[1] FALSE
> gIntersects(spa_polys[13], spa_polys[3])
Error in RGEOSBinPredFunc(spgeom1, spgeom2, byid, func) : 
  IllegalArgumentException: point array must contain 0 or >1 elements
> gIntersects(spa_polys[13], spa_polys[13])
[1] TRUE
pe-perry
  • 2,591
  • 2
  • 22
  • 33
  • 1
    Thanks! thats my second problem, how can I examine if the data is a line or point or polygon and how can i filter them out of my set? – quallenjäger Nov 22 '15 at 01:36
  • @quallenjäger I don't know how we can examine whether an `SpatialPolygons` object is a line or point or polygon but we can filter them out before we convert `coords` from `data.frame` to `SpatialPolygons`. – pe-perry Nov 25 '15 at 09:29