ropensci / stplanr

Sustainable transport planning with R
https://docs.ropensci.org/stplanr
Other
416 stars 66 forks source link

`rnet_merge()` fails when inputs are projected #549

Closed Robinlovelace closed 9 months ago

Robinlovelace commented 9 months ago

As illustrated in the minimal reproducible example below. Heads-up @wangzhao0217 I think this may have been the issue you were hitting in #538

library(stplanr)
library(sf)
#> Linking to GEOS 3.11.1, GDAL 3.6.4, PROJ 9.1.1; sf_use_s2() is TRUE
rnet_x = sf::read_sf("https://github.com/nptscot/npt/releases/download/v2/rnet_x_thurso.geojson")
rnet_y = sf::read_sf("https://github.com/nptscot/npt/releases/download/v2/rnet_y_thurso.geojson")

name_list = names(rnet_y)
funs = list()

# Loop through each name and assign it a function based on specific conditions
for (name in name_list) {
  if (name == "geometry") {
    next  # Skip the current iteration
  } else if (name %in% c("Gradient", "Quietness")) {
    funs[[name]] = mean
  } else {
    funs[[name]] = sum
  }
}

nrow(rnet_x)
#> [1] 913
nrow(rnet_y)
#> [1] 639

# Convert to projected geometry:
rnet_xp = st_transform(rnet_x, "EPSG:27700")
rnet_yp = st_transform(rnet_y, "EPSG:27700")

runtime = system.time({
  rnet_merged = rnet_merge(rnet_x, rnet_y, dist = 20, segment_length = 10, funs = funs, max_angle_diff = 20)
})
#> Warning: attribute variables are assumed to be spatially constant throughout
#> all geometries
#> Warning in st_cast.sf(sf::st_cast(x, "MULTILINESTRING"), "LINESTRING"):
#> repeating attributes for all sub-geometries for which they may not be constant
#> Joining with `by = join_by(identifier)`
#> Warning: st_centroid assumes attributes are constant over geometries
#> Joining with `by = join_by(identifier)`
# Segments per second:
nrow(rnet_x) / runtime[3]
#> elapsed 
#> 8.77623

# Convert to projected geometry:
rnet_xp = st_transform(rnet_x, "EPSG:27700")
rnet_yp = st_transform(rnet_y, "EPSG:27700")

runtime = system.time({
  rnet_merged = rnet_merge(rnet_xp, rnet_yp, dist = 20, segment_length = 10, funs = funs, max_angle_diff = 20)
})
#> Warning: attribute variables are assumed to be spatially constant throughout
#> all geometries

#> Warning: repeating attributes for all sub-geometries for which they may not be
#> constant
#> Joining with `by = join_by(identifier)`
#> Error in lwgeom::st_geod_azimuth(p[i:(i + 1)]): st_is_longlat(x) is not TRUE

Created on 2023-10-04 with reprex v2.0.2

wangzhao0217 commented 9 months ago

Hi Robin, The issue with rnet_merge() still persists. I added more places for testing:

points_of_interest = c("Thurso", "Girlsta",  "Edinburgh")

image

At Edinburgh, works perfectly! image but no results for Thurso and Girlsta image image

Robinlovelace commented 9 months ago

Thanks Zhao, this is a good description of the consequence of this issue. Will work on it.

wangzhao0217 commented 9 months ago

Hi Robin, I found the issue is related to rnet_subset(), not rnet_merge(). Executing the code yields the correct results:

rnet_merged = rnet_merge(rnet_x, rnet_y, dist = 20, segment_length = 10, funs = funs, max_angle_diff = 20, crs = "EPSG:27700",subset_x = FALSE) 

image image

I will close this issue open other one.

Robinlovelace commented 9 months ago

Yes: that makes sense. Try without running rnet_subset()?