Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

line2points doesn't work if the geometry column isn't called geometry #358

Open
agila5 opened this issue Nov 5, 2019 · 1 comment
Open

Comments

@agila5
Copy link
Collaborator

agila5 commented Nov 5, 2019

Reprex of the problem

# packages
library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
library(stplanr)

# create some fake data
my_sfc_linestring <- st_sfc(
  st_linestring(matrix(c(1, 1, 2, 2, 3, 3), ncol = 2, byrow = TRUE))
)
my_sf_linestring <- st_as_sf(data.frame(ID = 1), geometry = my_sfc_linestring)
line2points(my_sf_linestring)
#> Simple feature collection with 2 features and 1 field
#> geometry type:  POINT
#> dimension:      XY
#> bbox:           xmin: 1 ymin: 1 xmax: 3 ymax: 3
#> epsg (SRID):    NA
#> proj4string:    NA
#>   id    geometry
#> 1  1 POINT (1 1)
#> 2  1 POINT (3 3)
colnames(my_sf_linestring)
#> [1] "ID"       "geometry"

# save data
st_write(my_sf_linestring, "my_sf_linestring.gpkg")
#> Updating layer `my_sf_linestring' to data source `my_sf_linestring.gpkg' using driver `GPKG'
#> Writing 1 features with 1 fields and geometry type Line String.

# read that same data
my_sf_linestring <- st_read("my_sf_linestring.gpkg")
#> Reading layer `my_sf_linestring' from data source `C:\Users\Utente\AppData\Local\Temp\RtmpEJviv4\reprex20fc24412f88\my_sf_linestring.gpkg' using driver `GPKG'
#> Simple feature collection with 1 feature and 1 field
#> geometry type:  LINESTRING
#> dimension:      XY
#> bbox:           xmin: 1 ymin: 1 xmax: 3 ymax: 3
#> epsg (SRID):    NA
#> proj4string:    NA
line2points(my_sf_linestring)
#> Error in x_coords[o_indices] <- sapply(l$geometry, `[[`, 1): replacement has length zero
colnames(my_sf_linestring)
#> [1] "ID"   "geom"

Created on 2019-11-05 by the reprex package (v0.3.0)

The problem lies here I think

stplanr/R/od-funs.R

Lines 388 to 391 in b2fd352

x_coords[o_indices] <- sapply(l$geometry, `[[`, 1) # first (x) element of each line
x_coords[d_indices] <- sapply(l$geometry, function(x) x[length(x) / 2]) # last (x) element of each line
y_coords[o_indices] <- sapply(l$geometry, function(x) x[length(x) / 2 + 1]) # first (y) element of each line
y_coords[d_indices] <- sapply(l$geometry, tail, n = 1) # last (y) element of each line

I can fill a PR in the next days.

@Robinlovelace
Copy link
Member

Thanks Andrea, as discussed, I'm confident this should be a quick fix. Many thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants