Skip to content

Spatial indexing of GeoAgents #104

@wang-boyu

Description

@wang-boyu

What's the problem this feature will solve?

Mesa-Geo uses RTree for spatial indexing of GeoAgents. When such indexing is not necessary (e.g., no neighborhood queries are needed), it could be a performance bottleneck, especially if there's a huge number of GeoAgents. See wang-boyu/agents-and-networks-in-python#2.

On the other hand, when indexing is actually needed, it happens only when adding agents:

def add_agents(self, agents):
"""Add a list of GeoAgents to the layer without checking their crs.
GeoAgents must have the same crs to avoid incorrect spatial indexing results.
To change the crs of a GeoAgent, use `GeoAgent.to_crs()` method. Refer to
`GeoSpace._check_agent()` as an example.
This function may also be called with a single GeoAgent.
Args:
agents: List of GeoAgents, or a single GeoAgent, to be added into the layer.
"""
if isinstance(agents, GeoAgent):
agent = agents
self.idx.insert(id(agent), agent.geometry.bounds, None)
self.idx.agents[id(agent)] = agent
else:
self._recreate_rtree(agents)

Whenever agents move and need re-index, users need to manually call GeoSpace._recreate_rtree(). For example:

self.space._recreate_rtree() # Recalculate spatial tree, because agents are moving

Describe the solution you'd like

  • Do not perform spatial indexing when it is not needed.
  • When spatial indexing is needed, automatically keep track of agents' movements and re-index accordingly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementRelease notes labelsprintA task that might be good to tackle during sprints!

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions