pico-tracker/sim/geofence/geofence_prefix.ipynb

236 wiersze
6.4 KiB
Plaintext
Czysty Zwykły widok Historia

{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Import other notebooks\n",
"import io\n",
"from IPython.nbformat import current\n",
"\n",
"def execute_notebook(nbfile):\n",
" with io.open(nbfile) as f:\n",
" nb = current.read(f, 'json')\n",
" \n",
" ip = get_ipython()\n",
" for cell in nb.worksheets[0].cells:\n",
" if cell.cell_type != 'code':\n",
" continue\n",
" ip.run_cell(cell.input)\n",
"\n",
"execute_notebook(\"country_excludes.ipynb\")\n",
"execute_notebook(\"geofence_lib.ipynb\")\n",
"execute_notebook(\"header_files.ipynb\")\n",
"\n",
"import matplotlib"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 46
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import json\n",
"json_data = open(\"countries_world.json\").read()\n",
"countries = json.loads(json_data)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def prepare_simple_grids(grids, tolerance=2*12*1000): # Approx 12km\n",
" simple = [simplify_points(points, tolerance) for points in grids]\n",
" return [s for s in simple if len(s) > 2]"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 9
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"c_prefix = []\n",
"\n",
"for c in countries:\n",
" if (c['prefix']):\n",
" c_prefix.append(c)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "KeyError",
"evalue": "u'prefix'",
"output_type": "pyerr",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-17-23d6a2b1a119>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mc\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mcountries\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0;32mif\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mc\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'prefix'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0mc_prefix\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mKeyError\u001b[0m: u'prefix'"
]
}
],
"prompt_number": 17
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print len(c_prefix)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"46\n"
]
}
],
"prompt_number": 18
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"for c in c_prefix:\n",
" c['prefix_grids'] = prepare_simple_grids(c['simple_grids'], tolerance=10 * 2000) # ~10km\n",
" c['prefix_outlines'] = fix_antiprime_outlines(ea_grid_to_lonlat(c['prefix_grids']))"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Swap!\n",
"Swap!\n",
"Swap!\n",
"Swap!\n",
"Swap!\n",
"Swap!\n",
"Swap!\n",
"Swap!\n"
]
}
],
"prompt_number": 26
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Filter out zero-length outlines\n",
"for c in c_prefix:\n",
" \n",
" filtered_outlines = []\n",
" for outline in c['prefix_outlines']:\n",
" if (len(outline) > 0):\n",
" filtered_outlines.append(outline)\n",
" \n",
" c['prefix_outlines'] = filtered_outlines"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 27
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"n_outlines = 0\n",
"n_points = 0\n",
"\n",
"for c in c_prefix:\n",
" n_outlines = n_outlines + len(c['prefix_outlines'])\n",
" \n",
" for outline in c['prefix_outlines']:\n",
" n_points = n_points + len(outline)\n",
" \n",
"print \"Total number of points {}\".format(n_points)\n",
"print \"Across {} outlines\".format(n_outlines)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Total number of points 4503\n",
"Across 157 outlines\n"
]
}
],
"prompt_number": 28
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Draw map\n",
"m = Basemap(projection='mill')\n",
" \n",
"m.fillcontinents(color='0.96')\n",
"m.drawcoastlines()\n",
"\n",
"for c in c_prefix:\n",
" draw_outlines(m, c['prefix_outlines'], c['colour'])"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "display_data",
"text": [
"<matplotlib.figure.Figure at 0x7fcf83335d10>"
]
}
],
"prompt_number": 31
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"write_prefix(c_prefix)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 47
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}