I have a stars raster object called raster. I am trying to run edge detection on it like:
image(rot90c(edge.detect(raster[[1]], thresh1=1, thresh2=80, noise="gaussian", noise.s=3, method="Canny")))
This works fine, but when I try to crop the raster against an sf object other_object using:
image(rot90c(edge.detect(st_crop(raster,other_object)[[1]], thresh1=1, thresh2=80, noise="gaussian", noise.s=3, method="Canny")))
I get errors due to NAs. Is there any way to create a cropped raster such that the edge detection method doesn't throw an error due to missing regions from the image?
Related
Using a raster data set and a shapefile to find landscape metrics within buffers of raster. Sometimes the extents overlay and sometimes they do not overlap (using the same code). The extents are not the same despite using the spTransform function. How do I solve this? in Arc they do overlap.
library(raster)
library(rgdal)
CDL2020 <- raster("CDL_2020_38.tif")
Buffer_10 <- readOGR("LAND_ANALYSIS_3.0/10km_Buffer_2020.shp")
#Set to same projection
Buffer_10 <- spTransform(Buffer_10,proj4string(CDL2020))
Using this code I imported and set to the same projections, however they are still different extents.
Package landscapemetrics can calculate area of each patch for a given raster file, shape of that patch and so on. I want to have not only tibble-frame with patch metrics calculated, but a new raster where each pixel within specific patch will have a value of the area of that patch, shape indicator and so on. We can do it with function spatialize_lsm() (it produces a Large list nested object with probably RasterObject objects within):
library(landscapemetrics)
plot(podlasie_ccilc) # this raster data is provided with package
podlasie.metrics.area <- spatialize_lsm(podlasie_ccilc, what = 'lsm_p_area') # creates a list
plot(podlasie.metrics.area) # produces an error...
How to get a desirable raster file with patch metrics from that list? I guess it is a question of raster package or something else, since landscapemetrics documentation tells nothing about this step.
I not that this data and new raster do not have resolution of the pixel like in meters (30, 30 for Landsat satellite image, for example). So we cannot plot the new raster produced:
podlasie.metrics.area[[1]]
plot(podlasie.metrics.area[[1]])
So I guess landscapemetrics cannot deal with such rasters, we can even use its function to check a suitability of the prior raster for patch discovering:
check_landscape(podlasie_ccilc)
Upd. I did it for the Landsat dataset with resolution 30, 30 and it produced patch area raster, but again I cannot open/show/save as raster it, because of the same error.
Package maintainer helps to solve a problem (yes, it is just related to the structure of list):
plot(podlasie.metrics.area[[1]]$lsm_p_area)
How do I use the multiple-polygonal of dust's sources in a wide area at google earth engine? (This polygon is separate and unmatched) The program runs but does not output the error occurred:
A region must be a GeoJSON Polygon or LinearRing. Got: 'GeometryCollection'.
I cannot use from box around area, because that area is big
From what i understand of your question you are trying to clip an image using multiple polygons in a same object. If that is the case, then it should not be an issue. You can clip the image with your multiple-polygons as follows
image = image.clip(multigeometry);
However, I assume that you are getting your error during exporting the clipped image. If this is the case then you probably got the error because u used the multi-geometry in "region" argument in the Export function. This is because the export REQUIRES you to have a polygon or linearRing to get the extents of your raster. So you need to use the box in this case. However, the exported image will have the pixels you clipped out as masked pixels.
If you would like to have a different image for different polygons then you could iterate through the polygons to create a clipped raster for each polygon and then export them.
I want to clip a raster using shapefile in r as a shape of shapefile. I used crop function but it clipping as extents not as a shape. Please help me
Crop, as stated in the manual pages, crops to the extent of the object,
for your purposes you may use raster::mask(), with the same syntaxis of crop
mask function "Creates a new Raster* object that has the same values as x, except for the cells that are NA (or other maskvalue) in a ’mask’."
You may pass inverse=T so you get those values out of the spatialPolygon.
I am trying to create a species distribution model in R. I have created raster layers in ArcMap and have imported them into R. They cannot be stacked unless the extents are exactly the same and they all have the same number of rows and columns.
However, when I alter these factors to successfully stack them they lose all their values and my stacked data frame is just filled with NAs.
Does anyone know how I can alter the extent and resolution of my raster layers so they can be successfully stacked -- so I can then attach environmental info to presence points.
Cheers
One way to do this is to choose a raster that has the projection and extent that you want and use that as a template for the others
For example, if you have rasterA and rasterB. You can use projectRaster() to make a new version of rasterA with the same extent and resolution as rasterB. You should then be able to stack new.rasterA & rasterB.
new.rasterA <- projectRaster(rasterB, rasterA) # define the projection and extent
r.stack <- stack(new.rasterA, rasterB) # add them to a raster stack object
I had the same issue and I solved this in arcgis by snapping each raster to a mask of my study area.
This can be done by clicking geoprocessing -> environments -> processing extent - then select a layer you want to snap to in the snap raster box. I did this before I extracted (clipped) each layer and it worked perfectly. You can check the extent in properties when you are done for each layer you do to double check before you upload them into R.