[R] point.in.polygon help

4 views
Skip to first unread message

karengrace84

unread,
May 21, 2013, 4:29:55 PM5/21/13
to r-h...@r-project.org
I am new to mapping with R, and I would like to use the point.in.polygon
function from the sp package, but I am unsure of how to get my data in the
correct format for the function. The generic form of the function is as
follows:

point.in.polygon(point.x, point.y, pol.x, pol.y, mode.checked=FALSE)

I have no problem with the point.x and point.y inputs. I have a list of gps
longitudes and latitudes that will go in fine. My problem is with the pol.x
and pol.y input. My polygon is currently in the form of a
SpatialPolygonsDataFrame created by inputting shp files with the rgdal
package.

How do I get a numerical array of the x- and y-coordinates from my polygon
that will go into the point.in.polygon function?



--
View this message in context: http://r.789695.n4.nabble.com/point-in-polygon-help-tp4667645.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-h...@r-project.org 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.

MacQueen, Don

unread,
May 22, 2013, 4:37:37 PM5/22/13
to karengrace84, r-h...@r-project.org
First suggestion is to ask the question on r-sig-geo.

There is the over()
function in the sp package, though it may require you to put your points
in a spatial class object.


For a crude brute-force approach that does not easily generalize, but
might be the quickest short-term solution for you right now, and assuming
your object has just a single polygon, try this:

your.spdf@polygons[[1]]@Polygons[[1]]@coords


That should give you a two column matrix representing your polygon.

You may need to remove the last row for pol.x and pol.y, since in this
implementation the last row = the first row.


-Don

--
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062

Daisy Englert Duursma

unread,
May 23, 2013, 6:25:28 AM5/23/13
to karengrace84, r-h...@r-project.org
It would be useful to know what your ultimate goal is.
--
Daisy Englert Duursma
Department of Biological Sciences
Room E8C156
Macquarie University, North Ryde, NSW 2109
Australia

Tel +61 2 9850 9256

karengrace84

unread,
May 23, 2013, 9:03:57 AM5/23/13
to r-h...@r-project.org
I am looking at fish tagging data. I have gps coordinates of where each fish
was tagged and released, and I have a map of 10 coastal basins of the state
of Louisiana. I am trying to determine which basin each fish was tagged in.



--
View this message in context: http://r.789695.n4.nabble.com/point-in-polygon-help-tp4667645p4667808.html

MacQueen, Don

unread,
May 23, 2013, 3:08:09 PM5/23/13
to karengrace84, r-h...@r-project.org
In that case I'd definitely look more at the over() function than that
ugly bit I suggested before.

Get your fish info into a SpatialPointsDataFrame

Since your polygons are in a SpatialPolygonsDataFrame, I would expect the
data frame part has one row per basin, and it contains the basin names or
other unique identifier. Loop through the basin names, subsetting the
SpatialPolygonsDataFrame for each each basin, then use the over() function
the with the fish SpatialPointsDataFrame to tell you which fish are in the
current basin.

That's an outline; there are obviously lots of details that would be
needed.

This should work even if, for example, a single basin consists of more
than one polygon (presumably non-overlapping).

There may be a more efficient way, but I don't know it off the top of my
head.

-Don

--
Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062





Reply all
Reply to author
Forward
0 new messages