JeremyGelb/spNetwork

Error in K-function Estimation

Janjua1991 opened this issue · 7 comments

Hi Jeremy. Thank you for updating and improving the spNetwork package.

I am facing some issues while running the k-function code.
I am using projected coordinates for both line and point shapefiles.
The error I am getting is :

Error in coordsLines[[nearest_line_index[x]]] :
attempt to select less than one element in get1index

Can you please help me to resolve this error?
Thanks

Hello There!

First of all, thank you for your interest in spNetwork! Could you send me a minimal sample of the data your are using and some code to replicate the bug?

I have reduced the network size and when i run the code again, this time i got different error.

I was running following Code:

library(spNetwork)
library(tmap)
library(rgdal)
library(sf)

Lines<- read_sf('D:/Data for jeremy/Sample Data/Sample_lines.shp')
Points <- read_sf('D:/Data for jeremy/Sample Data/Sample_Points.shp')

tm_shape(Lines) +
tm_lines("black") +
tm_shape(Points) +
tm_dots(col = "red", size = 0.1)

k_Incidents <- kfunctions(Lines, Points,
start = 1, end = 5000, step = 50,
width = 1000, nsim = 10, resolution = 50,
verbose = FALSE, conf_int = 0.05)

This is the error i got this time:
Error in data.frame(obs_k = k_vals, lower_k = k_stats[1, ], upper_k = k_stats[2, :
arguments imply differing number of rows: 101, 100

In another attempt, I tried to fix network topology issues. but again in the end I got an issue.
this was the error message I received when I was using the whole network and full point data set.
Error: cannot allocate vector of size 783.2 Mb

Sample Data.zip

Ok, I verified your example and indeed you found a bug! Thank you for sending me the data to reproduce it. It is very appreciated.

I will correct it as soon as possible. If you need your results quickly, a slight modification in your code would do the trick : replace the value of start by 0:

library(spNetwork)
library(tmap)
library(sf)

Lines<- st_read('C:/Users/Gelb/Desktop/TEMP/test_error/Sample.Data/Sample_lines.shp')
Points <- st_read('C:/Users/Gelb/Desktop/TEMP/test_error/Sample.Data/Sample_Points.shp')

tm_shape(Lines) +
  tm_lines("black") +
  tm_shape(Points) +
  tm_dots(col = "red", size = 0.1)



k_Incidents <- kfunctions(Lines, Points,
                          start = 0, end = 5000, step = 50,
                          width = 1000, nsim = 10, resolution = 50,
                          verbose = TRUE, conf_int = 0.05)

I will close this issue when the bug will be properly corrected.

Thank you for your quick response and guidance.
I am facing another error as well, please guide me on this too:
Error in igraph::distances(graph, v = snapped_events$vertex_id, to = snapped_events$vertex_id) :
At core/paths/dijkstra.c:148 : Duplicate vertices in `to', this is not allowed, Invalid value

I am providing here a complete code with data to replicate the results. I am also sharing the outcome showing the error:

Code

library(spNetwork)
library(tmap)
library(sf)

Lines<- st_read('D:/Data for jeremy/Datafile/Lines.shp')
Points<- st_read('D:/Data for jeremy/Datafile/Points.shp')

tm_shape(Lines) +
  tm_lines("black") +
  tm_shape(Points) +
  tm_dots(col = "red", size = 0.1)


k_results <- kfunctions(Lines, Points,
                          start = 0, end = 500, step = 10,
                          width = 20, nsim = 50, resolution = 50,
                          verbose = TRUE, conf_int = 0.05)

Outcome with error in last line

> library(spNetwork)
> library(tmap)
> library(sf)
> 
> Lines<- st_read('D:/Data for jeremy/Datafile/Lines.shp')
Reading layer `Lines' from data source `D:\Data for jeremy\Datafile\Lines.shp' using driver `ESRI Shapefile'
Simple feature collection with 19155 features and 10 fields
Geometry type: LINESTRING
Dimension:     XY
Bounding box:  xmin: -2125768 ymin: 4605261 xmax: -2112230 ymax: 4618447
Projected CRS: WGS 84 / UTM zone 15N
> Points<- st_read('D:/Data for jeremy/Datafile/Points.shp')
Reading layer `Points' from data source `D:\Data for jeremy\Datafile\Points.shp' using driver `ESRI Shapefile'
Simple feature collection with 5688 features and 2 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: -2125460 ymin: 4605663 xmax: -2112540 ymax: 4618229
Projected CRS: WGS 84 / UTM zone 15N
> 
> tm_shape(Lines) +
+   tm_lines("black") +
+   tm_shape(Points) +
+   tm_dots(col = "red", size = 0.1)
> 
> 
> k_results <- kfunctions(Lines, Points,
+                           start = 0, end = 500, step = 10,
+                           width = 20, nsim = 50, resolution = 50,
+                           verbose = TRUE, conf_int = 0.05)
[1] "Preparing data ..."
[1] "Snapping points on lines ..."
[1] "Building graph ..."
Error in igraph::distances(graph, v = snapped_events$vertex_id, to = snapped_events$vertex_id) : 
  At core/paths/dijkstra.c:148 : Duplicate vertices in `to', this is not allowed, Invalid value
>

Thank you again for the detailed problem description! It is really appreciated.

The error is caused by points that share the same location when they are snapped on the network. I will provide an error message about this case in the next version. Currently, you could just set the parameter agg to a small value like 2 (meters) so that points in a two meters radius will be merged (and their weights added). This should be a reasonable approximation and the function will work.

Again, thank you very much for your participation in spNetwork's bugs resolving!

This has been corrected in the new dev version. I am closing the issue. Feel free to open a new one if you find another bug