Last active
March 24, 2020 00:32
-
-
Save digitaltopo/5f866a23799431abc3ee313e370db2d0 to your computer and use it in GitHub Desktop.
Geoviews DataLink + PointDraw Stream - https://mybinder.org/v2/gist/digitaltopo/5f866a23799431abc3ee313e370db2d0/master
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: geoviews | |
dependencies: | |
- python=3.7 | |
- pandas | |
- geopandas | |
- holoviews | |
- geoviews |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
}, | |
"outputs": [], | |
"source": [ | |
"import pandas as pd\n", | |
"import geopandas as gpd\n", | |
"import geoviews as gv\n", | |
"import geoviews.tile_sources as gvts\n", | |
"import holoviews as hv\n", | |
"from holoviews import streams\n", | |
"from holoviews.plotting.links import DataLink\n", | |
"\n", | |
"print('Holoviews Version:', hv.__version__)\n", | |
"print('Geoviews Version:', gv.__version__)\n", | |
"\n", | |
"gv.extension('bokeh')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 1. Initial Run using empty points elements" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
}, | |
"outputs": [], | |
"source": [ | |
"# Elements, Stream and Datalink\n", | |
"points = gv.Points([], vdims=['notes'])\n", | |
"table = points.to(hv.Table)\n", | |
"points_stream = streams.PointDraw(data=points.columns(), source=points)\n", | |
"DataLink(points, table)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
}, | |
"outputs": [], | |
"source": [ | |
"# Plot and create some points using point draw tool\n", | |
"(gvts.OSM().options(global_extent=True) * points) + table" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
}, | |
"outputs": [], | |
"source": [ | |
"# Inspect Points Stream data\n", | |
"points_stream.data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
}, | |
"outputs": [], | |
"source": [ | |
"# Save Points Stream as a GeoJSON file\n", | |
"\n", | |
"# Create a Pandas Dataframe from point stream data\n", | |
"created_points_df = pd.DataFrame(points_stream.data)\n", | |
"\n", | |
"# Convert point dataframe to geo data frame\n", | |
"created_points_gdf = gpd.GeoDataFrame(created_points_df, geometry=gpd.points_from_xy(created_points_df.x, created_points_df.y))\n", | |
"\n", | |
"# Write a geojson file\n", | |
"created_points_gdf.to_file(filename=\"saved_points.geojson\", driver='GeoJSON')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"## 2. Second Run using saved geojson file" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
}, | |
"outputs": [], | |
"source": [ | |
"saved_points = gv.Points(gpd.read_file('saved_points.geojson'), vdims=['notes'])\n", | |
"saved_points_table = saved_points.to(hv.Table)\n", | |
"saved_points_stream = streams.PointDraw(data=saved_points.columns(), source=saved_points)\n", | |
"DataLink(saved_points, saved_points_table)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
}, | |
"outputs": [], | |
"source": [ | |
"# Inspect saved points\n", | |
"print(saved_points)\n", | |
"gvts.OSM().options(global_extent=True) * saved_points" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
}, | |
"outputs": [], | |
"source": [ | |
"# Inspect Saved points table\n", | |
"print(saved_points_table)\n", | |
"saved_points_table" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### When we try to plot table and points together again\n", | |
"\n", | |
"We get the following error:\n", | |
"```\n", | |
"ValueError: DataLink can only be applied if overlapping dimension values are equal, Longitude column on source does not match target\n", | |
"```" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
}, | |
"outputs": [], | |
"source": [ | |
"# Plot saved points\n", | |
"(gvts.OSM().options(global_extent=True) * points) + table" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.6" | |
}, | |
"varInspector": { | |
"cols": { | |
"lenName": 16, | |
"lenType": 16, | |
"lenVar": 40 | |
}, | |
"kernels_config": { | |
"python": { | |
"delete_cmd_postfix": "", | |
"delete_cmd_prefix": "del ", | |
"library": "var_list.py", | |
"varRefreshCmd": "print(var_dic_list())" | |
}, | |
"r": { | |
"delete_cmd_postfix": ") ", | |
"delete_cmd_prefix": "rm(", | |
"library": "var_list.r", | |
"varRefreshCmd": "cat(var_dic_list()) " | |
} | |
}, | |
"types_to_exclude": [ | |
"module", | |
"function", | |
"builtin_function_or_method", | |
"instance", | |
"_Feature" | |
], | |
"window_display": false | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment