R-ArcGIS / arcgislayers

ArcGIS Location Services
http://r.esri.com/arcgislayers/
Apache License 2.0
37 stars 8 forks source link

`arc_select()` ignores order of values in `fields` creating issue w/ `arc_read()` handling of `col_names` #185

Closed elipousson closed 2 months ago

elipousson commented 2 months ago

arc_select() ignores order of values in fields when ordering columns in the returned data frame. This creates an issue for arc_read() handling of col_names since users (like me) may just use the same order of values for col_names as they did in col_select which would apply new names based on position rather than match to the position of the originally supplied fields or col_select parameter.

library(arcgislayers)

layer <- arc_open(
  "https://geodata.md.gov/imap/rest/services/BusinessEconomy/MD_IncentiveZones/FeatureServer/2"
)

layer |> 
  arc_select(
    fields = c("sitename", "website", "county")
  )
#> Simple feature collection with 29 features and 3 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -8840109 ymin: 4576217 xmax: -8371707 ymax: 4822219
#> Projected CRS: WGS 84 / Pseudo-Mercator
#> First 10 features:
#>                                              sitename          county
#> 1              Berlin Arts and Entertainment District       Worcester
#> 2            Bethesda Arts and Entertainment District      Montgomery
#> 3           Cambridge Arts and Entertainment District      Dorchester
#> 4           Annapolis Arts and Entertainment District    Anne Arundel
#> 5          Cumberland Arts and Entertainment District        Allegany
#> 6  Downtown Frederick Arts and Entertainment District       Frederick
#> 7              Elkton Arts and Entertainment District           Cecil
#> 8             Gateway Arts and Entertainment District Prince George's
#> 9        Highlandtown Arts and Entertainment District  Baltimore City
#> 10  City of Frostburg Arts and Entertainment District        Allegany
#>                                                website
#> 1                        https://www.artsinberlin.org/
#> 2  http://www.bethesda.org/bethesda/arts-entertainment
#> 3                      http://www.choosecambridge.com/
#> 4                        http://www.annapolisarts.org/
#> 5                  http://www.alleganyartscouncil.org/
#> 6                    http://www.downtownfrederick.org/
#> 7                               http://elktonarts.com/
#> 8                            http://mygatewayarts.org/
#> 9                     http://www.highlandtownarts.com/
#> 10          http://www.downtownfrostburg.com/the-arts/
#>                          geometry
#> 1  MULTIPOLYGON (((-8372648 46...
#> 2  MULTIPOLYGON (((-8582418 47...
#> 3  MULTIPOLYGON (((-8469221 46...
#> 4  MULTIPOLYGON (((-8517250 47...
#> 5  MULTIPOLYGON (((-8769543 48...
#> 6  MULTIPOLYGON (((-8618474 47...
#> 7  MULTIPOLYGON (((-8441352 48...
#> 8  MULTIPOLYGON (((-8567015 47...
#> 9  MULTIPOLYGON (((-8525387 47...
#> 10 MULTIPOLYGON (((-8786104 48...

arc_read(
  "https://geodata.md.gov/imap/rest/services/BusinessEconomy/MD_IncentiveZones/FeatureServer/2",
  col_select = c("sitename", "website", "county"),
  col_names = c("ae_district", "url", "county")
)
#> Simple feature collection with 29 features and 3 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -8840109 ymin: 4576217 xmax: -8371707 ymax: 4822219
#> Projected CRS: WGS 84 / Pseudo-Mercator
#> First 10 features:
#>                                           ae_district             url
#> 1              Berlin Arts and Entertainment District       Worcester
#> 2            Bethesda Arts and Entertainment District      Montgomery
#> 3           Cambridge Arts and Entertainment District      Dorchester
#> 4           Annapolis Arts and Entertainment District    Anne Arundel
#> 5          Cumberland Arts and Entertainment District        Allegany
#> 6  Downtown Frederick Arts and Entertainment District       Frederick
#> 7              Elkton Arts and Entertainment District           Cecil
#> 8             Gateway Arts and Entertainment District Prince George's
#> 9        Highlandtown Arts and Entertainment District  Baltimore City
#> 10  City of Frostburg Arts and Entertainment District        Allegany
#>                                                 county
#> 1                        https://www.artsinberlin.org/
#> 2  http://www.bethesda.org/bethesda/arts-entertainment
#> 3                      http://www.choosecambridge.com/
#> 4                        http://www.annapolisarts.org/
#> 5                  http://www.alleganyartscouncil.org/
#> 6                    http://www.downtownfrederick.org/
#> 7                               http://elktonarts.com/
#> 8                            http://mygatewayarts.org/
#> 9                     http://www.highlandtownarts.com/
#> 10          http://www.downtownfrostburg.com/the-arts/
#>                          geometry
#> 1  MULTIPOLYGON (((-8372648 46...
#> 2  MULTIPOLYGON (((-8582418 47...
#> 3  MULTIPOLYGON (((-8469221 46...
#> 4  MULTIPOLYGON (((-8517250 47...
#> 5  MULTIPOLYGON (((-8769543 48...
#> 6  MULTIPOLYGON (((-8618474 47...
#> 7  MULTIPOLYGON (((-8441352 48...
#> 8  MULTIPOLYGON (((-8567015 47...
#> 9  MULTIPOLYGON (((-8525387 47...
#> 10 MULTIPOLYGON (((-8786104 48...

Created on 2024-04-24 with reprex v2.1.0

Expected behavior

I'd expect fields or col_select to be returned in the same order supplied regardless of the order in the source data.

I'm unsure if this is a regression or a bug that is newly revealed by the added functionality for arc_read(). This could be fixed in arc_read() and leave the existing default behavior in arc_select() intact—or it could be updated in both functions for consistency.