-
Notifications
You must be signed in to change notification settings - Fork 908
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
ENH: include crs in to_json (#1774) #2151
Conversation
Thanks! I am only worried about one thing. I would be more comfortable if we included it only in |
@martinfleis Updated according to your idea. |
@martinfleis docstring amended, thanks for the suggestion |
I just realised that this fails when a CRS cannot be represented by an (authority, code) tuple. Consider this example, using Gall-Peters Orthographic Projection defined via proj string. The CRS is perfectly valid, just pyproj cannot determine authority and code. import geopandas
world = geopandas.read_file(geopandas.datasets.get_path('nybb'))
world = world.set_geometry(world.centroid)
world = world.set_crs('+proj=cea +lon_0=0 +lat_ts=45 +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs', allow_override=True)
world.to_json() ---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/var/folders/2f/fhks6w_d0k556plcv3rfmshw0000gn/T/ipykernel_24983/3825038059.py in <module>
----> 1 world.to_json()
~/Git/geopandas/geopandas/geodataframe.py in to_json(self, na, show_bbox, drop_id, **kwargs)
749
750 if self.crs is not None:
--> 751 authority, code = self.crs.to_authority()
752 ogc_crs = f"urn:ogc:def:crs:{authority}::{code}"
753 geo["crs"] = {"type": "name", "properties": {"name": ogc_crs}}
TypeError: cannot unpack non-iterable NoneType object We should catch this, raise a warning that we were unable to create a compliant CRS string and return a JSON without CRS. |
Thanks for the change! I have pushed two commits in, as I initially just wanted to lint the code but then realised that we should remove the print of the CRS from the error message as it gets quite verbose, so I went ahead. We don't print the whole thing anywhere and I think we can easily leave it out here. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @simberaj, I'm pretty happy with this.
I just have one minor question for @martinfleis to weigh in on.
- Should inclusion of CRS in
to_json
be opt in? - e.g. as keyword argumentinclude_crs
, since it is not part of the current geojson spec? I don't use this method, but if you were say usingto_json
to write geojson to a file for a system not supporting crs, this would be breaking, and there's no good way to opt out (loads
the string, dropcrs
as a key anddumps
again) unless I'm missing something.
That a good point I didn't think about. I don't think it is a big issue though. If you use @simberaj Could you add the keyword? |
I don't know how important this is, but one potential additional concern: according to https://www.ogc.org/ogcUrnPolicy, there is a specific set of authorities that are currently recognized by the OGC spec (EDCS, EPSG, OGC, SI, UCUM, so eg not IGNF or ESRI). |
Quickly testing:
this then doesn't seem to include the CRS information in the resulting file (while it does for |
Co-authored-by: Matt Richards <45483497+m-richards@users.noreply.github.com>
I have updated this PR to closely match the 2016 GeoJSON spec. That means that when we save a gdf with a CRS equal to WGS84 or use When we use different CRS, a We also check for the set of allowed authorities and include CRS only if the authority is listed in the OGC URN Policy. This deviates from GDAL which saves CRS using the authority::code even for authorities like IGNF or ESRI. But it follows OGC. The question here is whether we want to do this - the resulting GeoJSON is not valid per the specification anyway (different CRS used and no CRS stored), so we may want to follow GDAL in saving the CRS anytime we are able to obtain authority:code tuple from pyproj. Removing the check for |
Thanks @simberaj for the original PR, and everyone for getting this over the finish line. |
Why does this change only apply to GeoDataFrames and not GeoSeries? |
@jparta because no one reailsed that we need to port that over to GeoSeries.to_json as well. Would you be willing to open a PR implementing the same on the GeoSeries? |
Co-authored-by: Jan Šimbera <jan.simbera@nanoenergies.cz> Co-authored-by: Martin Fleischmann <martin@martinfleischmann.net> Co-authored-by: Matt Richards <45483497+m-richards@users.noreply.github.com> Co-authored-by: Joris Van den Bossche <jorisvandenbossche@gmail.com>
Add an OGC CRS definition to the GeoJSON dictionary interface in
_to_geo()
, which propagates to__geo_interface__
andto_dict()
.Close #1774.