Skip to content

Instantly share code, notes, and snippets.

@tbicr
Last active August 29, 2015 14:07
Show Gist options
  • Save tbicr/abdbca151d822f03a4d1 to your computer and use it in GitHub Desktop.
Save tbicr/abdbca151d822f03a4d1 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:fdb617cb23c56e3fd4416064e62ebde6531bd1651bdb97f89b2b371a0eeaab07"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"import json\n",
"\n",
"import folium\n",
"import requests\n",
"from shapely.geometry import shape, mapping"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"eparhies = {\n",
" '\u041c\u0438\u043d\u0441\u043a\u0430\u044f \u0435\u043f\u0430\u0440\u0445\u0438\u044f': [\n",
" '\u041c\u0438\u043d\u0441\u043a\u0438\u0439 \u0440\u0430\u0439\u043e\u043d',\n",
" ],\n",
" '\u0411\u0440\u0435\u0441\u0442\u0441\u043a\u0430\u044f \u0435\u043f\u0430\u0440\u0445\u0438\u044f': [\n",
" '\u0411\u0440\u0435\u0441\u0442\u0441\u043a\u0438\u0439 \u0440\u0430\u0439\u043e\u043d',\n",
" '\u0411\u0435\u0440\u0451\u0437\u043e\u0432\u0441\u043a\u0438\u0439 \u0440\u0430\u0439\u043e\u043d',\n",
" '\u0414\u0440\u043e\u0433\u0438\u0447\u0438\u043d\u0441\u043a\u0438\u0439 \u0440\u0430\u0439\u043e\u043d',\n",
" '\u0416\u0430\u0431\u0438\u043d\u043a\u043e\u0432\u0441\u043a\u0438\u0439 \u0440\u0430\u0439\u043e\u043d',\n",
" '\u041a\u0430\u043c\u0435\u043d\u0435\u0446\u043a\u0438\u0439 \u0440\u0430\u0439\u043e\u043d',\n",
" '\u041a\u043e\u0431\u0440\u0438\u043d\u0441\u043a\u0438\u0439 \u0440\u0430\u0439\u043e\u043d',\n",
" '\u041c\u0430\u043b\u043e\u0440\u0438\u0442\u0441\u043a\u0438\u0439 \u0440\u0430\u0439\u043e\u043d',\n",
" '\u041f\u0440\u0443\u0436\u0430\u043d\u0441\u043a\u0438\u0439 \u0440\u0430\u0439\u043e\u043d',\n",
" ],\n",
" '\u0412\u0438\u0442\u0435\u0431\u0441\u043a\u0430\u044f \u0435\u043f\u0430\u0440\u0445\u0438\u044f': [\n",
" '\u0412\u0438\u0442\u0435\u0431\u0441\u043a\u0438\u0439 \u0440\u0430\u0439\u043e\u043d',\n",
" ],\n",
" '\u0413\u043e\u043c\u0435\u043b\u044c\u0441\u043a\u0430\u044f \u0435\u043f\u0430\u0440\u0445\u0438\u044f': [\n",
" '\u0413\u043e\u043c\u0435\u043b\u044c\u0441\u043a\u0438\u0439 \u0440\u0430\u0439\u043e\u043d',\n",
" ],\n",
" '\u0413\u0440\u043e\u0434\u043d\u0435\u043d\u0441\u043a\u0430\u044f \u0435\u043f\u0430\u0440\u0445\u0438\u044f': [\n",
" '\u0413\u0440\u043e\u0434\u043d\u0435\u043d\u0441\u043a\u0438\u0439 \u0440\u0430\u0439\u043e\u043d',\n",
" ],\n",
" '\u041c\u043e\u0433\u0438\u043b\u0451\u0432\u0441\u043a\u0430\u044f \u0435\u043f\u0430\u0440\u0445\u0438\u044f': [\n",
" '\u041c\u043e\u0433\u0438\u043b\u0451\u0432\u0441\u043a\u0438\u0439 \u0440\u0430\u0439\u043e\u043d',\n",
" ],\n",
" '\u041d\u043e\u0432\u043e\u0433\u0440\u0443\u0434\u0441\u043a\u0430\u044f \u0435\u043f\u0430\u0440\u0445\u0438\u044f': [\n",
" '\u041d\u043e\u0432\u043e\u0433\u0440\u0443\u0434\u0441\u043a\u0438\u0439 \u0440\u0430\u0439\u043e\u043d',\n",
" ],\n",
" '\u041f\u0438\u043d\u0441\u043a\u0430\u044f \u0435\u043f\u0430\u0440\u0445\u0438\u044f': [\n",
" '\u041f\u0438\u043d\u0441\u043a\u0438\u0439 \u0440\u0430\u0439\u043e\u043d',\n",
" ],\n",
" '\u041f\u043e\u043b\u043e\u0446\u043a\u0430\u044f \u0435\u043f\u0430\u0440\u0438\u0445\u044f': [\n",
" '\u041f\u043e\u043b\u043e\u0446\u043a\u0438\u0439 \u0440\u0430\u0439\u043e\u043d',\n",
" ],\n",
" '\u0422\u0443\u0440\u043e\u0432\u0441\u043a\u0430\u044f \u0435\u043f\u0430\u0440\u0445\u0438\u044f': [\n",
" '\u0422\u0443\u0440\u043e\u0432\u0441\u043a\u0438\u0439 \u0440\u0430\u0439\u043e\u043d',\n",
" ],\n",
"}"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"base_url = 'http://odata.by/api/action/datastore/search.json?resource_id=30685f8b-608c-4e1e-b15e-41cec3f124c7'\n",
"query = '&filters[subregion]={}&fields=geojson'\n",
"\n",
"features = []\n",
"\n",
"for eparhy, subregions in eparhies.items():\n",
" response = requests.get(base_url + query.format(','.join(subregions))).json()\n",
" geometries_union = None\n",
" for record in response['result']['records']:\n",
" geometry = shape(json.loads(record['geojson']))\n",
" if not geometries_union:\n",
" geometries_union = geometry\n",
" else:\n",
" geometries_union = geometries_union.union(geometry)\n",
" if geometries_union:\n",
" features.append({\n",
" 'type': 'Feature',\n",
" 'geometry': mapping(geometries_union),\n",
" 'properties': {\n",
" 'name': eparhy,\n",
" },\n",
" })\n",
" \n",
"geojson = json.dumps({\n",
" 'type': 'FeatureCollection',\n",
" 'features': features\n",
"})"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"map = folium.Map(location=[53.55, 27.33], zoom_start=6)\n",
"map.geo_json(geo_str=geojson)\n",
"map.create_map()\n",
"map.display()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"html": [
"<iframe class=\"folium-iframe\" src=\"files/map.html\" style=\"width: 960px; height: 500px; border: none; margin-left: 75px;\" scrolling=\"no\"></iframe>"
],
"metadata": {},
"output_type": "display_data",
"text": [
"<IPython.core.display.HTML at 0x7f8ef03d3550>"
]
}
],
"prompt_number": 4
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment