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

Methods for sf objects #95

Merged
merged 46 commits into from
Jan 11, 2017
Merged

Methods for sf objects #95

merged 46 commits into from
Jan 11, 2017

Conversation

ateucher
Copy link
Member

Hey @sckott - I'm not sure you'll want to merge this yet, but thought I'd let you know how it was going. I think it's mostly working well, but obviously have to flesh out the tests. If you haven't seen it, the sf vignette gives a good overview of the sf and related classes.

get_epsg(x[[geom_col]])
}

get_epsg.sfc <- function(x) attr(x, "epsg")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has now changed; sfc objects have a crs attribute which is a list with a epsg integer and a proj4string proj4string. I guess that only in case epsg is 4326 you can directly convert to geojson, otherwise you need to st_transform into epsg 4326.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, good to know, thanks @edzer. And thanks very much for taking the time to look.

Yes, I've been struggling with what to do with non-EPSG:4326 CRS's. As written, the sf to geojson methods don't depend on the sf package, adding st_transform would require it. @sckott what do you think, as I know you were hoping to keep the dependencies down? We could add it in Suggests (which I think we would need to anyways for tests), then conditionally transform to 4326 (with a message) if sf is available, otherwise throw an error telling the user they need to transform it? Or bite the bullet and add it in Imports, as I think (hope!) sf will replace sp as the standard.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I lean towards Suggests, but if needs to go in Imports, so be it

@ateucher
Copy link
Member Author

Ok I'll go with suggests. I wonder if in the same vein we can eventually move sp and rgdal to Suggests if can parse sp to json without them

@sckott
Copy link
Collaborator

sckott commented Nov 16, 2016

I wonder if in the same vein we can eventually move sp and rgdal to Suggests if can parse sp to json without them

that's my hope! at least rgdal via #41 - with geojson's ability to parse sp classes to json we don't have support for all sp classes

@edzer
Copy link

edzer commented Nov 16, 2016

Nothing wrong with Suggests: as long as you use constructs like this. Also pls use sf::st_crs rather than accessing attributes directly; works for more cases (sf and sfc), and makes your code live longer.

@ateucher
Copy link
Member Author

Yep, that is how I was planning on doing it. I think I need to access the crs attribute directly though, because we want to first check the CRS only to see if we need st_transform, otherwise we can go directly to json without depending on sf. I.e.:

epsg <- attr(x, "crs")[["epsg"]]
if (epsg != 4326)) {
  if (!requireNamespace("sf", quietly = TRUE)) {
    stop("Your input is not in a CRS that geojson supports and you don't have the 'sf' package installed. Please install and try again")
  } else {
    x <- sf::st_transform(x, 4326)
  }
}
## turn x into json

Unless I'm missing something else...?

@edzer
Copy link

edzer commented Nov 16, 2016

No that should work - I didn't anticipate you want to write the geojson without sf::st_write, good luck!

@ateucher
Copy link
Member Author

lol, thanks! Unless I'm really deceiving myself, because sf and geojson both use simple features, it's relatively simple to parse an sf[cg] object into a list that mirrors the geojson structure, then use JSONlite to turn it into a geojson character string. I haven't dealt with Z or M values yet, and I'm sure there are other cases that I'm missing, but inital tests are promising.

@edzer
Copy link

edzer commented Nov 16, 2016

Lucky you - the geojson standard excludes Z and M, and constraints to the first 7 types.

@ateucher
Copy link
Member Author

ateucher commented Nov 18, 2016

A couple of notes on the draft new GEOSJON standard:

  1. With regards to CRS, there is this:

    However, where all involved parties have a prior arrangement, alternative coordinate reference systems can be used without risk of data being misinterpreted.

    So there may be cases where converting to WGS84 is not desirable by the user. I think there are two options:

    1. Add an argument (which would have to be added to all methods) convert_wgs84with a default of TRUE, to allow user to override. OR
    2. Proceed with the conversion regardless of CRS and throw a warning if it's not WGS84.
  2. With regards to Z (elevation):

    Altitude or elevation MAY be included as an optional third element.

    So we CAN keep Z in coordinates, but not M.

Current approach: detect non-wgs84 (epsg:4326) and transform. Don't include crs member
@ateucher
Copy link
Member Author

@sckott any thoughts on number 1 above?

@sckott
Copy link
Collaborator

sckott commented Nov 21, 2016

@ateucher Seems like since WGS84 is the standard for geojson we could just convert it to that, but letting the user toggle that will make more people happy perhaps?

@ateucher
Copy link
Member Author

Thanks @sckott that works for me. Selfishly, I want to be able to bypass transformation to use in rmapshaper

@sckott
Copy link
Collaborator

sckott commented Nov 21, 2016

@ateucher wait, which one works?

@ateucher
Copy link
Member Author

ateucher commented Jan 8, 2017

@sckott I think this is ready for merge (or further review/testing by other eyes). It's passing on my fork, failing here with the map_gist tests.

I omitted dealing with non-epsg:4326 CRS until we decide to do it package-wide - didn't seem right to implement it only in these methods...

@ateucher
Copy link
Member Author

ateucher commented Jan 8, 2017

Just playing with Travis...

@sckott
Copy link
Collaborator

sckott commented Jan 8, 2017

i think its failing cause GITHUB_PAT is not avail on PR's, so if its just those tests failing all is good

@sckott
Copy link
Collaborator

sckott commented Jan 8, 2017

that makes sense to implement converting pkg wide later

@sckott
Copy link
Collaborator

sckott commented Jan 8, 2017

Thanks for the tests!

Can you add some examples though for the different sf classes for both files

@ateucher
Copy link
Member Author

ateucher commented Jan 8, 2017

Oh right, examples. Yep, will do.

Re Travis failing, I figured that it was GITHIB_PAT.

@ateucher
Copy link
Member Author

@sckott I think this is good to go now. I added examples (hopefully they're enough), and methods for geojson_write.

@sckott
Copy link
Collaborator

sckott commented Jan 11, 2017

Sweet, will look in morning

@sckott
Copy link
Collaborator

sckott commented Jan 11, 2017

LGTM

nice work sir @ateucher

@sckott sckott merged commit e35e654 into ropensci:master Jan 11, 2017
@ateucher
Copy link
Member Author

Woot! Thanks :)

@github-actions
Copy link

This pull request 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
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants