# giswerk

### Sidebar

#### Applied Stuff

r:r-gis:rastervector

# 3-Raster-Vector Operations

## Extract values for shapes(e.g SpatialPointDataFrame)

Wget raster values for overlaying vector data

  extract(rasterLayer,shapeLayer,df=TRUE)

#or to return only the e.g. mean values of e.g. overlaying polygons (identical to zonal statistics but works with vector data as zone features)

extract(rasterLayer,shapeLayer,fun=mean,df=TRUE)

## vector to raster/ rasterize

  #set the values of a raster with the desired extent and resolution ("baseraster")
#to NA:
values(baseraster)=NA
#...Then update this emty raster with the shape information:
rasterize(shapeLayer,baseraster)

If a table is to rasterize use the following code:

  rasterize(cbind(x_coordinates,y_coordinates),baseraster,field=Z)

With Z is the value which the raster cells should have.

## raster to polygons

  rasterToPolygons(rasterLayer)

## raster to points

 rasterToPoints(RasterLayer,spatial=TRUE)

## Euclidean distance

#distance from a point to all cells of a RasterLayer.
values(rasterLayer)=NA
distanceFromPoints(rasterLayer, SpatialPoint) 

If the distance is to be calculated from a raster object, see euclidean_distance in the chapter 1-Raster Operations. This function might also be useful for vector or line features: convert them to a raster and then use this function.

## zonal statistics

  zonal(rasterLayer, ZoneRaster, 'mean')