how do I read a shapefile into r in a format that leaflet can use - r

I'm trying to do some leaflet plots with San Francisco precinct shapefiles. It's worked before, but the precincts and their shapefiles have been updated & I can't seem to load them in a form that leaflet recognizes.
If I use st_read, the shapefile seems to load okay, but leaflet objects. If I do
sfprecincts <- readOGR(dsn=".",layer = "SF_DOE_PREC_2022_07_18_pg"), I get this warning message:
Warning messages:
1: In OGRSpatialRef(dsn, layer, morphFromESRI = morphFromESRI, dumpSRS = dumpSRS, :
Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=#null +wktext +no_defs
2: In OGRSpatialRef(dsn, layer, morphFromESRI = morphFromESRI, dumpSRS = dumpSRS, :
Discarded datum WGS_1984 in Proj4 definition: +proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=#null +wktext +no_defs
3: In showSRID(wkt2, "PROJ") :
Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=#null +wktext +no_defs +type=crs
4: In showSRID(wkt2, "PROJ") :
Discarded datum World Geodetic System 1984 in Proj4 definition

Related

mapview and plot show different output for the same rasterLayer, why?

Below is the rasterLayer RASTER_slope in a plot (4 NAs are shown in white):
Here is the metadata of RASTER_slope
class : RasterLayer
dimensions : 4, 4, 16 (nrow, ncol, ncell)
resolution : 500, 500 (x, y)
extent : 2227000, 2229000, 1316500, 1318500 (xmin, xmax, ymin, ymax)
crs : +proj=lcc +lat_0=12 +lon_0=-102 +lat_1=17.5 +lat_2=29.5 +x_0=2500000 +y_0=0 +datum=WGS84 +units=m +no_defs
source : memory
names : values
values : -143.2145, 214.1981 (min, max)
Then I used mapview to get a dynamic visualization of RASTER_slope:
> mapview(RASTER_slope)
Warning messages:
1: In showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj = prefer_proj) :
Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=#null +wktext +no_defs +type=crs
2: In showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj = prefer_proj) :
Discarded datum World Geodetic System 1984 in Proj4 definition
3: In showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj = prefer_proj) :
Discarded ellps WGS 84 in Proj4 definition: +proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=#null +wktext +no_defs +type=crs
4: In showSRID(uprojargs, format = "PROJ", multiline = "NO", prefer_proj = prefer_proj) :
Discarded datum World Geodetic System 1984 in Proj4 definition
The resulting mapview object looks like this:
The mapview object is displaying only 2 pixels with NA.
Probably I am wrong but I have the feeling that the key here is to pass a crs to RASTER_slope in a WKT format but I do not know how to do that.
Any help will be appreciated.
Following #dww, the key is in adding method="ngb" when calling mapview, no need to re project the rasterLayer, though. Here the code and output
mapview(RASTER_slope, method="ngb", na.color="transparent")

Converting from Behrmann CRS map to +proj=longlat WGS84 in R

