Closed IrenaItova closed 3 years ago
Hello ! I will have a look on this this weekend, could you write here the code used so that I can reproduce the bug ? Thank you very much for you interest in spNetwork !
Hey Jeremy,
Thanks for looking in to this! this is my code:
library(geojsonio) library(geojson) library(rgdal) library(stplanr) library(tidyverse) library(sf) library(spNetwork) library(spdep) library(sp)
cents_oa <- readOGR(file.path("01_DataInput/cents_london_oa/0_downloaded/Output_Areas__December_2011__Population_Weighted_Centroids.shp"))
nn_cents <- closest_points(cents_oa, cents_oa)
Response:
Error in closest_points(cents_oa, cents_oa) : could not find function "closest_points"
You can obtain the centroids from this site:
Great, thank you,
I am just a little bit puzzled , what do you want to do here? Do you want to find the closest neighbour of each point in the SpatialPointDataFrame cents_oa? To do so, do you want to use euclidean distance or network distance? In the second case, you will need a SpatialLineDataFrame representing the roads in your study area. In the first case, it is not necessary to use spNetwork, but a good old nearest neighbour search like :
require(fractal)
cents_oa$OID <- 1:nrow(cents_oa)
coords<- data.frame(coordinates(cents_oa))
coords$OID <- cents_oa$OID
neighbors <- findNeighbors(coords, n.neighbor = 1)
I you want to use a road network, let me know, I could give you some code here to do it.
Indeed, nearest neighbour. Thank you Jeremy (!), I was not aware of fractal
.
Would you mind giving me the code including the network-based search? Atm I was after Euclidean since that was obvious to me with closest_points
. Thanks again in advance, appreciate it!
There is a lot of package for R, do not worry if you do not know one ;-).
With spNetwork, there is currently no dedicated function to find the nearest neighbour of an observation on a network, but it is possible to find it by first creating a spatial neighbouring matrix, and then extracting for each feature its nearest neighbour. Here is a reproductible example with the datasets included in the package.
library(spNetwork)
# loading the data
networkgpkg <- system.file("extdata", "networks.gpkg",package = "spNetwork", mustWork = TRUE)
mtl_network <- rgdal::readOGR(networkgpkg,layer="mtl_network", verbose=FALSE)
eventsgpkg <- system.file("extdata", "events.gpkg", package = "spNetwork", mustWork = TRUE)
bike_accidents <- rgdal::readOGR(eventsgpkg,layer="bike_accidents", verbose=FALSE)
# for each observation in bike_accidents, finding the neighbours within a 3000m radius (on network)
listw <- network_listw(bike_accidents,
mtl_network,
maxdistance=3000,
line_weight = "length",
dist_func = 'identity', # I use here the identity function to keep the network distances
matrice_type='W', grid_shape = c(2,2))
# and then extracting for each observation its nearest neighbour
neighbours <- sapply(1:length(listw$neighbours), function(i){
listw$neighbours[[i]][listw$weights[[i]] == min(listw$weights[[i]])][[1]]
})
Thus, you will obtain a vector with the indices of the nearest observation for each row in bike_accidents. If a big network is used, it could be great to use the multicore version (network_listw.mc) and a finer grid (with higher values in grid_shape). If it is ok for you, I will close this issue
Wonderful! Thank you so much. Feel free to close the issue.
Perfect !
for completness, a new function has been added to the package today : network_knn, that will perform k-nearest neighbour search on a network without extra-code.
Thank you for developing this package, very much needed!
I have issue with
closest_points
for calculating the nearest neighbour between twoSpatialPointDataFrames
, it returns this message:Error in closest_points(x, y) : could not find function "closest_points"
I tried upgrading R to the latest 4.0.5 version, but that didn't help. I also tried downloading the package directly from your repo with
devtools::install_github("JeremyGelb/spNetwork")
with no luck either. Do you think I am missing any dependent package?