> Every page of the ganymede website is listed in https://eugo.io/docs/ganymede/llms.txt.

# Spatial Optimization (spopt)

> Spatial Optimization (spopt): An open-source Python library for solving optimization problems with spatial data

Web page: https://eugo.io/docs/ganymede/demo_notebooks/geospatial/pysal/spopt/
Notebook: https://eugo.io/docs/ganymede/demo_notebooks/geospatial/pysal/spopt/spopt.ipynb (source: https://github.com/eugo-inc/ganymede/blob/main/demo_notebooks/geospatial/pysal/spopt.ipynb)

Description:
- An open-source Python library for solving optimization problems with spatial data

Resources:
- [GitHub] https://github.com/pysal/spopt
- [Tutorials] https://pysal.org/spopt/tutorials.html

## Example 1

Regionalization

```python
import spopt, libpysal, geopandas, numpy

mexico = geopandas.read_file(libpysal.examples.get_path("mexicojoin.shp"))
mexico["count"] = 1

attrs = [f"PCGDP{year}" for year in range(1950, 2010, 10)]
w = libpysal.weights.Queen.from_dataframe(mexico)
mexico["count"], threshold_name, threshold, top_n = 1, "count", 4, 2
numpy.random.seed(123456)

model = spopt.region.MaxPHeuristic(mexico, w, attrs, threshold_name, threshold, top_n)
model.solve()

mexico["maxp_new"] = model.labels_

mexico.plot(column="maxp_new", categorical=True, figsize=(12,8), ec="w");
```

<figure class="gn-output"><img src="https://eugo.io/docs/ganymede/demo_notebooks/geospatial/pysal/spopt/output-3-1.png" width="1008" height="616" alt="Figure drawn by code cell 1" loading="lazy" decoding="async"></figure>
