How do I import an openstreetmaps shape file in R and extract lat/long centroids?

不问归期 提交于 2020-01-04 05:39:29

问题


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

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

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

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.


回答1:


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

library(maptools)

setwd("/your/data/path/")

places <- readShapeSpatial("places")
# ... your geospatial functions, like
plot(places)

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.

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.




回答2:


You need to use the shapefile (.shp) as the DSN. Turning the SpatialPointsDataFrame into a "normal" data.frame will give "ugh" column names for lang & lat, so you can rename them to make them more usable:

library(sp)
library(rgdal)

places <- readOGR("places/places.shp", "places", verbose=FALSE, stringsAsFactors=FALSE)

places_df <- setNames(as.data.frame(places),
                      c("osm_id", "name", "type", "population", "longitude", "latitude"))

head(places_df)

##     osm_id          name    type population longitude latitude
## 1 25431184   Stockertown village         NA -75.26156 40.75446
## 2 25716549 Mechanicsburg village         NA -77.00473 40.21020
## 3 25762119     Mansfield  hamlet         NA -77.07929 41.80569
## 4 25840249  New Columbia  hamlet         NA -76.87368 41.03368
## 5 25840585  Williamsport    town      29381 -77.00277 41.24933
## 6 25930002       Hershey    town      14257 -76.65060 40.28549



回答3:


I couldn't get this package to work but the package "shapefiles" did the work for me. It has a read.shapefile function that imports shapefiles.



来源:https://stackoverflow.com/questions/36669528/how-do-i-import-an-openstreetmaps-shape-file-in-r-and-extract-lat-long-centroids

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!