Geo — Vector
Vector substrate classes and utilities backed by GeoDataFrame.
dissmodel.geo.vector.spatial_model
dissmodel/geo/spatial_model.py
Classe base para modelos com suporte a GeoDataFrame e vizinhança.
Responsabilidade
Prover infraestrutura espacial — gdf, criação de vizinhança, acesso a vizinhos — sem impor nenhum contrato de regra de transição.
Hierarquia
Model (dissmodel.core)
└── SpatialModel ← este arquivo
├── CellularAutomaton gdf + rule(idx) por célula (pull)
└── (modelos livres) gdf + execute() orientado a fonte (push)
Por que separar
CellularAutomaton.rule(idx) assume que cada célula calcula seu próprio novo estado de forma independente (modelo pull). Modelos orientados a FONTE — como o Hidro do BR-MANGUE — modificam vizinhos a partir da fonte (modelo push). Eles precisam da infraestrutura espacial mas não podem usar o contrato de rule().
SpatialModel fornece: - self.gdf GeoDataFrame compartilhado - create_neighborhood() constrói _neighs via libpysal ou dict - neighs_id(idx) lista de índices vizinhos (cache) - neighs(idx) GeoDataFrame dos vizinhos - neighbor_values(idx, col) array numpy dos valores vizinhos
Uso
class MeuModelo(SpatialModel):
def __init__(self, gdf, meu_param=1.0, **kwargs):
super().__init__(gdf, **kwargs)
self.meu_param = meu_param
self.create_neighborhood()
def execute(self):
nivel = self.env.now() * self.meu_param
# lógica livre — orientada a fonte, por grupo, etc.
SpatialModel
Bases: Model
Model com suporte a GeoDataFrame e vizinhança.
Subclasse de Model que acrescenta infraestrutura espacial sem impor contrato de regra de transição. Pode ser herdada diretamente por modelos com execute() livre (push/fonte) ou indiretamente via CellularAutomaton (pull/rule).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gdf
|
GeoDataFrame
|
Grade ou polígonos da simulação. |
required |
step
|
float
|
Time increment per execution step, by default 1. |
1
|
start_time
|
float
|
Simulation start time, by default 0. |
0
|
end_time
|
float
|
Simulation end time, by default |
inf
|
name
|
str
|
Optional model name, by default |
''
|
**kwargs
|
Any
|
Extra keyword arguments forwarded to :class: |
{}
|
Source code in dissmodel/geo/vector/spatial_model.py
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
create_neighborhood(strategy=Queen, neighbors_dict=None, **kwargs)
Constrói e anexa a estrutura de vizinhança ao GeoDataFrame.
Popula gdf["_neighs"] com a lista de índices vizinhos por célula.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategy
|
type
|
Libpysal weight class (e.g. |
Queen
|
neighbors_dict
|
dict or str
|
Precomputed |
None
|
**kwargs
|
Any
|
Extra keyword arguments forwarded to the strategy. |
{}
|
Source code in dissmodel/geo/vector/spatial_model.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |
neighbor_values(idx, col)
Return the values of col for all neighbors of cell idx.
Faster than neighs(idx)[col] because it skips geometry overhead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
idx
|
any
|
Index of the cell in the GeoDataFrame. |
required |
col
|
str
|
Column name to retrieve. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of neighbor values. |
Source code in dissmodel/geo/vector/spatial_model.py
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
neighs(idx)
Return the neighboring cells of idx as a GeoDataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
idx
|
any
|
Index of the cell in the GeoDataFrame. |
required |
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
GeoDataFrame containing the neighboring rows. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the neighborhood has not been created yet. |
Source code in dissmodel/geo/vector/spatial_model.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | |
neighs_id(idx)
Return the neighbor indices for cell idx.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
idx
|
any
|
Index of the cell in the GeoDataFrame. |
required |
Returns:
| Type | Description |
|---|---|
list
|
List of neighbor indices. |
Source code in dissmodel/geo/vector/spatial_model.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
dissmodel.geo.vector.cellular_automaton
dissmodel/geo/cellular_automaton.py
Base class for spatial cellular automata backed by a GeoDataFrame.
Extends :class:~dissmodel.geo.spatial_model.SpatialModel with a
cell-by-cell transition rule loop.
For source-oriented (push) models that cannot use rule(idx), inherit
:class:~dissmodel.geo.spatial_model.SpatialModel directly and implement
execute() freely.
CellularAutomaton
Bases: SpatialModel, ABC
Base class for spatial cellular automata backed by a GeoDataFrame.
Extends :class:~dissmodel.geo.spatial_model.SpatialModel with
neighborhood management and a cell-by-cell transition rule loop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gdf
|
GeoDataFrame
|
GeoDataFrame with geometries and a state attribute. |
required |
state_attr
|
str
|
Column name representing the cell state, by default |
'state'
|
step
|
float
|
Time increment per execution step, by default 1. |
1
|
start_time
|
float
|
Simulation start time, by default 0. |
0
|
end_time
|
float
|
Simulation end time, by default |
inf
|
name
|
str
|
Optional model name, by default |
''
|
dim
|
tuple of int
|
Grid dimensions as |
None
|
**kwargs
|
Any
|
Extra keyword arguments forwarded to
:class: |
{}
|
Examples:
>>> class MyCA(CellularAutomaton):
... def rule(self, idx):
... return self.gdf.loc[idx, self.state_attr]
Source code in dissmodel/geo/vector/cellular_automaton.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |
execute()
Execute one simulation step by applying :meth:rule to every cell.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the neighborhood has not been created yet. |
Notes
Because :meth:rule is an arbitrary Python function, the update
cannot be vectorized automatically. Performance-critical subclasses
should prefer :meth:neighbor_values over :meth:neighs inside
rule to avoid geometry overhead on every lookup.
Source code in dissmodel/geo/vector/cellular_automaton.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |
initialize()
Set up the initial model state.
Override in subclasses to define the starting conditions.
Source code in dissmodel/geo/vector/cellular_automaton.py
80 81 82 83 84 85 86 | |
rule(idx)
abstractmethod
Transition rule applied to each cell.
Must be overridden in subclasses to define the state transition logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
idx
|
any
|
Index of the cell being evaluated. |
required |
Returns:
| Type | Description |
|---|---|
any
|
New state value for the cell. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If not overridden by the subclass. |
Source code in dissmodel/geo/vector/cellular_automaton.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | |
dissmodel.geo.vector.vector_grid
vector_grid(gdf=None, bounds=None, resolution=None, dimension=None, attrs=None, crs=None)
Create a regular grid of fixed-size cells.
Exactly one of the following input combinations must be provided:
dimension+resolution— abstract grid with no geographic locationbounds+resolution— grid fitted to a bounding box by cell sizebounds+dimension— grid fitted to a bounding box by cell countgdf— bounds are extracted from the GeoDataFrame
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gdf
|
GeoDataFrame
|
GeoDataFrame used to extract the bounding box. |
None
|
bounds
|
tuple of float
|
Bounding box as |
None
|
resolution
|
float
|
Cell size in coordinate units. |
None
|
dimension
|
tuple of int
|
Grid shape as |
None
|
attrs
|
dict
|
Extra attributes added to every cell, e.g. |
None
|
crs
|
str or int
|
Coordinate reference system. If |
None
|
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
Regular grid where each row is a cell with a Polygon geometry,
indexed by |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input combination is insufficient to define the grid. |
Examples:
>>> gdf = vector_grid(dimension=(3, 3), resolution=1.0)
>>> len(gdf)
9
>>> gdf.index[0]
'0-0'
Source code in dissmodel/geo/vector/vector_grid.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |
parse_idx(idx)
Extract row and col from an index string in 'row-col' format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
idx
|
str
|
Index string in |
required |
Returns:
| Type | Description |
|---|---|
GridPos
|
Named tuple with fields |
Examples:
>>> pos = parse_idx('3-4')
>>> pos.row
3
>>> pos.col
4
>>> row, col = parse_idx('3-4') # tuple unpacking still works
Source code in dissmodel/geo/vector/vector_grid.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | |
dissmodel.geo.vector.fill
FillStrategy
Bases: str, Enum
Available fill strategies for populating GeoDataFrame attributes.
Attributes:
| Name | Type | Description |
|---|---|---|
ZONAL_STATS |
str
|
Fill cells with statistics extracted from a raster. |
MIN_DISTANCE |
str
|
Fill cells with the minimum distance to a target GeoDataFrame. |
RANDOM_SAMPLE |
str
|
Fill cells with random samples drawn from a distribution. |
PATTERN |
str
|
Fill cells using a 2-D pattern matrix. |
Examples:
>>> FillStrategy.RANDOM_SAMPLE
<FillStrategy.RANDOM_SAMPLE: 'random_sample'>
>>> FillStrategy("pattern")
<FillStrategy.PATTERN: 'pattern'>
Source code in dissmodel/geo/vector/fill.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | |
fill(strategy, **kwargs)
Execute a fill strategy by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strategy
|
FillStrategy or str
|
Strategy to execute. Accepted values: |
required |
**kwargs
|
Any
|
Arguments forwarded to the chosen strategy function. |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
Whatever the strategy function returns. Most strategies mutate the
GeoDataFrame in place and return |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
>>> fill(FillStrategy.RANDOM_SAMPLE, gdf=grid, attr="state",
... data=[0, 1], seed=42)
>>> fill("min_distance", from_gdf=grid, to_gdf=roads,
... attr_name="dist_road")
>>> fill(FillStrategy.PATTERN, gdf=grid, attr="zone",
... pattern=[[1, 2], [3, 4]])
Source code in dissmodel/geo/vector/fill.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | |
register_strategy(name)
Register a fill strategy under the given name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
FillStrategy
|
Key under which the strategy will be registered. |
required |
Returns:
| Type | Description |
|---|---|
Callable
|
Decorator that registers and returns the decorated function. |
Source code in dissmodel/geo/vector/fill.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
dissmodel.geo.vector.neighborhood
attach_neighbors(gdf, strategy=None, neighbors_dict=None, **kwargs)
Attach a neighborhood structure to a GeoDataFrame.
Adds a '_neighs' column containing the list of neighbor indices for
each cell. Mutates and returns the same GeoDataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gdf
|
GeoDataFrame
|
GeoDataFrame whose cells will receive the neighborhood column. |
required |
strategy
|
WeightStrategy
|
Libpysal weight class (e.g. |
None
|
neighbors_dict
|
dict or str
|
Precomputed neighborhood. Accepted formats:
|
None
|
**kwargs
|
Any
|
Extra keyword arguments forwarded to |
{}
|
Returns:
| Type | Description |
|---|---|
GeoDataFrame
|
The same |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If a string path is provided in |
ValueError
|
If |
ValueError
|
If neither |
Examples:
>>> from libpysal.weights import Queen
>>> gdf = attach_neighbors(gdf, strategy=Queen)
>>> gdf = attach_neighbors(gdf, neighbors_dict="neighborhood.json")
>>> gdf = attach_neighbors(gdf, strategy=Queen, ids="cell_id")
Source code in dissmodel/geo/vector/neighborhood.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | |