I want to subset out a shapefile (the .shp and associated files are here) into another one bounded by a set of coordinates, say between longs [80,90] and lats [20,30], and t
Another way:
library(raster)
s <- shapefile("/path/asia_rivers.shp")
sub <- crop(s, extent(80, 90, 20, 30))
shapefile(sub, 'cropped_rivers.shp')
I know this has been answered, but I think you can do exactly what you want using PBSmapping
. PBSmapping
has a function to clip Polysets (for polygon and lines 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 )
The keep extra allows you to keep non-standard columns when you do the clipping (I assume for attribute data).
You could 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")