Skip to content

Commit f28657b

Browse files
authored
explain the _repr_svg mechanism (#240)
1 parent e4f299d commit f28657b

3 files changed

Lines changed: 73 additions & 37 deletions

File tree

workshop/jupyter/content/notebooks/02-geometry.ipynb

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"source": [
77
"# Geometry\n",
88
"\n",
9-
"This lesson brings you a bit back to highschool math: (Euclidian) Geometry, \n",
9+
"This lesson brings you a bit back to highschool math: ([Euclidian](https://en.wikipedia.org/wiki/Euclidean_geometry)) Geometry, \n",
1010
"hopefully with more fun as within (geo)spatial IT/GIS we usually deal with\n",
1111
"real-world objects (roads, lakes, forests, etc) abstracted as geometries.\n",
1212
"\n",
@@ -204,7 +204,31 @@
204204
"source": [
205205
"from shapely.geometry import Polygon\n",
206206
"polygon = Polygon([(0, 0), (3, 0), (3, 4)])\n",
207-
"polygon"
207+
"polygon",
208+
""
209+
]
210+
},
211+
{
212+
"cell_type": "markdown",
213+
"metadata": {},
214+
"source": [
215+
"Jupyter (IPython) benefits from Shapely's `geometry._repr_svg_()` method to render any shapely geometry as image.\n",
216+
"Whenever you `log` the value of a shapely geometry, an image (svg) is rendered of the geometry."
217+
]
218+
},
219+
{
220+
"cell_type": "code",
221+
"execution_count": null,
222+
"metadata": {
223+
"pycharm": {
224+
"name": "#%%\n"
225+
}
226+
},
227+
"outputs": [],
228+
"source": [
229+
"# Explicit use of _repr_svg_() method\n",
230+
"svg = polygon._repr_svg_()\n",
231+
"print(svg)"
208232
]
209233
},
210234
{

workshop/jupyter/content/notebooks/09-publishing.ipynb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"source": [
77
"# Publishing\n",
88
"\n",
9-
"**NB This Section is optional and not included in the FOSS4G 2024 Workshop**\n",
9+
"**NB This Section is optional and not included in the FOSS4G 2026 Workshop**\n",
1010
"\n",
1111
"It may still be valuable to follow if time left or at home. Mind that the service containers can only be run in a local (Docker)\n",
1212
"environment, i.e. not through the Workshop Binder version in the cloud.\n",
@@ -58,8 +58,7 @@
5858
"\n",
5959
"Here you will see 10 feature collections listed on the resulting webpage. Feature collections are\n",
6060
"identified by the `\"itemType\": \"feature\"` in the collection definition in the JSON response.\n",
61-
"\n",
62-
"Now let's add the WOUDC station data to our pygeoapi instance.\n"
61+
"\n"
6362
]
6463
},
6564
{
@@ -116,7 +115,12 @@
116115
"# Get items (paged) in Lakes collection\n",
117116
"lakes = oa_feat.collection('lakes')\n",
118117
"lakes_query = oa_feat.collection_items('lakes')\n",
119-
"lakes_query['features'][0]"
118+
"# Plot first lake\n",
119+
"lake = lakes_query['features'][0]\n",
120+
"print(lake.get('properties').get('name'))\n",
121+
"from shapely.geometry import shape\n",
122+
"lakepolygon = shape(lake.get('geometry'))\n",
123+
"lakepolygon"
120124
]
121125
},
122126
{

workshop/jupyter/content/notebooks/10-remote-data.ipynb

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"`OWSLib` enables you to connect to these services as \n",
2525
"a client, mainly to fetch and query data from them. \n",
2626
"Currently WMS, WFS, WCS, CSW, WPS, SOS, WMC and the more recent OGC APIs like \n",
27-
"*OGC API - Features* (formerly called \"WFS version 3\") are supported.\n",
27+
"*OGC API - Features* are supported.\n",
2828
"The list of supported services is growing. \n",
2929
"\n",
3030
"Documentation: https://owslib.readthedocs.io\n"
@@ -150,7 +150,14 @@
150150
"# Get items (paged) in Lakes collection\n",
151151
"lakes = oa_feat.collection('lakes')\n",
152152
"lakes_query = oa_feat.collection_items('lakes')\n",
153-
"lakes_query['features'][0]"
153+
"lakes_query['features'][0]\n",
154+
"\n",
155+
"# Plot first lake\n",
156+
"lake = lakes_query['features'][0]\n",
157+
"print(lake.get('properties').get('name'))\n",
158+
"from shapely.geometry import shape\n",
159+
"lakepolygon = shape(lake.get('geometry'))\n",
160+
"lakepolygon"
154161
]
155162
},
156163
{
@@ -258,7 +265,7 @@
258265
"cell_type": "markdown",
259266
"metadata": {},
260267
"source": [
261-
"Download and save the data (image)"
268+
"Fetch and display the image via WMS"
262269
]
263270
},
264271
{
@@ -278,30 +285,8 @@
278285
" bbox=[1.0, 50.0, 10.0, 54.0],\n",
279286
" format=\"image/png\")\n",
280287
"\n",
281-
"save_fp = 'test/10-wms.png'\n",
282-
"with open(save_fp, 'wb') as out:\n",
283-
"\tout.write(img.read())"
284-
]
285-
},
286-
{
287-
"cell_type": "markdown",
288-
"metadata": {},
289-
"source": [
290-
"Display image in Notebook or view in browser via [this link](test/10-wms.png)."
291-
]
292-
},
293-
{
294-
"cell_type": "code",
295-
"execution_count": null,
296-
"metadata": {
297-
"pycharm": {
298-
"name": "#%%\n"
299-
}
300-
},
301-
"outputs": [],
302-
"source": [
303-
"from IPython.display import Image\n",
304-
"Image(filename=save_fp)"
288+
"from IPython.display import Image, display\n",
289+
"display(Image(data=img.read()))\n"
305290
]
306291
},
307292
{
@@ -500,7 +485,20 @@
500485
},
501486
"outputs": [],
502487
"source": [
503-
"response = wfs.getfeature(typename='ms:cities', bbox=(15.7500260759, 42.65, 19.59976, 45.2337767604))"
488+
"response = wfs.getfeature(typename='ms:cities', bbox=(15.7500260759, 42.65, 19.59976, 45.2337767604))\n",
489+
"data = response.read()\n",
490+
"\n",
491+
"# Parse the GML response as Shapely geometries\n",
492+
"from io import BytesIO\n",
493+
"from geopandas import read_file\n",
494+
"gdf = read_file(BytesIO(data))\n",
495+
"\n",
496+
"# Combine the points in a MultiPoint\n",
497+
"from shapely import MultiPoint\n",
498+
"mpgeom = MultiPoint(list(gdf.geometry))\n",
499+
"\n",
500+
"# Plot the geometry\n",
501+
"mpgeom"
504502
]
505503
},
506504
{
@@ -888,7 +886,7 @@
888886
"tags": []
889887
},
890888
"source": [
891-
"Download data from selected layer and write to a file."
889+
"Download data from selected layer and plot."
892890
]
893891
},
894892
{
@@ -908,9 +906,19 @@
908906
"source": [
909907
"identifier = 'ge:boreholes_cores_rbins'\n",
910908
"features = boreholes_wfs.getfeature([identifier])\n",
909+
"data = features.read()\n",
910+
"\n",
911+
"# Parse the GML response as Shapely geometries\n",
912+
"from io import BytesIO\n",
913+
"from geopandas import read_file\n",
914+
"gdf = read_file(BytesIO(data))\n",
915+
"\n",
916+
"# Combine the points in a MultiPoint\n",
917+
"from shapely import MultiPoint\n",
918+
"mpgeom = MultiPoint(list(gdf.geometry))\n",
911919
"\n",
912-
"with open('test/10-boreholes.gml', 'w', encoding='UTF-8') as out:\n",
913-
" out.write(str(features.read(), 'UTF-8'))"
920+
"# Plot the geometry\n",
921+
"mpgeom"
914922
]
915923
},
916924
{

0 commit comments

Comments
 (0)