Counting overlapping polygons in a given area

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Counting overlapping polygons in a given area

Andrew Bernj
Hi,

I am used to work with ArcGIS but I am new to R spatial functionalities.
So, I have been searching in the archives of this list, but did not find a
previous question exaclty on what I have to accomplish.

It is the following. I have two shapefiles, one with US city and town
divisions, where each polygon is a city/town (UScities.shp) and another
with some US religious groups, where polygons represent the area where
there are significant followers of a given religion (USrelig.shp).

So, in UScities.shp of course the polygons do not overlap. While in
USrelig.shp polygons do overlap, since followers from different religions
can be present in the same place.

What I want do is to count how many religious groups according to
USrelig.shp there are in each city present in UScities.shp. It means, to
count how many polygons overlap in USrelig.shp for each polygon division
given by UScities.shp.


Is there a way to do that in R? Thanks in advance for any tip you may have
on this task.

Bernj

        [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Reply | Threaded
Open this post in threaded view
|

Re: Counting overlapping polygons in a given area

Roger Bivand
Administrator
On Thu, 17 Apr 2014, Andrew Bernj wrote:

> Hi,
>
> I am used to work with ArcGIS but I am new to R spatial functionalities.
> So, I have been searching in the archives of this list, but did not find a
> previous question exaclty on what I have to accomplish.

On lists in this community, it is usual to follow the instructions, in
particular to provide a toy example illustrating your problem. If you
don't, nobody knows whether the suggested resolution matches your problem.
You also posted HTML-mail; please use plain text only, so that included
code may be pasted straight into an R console.

>
> It is the following. I have two shapefiles, one with US city and town
> divisions, where each polygon is a city/town (UScities.shp) and another
> with some US religious groups, where polygons represent the area where
> there are significant followers of a given religion (USrelig.shp).
>
> So, in UScities.shp of course the polygons do not overlap. While in
> USrelig.shp polygons do overlap, since followers from different religions
> can be present in the same place.

library(sp)
library(rgdal)
dsn <- system.file("vectors", package = "rgdal")[1]
scot_BNG <- readOGR(dsn=dsn, layer="scot_BNG")
plot(scot_BNG, axes=TRUE)

gives something like UScities.shp.

set.seed(1)
pts <- spsample(scot_BNG, n=200, type="random")
plot(pts, add=TRUE, col="red")
library(rgeos)
polys <- gBuffer(pts, width=50000, byid=TRUE)
plot(polys, add=TRUE, border="red")

gives something like USrelig.shp.

library(rgeos)
gO <- gOverlaps(scot_BNG, polys, byid=c(TRUE, TRUE))
dim(gO)

makes a logical matrix with TRUE where scot_BNG[i,] overlaps with
polys[j].

count_by_city <- apply(gO, 2, sum)

The output vector is named using row.names(scot_BNG), so if you need
recognizable names in the output, change the input row.names to something
informative.

This is OK if the number of cities times the number of religions is
moderate. If the product is large, try the development version of rgeos
from:

https://r-forge.r-project.org/R/?group_id=602

and use:

gO <- gOverlaps(scot_BNG, polys, byid=c(TRUE, TRUE), returnDense=FALSE)
length(gO)
count_by_city1 <- sapply(gO, length)

and in this case we see that they are equivalent:

all.equal(count_by_city, count_by_city1)

Hope this helps,

Roger

>
> What I want do is to count how many religious groups according to
> USrelig.shp there are in each city present in UScities.shp. It means, to
> count how many polygons overlap in USrelig.shp for each polygon division
> given by UScities.shp.
>
>
> Is there a way to do that in R? Thanks in advance for any tip you may have
> on this task.
>
> Bernj
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> [hidden email]
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

--
Roger Bivand
Department of Economics, Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 91 00
e-mail: [hidden email]

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
Roger Bivand
Department of Economics
Norwegian School of Economics
Helleveien 30
N-5045 Bergen, Norway
Reply | Threaded
Open this post in threaded view
|

Re: Counting overlapping polygons in a given area

Andrew Bernj
Dear Roger,

Thank you very much for this example. It was exactly what I needed to
get started on this issue. Especially the final part, using the
development version of rgeos. I have been trying some different things
before and I suspect one of the problems had to do with the fact that
I have just too many cities*religions. It helped a lot with other
problems I had to solve.

Also, I do apologize for not sending my previous code or a toy
exmaple. I will remember that in the future.

Best,

Andrew



2014-04-17 5:19 GMT-04:00 Roger Bivand <[hidden email]>:

> On Thu, 17 Apr 2014, Andrew Bernj wrote:
>
>> Hi,
>>
>> I am used to work with ArcGIS but I am new to R spatial functionalities.
>> So, I have been searching in the archives of this list, but did not find a
>> previous question exaclty on what I have to accomplish.
>
>
> On lists in this community, it is usual to follow the instructions, in
> particular to provide a toy example illustrating your problem. If you don't,
> nobody knows whether the suggested resolution matches your problem. You also
> posted HTML-mail; please use plain text only, so that included code may be
> pasted straight into an R console.
>
>
>>
>> It is the following. I have two shapefiles, one with US city and town
>> divisions, where each polygon is a city/town (UScities.shp) and another
>> with some US religious groups, where polygons represent the area where
>> there are significant followers of a given religion (USrelig.shp).
>>
>> So, in UScities.shp of course the polygons do not overlap. While in
>> USrelig.shp polygons do overlap, since followers from different religions
>> can be present in the same place.
>
>
> library(sp)
> library(rgdal)
> dsn <- system.file("vectors", package = "rgdal")[1]
> scot_BNG <- readOGR(dsn=dsn, layer="scot_BNG")
> plot(scot_BNG, axes=TRUE)
>
> gives something like UScities.shp.
>
> set.seed(1)
> pts <- spsample(scot_BNG, n=200, type="random")
> plot(pts, add=TRUE, col="red")
> library(rgeos)
> polys <- gBuffer(pts, width=50000, byid=TRUE)
> plot(polys, add=TRUE, border="red")
>
> gives something like USrelig.shp.
>
> library(rgeos)
> gO <- gOverlaps(scot_BNG, polys, byid=c(TRUE, TRUE))
> dim(gO)
>
> makes a logical matrix with TRUE where scot_BNG[i,] overlaps with polys[j].
>
> count_by_city <- apply(gO, 2, sum)
>
> The output vector is named using row.names(scot_BNG), so if you need
> recognizable names in the output, change the input row.names to something
> informative.
>
> This is OK if the number of cities times the number of religions is
> moderate. If the product is large, try the development version of rgeos
> from:
>
> https://r-forge.r-project.org/R/?group_id=602
>
> and use:
>
> gO <- gOverlaps(scot_BNG, polys, byid=c(TRUE, TRUE), returnDense=FALSE)
> length(gO)
> count_by_city1 <- sapply(gO, length)
>
> and in this case we see that they are equivalent:
>
> all.equal(count_by_city, count_by_city1)
>
> Hope this helps,
>
> Roger
>
>>
>> What I want do is to count how many religious groups according to
>> USrelig.shp there are in each city present in UScities.shp. It means, to
>> count how many polygons overlap in USrelig.shp for each polygon division
>> given by UScities.shp.
>>
>>
>> Is there a way to do that in R? Thanks in advance for any tip you may have
>> on this task.
>>
>> Bernj
>>
>>         [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> [hidden email]
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>
> --
> Roger Bivand
> Department of Economics, Norwegian School of Economics,
> Helleveien 30, N-5045 Bergen, Norway.
> voice: +47 55 95 93 55; fax +47 55 95 91 00
> e-mail: [hidden email]
>

_______________________________________________
R-sig-Geo mailing list
[hidden email]
https://stat.ethz.ch/mailman/listinfo/r-sig-geo