本文介绍了R/GIS:如何通过经纬度的边界框将shapefile子集化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想分出一个shapefile(.shp和相关文件位于此处)到另一个受一组坐标限制的坐标中,例如在long [80,90]和lats [20,30]之间,然后将其写为另一个shapefile.如果我使用maptools包:

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 then write this out as another shapefile. If I use the maptools package:

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

,然后用as.data.frame(df)查看文件的结构,我找不到通过坐标进行子集化的任何明显方法.我可以使用PBSmapping包作为子集:

and then look at the structure of the file with as.data.frame(df), I can't find any obvious way of subsetting by coordinates. I can use the PBSmapping package to subset:

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

,但是我似乎无法将其强制为SpatialLines数据框,该数据框可以通过maptools中的writeSpatialShape()导出.我不断收到此错误:Error in PolySet2SpatialLines(df_sub) : unknown coordinate reference system.当然,我缺少一些非常基本的知识,应该有一种简便的方法可以通过地理坐标子集地理数据吗?

but then I can't seem to be able to coerce this into a SpatialLines data frame which can be exported via writeSpatialShape() in maptools. I keep getting this error: Error in PolySet2SpatialLines(df_sub) : unknown coordinate reference system. Surely I am missing something very basic and there should be an easy way of subsetting geo-data by geo-coordinates?

推荐答案

您可以尝试以下操作:

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

这篇关于R/GIS:如何通过经纬度的边界框将shapefile子集化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 11:05