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.2.0.tar.gz (25.4 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.2.0-py3-none-any.whl (20.1 kB view details)

Uploaded Python 3

File details

Details for the file citygrid-0.2.0.tar.gz.

File metadata

  • Download URL: citygrid-0.2.0.tar.gz
  • Upload date:
  • Size: 25.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.5

File hashes

Hashes for citygrid-0.2.0.tar.gz
Algorithm Hash digest
SHA256 ea12ca751cb5aeb96e7cc1ea5698d32700329f79426c3ac78d2bedcc74bbec3a
MD5 cc410fa801dba75e90e42b8f7d3fcbf0
BLAKE2b-256 82507e739bd01144eb80e9ca79e12bf943afa77ef60b442daaca982da4c6a56c

See more details on using hashes here.

File details

Details for the file citygrid-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: citygrid-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 20.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.14.5

File hashes

Hashes for citygrid-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 040084e18fa68a29764b912416d574ca079974ad89f89a1e55eac66f2e1890ce
MD5 04b2a2f03a86d9f63922ac0fb216723e
BLAKE2b-256 8a2a45d160054dc962d0dc71441ca514d8141560a7456bcdf704d5e9cbdf796a

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 files

0.1.0

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