Last active
December 8, 2015 19:21
-
-
Save SamPenrose/77aa362ba1d40ec77257 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"nbformat_minor": 0, "cells": [{"execution_count": 1, "cell_type": "code", "source": "os.system('pip install folium')\nimport folium", "outputs": [], "metadata": {"collapsed": true, "trusted": true}}, {"execution_count": 3, "cell_type": "code", "source": "from pyspark.sql import SQLContext\nfrom pyspark.sql.types import *\n\ndataset = sqlContext.load(\"s3://telemetry-parquet/executive-stream/telemetry-executive-summary-3/submissionDateS3=20151130\", \"parquet\")", "outputs": [], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": 4, "cell_type": "code", "source": "dataset.count()", "outputs": [{"execution_count": 4, "output_type": "execute_result", "data": {"text/plain": "404338319L"}, "metadata": {}}], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": 9, "cell_type": "code", "source": "countries = dataset.groupBy('country').count().collect()", "outputs": [], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": 17, "cell_type": "code", "source": "# https://raw.githubusercontent.com/python-visualization/folium/master/examples/world-countries.json\n# edited to include a 2-letter country tag that matches what's in the ES\nimport ujson as json\nimport urllib\nu = urllib.urlopen('https://raw.githubusercontent.com/python-visualization/folium/master/examples/world-countries.json')\nmap_geo_data = json.load(u)\nfor d in map_geo_data['features']:\n d['id2'] = d['id'][:2]\nwith open('world-countries-2.json', 'w') as f:\n json.dump(map_geo_data, f)", "outputs": [], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": 13, "cell_type": "code", "source": "import numpy, pandas as pd\ncdf = pd.DataFrame([r.asDict() for r in countries])", "outputs": [], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": 22, "cell_type": "code", "source": "countries.sort(key=lambda r: r.count)\ncolumns = ['country', 'count']\nthreshold_scale = numpy.linspace(countries[0].count, countries[-1].count, 6, dtype=int).tolist()\nmapa = folium.Map(tiles=\"Mapbox Bright\", zoom_start=3)\nmapa.geo_json(geo_path='world-countries-2.json', data=cdf, columns=columns, fill_color='YlGn',\n threshold_scale=threshold_scale, key_on='feature.id2')\nmapa", "outputs": [{"execution_count": 22, "output_type": "execute_result", "data": {"text/plain": "<folium.folium.Map at 0x7ffb14183ad0>", "text/html": "<iframe srcdoc=\"<!DOCTYPE html>\n<head>\n <meta http-equiv="content-type" content="text/html; charset=UTF-8" />\n <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.css" />\n <script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js"></script>\n <script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js" charset="utf-8"></script>\n <script src="https://cdnjs.cloudflare.com/ajax/libs/queue-async/1.0.7/queue.min.js"></script>\n\n \n \n \n \n\n <style>\n\n html, body {\n width: 100%;\n height: 100%;\n margin: 0;\n padding: 0;\n }\n\n .legend {\n padding: 0px 0px;\n font: 10px sans-serif;\n background: white;\n background: rgba(255,255,255,0.8);\n box-shadow: 0 0 15px rgba(0,0,0,0.2);\n border-radius: 5px;\n }\n\n .key path {\n display: none;\n }\n\n </style>\n</head>\n\n<body>\n\n <div id="map" style="width: 100%; height: 100%"></div>\n\n <script>\n\n queue()\n .defer(function(callback){callback(null, JSON.parse('[{"BD": 2054650, "BE": 3228503, "BF": 71764, "BG": 2179362, "BA": 626974, "BB": 28352, "WF": 1548, "BL": 5, "BM": 7905, "BN": 47292, "BO": 445625, "BH": 56042, "BI": 28220, "BJ": 54509, "BT": 18887, "JM": 91614, "BW": 59655, "WS": 2678, "BQ": 2367, "BR": 18178398, "BS": 21019, "JE": 12187, "BY": 1000470, "BZ": 25720, "LV": 801151, "RW": 103953, "RS": 1745287, "LT": 1291108, "RE": 235855, "LU": 255966, "TJ": 13616, "RO": 2882883, "TK": 2, "GW": 2893, "GU": 17551, "GT": 284383, "GR": 3591144, "GQ": 3353, "GP": 73294, "JP": 7284242, "GY": 28903, "GG": 8151, "GF": 39117, "GE": 258529, "GD": 6019, "GB": 9815089, "GA": 36540, "GN": 28458, "GM": 20970, "GL": 5390, "KW": 107461, "GI": 6026, "GH": 344110, "OM": 90215, "IL": 549003, "JO": 163595, "HR": 1251285, "HT": 32681, "HU": 4183152, "HK": 869743, "HN": 132796, "KR": 456602, "CR": 354955, "VE": 2759655, "PR": 214594, "PS": 334302, "PW": 981, "PT": 1596399, "KN": 3837, "VG": 2363, "IQ": 510370, "PA": 154692, "PF": 49330, "PG": 12638, "PE": 1161249, "PK": 1700292, "PH": 2310127, "PN": 3, "PL": 19083203, "PM": 4532, "ZM": 85466, "RU": 17451840, "EE": 526629, "EG": 4994451, "ZA": 1495490, "EC": 2291905, "IT": 13051670, "VN": 3229957, "KZ": 1397153, "ET": 263054, "ZW": 128897, "KY": 9441, "ES": 8685366, "ER": 1942, "ME": 96271, "MD": 339566, "MG": 167703, "MF": 1994, "MA": 1222729, "MC": 16827, "UZ": 103138, "MM": 189478, "ML": 56220, "MO": 40162, "MN": 396697, "MH": 3043, "MK": 342921, "MU": 137396, "MT": 64397, "MW": 33583, "MV": 46409, "MQ": 56420, "MP": 4642, "MS": 195, "MR": 26670, "IM": 13649, "UG": 197539, "MY": 2177671, "MX": 5099746, "MZ": 74680, "FR": 27617202, "IO": 645, "FI": 2769993, "FJ": 58308, "FK": 383, "FM": 5786, "FO": 11965, "NI": 150601, "NL": 4310689, "NO": 1078271, "NA": 57958, "VU": 4966, "NC": 75095, "NE": 25586, "NF": 5, "NG": 827538, "NZ": 852959, "NP": 330411, "NR": 324, "NU": 40, "CK": 1646, "CI": 255145, "CH": 3036279, "CO": 1888221, "CN": 10058044, "CM": 213998, "CL": 1319480, "CC": 3, "CA": 8076629, "LB": 195588, "CG": 24553, "CF": 2344, "CD": 60887, "CZ": 4176570, "CY": 182742, "CX": 10, "UY": 414786, "PY": 240182, "CW": 14250, "CV": 14410, "CU": 65185, "SZ": 9536, "SY": 234407, "SX": 4341, "KG": 61738, "KE": 859212, "SS": 6346, "SR": 23313, "KI": 741, "KH": 361972, "SV": 243632, "KM": 4054, "ST": 2188, "SK": 1740217, "SJ": 6, "SI": 943810, "KP": 334, "SO": 23991, "SN": 216188, "SM": 9222, "SL": 13954, "SC": 10499, "SB": 2644, "SA": 979030, "SG": 979943, "SE": 1998135, "SD": 164655, "DO": 250851, "DM": 4678, "DJ": 16315, "DK": 1120380, "DE": 40933979, "YE": 61579, "Other": 393658, "DZ": 2261662, "US": 61763898, "YT": 18115, "TZ": 224451, "LC": 9550, "LA": 67895, "TV": 172, "TW": 1832289, "TT": 89211, "TR": 3566119, "LK": 559257, "LI": 16419, "TN": 459905, "TO": 3077, "TL": 13004, "TM": 4496, "LR": 5651, "LS": 8504, "TH": 2107862, "TG": 51106, "TD": 1818, "TC": 2480, "LY": 114683, "VA": 2288, "VC": 4254, "AE": 582969, "AD": 13667, "AG": 7872, "AF": 61900, "AI": 781, "VI": 15614, "IS": 105121, "IR": 4526109, "AM": 211656, "AL": 191560, "AO": 51656, "AS": 3286, "AR": 3963509, "AU": 4497309, "AT": 3353821, "AW": 9878, "IN": 15732508, "AX": 8571, "AZ": 110656, "IE": 970957, "ID": 10727092, "UA": 5730368, "QA": 137277}]'))})\n .defer(d3.json, 'world-countries-2.json')\n .await(makeMap)\n\n function onEachFeature(feature, layer) {\n // does this feature have a property named popupContent?\n if (feature.properties && feature.properties.popupContent) {\n layer.bindPopup(feature.properties.popupContent);\n }\n };\n\n function makeMap(error, data_1,gjson_1) {\n\n \n\n \n\n function matchKey(datapoint, key_variable){\n if (typeof key_variable[0][datapoint] === 'undefined') {\n return null;\n }\n else {\n return parseFloat(key_variable[0][datapoint]);\n };\n };\n\n \n var color = d3.scale.threshold()\n .domain([13667, 36713, 59759, 82805, 105851, 128897])\n .range(['#FFFFCC', '#D9F0A3', '#ADDD8E', '#78C679', '#41AB5D', '#238443', '#005A32']);\n \n\n var map = L.map('map').setView([0, 0], 1);\n\n L.tileLayer('https://{s}.tiles.mapbox.com/v3/mapbox.world-bright/{z}/{x}/{y}.png', {\n maxZoom: 18,\n minZoom: 1,\n attribution: 'Map tiles by <a href="http://www.mapbox.com/m">Mapbox</a> Data by <a href="http://openstreetmap.org">OpenStreetMap</a>, under <a href="http://creativecommons.org/licenses/by-sa/3.0">CC BY SA</a>.'\n }).addTo(map);\n\n \n function style_1(feature) {\n return {\n fillColor: color(matchKey(feature.id2, data_1)),\n weight: 1,\n opacity: 1,\n color: 'black',\n fillOpacity: 0.6\n };\n}\n \n\n \n gJson_layer_1 = L.geoJson(gjson_1, {style: style_1,onEachFeature: onEachFeature}).addTo(map)\n \n\n \n var legend = L.control({position: 'topright'});\n\n legend.onAdd = function (map) {var div = L.DomUtil.create('div', 'legend'); return div};\n\n legend.addTo(map);\n\n var x = d3.scale.linear()\n .domain([0, 141786])\n .range([0, 400]);\n\n var xAxis = d3.svg.axis()\n .scale(x)\n .orient("top")\n .tickSize(1)\n .tickValues([13667, 36713, 59759, 82805, 105851, 128897]);\n\n var svg = d3.select(".legend.leaflet-control").append("svg")\n .attr("id", 'legend')\n .attr("width", 450)\n .attr("height", 40);\n\n var g = svg.append("g")\n .attr("class", "key")\n .attr("transform", "translate(25,16)");\n\n g.selectAll("rect")\n .data(color.range().map(function(d, i) {\n return {\n x0: i ? x(color.domain()[i - 1]) : x.range()[0],\n x1: i < color.domain().length ? x(color.domain()[i]) : x.range()[1],\n z: d\n };\n }))\n .enter().append("rect")\n .attr("height", 10)\n .attr("x", function(d) { return d.x0; })\n .attr("width", function(d) { return d.x1 - d.x0; })\n .style("fill", function(d) { return d.z; });\n\n g.call(xAxis).append("text")\n .attr("class", "caption")\n .attr("y", 21)\n .text('count');\n \n\n \n\n \n\n \n\n \n\n \n\n var autobounds = L.featureGroup([]).getBounds()\n\n\n\n\n\nmap.fitBounds(autobounds,\n {"padding": [30, 30]}\n);\n\n };\n\n </script>\n</body>\" style=\"width: 100%; height: 500px; border: none\"></iframe>"}, "metadata": {}}], "metadata": {"collapsed": false, "trusted": true}}, {"execution_count": null, "cell_type": "code", "source": "", "outputs": [], "metadata": {"collapsed": true, "trusted": true}}], "nbformat": 4, "metadata": {"kernelspec": {"display_name": "Python 2", "name": "python2", "language": "python"}, "language_info": {"mimetype": "text/x-python", "nbconvert_exporter": "python", "version": "2.7.9", "name": "python", "file_extension": ".py", "pygments_lexer": "ipython2", "codemirror_mode": {"version": 2, "name": "ipython"}}}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment