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

MULTICURVE to MULTILINESTRING? #1194

Closed
dblodgett-usgs opened this issue Nov 18, 2019 · 3 comments
Closed

MULTICURVE to MULTILINESTRING? #1194

dblodgett-usgs opened this issue Nov 18, 2019 · 3 comments

Comments

@dblodgett-usgs
Copy link
Contributor

I ran into some data from here. (400mb zipped gdb) that has one geometry that comes back as a "MULTICURVE/COMPOUNDCURVE".

This was causing st_simplify to fail with the error: ParseException: Unknown WKB type 11 and I can't plot the geometry.

I'm wondering if it would be a terrible idea to attempt piece-wise coercion to LINESTRING to create a MULTILINESTRING out of a MULTICURVE?

Happy to hear, no that's a terrible idea because of side affects or whatever. We can just close this for a future me to find when googling error messages. But there's a potential fix for the issue shown below and I'll submit for consideration -- I'm not familiar enough with the vagaries of sf geometries to feel comfortable submitting a PR for this though.

Here's a reprex showing what I'm talking about and my hack to "fix" it.

library(sf)
#> Linking to GEOS 3.7.2, GDAL 2.4.1, PROJ 5.2.0
# WKT truncated for reprex
wkt <- "MULTICURVE (COMPOUNDCURVE (LINESTRING (-83.62333 35.55244, -83.62328 35.55232, -83.62323 35.55223, -83.62319 35.55216, -83.62312 35.55209, -83.6231 35.55207), CIRCULARSTRING (-83.6231 35.55207, -83.62307 35.55205, -83.62302 35.55204), LINESTRING (-83.62302 35.55204, -83.62299 35.55203, -83.62289 35.55198, -83.62281 35.55189, -83.62271 35.55182)))"

g <- st_as_sfc(wkt)
g <- st_sf(demo = "test", geom = g, crs = 4326)

try(plot(st_geometry(g)), silent = FALSE)
#> Error in xy.coords(x, y, xlabel, ylabel, log) : 
#>   'x' is a list, but does not have components 'x' and 'y'
try(st_simplify(st_transform(g, 5070), dTolerance = 10), silent = FALSE)
#> Error in CPL_geos_op("simplify", x, numeric(0), integer(0), preserveTopology = rep(preserveTopology,  : 
#>   Evaluation error: ParseException: Unknown WKB type 11.

fix <- which(sapply(st_geometry(g), function(x) class(x)[2]) != "MULTILINESTRING")
for(f in fix) {
  st_geometry(g)[[f]] <- st_multilinestring(lapply(st_geometry(g)[[f]][[1]], st_cast, to = "LINESTRING"), dim = "XY")
}

if(length(fix) > 0) st_geometry(g) <- st_sfc(st_geometry(g)[[1]], crs = st_crs(g))

plot(st_geometry(g))

st_geometry(g)
#> Geometry set for 1 feature 
#> geometry type:  MULTILINESTRING
#> dimension:      XY
#> bbox:           xmin: -83.62333 ymin: 35.55182 xmax: -83.62271 ymax: 35.55244
#> epsg (SRID):    4326
#> proj4string:    +proj=longlat +datum=WGS84 +no_defs
#> MULTILINESTRING ((-83.62333 35.55244, -83.62328...
st_as_text(st_geometry(g))
#> [1] "MULTILINESTRING ((-83.62333 35.55244, -83.62328 35.55232, -83.62323 35.55223, -83.62319 35.55216, -83.62312 35.55209, -83.6231 35.55207), (-83.6231 35.55207, -83.62309 35.55206, -83.62309 35.55206, -83.62308 35.55206, -83.62307 35.55205, -83.62307 35.55205, -83.62306 35.55205, -83.62305 35.55204, -83.62304 35.55204, -83.62304 35.55204, -83.62303 35.55204, -83.62302 35.55204), (-83.62302 35.55204, -83.62299 35.55203, -83.62289 35.55198, -83.62281 35.55189, -83.62271 35.55182))"

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

@rsbivand
Copy link
Member

I believe that you can use the utility ogr2ogr through gdal_utils() in sf to turn curves into line strings manually before reading, not ideal, but worked - cannot now find the relevant issue (I might have raised it by email too).

@edzer edzer closed this as completed in 587b90d Nov 18, 2019
@edzer
Copy link
Member

edzer commented Nov 18, 2019

st_cast(g, "MULTILINESTRING")

should do it (now).

@dblodgett-usgs
Copy link
Contributor Author

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

3 participants