Ivo
Ivo

Reputation: 4200

Latitude/Longitude to NUTS3 (in R)

I have a dataset of latitude & longitude values, like this:

df_latlon <- tibble("lats" =  c(52.5200, 48.8567, 51.5072),
                    "longs" = c(13.4050,  2.2885,  0.1276)) # DE-Berlin, FR-Paris, CH-Römerwil LU

I would now like to convert these lat/lon coordinates to NUTS3 units (http://nuts.geovocab.org/id/nuts3.html) by looking up in which polygon they are situated.

The desired output is something like this:

df_latlon %>% mutate(NUTS3 = function_to_be_found(latitude=lat, longitude=lon))
#    lats  longs NUTS3
# 1  52.5 13.4   DE300
# 2  48.9  2.29  FR101
# 3  51.5  0.128 CH061

Is anyone aware of an existing implementation of this? Many thanks in advance :)

Upvotes: 3

Views: 619

Answers (1)

Grzegorz Sapijaszko
Grzegorz Sapijaszko

Reputation: 3604

You can use giscoR package:

df_latlon <- tibble::tibble("lats" =  c(52.5200, 48.8567, 51.5072),
                    "longs" = c(13.4050,  2.2885,  0.1276)) # DE-Berlin, FR-Paris, CH-Römerwil LU

df_latlon |>
  sf::st_as_sf(coords = c("longs", "lats"), crs = 4326) |>
  sf::st_join(giscoR::gisco_nuts)  |>
  subset(LEVL_CODE == 3)
#> Simple feature collection with 2 features and 9 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 2.2885 ymin: 48.8567 xmax: 13.405 ymax: 52.52
#> Geodetic CRS:  WGS 84
#> # A tibble: 2 × 10
#>           geometry NAME_LATN MOUNT_TYPE CNTR_CODE URBN_TYPE COAST_TYPE FID  
#>        <POINT [°]> <chr>          <dbl> <chr>         <dbl>      <dbl> <chr>
#> 1   (13.405 52.52) Berlin             4 DE                1          3 DE300
#> 2 (2.2885 48.8567) Paris              4 FR                1          3 FR101
#> # … with 3 more variables: NUTS_NAME <chr>, NUTS_ID <chr>, LEVL_CODE <dbl>

There is an issue with your 3rd point - seems, it appears outside of UK polygon, therefore is not in output data frame.

Created on 2022-10-16 with reprex v2.0.2

EDIT with replaced coordinates for 3rd point, Römerswil

df_latlon <- tibble::tibble("lats" =  c(52.5200, 48.8567, 47.1770),
                    "longs" = c(13.4050,  2.2885,  8.2539)) # DE-Berlin, FR-Paris, CH-Römerswil LU

df_latlon |>
  sf::st_as_sf(coords = c("longs", "lats"), crs = 4326) |>
  sf::st_join(giscoR::gisco_nuts)  |>
  subset(LEVL_CODE == 3)
#> Simple feature collection with 3 features and 9 fields
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 2.2885 ymin: 47.177 xmax: 13.405 ymax: 52.52
#> Geodetic CRS:  WGS 84
#> # A tibble: 3 × 10
#>           geometry NAME_LATN MOUNT_TYPE CNTR_CODE URBN_TYPE COAST_TYPE FID  
#>        <POINT [°]> <chr>          <dbl> <chr>         <dbl>      <dbl> <chr>
#> 1   (13.405 52.52) Berlin             4 DE                1          3 DE300
#> 2 (2.2885 48.8567) Paris              4 FR                1          3 FR101
#> 3  (8.2539 47.177) Luzern             3 CH                2          0 CH061
#> # … with 3 more variables: NUTS_NAME <chr>, NUTS_ID <chr>, LEVL_CODE <dbl>

Created on 2022-10-16 with reprex v2.0.2

Upvotes: 1

Related Questions