CartaTerra

← Guides & help

Python on the map

7 min read

Python gives you each layer on your map as a GeoDataFrame, the table-plus-shapes type from the geopandas library. You can filter, measure, buffer and chart with pandas, geopandas and matplotlib, and save a result as a new layer. Python runs in your browser (Pyodide, Python 3.14 built for the web), walled off from the page, so your code can't see your account or the rest of CartaTerra. It opens from the side rail into the bottom dock, and is part of Pro (see plans).

Where to find Python

  1. Sign in with Pro. Data tools on the map covers the rail and bottom dock.
  2. Add the layers you want to work with. Put them on your map first, from Add layer or with Upload.
  3. Press Python on the side rail, in the data tools section (Tables, SQL, Python, R). If you've hidden it under + › Show on rail, turn it back on there.

Python opens in the bottom dock, as a tab beside Tables, SQL and R. Opening it stacks with tabs already there. Press Python again while that tab is showing to close it, or use the small × on the tab. The − at the right end of the tab row minimizes the whole dock and keeps your code and results, and the Python rail button brings it back. Map settings stays the button directly above + and opens the full settings; it is not in this dock.

Python loads on your first run

Python isn't part of the page. The first time you press Run, your browser loads Python itself, about 6 MB, plus only the packages your code uses:

While it loads, the panel shows a bar and how far along it is, such as "Loading Python… 12 of 19 MB". Your browser keeps what it loaded on this device, so the next run, even after a reload, starts without loading it again. If loading fails, you'll see "Couldn't load Python. Check your connection and try again."

Your layers: layers["name"]

The Examples chips under the box fill in code for the layer you picked: First rows, Buffer 1 mile, Area in sq mi (for area layers) and Bar chart (when the layer has a field to count). Nothing runs until you press Run. Your code is kept in this browser, so it's still there next time.

Show results: print, show and charts

A run that shows nothing says "Done. Nothing to show: use show(df), print() or add_layer()."

Save a result as a layer: add_layer

add_layer(gdf, "name") offers a GeoDataFrame as a new layer. It shows under Results with its feature count and a Save as layer button. Saving adds the layer to your map and opens it in Tables.

Once it's a layer, Export… at the top of Tables (or Export map… in the map menu) saves it as Shapefile, GeoPackage, CSV, Excel, KML/KMZ, GeoJSON or GPX.

Worked examples

Examples 1 and 2 use a layer of your own called Pins. Examples 3 to 5 use built-in layers: add National Park Service Sites, Hospitals and States (Census) to your map first.

1. Look at a layer.

g = layers["pins"]
print(len(g), "features")
show(g.head(20), "Pins")

2. Buffer by a real distance. Distances in longitude and latitude are in degrees, so project first. EPSG:5070 is an equal-area projection for the lower 48 states, in meters:

g = layers["pins"]
b = g.to_crs(5070).buffer(1609.344).to_crs(4326)
add_layer(gpd.GeoDataFrame(g.drop(columns="geometry"), geometry=b, crs=4326), "Pins 1 mile buffer")

3. Area in square miles, largest first.

g = layers["national_park_service_sites"].copy()
g["sq_mi"] = (g.to_crs(5070).area / 2_589_988.11).round(2)
top = g.sort_values("sq_mi", ascending=False)
show(top.filter(items=["name", "sq_mi"]), "Largest park units")

4. Count points inside each area, and map it. Hospitals per state:

pts, areas = layers["hospitals"], layers["states_census"]
j = gpd.sjoin(pts, areas, predicate="within")
counts = j.groupby("index_right").size()
areas = areas.assign(hospitals=counts.reindex(areas.index, fill_value=0))
add_layer(areas, "Hospitals per state")

Save it, then use Style to color the states by hospitals.

5. A bar chart of a field. The 15 states with the most hospitals:

import matplotlib.pyplot as plt
layers["hospitals"]["state"].value_counts().head(15).plot.bar(title="Hospitals by state")
plt.tight_layout()

Common errors

Related: SQL querying · Data tools on the map · Upload and export map files · All guides