-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
284 additions
and
46 deletions.
There are no files selected for viewing
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
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
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
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import time | ||
import jinja2 | ||
import os | ||
import os.path | ||
import tempfile | ||
|
||
import webbrowser | ||
from IPython.display import IFrame | ||
|
||
|
||
TEMPLATES_PATH = os.path.join(os.path.dirname(__file__), './templates/') | ||
j2_env = jinja2.Environment(loader=jinja2.FileSystemLoader(TEMPLATES_PATH), | ||
trim_blocks=True) | ||
|
||
def render_json_to_html(json_input, mapbox_api_key=None): | ||
js = j2_env.get_template('index.j2') | ||
html = js.render( | ||
mapbox_api_key=mapbox_api_key, | ||
json_input=json_input) | ||
return html | ||
|
||
|
||
def display_html(filename=None): | ||
"""Converts HTML into a temporary file and open it in the system browser or IPython/Jupyter Notebook IFrame.""" | ||
try: | ||
return IFrame(filename, height=500, width=500) | ||
except Exception: | ||
try: | ||
url = 'file://{}'.format(filename) | ||
# Hack to prevent blank page | ||
time.sleep(0.5) | ||
webbrowser.open(url) | ||
except Exception: | ||
raise | ||
|
||
|
||
def open_named_or_temporary_file(filename=''): | ||
if filename: | ||
filename = add_html_extension(filename) | ||
return open(filename, 'w+') | ||
return tempfile.NamedTemporaryFile( | ||
suffix='.html', dir=os.cwd(), delete=False) | ||
|
||
|
||
def add_html_extension(fname): | ||
if fname.endswith('.html') or fname.endswith('.htm'): | ||
return fname | ||
if fname is None: | ||
raise Exception("File has no name") | ||
return fname + '.html' | ||
|
||
|
||
def deck_to_html(deck_json, mapbox_key, filename=None): | ||
html = render_json_to_html(deck_json, mapbox_key) | ||
f = open_named_or_temporary_file(filename) | ||
f.write(html) | ||
f.close() | ||
display_html(f.name) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<script src='https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js'></script> | ||
<link rel="stylesheet" href="https://api.tiles.mapbox.com/mapbox-gl-js/v0.44.1/mapbox-gl.css" /> | ||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css" /> | ||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" /> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<html> | ||
<head> | ||
<meta http-equiv="content-type" content="text/html; charset=UTF-8" /> | ||
<title>pydeck</title> | ||
{% include 'imports.j2' %} | ||
<style> | ||
{% include 'style.j2' %} | ||
</style> | ||
</head> | ||
<body> | ||
<div id="deck-container"> | ||
<div id="deck-map-wrapper"> | ||
<canvas id="deck-map-container"> | ||
</canvas> | ||
<div id="map"> | ||
</div> | ||
</div> | ||
</div> | ||
</body> | ||
<script> | ||
{% include 'js.j2' %} | ||
</script> | ||
</html> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
requirejs.config({ | ||
paths: { | ||
deck: 'https://unpkg.com/@deck.gl/core@7.1.0/dist.min', | ||
deckjson: 'https://unpkg.com/@deck.gl/json@7.1.0/dist.min', | ||
decklayers: 'https://unpkg.com/@deck.gl/layers@7.1.0/dist.min', | ||
deckaggregate: 'https://unpkg.com/@deck.gl/aggregation-layers@7.1.0/dist.min', | ||
mapboxgl: 'https://api.tiles.mapbox.com/mapbox-gl-js/v0.44.1/mapbox-gl', | ||
luma: 'https://unpkg.com/@luma.gl/core@7.1.0/dist/dist.min' | ||
}, | ||
shim : { | ||
'deckaggregate': ['deck', 'decklayers', 'luma'] | ||
} | ||
}); | ||
|
||
let config, mapLayer, jsonInput, jsonConverter; | ||
|
||
jsonInput = JSON.parse(JSON.stringify({{json_input}})); | ||
|
||
function setMapProps(map, props) { | ||
if ('viewState' in props && props.viewState.longitude && props.viewState.latitude) { | ||
const {viewState} = props; | ||
map.jumpTo({ | ||
center: [viewState.longitude, viewState.latitude], | ||
zoom: Number.isFinite(viewState.zoom) ? viewState.zoom : 10, | ||
bearing: viewState.bearing || 0, | ||
pitch: viewState.pitch || 0 | ||
}); | ||
} | ||
|
||
if (props.map && 'style' in props.map) { | ||
if (props.map.style !== map.deckStyle) { | ||
map.setStyle(props.map.style); | ||
map.deckStyle = props.map.style; | ||
} | ||
} | ||
} | ||
|
||
|
||
function onViewportChange({viewState}) { | ||
config.setProps({viewState}); | ||
setMapProps(mapLayer, {viewState}); | ||
} | ||
|
||
|
||
function valueChanged() { | ||
const jsonProps = jsonConverter.convertJsonToDeckProps(jsonInput); | ||
config.setProps(jsonProps); | ||
setMapProps(mapLayer, config.props); | ||
} | ||
|
||
|
||
function init(luma, deckJson, deckgl, deckglLayers, mapboxgl, deckAggregationLayers) { | ||
mapboxgl.accessToken = '{{mapbox_api_key}}', | ||
mapLayer = new mapboxgl.Map({ | ||
container: 'map', | ||
interactive: false, | ||
style: null | ||
}); | ||
|
||
jsonConverter = new deckJson._JSONConverter({ | ||
configuration: { | ||
layers: {...deckglLayers, ...deckAggregationLayers} | ||
} | ||
}); | ||
|
||
config = new deckgl.Deck({ | ||
canvas: 'deck-map-container', | ||
height: '100%', | ||
width: '100%', | ||
onLoad: valueChanged.bind(this), | ||
views: [new deckgl.MapView()], | ||
onViewStateChange: onViewportChange.bind(this) | ||
}); | ||
} | ||
|
||
requirejs(['luma', 'deckjson', 'deck', 'decklayers', 'mapboxgl', 'deckaggregate'], init); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
body { | ||
margin: 0; | ||
padding: 0; | ||
overflow: hidden; | ||
} | ||
|
||
#deck-map-container { | ||
width: 100%; | ||
height: 100%; | ||
background-color: black; | ||
} | ||
|
||
#map { | ||
pointer-events: none; | ||
height: 100%; | ||
width: 100%; | ||
position: absolute; | ||
z-index: 1; | ||
} | ||
|
||
#deckgl-overlay { | ||
z-index: 2; | ||
} | ||
|
||
#deck-map-wrapper { | ||
width: 100%; | ||
height: 100%; | ||
} | ||
|
||
#deck-container { | ||
width: 100vw; | ||
height: 100vh; | ||
} |
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
Empty file.
Oops, something went wrong.