[R-sig-Geo] gUnaryUnion does not work on my SpatialPolygonsDataFrame?

Ulrike Grömping groemping at beuth-hochschule.de
Thu Jul 9 07:18:03 CEST 2015


Hi Edzer,

thanks a lot. In fact, a does contain what I want (but see also below).
gUnionCascaded or gUnaryUnion should have worked as well though; they 
did work in a similar situation combining smaller voting areas from 
German elections into larger administrative regions; as BRFW is a column 
in the @data slot of BW_EU, I expected this to work.

When using the aggregate method for the BW_EU, the only downside is that 
it creates a lot of NA columns with a lot of warnings, because the @data 
slot contains several non-numeric variables and the aggregation function 
is mean. Seeing this, I tried to make use of it by using the aggregation 
at a later step, changing the function to sum so that I would have 
aggregated numbers of votes. However, with FUN=sum, the aggregate step 
refused to work, because it complained about character arguments. 
Obviously this is inherent in the sum or mean functions, which react 
differently to character arguments. User comfort for the aggregate 
method would increase if it would apply the respective functions to 
those columns where it makes sense, but that's of course ambitious ...

The gUnion functions take a very conservative approach and do not 
include any of the data - the result is not even a 
SpatialPolygonsDataFrame object any more. Also not very nice ...

I think I will delete all character columns except BRFW and use 
aggregate with sum ...
But I would still like to understand why gUnaryUnion works with the one 
but not with the other file.

Best,
Ulrike

#############################################
## Edzer answered
Hi Ulrike, does

require(sp)
a = aggregate(BW_EU, list(BW_EU$BRFW), dissolve = TRUE)

plot(BW_EU, border = 'grey')
plot(a, add = TRUE)

give what you want? I believe that none of the rgeos functions (can) do
anything with polygon attributes, such as BRFW.

Best regards,

Am 08.07.2015 um 20:57 schrieb Ulrike Grömping:
> Dear R-sig-Geos,
>
> I am quite new to geographical data in R and also to shape files etc., 
> and I have encountered the following difficulty: After reading a 
> publicly available shape file on recent EU elections in Berlin (link 
> https://www.statistik-berlin-brandenburg.de/opendata/RBS_OD_STI_EU1405.zip)
> with the ReadShapePoly command, it contains the Berlin EU election 
> "Stimmbezirke" (variable STI), as expected, and they can be plotted.
> When using the gUnaryUnion or the gUnionCascaded function from rgeos 
> for uniting several "Stimmbezirke" into the larger postal voting units 
> (variable BRFW), the result is one single polygon instead of the 
> expected 529 such units; plotting it shows a shape that does not 
> explain itself to me.
>
> BW_EU <- readShapePoly("RBS_OD_STI_EU1405.shp")  ## shape directory is 
> working directory
> plot(BW_EU)   ## plots the expected "Stimmbezirke"
> BW_EU_BRFW <- gUnionCascaded(BW_EU, "BRFW")  ## gUnaryUnion does not 
> make a difference
> plot(BW_EU_BRFW)   ## plots only a single outline
>
> I then tried to use gUnaryUnion with the variable STI (which I 
> expected to return the unchanged polygons), which apparently returned 
> exactly the same outline as the one obtained with BRFW. Presumable, I 
> am overlooking something basic here, but I fail to see what. Can 
> someone help?
>
> Thanks in advance!
> Best, Ulrike
>


-- 
##############################################
## Prof. Dr. Ulrike Groemping
## FB II
## Beuth University of Applied Sciences Berlin
##############################################
## prof.beuth-hochschule.de/groemping
## Phone: +49(0)30 4504 5127
## Fax:   +49(0)30 4504 66 5127
## Home office: +49(0)30 394 04 863
##############################################



More information about the R-sig-Geo mailing list