[R] Calculating volume under polygons

javad bayat j@b@y@t194 @end|ng |rom gm@||@com
Mon Nov 20 13:30:00 CET 2023


Dear all;
I am trying to calculate volume under each polygon of a shapefile according
to a DEM.
when I run the code, it gives me an error as follows.
"
Error in h(simpleError(msg, call)) :
  error in evaluating the argument 'x' in selecting a method for function
'addAttrToGeom': sp supports Z dimension only for POINT and MULTIPOINT.
use `st_zm(...)` to coerce to XY dimensions
"
I want to have a table that contains one column corresponding to the
polygon rank and another that contains the volume on that polyon.
I would be more than happy if anyone could help me.
I provided codes at the end of this email.
Sincerely


##########################################################################################
library(raster);
library(sf)
# Load the DEM raster and shapefile
r <- raster("E:/Base1.tif")
p <- read_sf(dsn = "E:/Sites.shp", layer = " Sites")
# Extract the values of the DEM raster for each polygon
values <- extract(r, p)
  Error in h(simpleError(msg, call)) :
  error in evaluating the argument 'x' in selecting a method for function
'addAttrToGeom': sp supports Z dimension only for POINT and MULTIPOINT.
use `st_zm(...)` to coerce to XY dimensions

# Calculate the volume of each polygon
volumes <- sapply(values, function(x) rasterVolume(x, r))
# Print the results
for (i in 1:length(volumes)) {
  cat(sprintf("Volume under polygon %d: %f\n", i, volumes[i]))
}
##########################################################################################





-- 
Best Regards
Javad Bayat
M.Sc. Environment Engineering
Alternative Mail: bayat194 using yahoo.com

	[[alternative HTML version deleted]]



More information about the R-help mailing list