I have a shapefile of grid cells of 200km x 200km covering land areas of the world in Behrmann Equal Area Cylindrical projection. My goal is to convert the shapefile to +proj=longlat WGS84 format so I can match it to maps in commonly used projections such as wrld_simpl in maptools. However, I have not been successful and would appreciate some help with this.
rm(list = ls())
library(RCurl)
library(raster)
library(maptools)
library(rgdal)
data("wrld_simpl")
tmp <- tempfile() download.file("https://github.com/darunabas/extras/blob/master/temp_shapefile.zip?raw=true", destfile = tmp)
unzip(tmp, exdir = ".")
s <- rgdal::readOGR("temp_shapefile")
proj4string(s) = CRS("+proj=cea +lon_0=0 +lat_ts=30 +x_0=0 +y_0=0 +datum=WGS84 +ellps=WGS84 +units=m +no_defs")
p <- spTransform(s, CRS("+proj=longlat +datum=WGS84"))
I got the following error:
non finite transformation detected:
[,1] [,2] [,3] [,4]
Error in .spTransform_Polygon(input[[i]], to_args = to_args, from_args = from_args, :
failure in Polygons 1106 Polygon 1 points
In addition: Warning message:
In .spTransform_Polygon(input[[i]], to_args = to_args, from_args = from_args, :
2 projected point(s) not finite
I'm not very experienced in the geo-worls, but this might help:
library( sf )
sf <- read_sf( "./temp_shapefile.shp")
st_crs( sf ) <- "+proj=cea +lon_0=0 +lat_ts=30 +x_0=0 +y_0=0 +datum=WGS84 +ellps=WGS84 +units=m +no_defs"
sf2 <- st_transform( sf, crs = "+proj=longlat +datum=WGS84" )

Unmatch raster and polygon data in R

The polygons (read in from a shapefile) are :
class : SpatialPolygonsDataFrame
features : 653
extent : -7115213, 4895580, 1368240, 7805331 (xmin, xmax, ymin,
ymax)
coord. ref. : +proj=aea +lat_1=55 +lat_2=65 +lat_0=50 +lon_0=-154
+x_0=0 +y_0=0 +datum=NAD27 +units=us-ft +no_defs +ellps=clrk66
+nadgrids=#conus,#alaska,#ntv2_0.gsb,#ntv1_can.dat
variables : 6
names : cat, NAME, AREA_MI, lccount, lcsum,
lcmean
min values : 1, Alaska, 1.006402e+00, 1.000000e+01, 0.000000e+00,
0.00000000
max values : 99, Alaska, 9.945810e-01, 9.900000e+01, 9.960000e+02,
12.00000000
The raster (elevation) data is
class : RasterLayer
dimensions : 6800, 9200, 62560000 (nrow, ncol, ncell)
resolution : 1305.521, 946.6311 (x, y)
extent : -7115213, 4895580, 1368240, 7805331 (xmin, xmax, ymin,
ymax)
coord. ref. : +proj=aea +lat_1=55 +lat_2=65 +lat_0=50 +lon_0=-154
+x_0=0 +y_0=0 +datum=NAD27 +units=us-ft +no_defs +ellps=clrk66
+nadgrids=#conus,#alaska,#ntv2_0.gsb,#ntv1_can.dat
data source : /Users/hong/Documents/GitHub/Alaska/akshd300m.tif
names : akshd300m
values : 0, 255 (min, max)
Does anyone how to solve this? I use tmap to plot this figure
Although the coordinate reference system is reported as +proj=aea +lat_1=55 +lat_2=65 +lat_0=50 +lon_0=-154 +x_0=0 +y_0=0 +datum=NAD27 +units=us-ft +no_defs +ellps=clrk66 for both data sets, clearly that cannot be true. It should be easy to figure out which one is correct by transforming known polygons to that crs. For example
library(raster)
library(rgdal)
usa <- getData('GADM', country='USA', level=1)
ak <- usa[usa$NAME_1 == 'Alaska', ]
ak_asa <- spTransform(ak, '+proj=aea +lat_1=55 +lat_2=65 +lat_0=50 +lon_0=-154 +x_0=0 +y_0=0 +datum=NAD27 +units=us-ft +no_defs +ellps=clrk66')
And plotting that one as well. Having established which one is correct (hopefully one of the two is), you can then try to trace back what went wrong with the other one. With the information you have provided we cannot figure that out for you.

R over(): Erreur : identicalCRS(x, y) is not TRUE but when checked CRS are the same

I have a problem to use the over function.
CRS.new=CRS("+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")
proj4string(ANFR_IDF)=CRS.new
proj4string(parti)=CRS.new
test=over(pc100m,parti)
> Erreur : identicalCRS(x, y) is not TRUE
crs(parti)
> CRS arguments:
+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
crs(ANFR_IDF)
> CRS arguments:
+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
test=over(parti,pc100m)
>Erreur : identicalCRS(x, y) is not TRUE
I don't understand what is hapenning. Any idea?
Many thanks!
I had a similar issue in my program when I used the same projection string.
I added "+init=epsg:4326" component at the beginning of the projection string and noticed some transformation operations (package concaveman in my case) remove this component, so identicalCRS function returned FALSE.
Try to include "+init" component in your projection string.

"Failure during raster IO" when converting raster to data.frame?

Anyone have any idea what may be happening? Here is my console printout. What's weird is that the first four as.data.frame lines run without issue but the last one gives me the "Failure during raster IO" error.
library("raster", lib.loc="~/R/win-library/3.3")
library("rgdal", lib.loc="~/R/win-library/3.3")
#> rgdal: version: 1.1-10, (SVN revision 622)
#> Geospatial Data Abstraction Library extensions to R successfully loaded
#> Loaded GDAL runtime: GDAL 2.0.1, released 2015/09/15
#> Path to GDAL shared files: C:/Users/JV-Desktop/Documents/R/win-library/3.3/rgdal/gdal
#> Loaded PROJ.4 runtime: Rel. 4.9.2, 08 September 2015, [PJ_VERSION: 492]
#> Path to PROJ.4 shared files: C:/Users/JV-Desktop/Documents/R/win-library/3.3/rgdal/proj
liMax <- as.data.frame(raster("D:/LiDAR/LiDAR/30_m/m_30_max.tif"), xy=TRUE)
CanopyBase <- as.data.frame(raster("D:/LiDAR/LiDAR/30_m/m_30_avg.tif") - raster("D:/LiDAR/LiDAR/30_m/m_30_std.tif"), xy=TRUE)
rawData.li <- merge(liMax,CanopyBase)
processed.li <- rawData.li[complete.cases(rawData.li),]
satMax <- as.data.frame(stack("D:/LiDAR/Landsat/For_Joe/Max_2015.tif"), xy=TRUE)
satMed <- as.data.frame(stack("D:/LiDAR/Landsat/For_Joe/Med_2015.tif"), xy=TRUE)
#> Error: Failure during raster IO
In case it helps here is the printout for the info on the last two files:
stack("D:/LiDAR/Landsat/For_Joe/Max_2015.tif")
# class : RasterStack
# dimensions : 5640, 8763, 49423320, 7 (nrow, ncol, ncell, nlayers)
# resolution : 30, 30 (x, y)
# extent : -1491300, -1228410, 2709840, 2879040 (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
# names : Max_2015.1, Max_2015.2, Max_2015.3, Max_2015.4, Max_2015.5, Max_2015.6, Max_2015.7
stack("D:/LiDAR/Landsat/For_Joe/Med_2015.tif")
# class : RasterStack
# dimensions : 5640, 8763, 49423320, 7 (nrow, ncol, ncell, nlayers)
# resolution : 30, 30 (x, y)
# extent : -1491300, -1228410, 2709840, 2879040 (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
# names : Med_2015.1, Med_2015.2, Med_2015.3, Med_2015.4, Med_2015.5, Med_2015.6, Med_2015.7
# min values : -0.3208517, -0.5767694, -0.5474564, ?, ?, ?, ?
# max values : 1, 1, 1, ?, ?, ?, ?
I hope this is enough info and I am asking the question correctly. I am new to stackoverflow and relatively new to R and coding in general. Thanks for any insight you can give.

Resources