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