[Rgeos-devel] How best to work with row.names output from gUnaryUnion?
Roger Bivand
Roger.Bivand at nhh.no
Mon Feb 24 21:31:38 CET 2020
The list is not used. Either email the package maintainer, post on R-sig-geo, or - much better - use the sf package for all new projects involving GEOS in R - sf does do what you appear to want.
--
Roger Bivand
Norwegian School of Economics
Helleveien 30, 5045 Bergen, Norway
Roger.Bivand at nhh.no
________________________________________
Fra: Rgeos-devel <rgeos-devel-bounces at lists.r-forge.r-project.org> på vegne av Roger André <randre at gmail.com>
Sendt: mandag 24. februar 2020 21.02
Til: rgeos-devel at lists.r-forge.r-project.org
Emne: [Rgeos-devel] How best to work with row.names output from gUnaryUnion?
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
--
More information about the Rgeos-devel
mailing list