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

Viable approach to ST_LineSubString? #16

Closed
dblodgett-usgs opened this issue Mar 7, 2018 · 4 comments
Closed

Viable approach to ST_LineSubString? #16

dblodgett-usgs opened this issue Mar 7, 2018 · 4 comments

Comments

@dblodgett-usgs
Copy link

I worked up a decent approach (in that it seems quite fast working with >70k river features) to the same geometric function as ST_LineSubString over here: https://gist.github.com/dblodgett-usgs/cf87392c02d73f1b7d16153d2b66a8f3 It is written to orchestrate over an sf LINESTRING dataframe though. Some work would be required to boil it down to the basic function.

@edzer you told me to raise this here, but since it doesn't use lwgeom, (it's dplyr / sf) is it really appropriate?

This function is pretty GIS - swiss army knife, but it would be good to have a ST_LineSubString function some place... fine to move on if this doesn't fit, but wanted to offer it up if it seems applicable.

@edzer
Copy link
Member

edzer commented Mar 7, 2018

I pointed you here because the PostGIS function with that name (most likely) uses this. It should be fairly trivial to incorporate that into lwgeom, which interfaces this library.

edzer added a commit that referenced this issue Mar 7, 2018
@dblodgett-usgs
Copy link
Author

Cool! I'll test this out soon.

@dblodgett-usgs
Copy link
Author

dblodgett-usgs commented Mar 7, 2018

Dang. Nice. That was quick!!

Somebody else can create an apply-like function where I wrote a dumb loop, but this works pretty good as far as I can tell.

devtools::install_github("r-spatial/lwgeom")

geojson <- '{
"type": "FeatureCollection",
"name": "sample",
"features": [
{ "type": "Feature", "properties": { "ID": 1 }, "geometry": { "type": "LineString", "coordinates": [ [ -122.916097119649635, 38.229575873993497 ], [ -122.916154986316201, 38.229346873993904 ], [ -122.916676986315338, 38.228614207328235 ], [ -122.917430786314128, 38.227148873997294 ], [ -122.917488319647418, 38.226210273998674 ], [ -122.917371986314322, 38.22579827399926 ], [ -122.917400319647584, 38.224516407334704 ], [ -122.918995719645125, 38.223348274003115 ], [ -122.920127119643382, 38.223004607336975 ], [ -122.921171719641791, 38.222546407337802 ], [ -122.922186919640126, 38.221950807338601 ], [ -122.922795786305926, 38.221286674006421 ] ] } }
]
}'

sf <- sf::st_transform(sf::st_read(geojson), 5070)

split <- lwgeom::st_linesubstring(sf$geometry[[1]], 0, (1/3))

plot(sf$geometry[[1]], col="red")
plot(split, col = "blue", add = TRUE)

out_spec <- data.frame(ID = c(1,1,1),  newID = c(1, 2, 3), start = c(0, (1/3), (2/3)), end = c((1/3), (2/3), (1)))

out_geo <- list()

for (i in 1:nrow(out_spec)) {
  out_geo[[out_spec$newID[i]]] <- lwgeom::st_linesubstring(sf$geometry[out_spec$ID[i]], out_spec$start[i], out_spec$end[i])[[1]]
}

out <- sf::st_sf(out_spec, sf::st_sfc(out_geo))

plot(dplyr::select(out, newID))

screen shot 2018-03-07 at 2 21 58 pm

screen shot 2018-03-07 at 2 42 30 pm

@dblodgett-usgs
Copy link
Author

For the record, then I'll leave this, I did implement a wrapper function that works against a whole table of LINESTRING features using the lwgeom function. It is at the bottom of the gist linked above.

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