R/GIS: How to subset a shapefile by a lat-long bounding box?

后端 未结 3 1703
广开言路
广开言路 2020-12-17 00:10

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

相关标签:
3条回答
  • 2020-12-17 00:17

    Another way:

    library(raster)
    
    s <- shapefile("/path/asia_rivers.shp")
    
    sub <- crop(s, extent(80, 90, 20, 30))
    
    shapefile(sub, 'cropped_rivers.shp')
    
    0 讨论(0)
  • 2020-12-17 00:29

    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).

    0 讨论(0)
  • 2020-12-17 00:30

    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")
    
    0 讨论(0)
提交回复
热议问题