JeremyGelb / spNetwork

An R package to perform spatial analysis on networks.
GNU General Public License v2.0
34 stars 2 forks source link

Error in check_geometries(lines, samples, events, study_area) #5

Closed FelipeSBarros closed 2 years ago

FelipeSBarros commented 2 years ago

Hello there, @JeremyGelb ! First of all, thnks and congratulations for this amazing work!

I could run the tutorials just fine, so I decided to give a try to a data set I have, and I am getting the following error:

Error in check_geometries(lines, samples, events, study_area) :    the lines must be simple geometries,
considere using the function sp::disaggregate

by this time I can hear you asking for me to give more context, so:

ruas <- read_sf("../jupyter/graph.gpkg", layer='edges')
ruas <- st_transform(ruas, 32723)
ruas <- as(ruas, "Spatial")
ruas <- sp::disaggregate(ruas)

# then calculating some lixels to use as sampling points
lixels <- lixelize_lines(ruas, 50, mindist = 10)
lixels <- sp::disaggregate(lixels)
samples <- lines_center(lixels)

densities <- nkde(ruas,
                  events = fc,
                  w = rep(1,nrow(fc)),
                  samples = samples,
                  kernel_name = "quartic",
                  bw = 300, 
                  div= "bw",
                  method = "discontinuous",
                  digits = 2, tol =  0.1,
                  grid_shape = c(1,1),
                  max_depth = 8,
                  agg = 5, sparse = TRUE,
                  verbose = TRUE)

As you can read I haeve already tryed with sp::disagragate, but still having the same error. Do you have any glue about what I am doing wrong? Best

JeremyGelb commented 2 years ago

Hello @FelipeSBarros,

Thank you for your interest in spNetwork !

I think I know what is the cause of the error. The function sp::disagragate should be used on the dataset ruas, not lixels. As you can see in your code, the lixels are used to extract their centers as sampling points for the density estimation. They do not compose the underlying network. ruas is the used road network.

To create a network, the package needs simple linestrings, but often, GISs save lines data as multilinestrings, that is why the check function raise an error when they are not disagragated.

Let me know if it solves your issue !

PS: if you realize a nice map with spNetwork, please consider sharing it with me. I would love to add some more visual examples in the package description (obviously, you will be cited as the author of the map)

FelipeSBarros commented 2 years ago

That's right, @JeremyGelb . The code In have shared I was using the disagregate in the linxel. But I have tried using it in Russ dataset and got the same error. And sorry for questioning, but why the package is not using sf class?

Hope I can run my analysis so I can share with you:) Thanks for your time

JeremyGelb commented 2 years ago

Well, this is strange, could you share with me the gpkg file and the points you plan to use (only a subset is enough if the data is private). It could be a bug or a special case with data coming from OpenStreetMap. I need to investigate.

spNetwork is currently using sp because I was more confident with its structure when I started the development. I plan to migrate to sf soon (2022 ?) considering that rgeos and maptools will be deprecated in 2023. But ultimately this does affect that much the user because conversion between sp and sf like objects are easy nowadays.

JeremyGelb commented 2 years ago

@FelipeSBarros, Before sending me the data, you could use the function gIsSimple from rgeos (errors <- subset(ruas, gIsSimple(ruas , byid=TRUE) == FALSE) to find which features do not have simple geometries. Maybe some of them have a NULL geometry (could happen with OSM). You could remove them from the dataset or edit them is QGIS if needed.

FelipeSBarros commented 2 years ago

Hey @JeremyGelb ! Thanks for your help. I could run the code here with more attention. I found, using gIsSimple, that a few features were note simple feature. I fixed removing then, as my intention for now is to have an over view of the packge.

Do you know if there is a python module with those analysis?

JeremyGelb commented 2 years ago

Hi @FelipeSBarros,

Excellent! I am happy that everything is running now. I don't know any module to do NKDE analysis in python. You could play arround with rpy2 and reticulate to integrate R and python to use spNetwork within python, but it might no be very handy.

With your consent, I will close this issue because the "bug" is solved. Feel free to open a new one if you encounter another problem.

FelipeSBarros commented 2 years ago

Yes, @JeremyGelb . Not a problem! I will close, but if you don't mind, could I keep the conversation here? I am still studying the method I probably will have some doubts. Thanks in Advance

JeremyGelb commented 2 years ago

No problem, you can keep the conversation here

JeremyGelb commented 2 years ago

Note: the error message has been changed to mention the function rgeos::gIsSimple

the lines must be simple geometries, considere using the function sp::disaggregate . You can also check which geometries are not simple with the fuction rgeos::gIsSimple