rOpenGov / giscoR

Download geospatial data from GISCO API - Eurostat
https://ropengov.github.io/giscoR/
GNU General Public License v3.0
70 stars 1 forks source link

gisco_get_nuts does not consider country argument when nuts_level = "0" AND spatialtype = "BN" #50

Closed umbe1987 closed 1 year ago

umbe1987 commented 1 year ago

When I try to run this code:

country_lines <- gisco_get_nuts(
  year = "2016",
  epsg = "3035",
  resolution = "20",
  nuts_level = "0",
  spatialtype = "BN",
  country = c("Czechia", "Germany", "Lithuania", "Estonia", "Italy", "Spain")
)

I get this map

ggplot(country_lines) + geom_sf()

image

However, if I use another spatialtype other than "BN", I get the correct countries (same applies to spatialtype = "LB":

country_lines <- gisco_get_nuts(
  year = "2016",
  epsg = "3035",
  resolution = "20",
  nuts_level = "0",
  spatialtype = "RG",
  country = c("Czechia", "Germany", "Lithuania", "Estonia", "Italy", "Spain")
)
ggplot(country_lines) + geom_sf()

image

dieghernan commented 1 year ago

Yes, this is expected, since GISCO data does not provide a LINESTRING at country level, but at segment (boundary?) level. So on the data provided there is not an easy way to identify to which countr(ies)y a LINESTRING belongs to, see the data:

library(giscoR)

country_lines <- gisco_get_nuts(
  year = "2016",
  epsg = "3035",
  resolution = "20",
  nuts_level = "0",
  spatialtype = "BN",
  country = c("Czechia", "Germany", "Lithuania", "Estonia", "Italy", "Spain")
)

nrow(country_lines)
#> [1] 1253
names(country_lines)
#> [1] "EU_FLAG"    "OTHR_FLAG"  "COAS_FLAG"  "FID"        "EFTA_FLAG" 
#> [6] "CC_FLAG"    "NUTS_BN_ID" "LEVL_CODE"  "geometry"

tibble::as_tibble(country_lines)
#> # A tibble: 1,253 × 9
#>    EU_FLAG OTHR_FLAG COAS_FLAG   FID EFTA_FLAG CC_FLAG NUTS_BN_ID LEVL_CODE
#>    <chr>   <chr>     <chr>     <dbl> <chr>     <chr>        <dbl>     <dbl>
#>  1 T       F         T           190 F         F              190         0
#>  2 T       F         T           195 F         F              195         0
#>  3 T       F         T           196 F         F              196         0
#>  4 T       F         T           210 F         F              210         0
#>  5 T       F         T           217 F         F              217         0
#>  6 T       F         T           218 F         F              218         0
#>  7 T       F         T           219 F         F              219         0
#>  8 T       F         F           223 F         F              223         0
#>  9 T       F         F           224 F         F              224         0
#> 10 T       F         T           226 F         F              226         0
#> # … with 1,243 more rows, and 1 more variable: geometry <LINESTRING [m]>

plot(country_lines[1:10, ]$geometry, col = "red", axes=TRUE)

So depending on your use case, you can transform the POLYGON to a LINESTRING with sf::st_cast(), if this may help:

library(giscoR)

country_lines2 <- gisco_get_nuts(
  year = "2016",
  epsg = "3035",
  resolution = "20",
  nuts_level = "0",
  spatialtype = "RG",
  country = c("Czechia", "Germany", "Lithuania", "Estonia", "Italy", "Spain")
) |>
  sf::st_cast("MULTILINESTRING") |>
  sf::st_cast("LINESTRING")
#> Warning in st_cast.sf(sf::st_cast(gisco_get_nuts(year = "2016", epsg = "3035", :
#> repeating attributes for all sub-geometries for which they may not be constant

nrow(country_lines2)
#> [1] 50
names(country_lines2)
#>  [1] "NUTS_ID"    "LEVL_CODE"  "URBN_TYPE"  "CNTR_CODE"  "NAME_LATN" 
#>  [6] "NUTS_NAME"  "MOUNT_TYPE" "COAST_TYPE" "FID"        "geometry"

tibble::as_tibble(country_lines2)
#> # A tibble: 50 × 10
#>    NUTS_ID LEVL_CODE URBN_TYPE CNTR_CODE NAME_LATN NUTS_…¹ MOUNT…² COAST…³ FID  
#>    <chr>       <dbl>     <dbl> <chr>     <chr>     <chr>     <dbl>   <dbl> <chr>
#>  1 ES              0         0 ES        ESPAÑA    ESPAÑA        0       0 ES   
#>  2 ES              0         0 ES        ESPAÑA    ESPAÑA        0       0 ES   
#>  3 ES              0         0 ES        ESPAÑA    ESPAÑA        0       0 ES   
#>  4 ES              0         0 ES        ESPAÑA    ESPAÑA        0       0 ES   
#>  5 ES              0         0 ES        ESPAÑA    ESPAÑA        0       0 ES   
#>  6 ES              0         0 ES        ESPAÑA    ESPAÑA        0       0 ES   
#>  7 ES              0         0 ES        ESPAÑA    ESPAÑA        0       0 ES   
#>  8 ES              0         0 ES        ESPAÑA    ESPAÑA        0       0 ES   
#>  9 ES              0         0 ES        ESPAÑA    ESPAÑA        0       0 ES   
#> 10 ES              0         0 ES        ESPAÑA    ESPAÑA        0       0 ES   
#> # … with 40 more rows, 1 more variable: geometry <LINESTRING [m]>, and
#> #   abbreviated variable names ¹​NUTS_NAME, ²​MOUNT_TYPE, ³​COAST_TYPE

plot(country_lines2$geometry, col = "red", axes=TRUE)

Created on 2023-01-09 with reprex v2.0.2

umbe1987 commented 1 year ago

Yes, this is expected, since GISCO data does not provide a LINESTRING at country level, but at segment (boundary?) level. So on the data provided there is not an easy way to identify to which countr(ies)y a LINESTRING belongs to, see the data:

library(giscoR)

country_lines <- gisco_get_nuts(
  year = "2016",
  epsg = "3035",
  resolution = "20",
  nuts_level = "0",
  spatialtype = "BN",
  country = c("Czechia", "Germany", "Lithuania", "Estonia", "Italy", "Spain")
)

nrow(country_lines)
#> [1] 1253
names(country_lines)
#> [1] "EU_FLAG"    "OTHR_FLAG"  "COAS_FLAG"  "FID"        "EFTA_FLAG" 
#> [6] "CC_FLAG"    "NUTS_BN_ID" "LEVL_CODE"  "geometry"

tibble::as_tibble(country_lines)
#> # A tibble: 1,253 × 9
#>    EU_FLAG OTHR_FLAG COAS_FLAG   FID EFTA_FLAG CC_FLAG NUTS_BN_ID LEVL_CODE
#>    <chr>   <chr>     <chr>     <dbl> <chr>     <chr>        <dbl>     <dbl>
#>  1 T       F         T           190 F         F              190         0
#>  2 T       F         T           195 F         F              195         0
#>  3 T       F         T           196 F         F              196         0
#>  4 T       F         T           210 F         F              210         0
#>  5 T       F         T           217 F         F              217         0
#>  6 T       F         T           218 F         F              218         0
#>  7 T       F         T           219 F         F              219         0
#>  8 T       F         F           223 F         F              223         0
#>  9 T       F         F           224 F         F              224         0
#> 10 T       F         T           226 F         F              226         0
#> # … with 1,243 more rows, and 1 more variable: geometry <LINESTRING [m]>

plot(country_lines[1:10, ]$geometry, col = "red", axes=TRUE)

So depending on your use case, you can transform the POLYGON to a LINESTRING with sf::st_cast(), if this may help:

library(giscoR)

country_lines2 <- gisco_get_nuts(
  year = "2016",
  epsg = "3035",
  resolution = "20",
  nuts_level = "0",
  spatialtype = "RG",
  country = c("Czechia", "Germany", "Lithuania", "Estonia", "Italy", "Spain")
) |>
  sf::st_cast("MULTILINESTRING") |>
  sf::st_cast("LINESTRING")
#> Warning in st_cast.sf(sf::st_cast(gisco_get_nuts(year = "2016", epsg = "3035", :
#> repeating attributes for all sub-geometries for which they may not be constant

nrow(country_lines2)
#> [1] 50
names(country_lines2)
#>  [1] "NUTS_ID"    "LEVL_CODE"  "URBN_TYPE"  "CNTR_CODE"  "NAME_LATN" 
#>  [6] "NUTS_NAME"  "MOUNT_TYPE" "COAST_TYPE" "FID"        "geometry"

tibble::as_tibble(country_lines2)
#> # A tibble: 50 × 10
#>    NUTS_ID LEVL_CODE URBN_TYPE CNTR_CODE NAME_LATN NUTS_…¹ MOUNT…² COAST…³ FID  
#>    <chr>       <dbl>     <dbl> <chr>     <chr>     <chr>     <dbl>   <dbl> <chr>
#>  1 ES              0         0 ES        ESPAÑA    ESPAÑA        0       0 ES   
#>  2 ES              0         0 ES        ESPAÑA    ESPAÑA        0       0 ES   
#>  3 ES              0         0 ES        ESPAÑA    ESPAÑA        0       0 ES   
#>  4 ES              0         0 ES        ESPAÑA    ESPAÑA        0       0 ES   
#>  5 ES              0         0 ES        ESPAÑA    ESPAÑA        0       0 ES   
#>  6 ES              0         0 ES        ESPAÑA    ESPAÑA        0       0 ES   
#>  7 ES              0         0 ES        ESPAÑA    ESPAÑA        0       0 ES   
#>  8 ES              0         0 ES        ESPAÑA    ESPAÑA        0       0 ES   
#>  9 ES              0         0 ES        ESPAÑA    ESPAÑA        0       0 ES   
#> 10 ES              0         0 ES        ESPAÑA    ESPAÑA        0       0 ES   
#> # … with 40 more rows, 1 more variable: geometry <LINESTRING [m]>, and
#> #   abbreviated variable names ¹​NUTS_NAME, ²​MOUNT_TYPE, ³​COAST_TYPE

plot(country_lines2$geometry, col = "red", axes=TRUE)

Created on 2023-01-09 with reprex v2.0.2

Thank you for your feedback, this should be fine.