r [sf] How to enable GEOS support? - r

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.

Related

Unable to read shapefiles with st_read() in 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.

Projection problem from gdal2tiles.py to R Leaflet

I do have a projection problem when making tiles with GDAL command and using the same for Leaflet in R. The steps I am following are:
I save the Tiff in QGIs with the following configuration:
In Centos Amazon Linux 2 and GDAL I Run the following command:
gdal2tiles.py --s_srs EPSG:3857-WGS84 usa_tif.tif tiles
After generating the tiles I get an openlayer.html which looks ok:
But when I add the file into Leafet in R I get the following:
Maybe you have an idea what is happening?
When I run:
which gdalinfo; gdalinfo --version
I get:
GDAL 3.2.1, released 2020/12/29
It was just a configuration in the Leaflet R code to:
tms = TRUE
addTiles(urlTemplate = "usa_tiles/{z}/{x}/{y}.png", group = "USA GDP", options = tileOptions(opacity = 0.6, tms = TRUE)) %>%

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.

Set geometry column name in sf::st_write()

As of sf version 0.9.0, st_write_db() and st_read_db() got defunct. The functions had the nice argument geom_name = 'wkb_geometry' which allowed a user to set the name of the simple feature column of the newly created table.
Can I still do this somehow with st_write() or do I have to do this in a separate call? E.g. for Postgres: ALTER TABLE x REANAME COLUMN geometry TO geom;
Maybe with the layer_options = argument? However I don't know where to look up its possibilities?
Local machine
sf_0.9-7 with GEOS 3.8.0, GDAL 3.0.4, PROJ 7.0.0.
Server
PostgreSQL 9.6.20
PostGIS
POSTGIS="2.3.3 r15473" GEOS="3.7.1-CAPI-1.11.1 27a5e771" PROJ="Rel. 4.9.3, 15 August 2016" GDAL="GDAL 2.2.3, released 2017/11/20" LIBXML="2.9.4" LIBJSON="0.12.1" TOPOLOGY RASTER
sf uses GDAL to read and write data. The GDAL documentation contains for every driver the list of possible layer creation options. For Postgres this can be found under https://gdal.org/drivers/vector/pg.html#layer-creation-options
Based on this layer_options="GEOMETRY_NAME=wkb_geometry" should do the trick for you.

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.

Resources