Introductory Tutorial#

Getting started#

You should be familiar with how Mesa works.

So let’s get started with some geometries! We will work with records of US states. We use the requests library to retrieve the data, but of course you can work with local data.

import warnings


import mesa
import mesa_geo as mg
import requests

url = ""
r = requests.get(url)
geojson_states = r.json()

First we create a State Agent and a GeoModel. Both should look familiar if you have worked with Mesa before.

class State(mg.GeoAgent):
    def __init__(self, unique_id, model, geometry, crs):
        super().__init__(unique_id, model, geometry, crs)

class GeoModel(mesa.Model):
    def __init__(self): = mg.GeoSpace()

        ac = mg.AgentCreator(agent_class=State, model=self)
        agents = ac.from_GeoJSON(GeoJSON=geojson_states, unique_id="NAME")

In the GeoModel we first create an instance of AgentCreator, where we provide the Agent class (State) and its required arguments, except geometry and unique_id. We then use the .from_GeoJSON function to create our agents from the geometries in the GeoJSON file. We provide the feature “name” as the key from which the agents get their unique_ids. Finally, we add the agents to the GeoSpace

Let’s instantiate our model and look at one of the agents:

m = GeoModel()

agent =[0]

If you work in the Jupyter Notebook your output should give you the name of the state and a visual representation of the geometry.

By default the AgentCreator also sets further agent attributes from the Feature properties.


Let’s start to do some spatial analysis. We can use usual Mesa function names to get neighboring states.

neighbors =
print([a.unique_id for a in neighbors])
['California', 'Colorado', 'Utah', 'Nevada', 'New Mexico']

To get a list of all states within a certain distance you can use the following:

[a.unique_id for a in, 600000)]
 'New Mexico',

The unit for the distance depends on the coordinate reference system (CRS) of the GeoSpace. Since we did not specify the CRS, Mesa-Geo defaults to the ‘Web Mercator’ projection (in meters). If you want to do some serious measurements you should always set an appropriate CRS, since the accuracy of Web Mercator declines with distance from the equator. We can achieve this by initializing the AgentCreator and the GeoSpace with the crs keyword crs="epsg:2163". Mesa-Geo then transforms all coordinates from the GeoJSON geographic coordinates into the set crs.

Going further#

To get a deeper understanding of Mesa-Geo you should check out the GeoSchelling example. It implements a Leaflet visualization which is similar to use as the CanvasGridVisualization of Mesa.

To add further functionality, I need feedback on which functionality is desired by users. Please post a message at Mesa-Geo discussions or open an issue if you have any ideas or recommendations.