[R] Using GIS data in R

Don MacQueen macq at llnl.gov
Thu Apr 1 20:55:27 CEST 2010

I'm currently doing a lot of simple GIS work in R, including points 
in polygon queries. My .Rprofile file has


With that as a starting point, I find that the data structures play 
well together.

Define a coordinate reference system object with
  crs.ll <-  CRS('+proj=longlat +ellps=GRS80 +datum=NAD83 +no_defs')

Load a shapefile with

   my.shp <- readOGR('directoryname','filename',  p4s=CRSargs(crs.ll) )

This will give you an object of class SpatialPolygonsDataFrame. 
readOGR() is in the rgdal package.

readShapeSpatial or readShapePoly from the maptools package should 
work as well, and I used to use them, but lately I've been using 

Then the overlay() function in the sp package will do your #2. But I 
do think you'll need your points to be one of the SpatialPoints 

Omitting the p4s argument from readOGR() might work, I'm not sure.
Or, I think you might be able to just supply the text string, i.e.,
     p4s='+proj=longlat +ellps=GRS80 +datum=NAD83 +no_defs'
but I have found it handy to have several projections predefined, as in
      crs.ll <-  CRS('+proj=longlat +ellps=GRS80 +datum=NAD83 +no_defs')
      crs.utm <- CRS('+init=epsg:32610')
for use in the spTransform() function.

Also, your question would go better on R-sig-geo mailing list.

A final note, some plotting functions need to have the sp package 
earlier in the search() path than maptools.


At 9:37 AM -0600 4/1/10, Scott Duke-Sylvester wrote:
>I have a simple problem: I need to load a ERSI shapefile of US states
>and check whether or not a set of points are within the boundary of
>these states. I have the shapefile, I have the coordinates but I'm
>having a great deal of difficulty bringing the two together. The
>problem is the various GIS packages for R do not play well with each
>other. sp, shapefiles, maptools, etc all use different data
>structures. Can someone suggest a simple set of commands that will
>work together that will:
>1) load the shapefile data.
>2) Allow me to test whether or not a (lng,lat) coordinate pair are
>inside or outside the polygons defined in the shapefile.
>Many thanks,
>Scott M. Duke-Sylvester
>Assistant Professor
>Department of Biology
>Office : 300 E. St. Mary Blvd
>              Billeaud Hall, Room 141
>              Lafayette, LA 70504
>Mailing address : UL Lafayette
>                   Department of Biology
>                   P.O.Box 42451
>                   Lafayette, LA 70504-2451
>Phone : 337 482 5304
>Fax   : 337 482 5834
>email : smd3729 at louisiana.edu
>R-help at r-project.org mailing list
>PLEASE do read the posting guide http://*www.*R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA

More information about the R-help mailing list