R / GIS: how to multiply a shapefile using a bounding box with a latitudinally long interval?

I want to multiply the shapefile (.shp and related files here ) into another, limited by a set of coordinates, say between long [80,90] and armor [20,30], and then write this as another shapefile. If I use the maptools package:

df = readShapeLines("/path/asia_rivers.shp")

and then look at the file structure using as.data.frame(df) , I cannot find an obvious way to subset the coordinates. I can use the PBSmapping package for a subset:

 df = importShapefile("/path/asia_rivers.shp") df_sub = subset(df, X>=80 & X<=90 & Y >=20 & Y <=30) 

but then I can’t get him to force it to a SpatialLines data SpatialLines , which can be exported via writeSpatialShape() to maptools . I keep getting this error: Error in PolySet2SpatialLines(df_sub) : unknown coordinate reference system . Of course, I'm missing something very basic and should there be an easy way to subset geodata by geo-coordinates?

+6
source share
3 answers

You can try the following:

 library(rgeos) rivers <- readWKT("MULTILINESTRING((15 5, 1 20, 200 25), (-5 -8,-10 -8,-15 -4), (0 10,100 5,20 230))") bbx <- readWKT("POLYGON((0 40, 20 40, 20 0, 0 0, 0 40))") rivers.cut <- gIntersection(rivers, bbx) plot(rivers, col="grey") plot(bbx, add=T, lty=2) plot(rivers.cut, add=T, col="blue") 
+6
source

I know this was answered, but I think you can do exactly what you want using PBSmapping . PBSmapping has a function for the Polysets clip (for the polygon and line data) so you can try:

 df <- importShapefile("/path/asia_rivers.shp") df_sub <- clipLines(df, xlim = c(80 , 90) , ylim = c(20 , 30), keepExtra = TRUE ) dfSL <- PolySet2SpatialLines( df_sub ) 

Holding additionally allows you to save non-standard columns when performing cropping (I assume attribute data).

+2
source

Another way:

 library(raster) s <- shapefile("/path/asia_rivers.shp") sub <- crop(s, extent(80, 90, 20, 30)) shapefile(sub, 'cropped_rivers.shp') 
+1
source

Source: https://habr.com/ru/post/911420/


All Articles