citygrid
Zone classification, road generation, pathfinding, and urban analytics for a grid of axis-aligned rectangular buildings.
Originally built for a physical LEGO-brick smart-city simulator, extracted into a standalone library because the underlying logic — "given a bunch of rectangles on a grid, classify zones, connect them with roads, and score the layout" — is generic enough to be useful for city-builder games, urban planning teaching tools, and GIS prototyping.
Zero required dependencies — pure Python, math/heapq/dataclasses only.
Install
pip install citygrid
What it does
Zoning (compute_zones) — connected-component BFS groups nearby
buildings into blocks, then classifies each block as Residential,
Commercial, Industrial, or Mixed based on height/footprint mix; empty
areas become Green space.
Roads (generate_roads) — two-phase A* generates a closed-circuit
road network along zone-block faces, connecting every block without
cutting through building footprints.
Pathfinding (pathfind) — shortest walkable route between two
buildings by ID, with real-world distance in meters.
Analytics (compute_all and friends) — population & per-capita
resource use, walkability score (Dijkstra distance-to-amenity), zone
balance flags (unemployment risk, ghost town, dormitory suburb, etc.),
crucial-service coverage (hospitals/schools/fire/police), an SDG
green-space score, a carbon-neutrality timeline, and rooftop solar
potential.
Usage
from citygrid import compute_zones, generate_roads, compute_all, GridConfig
# buildings: list of {id, grid_x, grid_y, width_studs, depth_studs,
# height_bricks, building_type (optional)}
buildings = [
{"id": 1, "grid_x": 0, "grid_y": 0, "width_studs": 2, "depth_studs": 2, "height_bricks": 6},
{"id": 2, "grid_x": 3, "grid_y": 0, "width_studs": 2, "depth_studs": 2, "height_bricks": 6},
{"id": 3, "grid_x": 0, "grid_y": 3, "width_studs": 2, "depth_studs": 2, "height_bricks": 1},
]
zones = compute_zones(buildings)
roads = generate_roads(buildings)
stats = compute_all(buildings, zones)
print(stats["population"]) # {'total_pop': ..., 'density': ..., 'energy': ..., ...}
print(stats["walkability"]) # {'score': ..., 'avg_dist': ..., 'unreachable': ...}
Custom grid size / thresholds
Every constant — grid dimensions, per-capita resource use, zone-balance
thresholds, walkability distance cap, and more — lives in one overridable
GridConfig:
from citygrid import GridConfig, compute_all
config = GridConfig(
grid_width=32, grid_height=32, # smaller board
stud_meters=5.0, # different real-world scale
walk_max_meters=800, # stricter walkability target
)
stats = compute_all(buildings, zones, config)
If you don't pass a config, every function uses GridConfig()'s
defaults (a 64x64 grid at 10 real-world meters per cell — the values this
library was originally tuned against).
Development
pip install -e ".[dev]"
pytest
License
MIT
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file citygrid-0.1.0.tar.gz.
File metadata
- Download URL: citygrid-0.1.0.tar.gz
- Upload date:
- Size: 16.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.14.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6db2c0a7b11319d976e6c442f657aabd346b91374a8f1d6a36a0ffb5bbd44ae9
|
|
| MD5 |
e113228dff5bc6eb2e7b57b2fa55aa10
|
|
| BLAKE2b-256 |
b89224ff5fce89dcc88665d2b402710f98ee5c569494ad5a714428ffeb5f8d2f
|
File details
Details for the file citygrid-0.1.0-py3-none-any.whl.
File metadata
- Download URL: citygrid-0.1.0-py3-none-any.whl
- Upload date:
- Size: 16.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/6.2.0 CPython/3.14.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
674b020ecfdf3ac4ddee4b1f541860c4ef6fbba4883d162aa0a7bbdb13510601
|
|
| MD5 |
2123b9576f61a6c9d75b2a01a6d15436
|
|
| BLAKE2b-256 |
c8dcd7c62df41b13b39f5ac7b36dbcfcaae8f7a68b1fa11ab385c2e3051a89b9
|