Open xiaochi-liu opened 11 months ago
Maybe because multiple points are associated with a single point on the network?
I think this is an error that occurs when blending sf objects with attributes into the network and every external point is matched with an existing node. Simpler reprex:
#packages
library(sf)
#> Linking to GEOS 3.11.2, GDAL 3.6.2, PROJ 9.2.0; sf_use_s2() is TRUE
library(sfnetworks)
# toy-data
l <- st_linestring(rbind(c(0, 0), c(1, 1)))
sfc <- as_sfnetwork(st_sfc(l))
external_point <- st_sfc(st_point(c(-1, -1)))
# plot
# plot(sfc, axes = TRUE, xlim = c(-1.5, 2), ylim = c(-1.5, 2), cex = 2, lwd = 2)
# plot(external_point, col = "red", add = TRUE, pch = 20, cex = 2)
# blend sfc object
sfc2 <- st_network_blend(sfc, external_point)
# the output is identical to original net
igraph::identical_graphs(sfc, sfc2)
#> [1] TRUE
# since the closest point to "external_point" is the boundary point of l.
# Nevertheless, if I add an attribute to "external_point"
external_sf <- st_sf(
data.frame(x = "A"),
geometry = external_point
)
st_network_blend(sfc, external_sf)
#> Error in `[[<-.data.frame`(`*tmp*`, i, value = 3:2): replacement has 2 rows, data has 0
Created on 2023-08-11 with reprex v2.0.2
I think the error is here
since new_feat_idxs
is created as follows
and is_new
might be a vector of just FALSE
values (implying that new_feat_idxs
is a vector with 0 elements).
Thank you very much @agila5 and @Robinlovelace!
I removed all attributes for the external points, which can then be blended into the network!
May I ask, is it possible to fix it? What I want to do after blending is to extract the network nodes that are generated by the points blended:
library(sf)
library(sfnetworks)
# toy-data
l = st_linestring(rbind(c(0, 0), c(1, 1)))
sfc = as_sfnetwork(st_sfc(l))
external_sf = st_sf(
data.frame(x = "A"),
geometry = external_point
)
# blend external points into the network (if successful)
sfc2 = st_network_blend(sfc, external_sf)
# extract the nodes that are generated by blending points
sfc2 %>%
activate("nodes") %>%
st_as_sf() %>%
filter(!is.na(A)) -> sf_point_blended
Thus, it would be great if the external points had some attributes.
Describe the bug Hi there. Thanks for this handy package!
I just got an minor error that couldn't blend certain points into the road network:![image](https://github.com/luukvdmeer/sfnetworks/assets/48895627/4c01fb97-b0f7-4c33-acbd-53d34b5f3e9c)
This is strange because the
st_network_blend()
method works perfectly fine for my other data points. Only the points shown above couldn't be blended. The test data can be accessed here.Reproducible example
Expected behavior The
sf_point_test
can be blended intosfnetwork_road_test
.R Session Info
Thank you very much for your kind guidance!