Vague warning message when extracting tiff data - r

I've unpacked the following libraries
library(BIOMASS)
library(readr)
library(sp)
library(raster)
library(rgdal)
library(ncdf4)
library(tidyverse)
I've succesfully stacked a tiff file
s1 <- stack('XXX.tif')
I now want to extract this using
s1_e <- extract(s1,pspcoord,method='bilinear')
I get the following warning message when running previous line
In proj4string(x) : CRS object has comment, which is lost in output
NOTE: Earlier in the code i had run the following line concerning proj4string
plotcoord <- SpatialPointsDataFrame(AGCplot[,3:4], AGCplot[,1:2],proj4string=CRS('+proj=longlat'))
pspcoord<-spTransform(plotcoord, CRS('+proj=utm +zone=21'))
Q: What does this warning message mean?

Related

RStudio, Rstudio.cloud Errors reading shapefiles

I've loaded multiple packages / tried numerous methods to read a .shp. None of the methods work and I'm unable to decipher google related suggestions. Running the code in R, the system freezes and the same code in RStudio.cloud only returns various error messages. A list of the packages and code are below. Ultimately, I'm trying to create maps, join with another file and produce a table of the results. Any suggestions would be appreciated.
library(rgeos)
library(sp)
library(rgdal)
library(maptools)
library(sf)
require(sf)
area <- read_sf(dsn = ".", layer = "2020ATLclip")
require(rgdal)
shape <- readOGR(dsn= ".", layer = "2020ATLclip.shp")
shape <- readOGR(dsn= ".", layer = "tl_2021_13_concity.shp")
library(raster)
l = readShapePoly("2020ATLclip.shp")
l = readShapePoly("tl_2021_13_concity.shp")
atl.poly <- readOGR("./data", layer = "tl_2021-13_concity.shp")
Cannot open layer 2020ATLclip
Error in CPL_read_ogr(dsn, layer, query, as.character(options), quiet, :
Opening layer failed.
In addition: Warning messages:
1: Unable to open /cloud/project/2020ATLclip.shx or /cloud/project/2020ATLclip.SHX. Set SHAPE_RESTORE_SHX config option to YES to restore or create it. (GDAL error 4)
2: Failed to open file /cloud/project/2020ATLclip.shp. It may be corrupt or read-only file accessed in update mode. (GDAL error 4)
>
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv, :
Cannot open layer
l = readShapePoly("2020ATLclip.shp")
Error in getinfo.shape(filen) : Error opening SHP file
In addition: Warning message:
readShapePoly is deprecated; use rgdal::readOGR or sf::st_read
> l = readShapePoly("tl_2021_13_concity.shp")
Error in getinfo.shape(filen) : Error opening SHP file
In addition: Warning message:
readShapePoly is deprecated; use rgdal::readOGR or sf::st_read
For both readOGR (from the rgdal package) and st_read (from the newer sf package), when importing a shapefile the dsn= parameter must point to the shp file - with extension *.shp. The layer= parameter is unnecessary with shapefiles. And you must also have the other components of the shapefile format in the same directory: the *.shx, *.dbf, and *.prj. Assuming you have these files in the current working directory, then...
If you insist on using the rgdal library:
library(rgdal)
shape <- readOGR(dsn= "2020ATLclip.shp")
# returns an object of class Spatial*DataFrame from sp
Preferably you should switch to sf, then
library(sf)
shape <- st_read(dsn = "2020ATLclip.shp")
# returns an object of class sf

Cause of warning when trying to simplify polygons?

I've got a leaflet map and I'm plotting some polygons on top of it. I'm trying to use rmapshaper::ms_simplify() to decrease the loading time for the map.
polygons_ll <- (
spTransform(polygons, CRS("+proj=longlat +datum=WGS84"))
)
polygons_ll <- ms_simplify(polygons_ll)
This is all within a R shiny application, which doesn't run after encountering the warning:
Warning in sp::proj4string(sp) :
CRS object has comment, which is lost in output
The app was functioning before the addition of the ms_simplify().
any advice is appreciated. Thanks
After doing some research and trial and error:
rgdal::set_rgdal_show_exportToProj4_warnings(FALSE)
will suppress the warning.
I ended up using:
crs <- CRS("+proj=longlat +datum=WGS84")
polygons_ll <- polygons %>%
rgeos::gSimplify(tol=25, topologyPreserve=TRUE) %>%
spTransform(crs)
# overwrite untransformed polygons with transformed polygons
polygons#polygons <- polygons#polygons
Maps are much faster to load now.

Error in Merging two shape files (a spatial polygons and spatial points) in R

My goal is to combine two shapefiles and extract the combined variables. However, I am getting the following error when I am trying to combine the two files: Error in as.vector(x): no method for coercing this S4 class to a vector
Below is my code:
#libraries
library(rgdal)
library(sp)
library(arulesViz)
library(rgdal)
library(raster)
library(maptools)
DHSshp <- readOGR("MWGE7AFL.shp")
district <- readOGR("mwi_admbnda_adm2_nso_20181016.shp")
summary(DHSshp)
summary(district)
is(DHSshp)
is(district)
subs_union <- union(DHSshp, district)
OR
pts.poly <- point.in.polygon(DHSshp, SAshp)
I failed to rule out that error after trying different ways around, I am hopeless. Please help
I hope this concern could be overcome by using the function
point.in.poly() of an r package 'SpatialEco',
i.e, use
SpatialEco::point.in.poly(DHSshp, district).
Regards!

Convert raster to im object

I would like to convert a raster to an im object but have so far been unable to do so.
Here is a reproducible example:
Create raster layer and add data to it:
r.toy <- raster(ncol=40, nrow=20)
r.toy[] <- rnorm(n=ncell(r.toy))
Plot the raster:
plot(r.toy)
Convert to image:
r.toy.im <- as.im(r.toy)
I receive the following error:
Error in as.im.default(r.toy) : Can't convert X to a pixel image
You just need to load the maptools package which can convert many spatial
formats in R:
library(raster)
library(spatstat)
library(maptools)
r.toy <- raster(ncol=40, nrow=20)
r.toy[] <- rnorm(n=ncell(r.toy))
r.toy.im <- as.im(r.toy)
plot(r.toy.im)
Created on 2018-11-11 by the reprex package (v0.2.1)

How can I convert from HDF (Harmonized Landsat and Sentinel-2 product) to TIFF in R?

I am a beginner in R and now I am working in the product at NASA: Harmonized Landsat and Sentinel-2. I downloaded the image in .hdf, but when I tried to convert in R to tiff, received an error.
I'm using this code:
library(gdalUtils)
library(rgdal)
library(raster)
gdal_setInstallation(verbose=TRUE)
getOption("gdalUtils_gdalPath")
setwd(dir = "C:/Users/HOME/Documents/TESTE")
getwd()
sds <- get_subdatasets('HLS.L30.T23LLG.2016004.v1.3.hdf')
sds
gdal_translate(sds[1], dst_dataset = "band1.tif", a_nodata = -1000)
This is the error:
Warning message:
running command
'"C:\ProgramData\Anaconda2\Library\bin\gdal_translate.exe" -a_nodata
"-1000" -of "GTiff" "782" "band1.tif"' had status 1
The image can be downloaded here: https://hls.gsfc.nasa.gov/data/v1.3/sites/BAH/L30/2016/23LLG/

Resources