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

Unable to download single country data if spatialtype is specified #75

Closed richardtc closed 5 months ago

richardtc commented 8 months ago

Entire dataset downloads if any spatialtype is specified. E.g.,

khm01 <- gisco_get_countries(resolution = "01", country = "KHM", spatialtype = "INLAND", cache_dir = "downloads/gisco")
plot(st_geometry(khm01))

R version 4.3.2 (2023-10-31) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 22.04.3 LTS giscoR_0.4.0

dieghernan commented 8 months ago

Hi,

Yes, thta's expected since the "INLAND dataset as provided by GISCO is not labelled in a consistent way, and the parameter country is ignored. I should document this better, I think.

Long explanation

There is a variable in that dataset named CNTR_CODE, but it just includes one of the countries to which the border belongs to. This makes very difficult to extract all the borders of a single country with a filter. Let me explain:

library(giscoR)
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1; sf_use_s2() is TRUE
library(tidyverse)

khm01 <- gisco_get_countries(resolution = "01", country = "KHM", spatialtype = "INLAND", cache_dir = "downloads/gisco")
plot(st_geometry(khm01))

This is where you are at this moment. See the variables of the dataset.


glimpse(khm01)
#> Rows: 540
#> Columns: 11
#> $ EU_FLAG      <chr> "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F…
#> $ OTHR_FLAG    <chr> "T", "T", "T", "T", "T", "T", "T", "T", "T", "T", "T", "T…
#> $ COAS_FLAG    <chr> "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F…
#> $ FID          <dbl> 2279, 6149, 6175, 6902, 6435, 6911, 6914, 6307, 6555, 505…
#> $ EFTA_FLAG    <chr> "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F…
#> $ CC_FLAG      <chr> "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F…
#> $ CNTR_BN_ID   <dbl> 2279, 6149, 6175, 6902, 6435, 6911, 6914, 6307, 6555, 505…
#> $ POL_STAT     <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
#> $ CNTR_BN_CODE <dbl> 2279, 6149, 6175, 6902, 6435, 6911, 6914, 6307, 6555, 505…
#> $ CNTR_CODE    <chr> "CL", "SZ", "BO", "ZA", "PY", "NA", "PY", "ZA", "ZA", "BR…
#> $ geometry     <LINESTRING [°]> LINESTRING (-68.60748 -54.8..., LINESTRING (32…

One may think that filtering by CNTR_CODE == "KH" would be enough, but check this. If I get which borders touches with the country polygon...

khm_cntry <- gisco_get_countries(resolution = "01", country = "KHM", cache_dir = "downloads/gisco")

# Get actual borders

inlands <- khm01[st_intersects(khm01, khm_cntry, sparse = FALSE), ]

inlands
#> Simple feature collection with 5 features and 10 fields
#> Geometry type: LINESTRING
#> Dimension:     XY
#> Bounding box:  xmin: 102.1439 ymin: 10.40613 xmax: 107.704 ymax: 22.40069
#> Geodetic CRS:  WGS 84
#>     EU_FLAG OTHR_FLAG COAS_FLAG   FID EFTA_FLAG CC_FLAG CNTR_BN_ID POL_STAT
#> 135       F         T         F 18321         F       F      18321        0
#> 152       F         T         F 19933         F       F      19933        0
#> 165       F         T         F 18184         F       F      18184        0
#> 197       F         T         F 17582         F       F      17582        0
#> 288       F         T         F 21207         F       F      21207        0
#>     CNTR_BN_CODE CNTR_CODE                       geometry
#> 135        18321        VN LINESTRING (107.5561 14.686...
#> 152        19933        LA LINESTRING (105.2001 14.342...
#> 165        18184        TH LINESTRING (102.9109 11.647...
#> 197        17582        VN LINESTRING (106.4434 11.822...
#> 288        21207        VN LINESTRING (102.1439 22.400...

# Get by id
library(ggplot2)
ggplot(inlands) +
  geom_sf(aes(color = as_factor(CNTR_BN_ID)))

Note that the borders seems to be right, however CNTR_CODE has values only for Thailand, Laos and Vietnam (TH LA, VN). What we can do is filter by CNTR_BN_CODE to get only the borders of interest (visually).


# Final inland of Cambodia
inland_KHM <- inlands %>%
  filter(CNTR_BN_ID != 21207)

ggplot(inland_KHM) +
  geom_sf(aes(color = as_factor(CNTR_BN_ID)))

Created on 2024-01-04 with reprex v2.0.2

dieghernan commented 8 months ago

See also #50 , very similar issue with another dataset