[Rgeos-devel] How best to work with row.names output from gUnaryUnion?

Roger André randre at gmail.com
Mon Feb 24 21:02:53 CET 2020


Hello,

I'm slowly learning the implementation details of rgeos after being a
longtime user of GDAL and PostGIS.  I was doing some work that necessitated
doing some spatial unions which are  aggregated by a feature attribute and
was surprised to see that the original dissolve field (or `group by` field)
wasn't returned in a data frame, but rather as row IDs.  This was
inconvenient for me, so I wrote a short helper function to give me back my
desired output.

dissolvePolygons <- function (
    input_SPDF, # SPDF object to dissolve
    diss_field # name of column to dissolve by, as string
    ) {
    diss_command <- sprintf("rgeos::gUnaryUnion(input_SPDF,
input_SPDF$%s)", diss_field)
    data_command <- sprintf("data.frame(%s = row.names(out_polys))",
diss_field)
    out_polys <- eval(parse(text = diss_command))
    data <- eval(parse(text = data_command))
    output_SPDF <- SpatialPolygonsDataFrame(out_polys,data)

    return(output_SPDF)
    }

# Usage
zones <- dissolvePolygons(sample_tracts, "zone")

I'm curious what a preferred method is to work with the original row
output?  I was finding it problematic to tie it back to other data and was
hoping to find some examples of how to do so.

Thanks,

Roger
--
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rgeos-devel/attachments/20200224/644cf961/attachment.html>


More information about the Rgeos-devel mailing list