|
1 | 1 | import pydeck as pdk
|
2 | 2 | import geopandas as gpd
|
3 | 3 |
|
4 |
| -world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres")) |
| 4 | +world = gpd.read_file("https://naciscdn.org/naturalearth/110m/cultural/ne_110m_admin_0_countries.zip") |
| 5 | +# deck.gl is only compatible with WGS84 |
| 6 | +world = world.to_crs("EPSG:4326") |
| 7 | +# Convert any multi polygons into individual polygons |
| 8 | +world = world.explode() |
5 | 9 |
|
6 |
| -centroids = gpd.GeoDataFrame() |
7 |
| -centroids["geometry"] = world.geometry.centroid |
8 |
| -centroids["name"] = world.name |
| 10 | +centroids = gpd.GeoDataFrame(geometry=world.geometry.centroid) |
| 11 | +centroids["name"] = world.NAME |
9 | 12 |
|
10 | 13 | layers = [
|
| 14 | + # Black background of the country polygons |
11 | 15 | pdk.Layer(
|
12 | 16 | "GeoJsonLayer",
|
13 | 17 | data=world,
|
14 | 18 | get_fill_color=[0, 0, 0],
|
15 | 19 | ),
|
| 20 | + |
| 21 | + # # Alternative way using PolygonLayer, should the above not work |
| 22 | + # pdk.Layer( |
| 23 | + # "PolygonLayer", |
| 24 | + # data=world, |
| 25 | + # get_polygon="geometry.coordinates", |
| 26 | + # get_fill_color=[0, 0, 0], |
| 27 | + # ), |
| 28 | + |
| 29 | + |
| 30 | + # Overlay country names at their centroids. |
16 | 31 | pdk.Layer(
|
17 | 32 | "TextLayer",
|
18 | 33 | data=centroids,
|
| 34 | + # Use this to get geometry coordinates out of a raw GeoDataFrame |
19 | 35 | get_position="geometry.coordinates",
|
20 | 36 | get_size=16,
|
21 | 37 | get_color=[255, 255, 255],
|
22 | 38 | get_text="name",
|
23 | 39 | ),
|
24 | 40 | ]
|
25 | 41 |
|
26 |
| -pdk.Deck(layers, map_provider=None).to_html("geopandas_integration.html", css_background_color="cornflowerblue") |
| 42 | +pdk.Deck(layers, map_provider=None).to_html("geopandas_integration.html", css_background_color="cornflowerblue", open_browser=True) |
0 commit comments