Skip to content

Instantly share code, notes, and snippets.

@hydrosquall
Created October 1, 2017 18:56
Show Gist options
  • Save hydrosquall/fda8a4cfe956318e65d5065d4a010c83 to your computer and use it in GitHub Desktop.
Save hydrosquall/fda8a4cfe956318e65d5065d4a010c83 to your computer and use it in GitHub Desktop.
adding stickers to existing viz
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Fix Dunkirk Visualization\n",
"\n",
"\n",
"A basic example showing how stickers can be added.\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
" % Total % Received % Xferd Average Speed Time Time Time Current\n",
" Dload Upload Total Spent Left Speed\n",
"\n",
" 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0\n",
" 10 3486k 10 367k 0 0 711k 0 0:00:04 --:--:-- 0:00:04 711k\n",
" 55 3486k 55 1951k 0 0 1287k 0 0:00:02 0:00:01 0:00:01 1287k\n",
" 94 3486k 94 3305k 0 0 1313k 0 0:00:02 0:00:02 --:--:-- 1313k\n",
"100 3486k 100 3486k 0 0 1320k 0 0:00:02 0:00:02 --:--:-- 1320k\n"
]
}
],
"source": [
"# Download an existing project\n",
"!curl \"https://raw.githubusercontent.com/DanielJWood/dunkirk/master/dunkirk.json\" > \"../data/external/dunkirk.json\""
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Project imports\n",
"import json\n",
"import os\n",
"ROOT = os.pardir"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# Open existing project\n",
"viz_path = os.path.join(ROOT, \"data\", \"external\", \"dunkirk.json\")\n",
"new_path = os.path.join(ROOT, \"data\", \"processed\", \"dunkirk-sticker.json\")\n",
"\n",
"with open(viz_path) as fp:\n",
" viz = json.load(fp)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[u'polygons']"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"viz['layers'][0].keys()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check how many cards there are\n",
"len(viz['groups'])"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Check how many layers are in the first group\n",
"len(viz['groups'][0]['layers'])"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[u'stickers', u'polygons']\n"
]
}
],
"source": [
"# Visualization is missing the required name key!\n",
"# Additionally, each layer can only have one type of item (Stickers, Polygons, Points)\n",
"# If you want more types of item, then make a new layer\n",
"print viz['groups'][0]['layers'][0].keys()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"# First, add a name/metadata to the layer\n",
"viz['groups'][0]['layers'][0]['metadata'] = {'name': \"polygon_layer\"}"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Then, create a new layer with at least 1 sticker\n",
"# Copy the sticker from Syria Visualization\n",
"# https://raw.githubusercontent.com/eartheos-app/api-docs/master/examples/syrian-conflict.json\n",
"# A sticker has 2 essential parts: image source, and where to position each corner.\n",
"syria_image = {\n",
" \"stickerImage\": \"https://s3.amazonaws.com/data.eartheos.com/images/rev_buildings_smoke.png\",\n",
" \"ll\": {\n",
" \"lat\": 34,\n",
" \"lon\": 37\n",
" },\n",
" \"ur\": {\n",
" \"lat\": 36,\n",
" \"lon\": 40\n",
" }\n",
"}\n",
"\n",
"# If you have more stickers to add, include them in the list below.\n",
"stickers = [syria_image]"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Put your stickers into a layer\n",
"sticker_layer = { 'stickers': stickers, \n",
" 'metadata' : { \n",
" 'name': \"syria_sticker_layer\"\n",
" } \n",
" }"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"# Add that layer to your base visualization\n",
"viz['groups'][0]['layers'].append(sticker_layer)"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Write result back to a new file\n",
"with open(new_path, 'wb') as fp:\n",
" json.dump(viz, fp)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Deploy file to dropbox for sharing\n",
"!cp \"C:\\Users\\cyick\\Projects\\eartheos_viz\\data\\processed\\dunkirk-sticker.json\" \"C:\\Users\\cyick\\Dropbox\\00_Senior\\Addressable\""
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python [default]",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment