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

Edzer Pebesma edzer.pebesma at uni-muenster.de
Thu Jul 9 21:02:28 CEST 2015

On 07/09/2015 07:18 AM, Ulrike Grömping wrote:
> 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 ...

aggregate(BW_EU, list(BW_EU$BRFW), FUN = function(x) x[1], dissolve = TRUE)

works for factor data. This use of sp::aggregate, where the aggregation
predicate (by) comes from the attributes, is modelled after (and largely
copied from) stats::aggregate.data.frame.

> 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.

g = gUnaryUnion(BW_EU, BW_EU$BRFW)

was what you want, and might be the form you used earlier.

> 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

Edzer Pebesma
Institute for Geoinformatics (ifgi),  University of Münster,
Heisenbergstraße 2, 48149 Münster, Germany; +49 251 83 33081
Journal of Statistical Software:   http://www.jstatsoft.org/
Computers & Geosciences:   http://elsevier.com/locate/cageo/
Spatial Statistics Society http://www.spatialstatistics.info

-------------- 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/20150709/c209d688/attachment.bin>

More information about the R-sig-Geo mailing list