ropensci / stplanr

Sustainable transport planning with R
https://docs.ropensci.org/stplanr
Other
420 stars 66 forks source link

route_dodgr #308

Closed yairlevy closed 5 years ago

yairlevy commented 5 years ago

Applying route_dodgr between an origin point location and many destination point locations, the function fails giving following error message:

xy has no named columns; assuming order is x then y xy has no named columns; assuming order is x then y Error in is_numeric_matrix(x) : is.numeric(x) && is.matrix(x) is not TRUE

Projecting these points on my network show that these are located within the limits of the network. So I assume some network attributes may cause the error. Help files however lack detailed description of sf network object. Any insight please?

Thank you.

Kind regards,

Yaïr Levy Senior GIS consultant

mpadge commented 5 years ago

Can you please give a reproducible example, ideally via reprex::reprex()? I'd guess without knowing details that that should be fixed if you install latest dodgr via remotes::install_github("atfutures/dodgr").

Robinlovelace commented 5 years ago

Closing for now as not reproducible. I'm planning a release that includes this new functionality. Please do provide a reproducible example @yairlevy and I can re-open this.

yairlevy commented 5 years ago

Sorry for reacting so late. Other tasks kept the best of my time.

Having updated dodgr doesn't solve the issue.

However, the code chunk resulting in error is the following:

Net produced between the coordinates of the bounding box of a first simple feature

pts <- matrix(st_bbox(sf1), ncol = 2, byrow = TRUE) colnames(pts) = c("x", "y") net <- dodgr_streetnet(pts = pts, expand = 0.2)

First route

from <- matrix(sf2$geometry[[1]], ncol = 2) #from = geometries of a simple feature object to <- buildsmatrix[1 : 223, ] #to = a subset of a coordinates matrix which results in an error after the #222nd row when used within routes, see following line.

routes <- route_dodgr(from = to, to = matrix(from, ncol = 2), net = net)

Would you require some data to test it, please let me know. A zipped version is 8,54 MB.

Thank you.

Kind regards,

Yaïr Levy Senior GIS consultant

mpadge commented 5 years ago

Could you please do the following two things:

  1. Paste all code necessary to reproduce your test case using reprex::reprex(), also including the following lines:
    packageVersion ("stplanr")
    packageVersion ("dodgr")
  2. Drag-and-drop the necessary data into this github issue - the file will be automatically uploaded here so we can then reproduce your code. Make sure your code directly uses the uploaded data (that is, starts with an unzip call and goes from there.)

Thanks! :smile:

yairlevy commented 5 years ago

Hereby:

#This script aims at calculating the distance between buildings and other features.
#Works but slowly until the 222nd destination.
#Packages Calling
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
library(stplanr)
library(spDataLarge)
library(osmdata)
#> Data (c) OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright
library(dplyr) #tbl_df
#> 
#> 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(dodgr) #route_dodgr
library(rgdal)
#> Loading required package: sp
#> rgdal: version: 1.4-3, (SVN revision 828)
#>  Geospatial Data Abstraction Library extensions to R successfully loaded
#>  Loaded GDAL runtime: GDAL 2.2.3, released 2017/11/20
#>  Path to GDAL shared files: C:/Users/YaLe/Documents/R/win-library/3.5/rgdal/gdal
#>  GDAL binary built with GEOS: TRUE 
#>  Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
#>  Path to PROJ.4 shared files: C:/Users/YaLe/Documents/R/win-library/3.5/rgdal/proj
#>  Linking to sp version: 1.3-1
library(raster)
#> 
#> Attaching package: 'raster'
#> The following object is masked from 'package:dplyr':
#> 
#>     select
library(reprex)
#Parameters
graphics.off()

#Directories
#3D GRB
setwd("C:/Users/YaLe/Documents/Projects/3Dmodels/Analysis/3d grbEN/3D_GRB_12025B250_Shapefile/Shapefile/BuildAdr/")
#Input
#Shapefiles
dir <- dir()
shpfiles <- dir[grep(".shp", dir)]
shpfiles <- shpfiles[-grep(".xml", shpfiles)]

shpfiles
#> character(0)
i = 1
shp1 <- shapefile(shpfiles[i], stringsAsFactors = FALSE)
#> Error in .local(x, ...): file.exists(extension(x, ".shp")) is not TRUE

#Objects for distances calculation directory
setwd("C:/Users/YaLe/Documents/Projects/3Dmodels/Analysis/Afstanden")

dir <- dir()
shpfiles <- dir[grep(".shp", dir)]
shpfiles <- shpfiles[-grep(".xml", shpfiles)]

shpfiles
#> [1] "SecOnderwijs.shp"  "SecOnderwijs2.shp"
i = 2
shp2 <- shapefile(shpfiles[i], stringsAsFactors = FALSE)
#Treatment

#Convert shp to sf
sf1 <- st_as_sf(shp1, Class = "sf")
#> Error in st_as_sf(shp1, Class = "sf"): object 'shp1' not found
sf2 <- st_as_sf(shp2, Class = "sf")

#Reprojection
sf1 <- st_transform(sf1, 4326)
#> Error in st_transform(sf1, 4326): object 'sf1' not found

#Get background mechelen data from Open Street Map for mapping improvement
mechelen <- osmdata::getbb("Mechelen", format_out = "sf_polygon")

#sf2 subset
sf2 <- st_intersection(sf2, mechelen)
#> although coordinates are longitude/latitude, st_intersection assumes that they are planar
#> Warning: attribute variables are assumed to be spatially constant
#> throughout all geometries

#Buildings matrix
sf101 <- sf1[0][[1]]
#> Error in eval(expr, envir, enclos): object 'sf1' not found
buildsmatrix <- matrix(st_centroid(sf101[[1]])[1 : 2], ncol = 2)
#> Error in st_centroid(sf101[[1]]): object 'sf101' not found
for (i in 2 : length(sf1[[1]])) #Allows to make routes from many points to a single one. Assuming routes direction don't matter.
  buildsmatrix <- rbind(buildsmatrix, st_centroid(sf101[[i]])[1 : 2])
#> Error in eval(expr, envir, enclos): object 'sf1' not found

#Roads network
pts <- matrix(st_bbox(sf1), ncol = 2, byrow = TRUE)
#> Error in st_bbox(sf1): object 'sf1' not found
colnames(pts) = c("x", "y")
#> Error in colnames(pts) = c("x", "y"): object 'pts' not found
net <- dodgr_streetnet(pts = pts, expand = 0.2)
#> Error in process_bbox(bbox, pts, expand): object 'pts' not found

#First route
from <- matrix(sf2$geometry[[1]], ncol = 2) 
to <- buildsmatrix[1 : 223, ] #Error at index 223,365
#> Error in eval(expr, envir, enclos): object 'buildsmatrix' not found
routes <- route_dodgr(from = to, to = matrix(from, ncol = 2), net = net)
#> Error in route_dodgr(from = to, to = matrix(from, ncol = 2), net = net): object 'to' not found

packageVersion("stplanr")
#> [1] '0.2.8'
packageVersion("dodgr")
#> [1] '0.1.4'

Created on 2019-05-17 by the reprex package (v0.2.1)

Thank you,

Kind regards,

Yaïr Levy Senior GIS consultant

mpadge commented 5 years ago

Great, thanks, can you now please just drag-and-drop the actual data into a comment panel? You said it was 8MB, which will be fine

yairlevy commented 5 years ago

This is the first input data creating the first simple feature object (sf1) building_footprints.zip

yairlevy commented 5 years ago

That is the second (sf2) SecOnderwijs2.zip

Thanks.

Robinlovelace commented 5 years ago

Thanks for sharing the data. Will be a great test case.