Unable to read shapefiles with st_read() in R - r

I am trying to read a shapefile
library(sf)
wards <- read_sf("Shape_files/benguluru_ward_boundaries_reprojected.shp")
and I am getting this error
Error in CPL_get_z_range(obj, 3) : z error - expecting three columns;
I googled it and it seems it has to do with z-dimension but I couldn't find a solution. How to solve this? Thanks in advance.
Edit:
This is what I am getting when I load the sf package
> library(sf)
Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
you can get the files here
Edit2: I was able to solve this by running extract z values as mentioned here and there dropping it while exporting the layer. I am able to read that new layer without any issues.

Related

spTransform() in R warnings with PROJ

I'm in R v 4.0.4 - sp v 1.4-5, and rgdal 1.5-23. I'm loading a shapefile using the rgdal package and then trying to simply apply spTransform() but I'm getting a ridiculous set of warnings that I can't get rid of. Anyone have any insight on this?
options("rgdal_show_exportToProj4_warnings"="none")
library(rgdal)
library(sp)
## Shapefile has a projection of WGS84
X <- readOGR(dsn=".", layer="myshapefile.shp")
UTM30 <- sf::st_crs(32630)$proj4string
transf <- spTransform(X,UTM30)
############################################################
### This warning gets repeated tens to hundreds of times ###
proj_as_proj_string: C:\PostgreSQL\13\share\contrib\postgis-3.1\proj\proj.db lacks DATABASE.LAYOUT.VERSION.MAJOR / DATABASE.LAYOUT.VERSION.MINOR metadata. It comes from another PROJ installation.
proj_as_wkt: C:\PostgreSQL\13\share\contrib\postgis-3.1\proj\proj.db lacks DATABASE.LAYOUT.VERSION.MAJOR / DATABASE.LAYOUT.VERSION.MINOR metadata. It comes from another PROJ installation.
proj_create: C:\PostgreSQL\13\share\contrib\postgis-3.1\proj\proj.db lacks DATABASE.LAYOUT.VERSION.MAJOR / DATABASE.LAYOUT.VERSION.MINOR metadata. It comes from another PROJ installation.
Anyone come across this yet and managed to squash it?
Cheers
Found this explanation from rgdal:
The 'GDAL' and 'PROJ' libraries are external to the package, and, when installing the package from source, must be correctly installed first; it is important that 'GDAL' < 3 be matched with 'PROJ' < 6. From 'rgdal' 1.5-8, installed with to 'GDAL' >=3, 'PROJ' >=6 and 'sp' >= 1.4, coordinate reference systems use 'WKT2_2019' strings, not 'PROJ' strings.

r [sf] How to enable GEOS support?

I'm trying to transform the CRS of polygons that cross the dateline using the sf package following the example here, but get the warning
Warning message:
In CPL_wrap_dateline(x, options, quiet) :
GDAL Error 6: GEOS support not enabled.
which causes the final part of the code to fail:
library(sf)
#Linking to GEOS 3.6.1, GDAL 2.1.3, proj.4 4.9.3
point1 <- st_point(c(-179.5, -50))
point2 <- st_point(c(0, -30))
points <- st_sfc(point1, point2, crs = 4236)
points_trans <- st_transform(points, 32621)
circles <- st_buffer(points_trans, 100000)
circles_latlon <- st_transform(circles, 4326)
circles_latlon2 <- st_wrap_dateline(circles_latlon, options = c("WRAPDATELINE=YES"))
I've looked online on how to enable GEOS support (or perhaps just updating GDAL), but can't find a simple explanation. Any suggestions?
I'm running MACOS 10.13.6; R 3.5.0 (checked also with 3.5.1, but same issue).
I finally solved this by
downloading and installing the updated GDAL and GEOS libraries from here
downloading the source file of sf from cran
(re)installing sf using R CMD INSTALL sf_0.6-3.tar.gz --configure-args='--with-gdal-config=/Library/Frameworks/GDAL.framework/Versions/2.2/unix/bin/gdal-config -with-geos-config=/Library/Frameworks/GEOS.framework/Versions/3B/unix/bin/geos-config'. Note that I needed to (re)add the .gz extension as apple conveniently hides it.

Error: isTRUE(gpclibPermitStatus()) is not TRUE

