Skip to content

Commit

Permalink
Merge 42db261 into ecc5c71
Browse files Browse the repository at this point in the history
  • Loading branch information
ajduberstein committed Feb 6, 2020
2 parents ecc5c71 + 42db261 commit cbea712
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
29 changes: 29 additions & 0 deletions bindings/pydeck/examples/point_cloud_layer.py
@@ -0,0 +1,29 @@
import pydeck
import pandas as pd


DATA_URL = "https://raw.githubusercontent.com/ajduberstein/geo_datasets/master/small_waterfall.csv"
df = pd.read_csv(DATA_URL)

target = [df.x.mean(), df.y.mean(), df.z.mean()]

point_cloud_layer = pydeck.Layer(
"PointCloudLayer",
data=DATA_URL,
get_position=["x", "y", "z"],
get_color=["r", "g", "b"],
get_normal=[0, 0, 15],
auto_highlight=True,
pickable=True,
point_size=3,
)

view_state = pydeck.ViewState(
target=target, controller=True, rotation_x=15, rotation_orbit=30, zoom=5.3
)
view = pydeck.View(type="OrbitView", controller=True)

r = pydeck.Deck(point_cloud_layer, initial_view_state=view_state, views=[view])
r.to_html(
"point_cloud_layer.html", css_background_color="#add8e6", notebook_display=False
)
123 changes: 123 additions & 0 deletions bindings/pydeck/examples/polygon_layer.py
@@ -0,0 +1,123 @@
import math

import pydeck
import pandas as pd

# Load in the JSON data
DATA_URL = "https://raw.githubusercontent.com/uber-common/deck.gl-data/master/examples/geojson/vancouver-blocks.json"
json = pd.read_json(DATA_URL)
df = pd.DataFrame()

# Custom color scale
COLOR_RANGE = [
[65, 182, 196],
[127, 205, 187],
[199, 233, 180],
[237, 248, 177],
[255, 255, 204],
[255, 237, 160],
[254, 217, 118],
[254, 178, 76],
[253, 141, 60],
[252, 78, 42],
[227, 26, 28],
[189, 0, 38],
[128, 0, 38],
]

BREAKS = [-0.6, -0.45, -0.3, -0.15, 0, 0.15, 0.3, 0.45, 0.6, 0.75, 0.9, 1.05, 1.2]


def color_scale(val):
for i, b in enumerate(BREAKS):
if val < b:
return COLOR_RANGE[i]
return COLOR_RANGE[i]


def calculate_elevation(val):
return math.sqrt(val) * 10


# Parse the geometry out in Pandas
df["coordinates"] = json["features"].apply(lambda row: row["geometry"]["coordinates"])
df["valuePerSqm"] = json["features"].apply(lambda row: row["properties"]["valuePerSqm"])
df["growth"] = json["features"].apply(lambda row: row["properties"]["growth"])
df["elevation"] = json["features"].apply(
lambda row: calculate_elevation(row["properties"]["valuePerSqm"])
)
df["fill_color"] = json["features"].apply(
lambda row: color_scale(row["properties"]["growth"])
)

# Add sunlight shadow to the polygons
sunlight = {
"@@type": "SunLight",
"timestamp": 1564696800000, # Date.UTC(2019, 7, 1, 22),
"color": [255, 255, 255],
"intensity": 1.0,
"_shadow": True,
}

ambient_light = {"@@type": "AmbientLight", "color": [255, 255, 255], "intensity": 1.0}

lighting_effect = {
"@@type": "LightingEffect",
"shadowColor": [0, 0, 0, 0.5],
"ambientLight": ambient_light,
"directionalLights": [sunlight],
}

view_state = pydeck.ViewState(
**{
"latitude": 49.254,
"longitude": -123.13,
"zoom": 11,
"maxZoom": 16,
"pitch": 45,
"bearing": 0,
}
)

LAND_COVER = [
[[-123.0, 49.196], [-123.0, 49.324], [-123.306, 49.324], [-123.306, 49.196]]
]

polygon_layer = pydeck.Layer(
"PolygonLayer",
LAND_COVER,
stroked=False,
# processes the data as a flat longitude-latitude pair
get_polygon="-",
get_fill_color=[0, 0, 0, 20],
)

polygon_layer = pydeck.Layer(
"PolygonLayer",
df,
id="geojson",
opacity=0.8,
stroked=False,
get_polygon="coordinates",
filled=True,
extruded=True,
wireframe=True,
get_elevation="elevation",
get_fill_color="fill_color",
get_line_color=[255, 255, 255],
auto_highlight=True,
pickable=True,
)

tooltip = {
"html": "<b>Value per Square Meter:</b> {valuePerSqm} <br /><b>Growth rate:</b> {growth}"
}

r = pydeck.Deck(
polygon_layer,
initial_view_state=view_state,
effects=[lighting_effect],
map_style="mapbox://styles/mapbox/light-v9",
tooltip=tooltip,
)
r.to_html("polygon_layer.html", notebook_display=False)

0 comments on commit cbea712

Please sign in to comment.