Projection problem from gdal2tiles.py to R Leaflet - r

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

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.

How to successfully save leaflet map in a .png image?

Out of geocoordinates, I want to draw simple plain points on a city map and save the output as a .png. I'm on Linux Ubuntu 22.04LTS. For the first part I found a pretty brief and straightforward leaflet solution.
library(leaflet); library(htmlwidgets); library(webshot)
m <- leaflet() %>%
addTiles("https://{s}.basemaps.cartocdn.com/dark_all/{z}/{x}/{y}.png") %>%
setView(-.12, 51.50, zoom=13)
m$x$options <- append(m$x$options, list("zoomControl" = FALSE))
m <- m %>%
addCircleMarkers(c(-.11, -.12, -.13), c(51.48, 51.52, 51.50),
radius=1, color="red")
m
However, the second part, saving the .png fails. I tried this solution,
saveWidget(m, "temp.html", selfcontained=TRUE)
webshot("temp.html", file="Rplot.png", cliprect="viewport")
but - using phantomjs 2.1.1 - it gives me these errors:
Auto configuration failed
139740646049728:error:25066067:DSO support routines:DLFCN_LOAD:could not load the shared library:dso_dlfcn.c:185:filename(libproviders.so): libproviders.so: cannot open shared object file: No such file or directory
139740646049728:error:25070067:DSO support routines:DSO_load:could not load the shared library:dso_lib.c:244:
139740646049728:error:0E07506E:configuration file routines:MODULE_LOAD_DSO:error loading dso:conf_mod.c:285:module=providers, path=providers
139740646049728:error:0E076071:configuration file routines:MODULE_RUN:unknown module name:conf_mod.c:222:module=providers
Error in webshot("temp.html", file = "Rplot.png", cliprect = "viewport") :
webshot.js returned failure value: 1
In addition: Warning message:
In is.null(x) || is.na(x) : 'length(x) = 4 > 1' in coercion to 'logical(1)'
Using slightly older version phantomjs 1.9.8 gives no error, but the image is completely blank. I used these sources to install phantomjs on Ubuntu: 1.9.8, 2.1.1. I looked into this answer, according to which you have to install the dev version, but I didn't find it useful, because I don't know of a dev version of phantomjs.
I don't necessarily need a interactive leaflet, but I thought the dark theme looks cool and corresponds to the research topic of nocturnal crimes. Thus, I'm openish to a static solution if it looks similar (if it won't use Google).
Use the new package webshot2, which uses the Chrome or Chromium headless browser instead of phantomjs.

How to export sf object to GDB using RPyGeo in R (Windows)?

I have a bunch of sf objects I'd like to export to GDB from R. I'm running R 4.0.2 on Windows 10. In this case the sf objects are all vector point data. The main reasons to export to GDB are to keep longer field names (the shapefile truncation is very annoying), and because GDBs are more desirable storage locations for our workflows.
Yes, I know about the ArcGisBinding package. I've got it to work in a test script but it's pretty unstable - often crashing and requiring a restart of R. This is a problem, because the sf objects I'd like to export come after an already long Rmd that reads in, formats and cleans the data. So it's not a simple manner of re-running the script until arc.write doesn't break. I could break up the script, but then I'd still have to read in a bunch of shapefiles. One option I haven't yet explored is using reticulate to call a python script instead of trying to do everything in R, but we're trying to do our analysis all in one place, if possible.
I'm pretty sure I've managed to set up RPyGeo appropriately, first setting my python path using the reticulate package. I'm doing it this way because IT restrictions means I can't edit PATH variables on my machine.
#package calls
library(sf)
library(spData)
library(reticulate)
#set python version in reticulate
py_path <- "C:/Program Files/ArcGIS/Pro/bin/Python/envs/arcgispro-py3/python.exe"
reticulate::use_python(python = py_path, required = TRUE)
#call RPyGeo
library(RPyGeo) # for potential point export
#output gdb
out.gdb <- "C:/LOCAL_PROJECTS/Output/Output.gdb"
#RPyGeo Parameters
# Note that, in order to use RPyGeo you need a working ArcMap or ArcGIS Pro installation on your computer.
# python path - note that this will change depending on which version of Arc one is using
# py_path <- "C:/Program Files/ArcGIS/Pro/bin/Python/envs/arcgispro-py3/python.exe"
arcpy <- rpygeo_build_env(workspace = out.gdb,
overwrite = TRUE,
extensions = c("Spatial","DataInteroperability"),
path = py_path)
I've tried a bunch of different tools to export an sf object, here using dummy data also used in the RPyGeo vignette
data(nz, package = "spData")
arcpy$Copy_management(in_data = nz,out_data = "nz_test")
arcpy$Copy_management(in_data = nz,out_data = file.path(out.gdb,"nz"))
arcpy$FeatureClassToGeodatabase_conversion(Input_Features = nz,Output_Geodatabase = out.gdb)
arcpy$FeatureClassToFeatureClass_conversion(in_features = nz,out_path = out.gdb,out_name = "nz")
arcpy$QuickExport_interop(Input = nz,Output = file.path(out.gdb,"nz"))
arcpy$CopyFeatures_management(in_features = nz,out_feature_class = file.path(out.gdb,"nz"))
arcpy$CopyFeatures_management(in_features = nz,out_feature_class = "nz")
Each time I get an error, for example:
Error in py_call_impl(callable, dots$args, dots$keywords) :
RuntimeError: Object: Error in executing tool
Detailed traceback:
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 3232, in CopyFeatures
raise e
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\management.py", line 3229, in CopyFeatures
retval = convertArcObjectToPythonObject(gp.CopyFeatures_management(*gp_fixargs((in_features, out_feature_class, config_keyword, spatial_grid_1, spatial_grid_2, spatial_grid_3), True)))
File "C:\Program Files\ArcGIS\Pro\Resources\ArcPy\arcpy\geoprocessing\_base.py", line 511, in <lambda>
return lambda *args: val(*gp_fixargs(args, True))
I'm not an expert in ArcPy by any means. Nor am I an expert in tracing errors inside packages. Am I making a simple syntax mistake? Is there something else that I'm missing? Any help would be much appreciated!

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.

ggvis, Error in normalizedPath()

I am running one of the "interactive" examples from the ggvis webpage (http://ggvis.rstudio.com/ggvis-basics.html#introduction) and I obtain an error.
I loaded the ggvis, dplyr and shiny packages.
Here is the code that I ran:
mtcars %>% ggvis(~wt) %>% layer_histograms(binwidth = input_slider(0, 2, step = 0.1))
And here is the output that I get.
Showing dynamic visualisation. Press Escape/Ctrl + C to stop.
Error in normalizePath(directoryPath, mustWork = TRUE) :
path[1]="/Builds/CRAN-QA-Simon/packages/mavericks-x86_64/Rlib/3.1/ggvis/www/ggvis": No such file or directory
I am using RStudio Version 0.98.507, and R version 3.1.0; Platform: x86_64-apple-darwin13.1.0 (64-bit).
I just found a similar thing trying to run a shiny app from RStudio.
My workaround was to devtools::install_packages("rstudio/ggvis").
I'll put a link to this from the ggvis google group.
Hope this helps

Resources