Friday, July 23, 2010
A common task in GIS analysis is to extract the value of a remotelysensed environmental variable at a point location. For instance wemay wish to extract the elevation of a field plot from a digitalelevation model. The elevation data is a raster (i.e. grid) and theplots are a point shapefile (or a simple text file of X, Ylocations). The command for doing this in ArcGIS isExtractValuesToPoints
available in the Spatial Analyst package.Situations may arise where ArcGIS is not the most efficient way ofextracting these values. So, here, I provide a brief overview of how toextract raster values to points in R and GRASS.
Extract Values to Points in R
This is strikingly easy is R. My work usually requires morestatistical sophistication than is available in ArcGIS. As aresult, I have completely switched to doing the extraction in R. Iknown I am going to end in R eventually, and it is easier to automatethan writing a long python script in ArcGIS.
Data required
For the purpose of this exercise. All the data must be have thesame spatial projection.
gr.asc
- an ESRI ASCII grid. This could also be an ArcGISbinary grid if you know how to use RGDAL. That perhaps will beanother post.
pt.shp
- a point shapefile.
You also need the maptools
and sp
packages in R.
The Code
library(maptools) # autoloads spgr <- readAsciiGrid("data/gr.asc")pt <- readShapePoints("data/pt.shp")overlay(gr, pt)
That is it. Fast, and easy.
Extracting Values in GRASS
Extracting raster values in GRASS is somewhat faster than in R, butit takes a little bit more planning in that you have to explicitlycreate the column that the raster values will go into.
Data Required
gr
: A GRASS gridpt
: A GRASS point dataset
The Code
The basic flow of this is that you create an empty column in thepoint dataset with the right data type (i.e. varchar(10)
stringof length 10, double precision
floating point numbers, int
integers). Then, fill the column with the raster values.
v.db.addcol map=pt columns="grval double precision"v.what.rast vector=pt raster=gr column=grval
0 comments:
Post a Comment