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

topojson_json with sf classes may not be working #139

Closed
sckott opened this issue Jun 25, 2018 · 2 comments
Closed

topojson_json with sf classes may not be working #139

sckott opened this issue Jun 25, 2018 · 2 comments
Labels
bug an unexpected problem or unintended behavior
Milestone

Comments

@sckott
Copy link
Collaborator

sckott commented Jun 25, 2018

Just noticed this cause I was going to reply to this old SO thread https://gis.stackexchange.com/questions/250760/convert-sf-or-geojson-to-topojson-in-r-geojsonio

Seems like topojson_json() is behaving badly for sf inputs, e.g.:

p1 <- rbind(c(0,0), c(1,0), c(3,2), c(2,4), c(1,4), c(0,0))
poly <- rbind(c(1,1), c(1,2), c(2,2), c(1,1))
poly_sfg <-st_polygon(list(p1))
topojson_json(poly_sfg)
#> {"type":"Topology","objects":{"foo":{"type":"GeometryCollection","geometries":[{"type":null}]}},"arcs":[]}

it's empty, whereas it should not be.

if we do this it works

topojson_json(poly_sfg, type = "auto")
#> {"type":"Topology","objects":{"foo":{"type":"Polygon","arcs":[[0]]}},"arcs":[[[0,0],[1,0],[3,2],[2,4],[1,4],[0,0]]],"bbox":[0,0,3,4]}

but not sure if that's the desired output? Doing type="auto" makes the other examples work as well

@ateucher ?

@sckott sckott added the bug an unexpected problem or unintended behavior label Jun 25, 2018
@sckott sckott added this to the v0.7 milestone Jun 25, 2018
@sckott
Copy link
Collaborator Author

sckott commented Jan 29, 2019

@ateucher does this approach seem reasonable? on branch https://github.com/ropensci/geojsonio/compare/topojson_json-auto-sf

Changed topojson_json to change type to auto if a sf class detected. and otherwise leave type as is.

library(geojsonio)
library(sf)
p1 <- rbind(c(0,0), c(1,0), c(3,2), c(2,4), c(1,4), c(0,0))
poly <- rbind(c(1,1), c(1,2), c(2,2), c(1,1))
poly_sfg <- st_polygon(list(p1))
topojson_json(poly_sfg)
#> sf/sfc/sfg class detected; using type="auto"
#> {"type":"Topology","objects":{"foo":{"type":"Polygon","arcs":[[0]]}},"arcs":[[[0,0],[1,0],[3,2],[2,4],[1,4],[0,0]]],"bbox":[0,0,3,4]}
topojson_list(poly_sfg)
#> sf/sfc/sfg class detected; using type="auto"
#> $type
#> [1] "Topology"
#> 
#> $objects
#> $objects$foo
#> $objects$foo$type
#> [1] "Polygon"
#> 
#> $objects$foo$arcs
#> $objects$foo$arcs[[1]]
#> [1] 0
#> 
#> 
#> 
#> 
#> $arcs
#> $arcs[[1]]
#> $arcs[[1]][[1]]
#> [1] 0 0
#> 
#> $arcs[[1]][[2]]
#> [1] 1 0
#> 
#> $arcs[[1]][[3]]
#> [1] 3 2
#> 
#> $arcs[[1]][[4]]
#> [1] 2 4
#> 
#> $arcs[[1]][[5]]
#> [1] 1 4
#> 
#> $arcs[[1]][[6]]
#> [1] 0 0
#> 
#> 
#> 
#> $bbox
#> [1] 0 0 3 4

@github-actions
Copy link

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.

@github-actions github-actions bot locked and limited conversation to collaborators Sep 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

1 participant