Python on the map
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
- Sign in with Pro. Data tools on the map covers the rail and bottom dock.
- Add the layers you want to work with. Put them on your map first, from Add layer or with Upload.
- 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:
- Code that uses layers, gpd or geopandas loads pandas, shapely and pyproj too, about 19 MB in all.
- Code that uses only pd or pandas comes to about 14 MB.
- A chart with matplotlib on top of your layers comes to about 28 MB.
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"]
- layers holds one GeoDataFrame per layer on your map, in longitude and latitude (EPSG:4326). Each layer has a short name, the same one SQL uses: lowercase, with spaces and symbols turned into underscores. "Toilets (public lands)" is layers["toilets_public_lands"] and "My Hikes" is layers["my_hikes"]. The full rules are in SQL querying.
- The sidebar name also works when only one layer has it. layers["Toilets (public lands)"] is that same layer. It works in a fresh tab and after you have opened SQL.
print(layers)lists the short name and, when it also works, the sidebar name. - The Layers chips insert the short name, so you don't have to work it out. The chip's own label stays the sidebar name. The short name is the one form that always means one layer: if two layers share a sidebar name, that name does not pick one of them, and the second short name ends in _2 (then _3), in the order the layers are listed, the same as in SQL. A sidebar name that is exactly another layer's short name is that other layer. Use each layer's own short name, which is what its chip inserts.
- Columns are your layer's fields, plus geometry. The app's own fields (such as _id) are left out.
- What's included. Layers with shapes and rows: your own, uploaded and analysis layers, and built-in layers that load in full. Layers from Add from a link and very large built-in layers that the map draws in tiles aren't included. Together the layers can have up to 50,000 features. If yours go over that, the ones that don't fit are left out, and the status line names them.
- A wrong name stops with "No layer named …" followed by the names you can use.
print(layers)lists them too.
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
- print() writes to the console under Results.
- show(df, "title") shows a DataFrame or Series as a table under Results, with the first 200 rows. The shape column is left out of the table.
- Charts. Draw with matplotlib (
import matplotlib.pyplot as plt). Each figure your code makes shows as an image under Results, sized to that pane, up to 6 per run. You don't need plt.show().
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.
- There is no feature-count cap. A result over 8 MB is refused, and the panel says so. Filter or simplify a bigger one first.
- Shapes in another projection are converted back to longitude and latitude for you. A GeoDataFrame with no projection is treated as longitude and latitude.
- add_layer needs a GeoDataFrame. To map plain numbers, keep the geometry column in your result.
- Pro. Saving a result is part of Pro. On another plan, Save as layer shows "Saving Python results is part of Pro."
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
- "Python stopped with an error (below)." The console shows the end of the traceback, starting at your code's line number. Check the line it names.
- "That took longer than a minute, so it was stopped." Each run gets a minute. Try a smaller layer or a simpler step. The next run starts a fresh Python, so anything you set up before is gone. One known slow call is crs.to_epsg(), which can take minutes in the browser. Use
print(g.crs)to see a projection. - "No layer named …" Check the name, or tap its Layers chip (it inserts the short name). The sidebar name works only when one layer has it. If the layer isn't on your map yet, add it, then run again.
- "add_layer() needs a GeoDataFrame" Wrap your result:
gpd.GeoDataFrame(df, geometry=shapes, crs=4326). - "That layer is over 8 MB." / "That layer has no shapes to put on the map." Filter or simplify the result, or keep its geometry column.
- "Python opens from the rail or the bottom dock." Press Python on the rail.
Related: SQL querying · Data tools on the map · Upload and export map files · All guides