[R-sig-Geo] Counting points within polygons
Agustin Lobo
alobolistas at gmail.com
Tue Oct 18 18:32:06 CEST 2011
I'm afraid I do not understand well the help page.
It seems to me that what I need is
"x = "SpatialPointsDataFrame", y = "SpatialPolygons"
equal to the previous method, except that an argument fn=xxx is
allowed, e.g. fn = mean which will then report a data.frame with the
mean values of the x points falling in each polygon (set) of y"
Thus:
> delme4 <- over(fireLC2[,5], BorneoLC,fn=sum)
where
> BorneoLC at data[1,]
LC ID LC_2 LCname LCabrv AREA PERIMETER
0 4 4505 4 Lowland Forest LwF 7.7e-05 0.036385
and
> fireLC2 at data[1,]
LC ID LCname LCabrv nbp
1 7 1 Plantation_Secondary Forest PltSF 1
but I get
Error in FUN(X[[1L]], ...) :
only defined on a data frame with all numeric variables
Calls: over ... do.call -> lapply -> Summary.data.frame -> lapply -> FUN
which I do not understand because fireLC2[,5] is numeric
(suggestion: could the user define the field(s) for which he wants fn
being applied? )
Beyond that, where the help page says:
"...which will then report a data.frame with the mean values of the x
points falling in each polygon (set) of y",
what happens if there is no points falling for a given polygon? Would
the record be eliminated or kept as NA ?
>From my point of view, having the option of getting a data.frame with
the same nb. of rows and same ordering as
the input Sp Polygons object would be an advantage.
Agus
2011/10/18 Edzer Pebesma <edzer.pebesma at uni-muenster.de>:
> Agus,
>
> Have you looked into "over", which is supposed to replace overlay?
>
> require(sp)
> vignette("over")
>
> On 10/18/2011 05:06 PM, Agustin Lobo wrote:
>>
>> Actually, neither alternative works if there are polygons with no
>> points, as those
>> are not kept in the output of overlay and just binding tp the table of
>> srdf is not possible.
>>
>> The second alternative looks easier for a join, but a problems comes
>> from the fact that
>> the output data.frame of
>> delme4<- overlay(meuse2[,13],srdf,fn=sum)
>>
>> has different row.names than that of the input srdf:
>>>
>>> row.names(srdf)
>>
>> [1] "r1" "r2" "r3"
>>>
>>> delme4<- overlay(meuse2[,13],srdf,fn=sum)
>>> row.names(delme4)
>>
>> [1] "X1" "X2" "X3"
>>
>> which is inconvenient, as the way to join delme4 to srdf would be
>> using the row.names.
>>
>> Agus
>>
>>
>> 2011/10/18 Agustin Lobo<alobolistas at gmail.com>:
>>>
>>> Hi!
>>>
>>> Given a Sp Points DF and an Sp Polygons DF, I want to add a field of
>>> number of points for each polygon
>>> to the Sp Pol. DF table. This is what I'm doing (using objects from
>>> the help page for sp::overlay()
>>> (meuse is the Sp Points DF and srdf the Sp Pol. DF)
>>>
>>>> delme3<- overlay(meuse,srdf)
>>>> delme3
>>>
>>> [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
>>> 1 1 1
>>> [38] 1 1 1 1 1 1 2 1 2 2 2 2 2 2 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3
>>> 3 3 3
>>> [75] 3 3 3 3 3 3 3 3 2 1 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 2 2 2 2 2
>>> 2 2 2
>>> [112] 2 2 2 2 2 2 3 2 2 2 2 2 1 2 2 2 2 1 1 2 2 1 2 2 2 2 3 3 3 3 3 3 3 3
>>> 3 3 3
>>> [149] 3 3 3 3 3 3 3
>>> newdata<- data.frame(srdf at data,nppoints=table(delme3))
>>> srdf2<- srdf
>>> srdf2 at data<- newdata
>>>
>>> An alternative:
>>> newdatap<- data.frame(meuse at data, nbp=rep(1,nrow(meuse)))
>>> meuse2<- meuse
>>> meuse2 at data<- newdatap
>>> delme4<- overlay(meuse2[,13],srdf,fn=sum)
>>> newdata<- data.frame(srdf at data,nppoints=delme4)
>>> srdf2<- srdf
>>> srdf2 at data<- newdata
>>>
>>> I wonder if there is a better way or, best, an standard R spatial
>>> function doing that already.
>>>
>>> Thanks!
>>>
>>> Agus
>>>
>>
>> _______________________________________________
>> 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
> Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
> 8333081, Fax: +49 251 8339763 http://ifgi.uni-muenster.de
> http://www.52north.org/geostatistics e.pebesma at wwu.de
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
More information about the R-sig-Geo
mailing list