Sergi Pérez
Sergi Pérez

Reputation: 1

Intersection between spatial lines and spatial polygon to identify

I am trying to identify the spatial lines that intersect with the polygon to eliminate them

I have tried various packages to do this work but all of them are giving the same error "Error in RGEOSBinPredFunc(spgeom1, spgeom2, byid, func) : IllegalArgumentException: point array must contain 0 or >1 elements"

Spatial lines and polygon can be downloaded from this link https://drive.google.com/drive/folders/1CWzZzZucBjhfAthApnOGgSEL7dLXlPt5

library (rgeos)

lines_onland<-gIntersection(my.lines, polygons,byid=TRUE) "Error in RGEOSBinPredFunc(spgeom1, spgeom2, byid, func) : IllegalArgumentException: point array must contain 0 or >1 elements"

lines_onland<-gIntersect(my.lines, polygons,byid=TRUE) "Error in RGEOSBinPredFunc(spgeom1, spgeom2, byid, func) : IllegalArgumentException: point array must contain 0 or >1 elements"

library(raster)

lines_onland<- raster::intersect(my.lines, polygons) Error in RGEOSBinPredFunc(spgeom1, spgeom2, byid, func) : IllegalArgumentException: point array must contain 0 or >1 elements

Lines crossing land

Upvotes: 0

Views: 298

Answers (1)

Robert Hijmans
Robert Hijmans

Reputation: 47181

Data

library(raster)
my.lines <- shapefile("my.lines.shp")
polygons <- shapefile("polygons.shp")

First find out where the error occurs

err <- NULL
for (i in 1:length(my.lines)) {
    x <- try(raster::intersect(my.lines[i,], polygons))
    if (class(x) == "try-error") {
        err <- c(err, i)
    }
}

err
#[1]  48  59 191 294

The error occurs for four lines that consist of a single location, which makes them invalid

geom(my.lines[err,])
#     object part cump         x        y
#[1,]      1    1    1 -28.53502 38.53658
#[2,]      2    1    2 -28.53413 38.53638
#[3,]      3    1    3 -28.54550 38.53687
#[4,]      4    1    4 -28.53340 38.53602

Remove the invalid lines and things work as they should

x <- raster::intersect(my.lines[-err,], polygons) 

Upvotes: 0

Related Questions