This question may be a duplicate of an earlier unanswered one. I still have the problem.
I am trying to use a zipcode shapefile and coming up with the following error:
tract <- readOGR(dsn = ".", layer = "cb_2013_us_zcta510_500k")
tract<-fortify(tract, region="GEOID10")
Error: isTRUE(gpclibPermitStatus()) is not TRUE
I have tried installing gpclib to fix this but then I get the following error:
install.packages("gpclib")
Installing package into ‘C:/Users/Nick/Documents/R/win-library/3.2’
(as ‘lib’ is unspecified)
Package which is only available in source form, and may need compilation of C/C++/Fortran: ‘gpclib’
These will not be installed
Help?
You can look at Hadley's master file for ggplot2/R/fortify-spatial.r. Based on this outside link, my understanding is that lines 31–34 (in it's current form) used to read something like
# Union together all polygons that make up a region
try_require(c("gpclib", "maptools"))
unioned <- unionSpatialPolygons(cp, invert(polys))
So back then one way to attack the problem was to turn on the license
library(rgdal)
library(maptools)
if (!require(gpclib)) install.packages("gpclib", type="source")
gpclibPermit()
As #rcs, #Edzer Pebesma, and this answer mention, rgeos should resolve the issue for more recent installations.
I ran into the same problem but the solution was slightly different than those listed above.
As mentioned by others, the issue is the dependency on gpclib, which is required by maptools.
However, after loading maptools, it provided the following message ...
> library('maptools')
Checking rgeos availability: FALSE
Note: when rgeos is not available, polygon geometry computations in maptools depend on gpclib,
which has a restricted licence. It is disabled by default;
to enable gpclib, type gpclibPermit()
So rgeos can be used instead of gpclib. To resolve, I did the following ...
install.packages('rgeos', type='source')
install.packages('rgdal', type='source')
The reinstallation of rgdal removes the dependency on gpclib and points to rgeos.
Hope this is helpful.
I learned this answer elsewhere: I had to type
install.packages("gpclib", type="source")
and it worked just fine.

Errors working through plotting shapefiles in the ggplot wiki

I'm trying to learn how to plot shapefiles with ggplot2 and I found a wiki at:
https://github.com/hadley/ggplot2/wiki/plotting-polygon-shapefiles
I downloaded and extracted the zip file provided into new directory on my desktop ( http://www.epa.gov/nheerl/arm/documents/design_doc/ecoregion_design.zip ).
I started reading through the article, going line by line but I'm running into a problem with fortify.
require("rgdal")
require("maptools")
require("ggplot2")
require("plyr")
> my_dsn <- "directory here"
>
> utah = readOGR(dsn=my_dsn, layer="eco_l3_ut")
OGR data source with driver: ESRI Shapefile
Source: "directory here", layer: "eco_l3_ut"
with 10 features and 7 fields
Feature type: wkbPolygon with 2 dimensions
> utah#data$id = rownames(utah#data)
> utah.points = fortify(utah, region="id")
Error: isTRUE(gpclibPermitStatus()) is not TRUE
I looked at the ?fortify help page and get the jist of how it should be used. I think its being used correctly. I also found another question with the same error on SO,
Getting error while using fortify function in R (ggplot)
but I can't figure out how to apply what is being said for my solution. I might help if I had access to dropbox right now but I don't.
The short answer is to install the rgeos package and include it at the top.
A longer answer can be found here (and elsewhere): https://stat.ethz.ch/pipermail/r-sig-geo/2012-July/015685.html

Shapefiles in R

I'm relatively new to using Shapefiles in R. I want to create visualizations of country and district level data for India for which I'm using maptools in R. For the shapefiles, I got the data from gadm.org which includes state/district/subdistrict level data for India.
However, I'm trying to load the file in R and it's just not happening.This is my code :
library(maptools)
dist <- readShapePoly("IND_adm2.shp")
Here IND_adm2.shp is the district level shape file for India.
R is unable to read the file, giving me this error:
"Error in getinfo.shape(filen) : Error opening SHP file"
I'm not really sure what the problem is.I have R 3.0.1, have the files in the appropriate directory but something's just not right. I'm also new to making maps in R so it's been a bit annoying. Any ideas on how to fix this?
You could use the package rgdal
library(rgdal)
library(sp)
shp <- readOGR("path/to/shpfiles", "IND_adm2")
plot(shp)

Resources