-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathscatter_map_w_layers.py
More file actions
68 lines (56 loc) · 1.74 KB
/
Copy pathscatter_map_w_layers.py
File metadata and controls
68 lines (56 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# /// script
# requires-python = ">=3.9"
# dependencies = [
# "datashader",
# "pandas",
# "plotly",
# "zstandard",
# "colorcet",
# ]
# ///
"""Create pickled fig for use in integration tests."""
import pickle
from pathlib import Path
import datashader as ds
import datashader.transfer_functions as tf
import pandas as pd
import plotly.express as px
import zstandard as zstd
from colorcet import fire
cctx = zstd.ZstdCompressor(level=20)
df = pd.read_csv(
"https://raw.githubusercontent.com/plotly/datasets/master/uber-rides-data1.csv",
)
dff = (
df.query("Lat < 40.82")
.query("Lat > 40.70")
.query("Lon > -74.02")
.query("Lon < -73.91")
)
cvs = ds.Canvas(plot_width=1000, plot_height=1000)
agg = cvs.points(dff, x="Lon", y="Lat")
# agg is an xarray object, see http://xarray.pydata.org/en/stable/ for more details
coords_lat, coords_lon = agg.coords["Lat"].to_numpy(), agg.coords["Lon"].to_numpy()
# Corners of the image
coordinates = [
[coords_lon[0], coords_lat[0]],
[coords_lon[-1], coords_lat[0]],
[coords_lon[-1], coords_lat[-1]],
[coords_lon[0], coords_lat[-1]],
]
img = tf.shade(agg, cmap=fire)[::-1].to_pil()
# Trick to create rapidly a figure with map axes
fig = px.scatter_map(dff[:1], lat="Lat", lon="Lon", zoom=12)
# Add the datashader image as a tile map layer image
fig.update_layout(
map_style="carto-darkmatter",
map_layers=[{"sourcetype": "image", "source": img, "coordinates": coordinates}],
)
raw = pickle.dumps(fig, protocol=5) # >=3.8
compressed = cctx.compress(raw)
with Path(f"./figs/{Path(__file__).stem}.pkl.zst").open("wb") as f:
f.write(compressed)
print( # noqa: T201
f"{Path(__file__).stem}.pkl: "
f"{len(raw) / 1024:.1f} -> {len(compressed) / 1024:.1f} KB",
)