I am kind of new to R and especially to working with GIS in R, so I hope I explain this right.
I want to get the function crop (x,y...) from package raster to merge/overlay (not sure what's the correct expression to use) a raster file with a shapefile. Essentially, the shapefile is a 5 x 5 km grid of Sweden and the raster is a landuse raster of Sweden. By using the crop function I want to get a result which, for each 5x5 km square from the shapefile, gives information extracted from the raster file about the type of landuse in each square.
However, when I use crop and then write.csv from what I cropped 'crop (landuse, ekrut)" (see code below), I basically just get the ekrut (shapefile) as output csv file, there are no columns added of the landuse raster indicating which square has which land use class.
I am sorry but I cannot share the actual shapefile here, if it will make a difference somehow, the landuse data can be downloaded from here: http://gpt.vic-metria.nu/data/land/NMD/NMD2018_basskikt_ogeneraliserad_Sverige_v1_0.zip
Here's what I tried to do so far
library (raster)
library (rgdal)
library (sp)
this is the coordinate system/projection for the GIS file. Each coordinate system has an epsg code (http://spatialreference.org/).
sweref.def <- "+init=epsg:3006" 
# read in shapefile
ekrut <- readOGR (dsn   = "//storage-al.slu.se/student$/nilc0001/Desktop/Nina/Ekrut", 
                  layer = "ekrut_5x5_flat", 
                  p4s   = sweref.def) 
ekrut
# class       : SpatialPolygonsDataFrame 
# features    : 19192 
# extent      : 266333, 921700, 6131565, 7675329  (xmin, xmax, ymin, ymax)
# coord. ref. : +init=epsg:3006 +proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs 
# variables   : 7
# names       :    AREA, PERIMETER,  GGD_, GGD_ID,     BK, Bk_num, BK_flat 
# min values  : 2.5e+07,     20000,     2,      0, 10A 0f, 012 77,   10A0f 
# max values  : 2.5e+07,     20000, 19208,  19230,  9J 9d, 329 48,    9J9d 
#read in raster
landuse <- raster ("nmd2018bas_ogeneraliserad_v1_0.tif")
landuse
# class       : RasterLayer 
# dimensions  : 157992, 71273, 11260563816  (nrow, ncol, ncell)
# resolution  : 10, 10  (x, y)
# extent      : 208450, 921180, 6091140, 7671060  (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs 
# data source : //storage- al.slu.se/student$/nilc0001/Desktop/Nina/landuse/nmd2018bas_ogeneraliserad_v1_0.tif 
# names       : nmd2018bas_ogeneraliserad_v1_0 
# values      : 0, 128  (min, max)
# attributes  :
#         ID      COUNT Opacity Klass
#  from:   0 5204803484       0      
#  to  : 255          0       0   
#first few rows of attribute table of landuse
levels (landuse)   
# [[1]]
#      ID      COUNT Opacity                                              Klass
# 1     0 5204803484       0                                                   
# 2     1          0     255                                                   
# 3     2  382320369     255                                      Öppen våtmark
# 4     3  258249590     255                                           Åkermark
# crop and write csv
landuse.ekrut <- crop (landuse, ekrut)
write.csv (landuse.ekrut,"landuse.ek.csv")
Like I said, when I use crop and then write.csv from what I cropped I basically just get the ekrut (shapefile) as output csv file, there are no columns added of the landuse raster indicating which square has which land use class.
I would like to have a .csv which, for each square (there are 19 191 of them), gives me information on what type of landuse is present in this square.
If there is a better way to approach this, please indicate. I hope I was clear with my explanation!
Thanks.
 
     
    
 
    