Skip to content

Commit

Permalink
Add tutorial for geosphere and supporting kml (#98)
Browse files Browse the repository at this point in the history
* Add tutorial for geosphere and supporting kml

* Move tutorial to docs
  • Loading branch information
micah-prime authored May 29, 2024
1 parent 55a6d95 commit 7dfc2ec
Show file tree
Hide file tree
Showing 2 changed files with 216 additions and 0 deletions.
48 changes: 48 additions & 0 deletions docs/gallery/data/geosphere_test.kml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
<name>geosphere_test.kml</name>
<StyleMap id="m_ylw-pushpin">
<Pair>
<key>normal</key>
<styleUrl>#s_ylw-pushpin</styleUrl>
</Pair>
<Pair>
<key>highlight</key>
<styleUrl>#s_ylw-pushpin_hl</styleUrl>
</Pair>
</StyleMap>
<Style id="s_ylw-pushpin">
<IconStyle>
<scale>1.1</scale>
<Icon>
<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
</Icon>
<hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/>
</IconStyle>
</Style>
<Style id="s_ylw-pushpin_hl">
<IconStyle>
<scale>1.3</scale>
<Icon>
<href>http://maps.google.com/mapfiles/kml/pushpin/ylw-pushpin.png</href>
</Icon>
<hotSpot x="20" y="2" xunits="pixels" yunits="pixels"/>
</IconStyle>
</Style>
<Placemark>
<name>geosphere_test</name>
<styleUrl>#m_ylw-pushpin</styleUrl>
<Polygon>
<tessellate>1</tessellate>
<outerBoundaryIs>
<LinearRing>
<coordinates>
13.30620324349142,47.53902871663209,0 13.01755054381177,46.92732016483155,0 14.11618099527536,46.75743049141452,0 14.36846777946924,47.37742007845974,0 13.30620324349142,47.53902871663209,0
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</Document>
</kml>
168 changes: 168 additions & 0 deletions docs/gallery/data/geosphere_tutorial.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "39a8c83c-4d90-498a-b1e0-078df578db42",
"metadata": {},
"source": [
"# Geosphere Austria tutorial\n",
"This tutorial walks through the use of a point data class for accessing station data in Austria\n",
"A video tutorial of this notebook can be found [here](https://www.loom.com/share/fcced7c00c47400e9a2b8b7b017949e5?sid=6a4cc255-48d0-48f0-8e14-a8aed0ef4bb3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "da7e02c8-6932-4310-8108-7ad7f6bfbade",
"metadata": {},
"outputs": [],
"source": [
"# import metloom class https://data.hub.geosphere.at/dataset/\n",
"from metloom.pointdata import GeoSphereHistPointData"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ee565b4d-e25d-4f43-8d85-f3b9fef5b31b",
"metadata": {},
"outputs": [],
"source": [
"# import packages\n",
"import geopandas as gpd\n",
"import pandas as pd\n",
"import fiona\n",
"import matplotlib.pyplot as plt\n",
"fiona.drvsupport.supported_drivers['kml'] = 'rw'\n",
"fiona.drvsupport.supported_drivers['KML'] = 'rw'"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "16111f97-d305-4358-9088-eccb5b18cf99",
"metadata": {},
"outputs": [],
"source": [
"# Install mapclassify and folium for gdf.explore()\n",
"!pip install mapclassify\n",
"!pip install folium"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "31656640-c479-4a2a-aa11-2ada3b5d6194",
"metadata": {},
"outputs": [],
"source": [
"# read and explore shape\n",
"gdf = gpd.read_file(\"./data/geosphere_test.kml\")\n",
"gdf.explore()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2fe8a969-ff71-4beb-8259-556c66309f25",
"metadata": {},
"outputs": [],
"source": [
"# Define variable of interest https://github.com/M3Works/metloom/blob/main/metloom/variables.py\n",
"variable = GeoSphereHistPointData.ALLOWED_VARIABLES.TEMP\n",
"# Get the points within our region of interest\n",
"points = GeoSphereHistPointData.points_from_geometry(gdf, [variable])\n",
"print(len(points))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "028e0a56-aa66-43a7-b554-16dcfe2c35bc",
"metadata": {},
"outputs": [],
"source": [
"# Explore the points we located\n",
"points_df = points.to_dataframe()\n",
"points_df = points_df.set_crs(\"EPSG:4326\")\n",
"\n",
"points_df.explore()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bb075ff5-50a5-4de2-8362-511c669e61b5",
"metadata": {},
"outputs": [],
"source": [
"# define start and end date of interest\n",
"start = pd.to_datetime(\"2023-01-01\")\n",
"end = pd.to_datetime(\"2023-01-11\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "18544661-2f36-4097-b26f-2d6508acdc52",
"metadata": {},
"outputs": [],
"source": [
"# dataframe for storing combined data\n",
"final_df = pd.DataFrame()\n",
"\n",
"# loop through first 15 points and store the data\n",
"for p in points.points[:15]:\n",
" result = p.get_daily_data(start, end, [variable])\n",
" if result is None:\n",
" print(f\"{p.name} did not return data\")\n",
" else:\n",
" # reset the index to just be datetime\n",
" result = result.reset_index().set_index(\"datetime\")\n",
" # store off the data in the final dataframe\n",
" final_df[p.name + p.id] = result[variable.name]\n",
"final_df"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "55be4633-430d-46bb-8c8d-9792bca0c365",
"metadata": {},
"outputs": [],
"source": [
"# Plot the timeseries\n",
"final_df.plot(ylabel=variable.name)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "73cde7e4-2e99-4a3d-a19a-5a2b909737b1",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.15"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit 7dfc2ec

Please sign in to comment.