Extract Raster Values from Points using R

The R blog article encourages me to write this solution to extract Raster values from points in R.
In geospatial analysis, extracting the raster value of a point is a common task. If you have few raster files or few points; you can extract the raster value by overlaying a point on the top of the raster using ArcGIS. What will you do, if you have hundreds of raster files and thousands of points? The easy solution is use loop in Python and ArcGIS. Is loop efficient to use? No. Can loop be avoided? Yes.
Then how?
Follow the steps:
Step 1: Create a Raster stack or Raster brick of your raster files using “raster” package in R.
For example:
rasStack = stack(raster1, raster2,raster3 …rasterN)
Step 2:  Read point data, and convert them into spatial points data frame.
Sample: pointfile.csv
Point_ID
LONGITUDE
LATITUDE
1
48.765863
-94.745194
2
48.820552
-122.485318
3
48.233939
-107.857685
4
48.705964
-99.817363
For example:
pointCoordinates=read.csv(“pointfile.csv”)
coordinates(pointCoordinates)= ~ LONGITUDE+ LATITUDE
Step 3: Extract raster value by points
For example:
rasValue=extract(rasStack, pointCoordinates)
Step 4:  Combine raster values with point and save as a CSV file.
For example:
combinePointValue=cbind(pointCoordinates,rasValue)
write.table(combinePointValue,file=“combinedPointValue.csv”, append=FALSE, sep= “,”, row.names = FALSE, col.names=TRUE)
Step 5: You should get the results as following table.
Result: combinedPointValue.csv
Point_ID
LONGITUDE
LATITUDE
raster1
raster2
raster3
1
48.765863
-94.745194
200
500
-100
2
48.820552
-122.485318
178.94
18.90
10.94
3
48.233939
-107.857685
-30.74
-30.74
-0. 4
4
48.705964
-99.817363
0
110
-0.7

 

Advertisements

About Qiusheng Wu

Assistant Professor of Geography at Binghamton University, State University of New York

Posted on March 24, 2014, in Software and tagged . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

REDD+ for the Guiana Shield

Technical Cooperation Project

LidarBlog.com

Dr. Qiusheng Wu @ SUNY Binghamton

Writing Science

How to write papers that get cited and proposals that get funded

GIS In Ecology

Providing Training, Advice And Consultation On The Use Of GIS In Ecology

metrhispanic

On cities, land, ...

GeoAcademy

Open GIS: No Bounds

Scientia Plus Conscientia

Thoughts on Science and Nature

r4hydrology

Learning hydrology with R

Karl Hennermann

GIS at the University of Manchester

GIS and Science

Applications of geospatial technology for scientific research and understanding.

GISblog.com

GIS, Mapping, Remote Sensing, Geodata, Geospatial news

Whitebox Geospatial Analysis Tools

Open-source GIS development and spatial analysis with Whitebox GAT

TopoToolbox

MATLAB-based software for topographic analysis

Anything Geospatial – AnyGeo

Dr. Qiusheng Wu @ SUNY Binghamton

GeospatialPython.com

Dr. Qiusheng Wu @ SUNY Binghamton

Another GIS Blog

Dr. Qiusheng Wu @ SUNY Binghamton

ArcPy Café

Get all your ArcGIS Python Recipes here!

Planet Python

Dr. Qiusheng Wu @ SUNY Binghamton

%d bloggers like this: