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

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.

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

Mapview in R and problem with projections/crs in Pacific area

I am trying to plot data in R using mapView for the grid in the Pacific that crosses the longitude 180deg. The native crs is "+proj=merc +lon_0=150 +lat_ts=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0".
The example of equivalent data is:
test.coords<-as.data.frame(cbind(c(runif(15,-180,-130),runif(5,160,180)),runif(20,40,60)))
test.sp <- SpatialPointsDataFrame(coords = cbind(test.coords$V1,test.coords$V2), data = test.coords,
proj4string = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))
test.sp <- spTransform(test.sp,
"+proj=merc +lon_0=150 +lat_ts=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0")
plot(test.sp)
mapview(test.sp)
When using plot function, the points are centered, while using mapView they get split between two sides of the map (linked image). Can the view using mapView be centered on a different longitude?
Using native crs for this map does not help. I get an error.
mapview(plot, native.crs=TRUE)
Warning message:
sf layer is not long-lat data
Thank you
mapView image Pacific
Ok, so this is a semi-optimal solution to your question for several reasons:
this will only work for point data
it may not scale well for large point collections
projecting to another CRS may not work anymore (though that is not really related, as the issue at hand is about visualising)
For sets of lines or polygons, we would need another approach, but I am currently not aware of a sp/sf native solution to round-tripping coordinates of an object.
library(sp)
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
library(mapview)
test.coords<-as.data.frame(cbind(c(runif(15,-180,-130),runif(5,160,180)),runif(20,40,60)))
test.sp <- SpatialPointsDataFrame(coords = cbind(test.coords$V1,test.coords$V2), data = test.coords,
proj4string = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"))
test_sf = st_as_sf(test.sp)
shift = function(x) {
geom = st_geometry(x)
st_geometry(x) = st_sfc(
lapply(seq_along(geom), function(i) {
geom[[i]][1] = ifelse(geom[[i]][1] < 0, geom[[i]][1] + 360, geom[[i]][1])
return(geom[[i]])
})
, crs = st_crs(geom)
)
return(x)
}
mapview(shift(test_sf)) +
mapview(test_sf, col.regions = "orange")
Created on 2019-12-11 by the reprex package (v0.3.0)
I chose to use sf rather than sp for this because mapview uses sf internally and was hoping to find a general approach to this problem which could potentially be integrated.
Here is a workaround for polygons - it produces some warnings, but overall does the job.
# transform grid to standard latitude and longitude
# original grid in crs "+proj=merc +lon_0=150 +lat_ts=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0"
grid<-spTransform(grid,"+init=epsg:4326")
summary(coordinates(grid))
# V1 V2
# Min. :-179.8 Min. :37.17
# 1st Qu.:-168.8 1st Qu.:51.70
# Median :-154.3 Median :55.02
# Mean :-118.0 Mean :54.65
# 3rd Qu.:-131.4 3rd Qu.:58.31
# Max. : 179.8 Max. :65.56
out <- lapply(1:length(grid), function(i) grid[i, ])
for (i in 1:length(grid)) {
cds <- slot(slot(slot(out[[i]], "polygons")[[1]], "Polygons")[[1]], "coords")
cds_polygon <- Polygons(list(Polygon(cds)), ID="out.x")
out.x <- SpatialPolygons(list(cds_polygon))
proj4string(out.x) <- CRS("+init=epsg:4326")
if (cds[1,1]>0) { # depending to which side one want to move polygons that are on both sides of International Date Line
out.y <- elide(out.x, shift=c(-360,0))
} else {
out.y<-out.x}
if(i==1){grid.shifted<-out.y} else {
grid.shifted<-raster::union(grid.shifted,out.y)
}
}
# add data in the original grid
grid.shifted <- SpatialPolygonsDataFrame(grid.shifted, grid#data, match.ID = F)
mapview(grid.shifted)

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.

Layer won't rasterize using raster package in r

I'm having trouble rasterizing a shapefile using the raster package in R.
shp<-shapefile(ZoneShape);
lcRas<-raster(lcRaster);
r<-raster(ncol=ncol(lcRas), nrow=nrow(lcRas), crs=CRS);
res(r)<-res(lcRas);
extent(r)<-extent(lcRas);
>r
class : RasterLayer
dimensions : 22610, 27959, 632152990 (nrow, ncol, ncell)
resolution : 1, 1 (x, y)
extent : 554739, 582698, 3837197, 3859807 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=15 +ellps=GRS80 +datum=NAD83 +units=m +no_defs +towgs84=0,0,0
shp$GID<-1:nrow(shp);
> shp
class : SpatialPolygonsDataFrame
nfeatures : 1
extent : 554838, 582597.6, 3837297, 3859707 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=15 +datum=NAD83 +units=m +no_defs +ellps=GRS80 +towgs84=0,0,0
nvariables : 14
names : SP_ID, NAME, Shape_Leng, Shape_Area, GID
>zoneRas<-rasterize(shp, r, "GID")
The following error is returned:
trying to get slot "coords" from an object of a basic class ("NULL") with no slots.
Can anyone see what I'm missing/screwing up here?
Thanks
This is a bug that happens when the SpatialPolygonsDataFrame object only has a single geometry. Here is a work-around:
zoneRas <- rasterize(shp, r, shp$GID)

Resources