Using GIS data in R

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Using GIS data in R

Scott Duke-Sylvester
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.

--
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 : [hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Using GIS data in R

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

    require(maptools)
    require(rgdal)

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
readOGR().

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
classes.


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.

-Don

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.
>
>--
>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 : [hidden email]
>
>______________________________________________
>[hidden email] mailing list
>https://*stat.ethz.ch/mailman/listinfo/r-help
>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
925-423-1062

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Using GIS data in R

Rolf Turner
In reply to this post by Scott Duke-Sylvester

On 2/04/2010, at 4:37 AM, 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.


You may get some mileage out of looking at Adrian Baddeley's vignette
``Handling shapefiles in the spatstat package'' (available at the
entry for spatstat under contributed extension packages on CRAN).

For item 2) you may find the inside.owin() function in spatstat useful.

        cheers,

                Rolf Turner

######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Using GIS data in R

JeffND
Hi Rolf,

I have a similar question. I want to test whether a point with certain coordinates is inside
a state, say Texas. It seems that inside.owin() only works for testing if a point lies in a
regular region, say a polygon. Since Texas has irregular boundary, how do we achieve this?
Or there is some alternative way we can use.

Thanks!
Jeff  
Reply | Threaded
Open this post in threaded view
|

Re: Using GIS data in R

Rolf Turner-3
On 21/11/11 08:01, JeffND wrote:

> Hi Rolf,
>
> I have a similar question. I want to test whether a point with certain
> coordinates is inside
> a state, say Texas. It seems that inside.owin() only works for testing if a
> point lies in a
> regular region, say a polygon. Since Texas has irregular boundary, how do we
> achieve this?
> Or there is some alternative way we can use.
>
> Thanks!
> Jeff
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Using-GIS-data-in-R-tp1748266p4089242.html
> Sent from the R help mailing list archive at Nabble.com.

It would be nice to have some context ... nabble strikes again.

But to get to your question:  How do you think the boundary of Texas
(irregular as it may be) is specified?  I expect that it is specified as
a polygon!!!  It may be *very* ``poly'' --- i.e. have a large number of
edges --- but it's still a polygon and inside.owin() will still work on it.

Have a look at the data set "nbfires" in the spatstat package.  New
Brunswick has an ``irregular boundary'' too!

Have you *tried* making your specification of Texas into an owin object?
Follow the instructions in the vignette given by:

     vignette("shapefiles")

     cheers,

         Rolf Turner

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Using GIS data in R

JeffND
Hi Rolf,

Thanks very much for suggestions! Sorry if I made my question not very
clear.

The context is as follows. I have a set of locations with coordinates
(latitude and longitude)
from Texas region. Some of the locations are within Texas while some of
them are not.
I am trying to find those locations within Texas by looking at if the
coordinate of each location
is within the boundary of Texas.

Now I agree with you that the boundary of Texas is polygon although it
looks like "irregular".
I find your previous post very helpful. inside.owin() will do the job
but I have to figure out
the boundary (as a polygon) of Texas. Since I do not have the shapefile
of Texas, this will be
difficult for me. I am not sure if the shapefile of the US states are
available in R. If it not,
how can I get the shapefiles? Thanks a lot for kind help!

Best regards,
Jeff




> On 21/11/11 08:01, JeffND wrote:
>> Hi Rolf,
>>
>> I have a similar question. I want to test whether a point with certain
>> coordinates is inside
>> a state, say Texas. It seems that inside.owin() only works for testing if a
>> point lies in a
>> regular region, say a polygon. Since Texas has irregular boundary, how do we
>> achieve this?
>> Or there is some alternative way we can use.
>>
>> Thanks!
>> Jeff
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/Using-GIS-data-in-R-tp1748266p4089242.html
>> Sent from the R help mailing list archive at Nabble.com.
> It would be nice to have some context ... nabble strikes again.
>
> But to get to your question:  How do you think the boundary of Texas
> (irregular as it may be) is specified?  I expect that it is specified as
> a polygon!!!  It may be *very* ``poly'' --- i.e. have a large number of
> edges --- but it's still a polygon and inside.owin() will still work on it.
>
> Have a look at the data set "nbfires" in the spatstat package.  New
> Brunswick has an ``irregular boundary'' too!
>
> Have you *tried* making your specification of Texas into an owin object?
> Follow the instructions in the vignette given by:
>
>       vignette("shapefiles")
>
>       cheers,
>
>           Rolf Turner

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Using GIS data in R

Rolf Turner-3
On 21/11/11 10:19, Zuofeng Shang wrote:

> Hi Rolf,
>
> Thanks very much for suggestions! Sorry if I made my question not very
> clear.
>
> The context is as follows. I have a set of locations with coordinates
> (latitude and longitude)
> from Texas region. Some of the locations are within Texas while some
> of them are not.
> I am trying to find those locations within Texas by looking at if the
> coordinate of each location
> is within the boundary of Texas.
>
> Now I agree with you that the boundary of Texas is polygon although it
> looks like "irregular".
> I find your previous post very helpful. inside.owin() will do the job
> but I have to figure out
> the boundary (as a polygon) of Texas. Since I do not have the
> shapefile of Texas, this will be
> difficult for me. I am not sure if the shapefile of the US states are
> available in R. If it not,
> how can I get the shapefiles? Thanks a lot for kind help!

This is not my area of expertise (if indeed I have such an area at all!
:-) ) but
I'm sure that maps of the states of the USA --- including Texas, even!
--- are readily available
from various sources.  I.e. from various GIS's or perhaps from the
"maps" package.

With a bit of luck someone on the list will chime in with something more
specific.

Or you could try asking "how/where can I get a shapefile specifying the
boundary
of Texas" on the R-Sig-Geo list.

HTH

     cheers,

         Rolf

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Using GIS data in R

JeffND
Many thanks Rolf! Your help is highly appreciated
since it helps me understand the clue of solving my problem.

Have a wonderful Sunday!
Jeff

于 2011/11/20 15:52, Rolf Turner 写道:

> On 21/11/11 10:19, Zuofeng Shang wrote:
>> Hi Rolf,
>>
>> Thanks very much for suggestions! Sorry if I made my question not very
>> clear.
>>
>> The context is as follows. I have a set of locations with coordinates
>> (latitude and longitude)
>> from Texas region. Some of the locations are within Texas while some
>> of them are not.
>> I am trying to find those locations within Texas by looking at if the
>> coordinate of each location
>> is within the boundary of Texas.
>>
>> Now I agree with you that the boundary of Texas is polygon although it
>> looks like "irregular".
>> I find your previous post very helpful. inside.owin() will do the job
>> but I have to figure out
>> the boundary (as a polygon) of Texas. Since I do not have the
>> shapefile of Texas, this will be
>> difficult for me. I am not sure if the shapefile of the US states are
>> available in R. If it not,
>> how can I get the shapefiles? Thanks a lot for kind help!
> This is not my area of expertise (if indeed I have such an area at all!
> :-) ) but
> I'm sure that maps of the states of the USA --- including Texas, even!
> --- are readily available
> from various sources.  I.e. from various GIS's or perhaps from the
> "maps" package.
>
> With a bit of luck someone on the list will chime in with something more
> specific.
>
> Or you could try asking "how/where can I get a shapefile specifying the
> boundary
> of Texas" on the R-Sig-Geo list.
>
> HTH
>
>       cheers,
>
>           Rolf


--

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.