Data Export & Source Updates¶
This notebook demonstrates:
- Export to GeoJSON: Get layer data as a GeoJSON dict
- Export to GeoDataFrame: Convert layer data to a GeoDataFrame
- Get Visible Features: Query all features currently visible in the viewport
- Update GeoJSON Source: Update layer data in place for real-time dashboards
In [ ]:
Copied!
# %pip install anymap-ts
# %pip install anymap-ts
Setup: Add GeoJSON Data¶
In [ ]:
Copied!
from anymap_ts import Map
geojson = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-122.4, 37.8]},
"properties": {"name": "San Francisco", "pop": 870000},
},
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-118.2, 34.1]},
"properties": {"name": "Los Angeles", "pop": 3900000},
},
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-73.9, 40.7]},
"properties": {"name": "New York", "pop": 8300000},
},
],
}
m = Map(center=[-98, 38], zoom=3)
m.add_geojson(geojson, name="cities", circle_radius=8, circle_color="#ff6600")
m
from anymap_ts import Map
geojson = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-122.4, 37.8]},
"properties": {"name": "San Francisco", "pop": 870000},
},
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-118.2, 34.1]},
"properties": {"name": "Los Angeles", "pop": 3900000},
},
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-73.9, 40.7]},
"properties": {"name": "New York", "pop": 8300000},
},
],
}
m = Map(center=[-98, 38], zoom=3)
m.add_geojson(geojson, name="cities", circle_radius=8, circle_color="#ff6600")
m
Export to GeoJSON¶
Get layer data as a GeoJSON dict. The first call triggers a query to the JavaScript side. Run it in one cell, then read the result in the next cell (the event loop processes the response between cells).
In [ ]:
Copied!
# Trigger the query (sends request to JavaScript)
m.to_geojson("cities")
# Trigger the query (sends request to JavaScript)
m.to_geojson("cities")
In [ ]:
Copied!
# Read the result (available after the event loop processes the response)
result = m.to_geojson()
print(result)
# Read the result (available after the event loop processes the response)
result = m.to_geojson()
print(result)
Get Visible Features¶
Query all features currently visible in the map viewport. Same two-cell pattern: trigger in one cell, read in the next.
In [ ]:
Copied!
# Trigger the query
m.get_visible_features(layers=["cities"])
# Trigger the query
m.get_visible_features(layers=["cities"])
In [ ]:
Copied!
# Read the result
result = m.get_visible_features()
print(result)
# Read the result
result = m.get_visible_features()
print(result)
Export to GeoDataFrame¶
Requires geopandas to be installed. Uses the same two-cell pattern.
In [ ]:
Copied!
# Trigger the query
m.to_geojson("cities")
# Trigger the query
m.to_geojson("cities")
In [ ]:
Copied!
# Read the result as a GeoDataFrame
gdf = m.to_geopandas()
if gdf is not None:
print(gdf)
# Read the result as a GeoDataFrame
gdf = m.to_geopandas()
if gdf is not None:
print(gdf)
Update GeoJSON Source¶
Update the data of an existing source in place — no need to remove and re-add layers. Essential for real-time/streaming data.
In [ ]:
Copied!
# Update with new data (e.g., add a city)
updated_geojson = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-122.4, 37.8]},
"properties": {"name": "San Francisco", "pop": 870000},
},
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-118.2, 34.1]},
"properties": {"name": "Los Angeles", "pop": 3900000},
},
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-73.9, 40.7]},
"properties": {"name": "New York", "pop": 8300000},
},
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-87.6, 41.9]},
"properties": {"name": "Chicago", "pop": 2700000},
},
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-95.4, 29.8]},
"properties": {"name": "Houston", "pop": 2300000},
},
],
}
m.update_geojson_source("cities", updated_geojson)
# Update with new data (e.g., add a city)
updated_geojson = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-122.4, 37.8]},
"properties": {"name": "San Francisco", "pop": 870000},
},
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-118.2, 34.1]},
"properties": {"name": "Los Angeles", "pop": 3900000},
},
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-73.9, 40.7]},
"properties": {"name": "New York", "pop": 8300000},
},
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-87.6, 41.9]},
"properties": {"name": "Chicago", "pop": 2700000},
},
{
"type": "Feature",
"geometry": {"type": "Point", "coordinates": [-95.4, 29.8]},
"properties": {"name": "Houston", "pop": 2300000},
},
],
}
m.update_geojson_source("cities", updated_geojson)
Custom Image for Symbol Layers¶
Load a custom icon to use in symbol layers.
In [ ]:
Copied!
m2 = Map(center=[-122.4, 37.8], zoom=10)
m2.add_basemap("OpenStreetMap")
m2.add_image(
"custom-marker",
"https://maplibre.org/maplibre-gl-js/docs/assets/custom_marker.png",
)
m2
m2 = Map(center=[-122.4, 37.8], zoom=10)
m2.add_basemap("OpenStreetMap")
m2.add_image(
"custom-marker",
"https://maplibre.org/maplibre-gl-js/docs/assets/custom_marker.png",
)
m2