如何在R中导入openstreetmaps形状文件并提取经/重心? [英] How do I import an openstreetmaps shape file in R and extract lat/long centroids?
问题描述
我正在尝试使用rgdal包在R中导入一个openstreetmaps形状文件. 我下载的形状文件包含5个组成部分:
I am trying to import an openstreetmaps shape file in R using the rgdal package. The shape file I downloaded has 5 components in it:
places.cpg
places.dbf
places.prj
places.shp
places.shx
可以在以下位置访问文件:
The files can be accessed at the following location:
https://drive.google.com/open?id=0B1ITb_7lHh1EUFVfVWc4ekRfSnc
我必须执行以下操作: 1)导入形状文件 2)在多边形的情况下,提取形状的点或质心的纬度/经度 3)将经纬度对附加到dbf文件中以进行一些分析
I have to do the following: 1) Import the shape file 2) Extract lat/long of the point or centroid of shape in case of polygon 3) Attach the lat/long pair to the dbf file to do some analysis
我停留在导入本身的第一阶段,正在运行以下代码:
I am stuck in the first stage of import itself, I am running the following code:
shape1 <- readOGR(dsn = "try", layer = "places")
"try"是我的工作目录中的文件夹,上面提到的openstreetmaps中的所有5个"places"文件都位于该文件夹中.
Here 'try' is the folder in my working directory where all the 5 'places' file from openstreetmaps mentioned above are located.
执行此操作时出现以下错误:
I get the following error when doing this:
Error in readOGR(dsn = "try", layer = "places") : no features found
In addition: Warning message:
In ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv =
use_iconv, : ogrInfo: all features NULL
我需要有关此导入的帮助.另外,如果有一种方法可以直接从一个场所形状文件中提取纬度/经度,我只需在excel中打开places.dbf文件并添加纬度/经度即可.
I need help with this import. Alternatively if there is a way to directly extract lat/long from one of the places shape file, I can just open the places.dbf file in excel and add the lat/long.
推荐答案
当您遇到readOGR()函数的问题时:另一种可能性是使用maptools
包.它创建SpatialXXXDataFrames
,因此您可以使用rgeos
等中的所有功能.
When you are facing troubles with the readOGR() function: Another possibility is to use the maptools
package. It creates SpatialXXXDataFrames
so you can use all the functions from rgeos
etc.
library(maptools)
setwd("/your/data/path/")
places <- readShapeSpatial("places")
# ... your geospatial functions, like
plot(places)
可能您将不得不调整空间数据的投影.对于OSM-Data,您需要在 spatialreference.org 中找到WGS84(或EPSG:4326)的proj4string
.
probably you will have to adjust the projection of the spatial data. For OSM-Data you will need to find the proj4string
for WGS84 (or EPSG:4326) at spatialreference.org.
因此您可以在R中调整投影:
Thus you can adjust your projection in R:
proj4string(places) <- CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
有关多边形质心的计算/提取",请参见此帖子.
For the calculation/"extraction" of polygon centroids see this post.
这篇关于如何在R中导入openstreetmaps形状文件并提取经/重心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!