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

include geojson serialization and geo_inferface #71

Merged
merged 1 commit into from
Apr 21, 2020

Conversation

mattijn
Copy link
Owner

@mattijn mattijn commented Apr 21, 2020

This PR includes support for the __geo_interface__ on the topology object. Since the __geo_interface__ is build upon the geojson standard this also means there is now a .to_geojson() function.

The pretty and validate parameter will probably change later to False and False instead of the current True, True.


With the __geo_interface__ in place, visualization can be done directly using the Topology object in Altair:

Prepare a Topology object:

import geopandas as gpd
import topojson

df = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
df = df[df.continent == 'Africa']

tp = topojson.Topology(df)
tp.toposimplify(1, inplace=True)

And then:

import altair as alt

# from this PR forward: with the __geo_interface__ serialization in place
# the serialization from TopoJSON to GeoJSON can also be done on the Python side
# btw: this happens within Altair
alt.Chart(tp).mark_geoshape().encode(color='name:N')

output_1_1

Compare this to the current approach (still and will be completely OK as well):

# conventional the serialization from TopoJSON to GeoJSON happens on the Vega side:
tp.to_alt(color='properties.name:N')

output_2_1


This PR also means that there will be two approaches to create a GeoDataFrame:

# conventional through fiona using the `TopoJSON` driver (under the hood)
tp.to_gdf().plot()

output_3_1

And now:

import json

# from this PR forward: using the .to_geojson() function
gpd.GeoDataFrame().from_features(
    json.loads(tp.to_geojson(validate=False))['features']
).plot()

output_3_1

Maybe the to_gdf() function should get a driver parameter, that can be set to TopoJSON or GeoJSON?

I will not close the relevant issues, since tests are not written yet.

@mattijn mattijn merged commit 3400c29 into master Apr 21, 2020
@martinfleis
Copy link
Contributor

Maybe the to_gdf() function should get a driver parameter, that can be set to TopoJSON or GeoJSON?

From my perspective, so a benchmark and implement in to_gdf() whatever is more performant. I would not complicate it with driver as the resulting GeoDataFrame should be exactly the same both ways. Keep in mind that you have to preserve CRS in the second example.

@mattijn mattijn deleted the add-__geo_interface__-support branch April 28, 2020 13:31
This was referenced Apr 28, 2020
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

Successfully merging this pull request may close these issues.

None yet

2 participants