rspatialgeocoding

geocoding nodes of graph dont find anything


I try to georeferenz the nodes of a network. So first I extract the nodes from the network like:

nodes_index <- rails_network %>%  activate(nodes) %>% as_tibble() %>% st_as_sf() %>% mutate(long = st_coordinates(.)[,1],
                                                                                            lat = st_coordinates(.)[,2])   %>%
  st_drop_geometry()

Therefor I get a df like:

str(dput(nodes_index[1:10,1:3]))
structure(list(ID = 1:10, long = c(1233616.92623604, 1223433.99025042, 
1378366.25773239, 1401157.10267164, 1479266.77880306, 1528588.72290147, 
1509447.42453216, 1499351.98566691, 1454285.44484453, 756209.126081893
), lat = c(6350711.55217875, 6355001.32493512, 6682635.84497815, 
6684119.9464045, 6676208.51447798, 6628470.59620128, 6654633.46946046, 
6891626.20630991, 6871266.57873021, 6660285.54473999)), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))
tibble [10 × 3] (S3: tbl_df/tbl/data.frame)
 $ ID  : int [1:10] 1 2 3 4 5 6 7 8 9 10
 $ long: num [1:10] 1233617 1223434 1378366 1401157 1479267 ...
 $ lat : num [1:10] 6350712 6355001 6682636 6684120 6676209 ...

and I try to geocode those by the library(revgeo) (I also tried tidygeocoder)

nodes_index1 <-    revgeo(latitude =  nodes_index$lat, longitude =  nodes_index$long, output = "frame")

But however, both ways will not find anything, eventhough confirming with library(mapview), that all nodes are where they should be.


Solution

  • Reverse geocoding methods generally expect geographic coordinates, but in your case the coordinate reference system (CRS) of rails_network seems to be projected -- node coordinate values are well outside expected ranges (-90 ... 90 / -180 ... 180)

    To fix this, you'd need to add a transform step, it looks OK in mapview as it handles transformation for you.

    Here's an example using roxel dataset:

    library(sf)
    #> Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE
    library(sfnetworks)
    library(tidygeocoder)
    library(dplyr)
    library(tidyr)
    
    # example dataset from sfnetworks, transformed from WGS84 to UTM Z32
    roxel_sfn <- 
      roxel |> 
      st_transform(25832) |> 
      as_sfnetwork()
    
    # nodes in projected crs (UTM, check actual point values):
    roxel_sfn |> 
      activate(nodes) |> 
      st_as_sf() |> 
      print(n = 3)
    #> Simple feature collection with 701 features and 0 fields
    #> Geometry type: POINT
    #> Dimension:     XY
    #> Bounding box:  xmin: 398470.4 ymin: 5755557 xmax: 400124.7 ymax: 5757747
    #> Projected CRS: ETRS89 / UTM zone 32N
    #> # A tibble: 701 × 1
    #>             geometry
    #>          <POINT [m]>
    #> 1 (399242.2 5757111)
    #> 2 (399224.7 5757134)
    #> 3 (399151.3 5756964)
    #> # ℹ 698 more rows
    
    plot(roxel_sfn, axes = TRUE)
    

    # tranform from UTM to geogrpahic coordinates (WGS84);
    # extract coordinate values
    roxel_coords_wgs84 <-
      roxel_sfn |> 
      activate(nodes) |> 
      st_as_sf() |> 
      st_transform("WGS84") |> 
      mutate(coord = st_coordinates(geometry) |> as_tibble()) |> 
      unnest(coord)
    
    roxel_coords_wgs84 |> print(n = 3)
    #> Simple feature collection with 701 features and 2 fields
    #> Geometry type: POINT
    #> Dimension:     XY
    #> Bounding box:  xmin: 7.522622 ymin: 51.94151 xmax: 7.546705 ymax: 51.9612
    #> Geodetic CRS:  WGS 84
    #> # A tibble: 701 × 3
    #>              geometry     X     Y
    #>           <POINT [°]> <dbl> <dbl>
    #> 1 (7.533722 51.95556)  7.53  52.0
    #> 2 (7.533461 51.95576)  7.53  52.0
    #> 3 (7.532442 51.95422)  7.53  52.0
    #> # ℹ 698 more rows
    
    # look up addresses for first 3 records
    reverse_geocode(roxel_coords_wgs84[1:3,], lat = Y, long = X)
    #> Passing 3 coordinates to the Nominatim single coordinate geocoder
    #> Query completed in: 3 seconds
    #> # A tibble: 3 × 4
    #>              geometry     X     Y address                                       
    #>           <POINT [°]> <dbl> <dbl> <chr>                                         
    #> 1 (7.533722 51.95556)  7.53  52.0 9, Havixbecker Straße, Roxel, Münster-West, M…
    #> 2 (7.533461 51.95576)  7.53  52.0 22, Havixbecker Straße, Roxel, Münster-West, …
    #> 3 (7.532442 51.95422)  7.53  52.0 Annette-von-Droste-Hülshoff-Straße, Roxel, Mü…
    

    Created on 2024-07-30 with reprex v2.1.0