Skip to content

Instantly share code, notes, and snippets.

@robcat
Created May 6, 2015 10:19
Show Gist options
  • Save robcat/eb305489fd28d7d869f2 to your computer and use it in GitHub Desktop.
Save robcat/eb305489fd28d7d869f2 to your computer and use it in GitHub Desktop.
Radiation Model
{
"metadata": {
"name": "",
"signature": "sha256:0924a8ac48928c23aa51897110f99c7ecba00d6458560a4fdabebb2f40dc5d17"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#Radiation Model"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Import country shapes from file"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import fiona\n",
"import shapely.geometry\n",
"countries = {}\n",
"for country in fiona.open('world_borders/TM_WORLD_BORDERS_SIMPL-0.3.shp'):\n",
" country['centroid'] = shapely.geometry.shape({\n",
" 'type' : \"Point\",\n",
" 'coordinates' : [country['properties']['LON'] , country['properties']['LAT']],\n",
" })\n",
" country['properties'] = dict(country['properties'])\n",
" country['surface'] = shapely.geometry.shape(country['geometry']).buffer(0)\n",
" countries[country['properties']['ISO2']] = country\n",
" del country['geometry']\n",
" del country['properties']['ISO2']"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 14
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Computes the radiating parts of the countries"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def radiating_countries(source, target):\n",
" distance = countries[source]['centroid'].distance(countries[target]['centroid'])\n",
" circle = countries[source]['centroid'].buffer(distance)\n",
" radiating_countries = []\n",
" for country in countries:\n",
" intersection = circle.intersection(countries[country]['surface'])\n",
" if intersection:\n",
" radiating_countries.append((country, intersection))\n",
" return radiating_countries"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 111
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Compute the effective radiating populations assuming a uniform distribution"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def radiating_population(source, target):\n",
" radiating_population = 0\n",
" for country, intersection in radiating_countries(source, target):\n",
" fraction = intersection.area/countries[country]['surface'].area\n",
" logging.debug(fraction)\n",
" radiating_population += fraction*countries[country]['properties']['POP2005']\n",
" return radiating_population"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 116
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Draw a sequence of shapes"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def draw_shapes(shapes):\n",
" from matplotlib import pyplot\n",
" import descartes\n",
" fig = pyplot.figure()\n",
" ax = fig.add_subplot(111)\n",
" for shape in shapes:\n",
" if shape.geom_type is 'Polygon':\n",
" ax.add_patch(PolygonPatch(shape))\n",
" else:\n",
" for polygon in shape:\n",
" ax.add_patch(PolygonPatch(polygon))\n",
" ax.autoscale()\n",
" ax.set_aspect('equal')"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 124
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment