Comments (2)
regarding minimum spanning tree this can be accomplished using spdep and sfdep. here's an example of creating an sfnetwork using an mstree.
library(sf)
library(dplyr)
library(sfdep)
library(sfnetworks)
geo <- sfdep::guerry_nb |>
select(geometry) |>
mutate(nb = st_complete_nb(n()),
wt = st_nb_dists(geometry, nb))
#> ! Polygon provided. Using point on surface.
listw <- recreate_listw(geo$nb, structure(geo$wt, "B" = TRUE))
ms <- spdep::mstree(listw)
ms_df <- as.data.frame(ms) |>
rlang::set_names(c("from", "to", "edge_length"))
geom <- sf::st_centroid(geo)[["geometry"]]
#> Warning in st_centroid.sf(geo): st_centroid assumes attributes are constant over
#> geometries of x
nodes <- geom
from <- geom[ms_df$from]
to <- geom[ms_df$to]
from <- as_tibble(st_coordinates(from)) |>
mutate(id = row_number())
to <- as_tibble(st_coordinates(to)) |>
mutate(id = row_number())
# need to find a way to arrange using base R
edges2 <- sfheaders::sf_linestring(
arrange(bind_rows(from, to), id),
x = "X",
y = "Y",
linestring_id = "id") |>
bind_cols(ms_df)
plot(sfnetwork(nodes, edges2))
#> Checking if spatial network structure is valid...
#> Spatial network structure is valid
Created on 2022-08-23 by the reprex package (v2.0.1)
from sfnetworks.
Creating an sfnetwork using spdep + sfdep for delaunay triangulation and gabriel neighbors
# spdep -------------------------------------------------------------------
library(sf)
#> Linking to GEOS 3.9.1, GDAL 3.2.3, PROJ 7.2.1; sf_use_s2() is TRUE
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(spdep)
#> Loading required package: sp
#> Loading required package: spData
#> To access larger datasets in this package, install the spDataLarge
#> package with: `install.packages('spDataLarge',
#> repos='https://nowosad.github.io/drat/', type='source')`
library(sfnetworks)
nodes <- st_centroid(sfdep::guerry)
#> Warning in st_centroid.sf(sfdep::guerry): st_centroid assumes attributes are
#> constant over geometries of x
geo <- st_geometry(nodes)
nb <- tri2nb(geo)
plot(nb, geo)
wt <- sfdep::st_nb_dists(geo, nb)
sfdep::st_as_graph(geo, nb, wt) |>
plot()
#> Checking if spatial network structure is valid...
#> Spatial network structure is valid
# gabriel neighbors
# ensure that it is symmetric to make sure no missing links
nb <- graph2nb(gabrielneigh(geo), sym = TRUE)
wt <- sfdep::st_nb_dists(geo, nb)
sfdep::st_as_graph(geo, nb, wt) |>
plot()
#> Checking if spatial network structure is valid...
#> Spatial network structure is valid
Created on 2022-08-23 by the reprex package (v2.0.1)
from sfnetworks.
Related Issues (20)
- Package archived on CRAN HOT 1
- Test fails with the development version of igraph HOT 2
- Network cleaning: to_spatial_contracted returns mapply error HOT 4
- Converting sf_linestrings to sfnetwork causes some intersections to shift HOT 1
- Calculating Driving Distances using sfnetworks HOT 1
- Extracting GPS coordinates from SF network build from OSM Data, provides those in a wrong order HOT 8
- If the grouping variable is out of order, `to_spatial_contracted` returns incorrect node geometries HOT 1
- st_network_blend unable to blend count locations into bike network HOT 5
- Extend plot.sfnetwork to include the possibility of specifying different args for nodes and edges HOT 1
- The `trunc_mat` function we use in print.sfnetwork is deprecated HOT 2
- Enhance integration with spatstat package HOT 2
- Support cppRouting HOT 16
- Error when blending points into network HOT 3
- Support typed library
- Print method does not work with spatially implicit edges HOT 5
- Something wrong with calculating shortest paths HOT 1
- Edges does not follow right directions HOT 2
- Quiet option to skip validity text
- Active weight column HOT 1
- Spatstat error in CRAN checks HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sfnetworks.