pico-tracker/sim/geofence/geofence_prefix.ipynb

239 wiersze
5.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": 11
},
{
"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": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def prepare_simple_grids(grids, tolerance=10*1000): # Approx 10km tolerance\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": 13
},
{
"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": {},
2016-02-02 20:22:57 +00:00
"outputs": [],
"prompt_number": 14
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"print len(c_prefix)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"224\n"
]
}
],
"prompt_number": 15
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"for c in c_prefix:\n",
" c['prefix_grids'] = prepare_simple_grids(c['simple_grids'], tolerance=10*1000) # 10km tolerance\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!"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Swap!\n",
"Swap!\n",
"Swap!\n",
"Swap!\n",
"Swap!\n",
"Swap!\n",
"Swap!\n"
]
}
],
"prompt_number": 16
},
{
"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": 17
},
{
"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 12299\n",
"Across 486 outlines\n"
]
}
],
"prompt_number": 18
},
{
"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 0x7f95f4074690>"
]
}
],
"prompt_number": 19
},
{
"cell_type": "code",
"collapsed": false,
"input": [
2016-02-02 20:22:57 +00:00
"write_prefixes(c_prefix)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 20
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
2016-02-02 20:22:57 +00:00
"outputs": [],
"prompt_number": 20
}
],
"metadata": {}
}
]
}