From randre at gmail.com Mon Feb 24 21:02:53 2020 From: randre at gmail.com (=?UTF-8?B?Um9nZXIgQW5kcsOp?=) Date: Mon, 24 Feb 2020 12:02:53 -0800 Subject: [Rgeos-devel] How best to work with row.names output from gUnaryUnion? Message-ID: 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: From Roger.Bivand at nhh.no Mon Feb 24 21:31:38 2020 From: Roger.Bivand at nhh.no (Roger Bivand) Date: Mon, 24 Feb 2020 20:31:38 +0000 Subject: [Rgeos-devel] How best to work with row.names output from gUnaryUnion? In-Reply-To: References: Message-ID: 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 på vegne av Roger André 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 --