[R-sig-Geo] matching points to a raster

rick reeves reeves at nceas.ucsb.edu
Fri Jun 5 19:15:17 CEST 2009


Hi Juliane:

I see that you are STILL pushing the outer limits of R - :}

I have been working a lot with combining point/vector and raster files 
using the overlay() method in the sp package.
I suspect that if you want to have two spatially registered layers 
(raster and point vector) and want to get the raster cell
underlying a spatially corresponding point, then overlay might be of use.

If you can more precisely specify your task (with an example), and 
possibly send a sample data set, I could give
a more specific answer...Meanwhile, as a starting point, here is an 
example of extracting values from a raster grid.
This might be a partial solution to your question -
#
# read vector point and raster files, both in unprojected geographic  
coordinates,
# covering the same area
#
  WaterSourcePoints <- 
readOGR("WaterSourcePointsLatLong.shp","WaterSourcePointsLatLong") # 
point shape file
  DEMImg     <- readGDAL("NevadaSiteDEMSub.tif")  # raster elevation map
#
# Extract from raster file the elevations 'underneath' each point in the 
file
#
  PointElevations = overlay(DEMImg,as(WaterSourcePoints,"SpatialPoints"))
#
# append the point file attributes with the elevation value vector
#
  StationsWithElev = cbind(WaterSourcePoints at data,PointElevations at data[,1])
#
# update the column names in the point shape file
#
  nc = ncol(StationsWithElev) 
  colnames(StationsWithElev) = 
c(colnames(StationsWithElev[1:nc-1]),"Elevation")    

Hope this helps, pls feel free to send more information!
Rick Reeves

Struve, Juliane wrote:
> Dear list,
>
> I am trying to incoporate spatial information (for example distance) into a movement model by reading an ascii -b converted raster and matching the raster with points of a movement path (for example fishmove$coord[fishmove$day==1]. Below is a section of my code to explain the problem. This code does not work because the coordinates of the movement path are not the same as those in the raster. Rounding the coordinates does not seem to solve the problem. Maybe the closest point would have to be found, but i am not sure how to approach this.  Is there another perhaps easier way ?
>
> Dist_DF=data.frame(readAsciiGrid("distance.txt"))
> Dist_shoreDF$coord = paste(round(Dist_DF$s1),round(Dist_DF$s2),sep=",")
> names(Dist_DF)[1]="Dist"
> Dist_DF$Dist[Dist_shoreDF$coord==fishmove$coord[fishmove$day==1]]
>
> I would be grateful for any help.
>
> Many thanks,
>
> Juliane
>
>
>
>
>
>
>
> 	[[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>   


-- 
Rick Reeves
Scientific Programmer/Analyst and Data Manager
National Center for Ecological Analysis and Synthesis
UC Santa Barbara
www.nceas.ucsb.edu
805 892 2533



More information about the R-sig-Geo mailing list