[R-sig-Geo] Problem with over() from sp package
Edzer Pebesma
edzer.pebesma at uni-muenster.de
Wed Apr 9 09:38:49 CEST 2014
The documentation of sp::over says, for over(x,y):
"at the spatial locations of object x retrieves the indexes or
attributes from spatial object y"
From your description it seems that you want the polygon attributes
at the locations of your points. For this, you try:
temp <- over(vec, xy_allClasses)
but that gives point indexes or attributes for all each of the polygons.
If xy_allClasses has no attributes, temp is correctly of class integer.
See also http://cran.r-project.org/web/packages/sp/vignettes/over.pdf
According to the documentation, and if I understand your problem
correctly, you want:
temp <- over(xy_allClasses, vec)
Your email contains a lot of code that seems irrelevant to your
question, and also does not contain a reproducible example, so my
answer can only be a wild guess. But I hope it helps,
On 04/08/2014 10:29 PM, Katharine Miller - NOAA Federal wrote:
> Hi
>
> I am new to the sp package. When I use over() with a
> SpatialPolygoneDataframe and SpatialPoints objects I get an object of class
> "integer", not a dataframe as the sp help documentation suggests.
>
> The SpatialPolygoneDataframe object is shape file imported using readOGR.
> The SpatialPoints object is a random sample of points within each polygon.
> I want to use this information to train a random forest model to classify a
> raster image. So I am using over () to combine the data from the two files
> so I can extract the variable name/class from my shapefile for each
> point. Unfortunately, the result of the over () application is simply a
> string of numbers with no attributes.
>
> Here is the code:
>
> # get shapefile
> vec<- readOGR('E:/', layer = "DVTraining")
> ## this is an objective of class SpatialPolygoneDataframe
>
> # Code for abstracting random points from each polygon to create training
> data
> for (x in 1:9) {
> # Get the metadata for all polygons for a particular class (based on the
> uniqueAtt variable)
> class_data<- vec[vec[[attName]]==uniqueAtt[x],]
> # Get the area of each polygon for a particular class
> areas <- sapply(slot(class_data, "polygons"), slot, "area")
> # Calculate the number of samples for each polygon based on the area in
> proportion to total area for a class
> nsamps <- ceiling(numsamps*(areas/sum(areas)))
> # Use random sampling to select training points (proportial based on
> area) from each polygon for a given class
> for (i in 1:dim(class_data)[1]) {
> xy_class <- spsample(class_data[i,], type="random", n=nsamps[i])
> # Add coordinates to create a list of random points for all polygons
> if (i == 1) cpts <- xy_class
> else cpts <- rbind(cpts, xy_class)
> }
> # The number of points might not match numsamps exactly.
> classpts <- cpts
> if (x == 1) {
> xy_allClasses<- classpts
> } else {
> xy_allClasses<- rbind(xy_allClasses, classpts)
> }
> }
>
>
> ### Now, combine the points and polygons to get the class number for each
> point
> temp <- over(vec, xy_allClasses)
>
> Temp is an object of class "integer" and as such does not have any
> attributes - so there are no attribute names for the points.
>
> Thanks for any help.
>
> Note: I borrowed this code from
> http://biodiversityinformatics.amnh.org/index.php?section=R_Scripts. The
> original code used readShapePoly to load the shapefile but when I tried
> that it came in without a coordinate reference, so I switched to readOGR.
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
--
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster
Heisenbergstraße 2, 48149 Münster, Germany. Phone: +49 251
83 33081 http://ifgi.uni-muenster.de GPG key ID 0xAC227795
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 490 bytes
Desc: OpenPGP digital signature
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20140409/606739eb/attachment.bin>
More information about the R-sig-Geo
mailing list