OSGeoLive-Notebooks/Fiona/fiona-shapely-mapnik.ipynb

190 lines
3.9 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from IPython.core.display import Image\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib inline\n",
"\n",
"import numpy as np\n",
"\n",
"import fiona\n",
"import mapnik\n",
"import shapely.geometry\n",
"\n",
"import os\n",
"\n",
"## Fiona, mapnik demo\n",
"## Live 8.5 * darkblue-b\n",
"##\n",
"## based on UoLPythonGroup/data-hack-0"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"BASE_FOLDER = '/home/user/data/north_carolina/shape'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"c_shapefile = os.path.join(BASE_FOLDER, 'nc_state.shp')\n",
"f = fiona.open(c_shapefile)\n",
"shps = list(f)\n",
"print( 'f: ',type(f))\n",
"print( f.schema)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"type(f[2])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## Use the Shapely geometry classes\n",
"## instantiate a Polygon from the Fiona collection "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import shapely.geometry\n",
"\n",
"geo = shapely.geometry.shape(f[0]['geometry'])\n",
"geo"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## plt.plot(*geo.xy) ## hmm not implemented"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## Mapnik\n",
"## load the Mapnik python interfaces\n",
"## read the shapefile directly with Mapnik libs\n",
"## use the IPython Image interface as the drawing target"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def show_mapnik(m):\n",
" \"\"\"Returns an IPython Image of the rendered map.\"\"\"\n",
" im = mapnik.Image(m.width, m.height)\n",
" mapnik.render(m, im)\n",
" return Image(data=im.tostring('png32'))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import mapnik\n",
"\n",
"m = mapnik.Map(600, 600)\n",
"\n",
"layer = mapnik.Layer('contour')\n",
"layer.datasource = mapnik.Shapefile(file=c_shapefile)\n",
"\n",
"style = mapnik.Style()\n",
"rule = mapnik.Rule()\n",
"\n",
"#mapnik.Color('green'),0.4\n",
"line_symbolizer = mapnik.LineSymbolizer()\n",
"rule.symbols.append(line_symbolizer)\n",
"\n",
"m.layers.append(layer)\n",
"style.rules.append(rule)\n",
"m.append_style('My Style', style)\n",
"layer.styles.append('My Style')\n",
"\n",
"m.layers.append(layer)\n",
"m.zoom_all()\n",
"show_mapnik(m)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"mapnik.LineSymbolizer?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 1
}