0

I am trying to extract the mean of a raster data to polygons with the following code:

library(rgdal)
library(raster)
library(maptools) 

agri_suitability=raster("suit")
regions <- readShapeSpatial("Regions.shp") #SpatialPolygonsDataFrame

plot(agri_suitability)
plot(regions, add=T)

enter image description here

polygon_agri_suitability=extract(agri_suitability,regions,fun=mean,na.rm=T)
regions@data = data.frame(regions@data, polygon_agri_suitability)

write.csv(regions,"suitability_mean.csv")

After data.frame I get an error:

arguments imply differing number of rows:1549, 0

My "Regions.shp" file in fact includes 1549 rows. But I don't know what this error means and how I can solve it.

  • polygon_agri_suitability turns a csv file including variables from the polygon layer but nothing from raster data. – D_B Jun 9 '18 at 22:14
  • I don't understand rest of your questions. But the code works with other polygon layers, and I don't understand what's different about this specific polygon layer ("Regions.shp"). – D_B Jun 9 '18 at 22:15
  • what do you mean by "clip with SpatialPolygons object"? where in the code you are referring to? what's the code? – D_B Jun 9 '18 at 22:16
  • Let us continue this discussion in chat. – Andre Silva Jun 9 '18 at 22:21
1

Thanks to @AndreSilva, I found the problem and the solution.

The problem was that the polygon and raster layers had different geographic systems. So I used Data Management Project tool of ArcMap to get both layers in WGS 1984. Now, the polygon layer exactly overlaps with the raster map, and the code works correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.