Skip to main content

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

citygrid-0.1.0.tar.gz (16.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

citygrid-0.1.0-py3-none-any.whl (16.6 kB view details)

Uploaded Python 3

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

Hashes for citygrid-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6db2c0a7b11319d976e6c442f657aabd346b91374a8f1d6a36a0ffb5bbd44ae9
MD5 e113228dff5bc6eb2e7b57b2fa55aa10
BLAKE2b-256 b89224ff5fce89dcc88665d2b402710f98ee5c569494ad5a714428ffeb5f8d2f

See more details on using hashes here.

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

Hashes for citygrid-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 674b020ecfdf3ac4ddee4b1f541860c4ef6fbba4883d162aa0a7bbdb13510601
MD5 2123b9576f61a6c9d75b2a01a6d15436
BLAKE2b-256 c8dcd7c62df41b13b39f5ac7b36dbcfcaae8f7a68b1fa11ab385c2e3051a89b9

See more details on using hashes here.

Release history Release notifications | RSS feed

0.2.0

2 files

This release

0.1.0 This release

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page