Metadata-Version: 2.5
Name: sitecontext
Version: 0.1.0
Summary: 3D site context for any building model from an address: OpenStreetMap roads and buildings, county parcels and USGS terrain, registered to the lot and exported to glTF and Google Earth.
Project-URL: Homepage, https://github.com/sumanthreddyc/sitecontext
Project-URL: Issues, https://github.com/sumanthreddyc/sitecontext/issues
Author: Sumanth Reddy Challaram
License-Expression: MIT
License-File: LICENSE
Keywords: aec,bim,gis,gltf,google-earth,kml,openstreetmap,parcels,site-analysis,terrain
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Topic :: Multimedia :: Graphics :: 3D Modeling
Classifier: Topic :: Scientific/Engineering :: GIS
Requires-Python: >=3.10
Requires-Dist: numpy>=1.24
Requires-Dist: shapely>=2.1
Provides-Extra: all
Requires-Dist: matplotlib>=3.7; extra == 'all'
Requires-Dist: pillow>=10; extra == 'all'
Requires-Dist: scipy>=1.10; extra == 'all'
Provides-Extra: dev
Requires-Dist: matplotlib>=3.7; extra == 'dev'
Requires-Dist: pillow>=10; extra == 'dev'
Requires-Dist: pytest>=8; extra == 'dev'
Requires-Dist: ruff>=0.6; extra == 'dev'
Requires-Dist: scipy>=1.10; extra == 'dev'
Provides-Extra: fit
Requires-Dist: scipy>=1.10; extra == 'fit'
Provides-Extra: plot
Requires-Dist: matplotlib>=3.7; extra == 'plot'
Provides-Extra: terrain
Requires-Dist: pillow>=10; extra == 'terrain'
Description-Content-Type: text/markdown

# sitecontext

**3D site context for any building model, from an address and open public data.**

Give it an address. It downloads the roads and buildings around it from OpenStreetMap, the parcels from the county,
and the terrain from USGS, then builds a 3D context model as glTF. If you have a building model, it fits the model
onto its lot from the property lines, so the context lands in your model's own coordinates. It can also place the
model in Google Earth Pro at the right position, height and heading.

![Pacific Palisades, 250 m around the public library: standing buildings in white, buildings destroyed in the January 2025 fire (as mapped in OpenStreetMap) in orange, on USGS terrain](https://raw.githubusercontent.com/sumanthreddyc/sitecontext/main/docs/img/palisades-orbit.gif)

<sub>250 m around the Palisades Branch Library, Los Angeles. White: standing buildings. Orange: buildings destroyed in the
January 2025 Palisades fire, as tagged in OpenStreetMap. Built in about a second from open data, rendered with the
included Blender script.</sub>

## Why

Every architecture, engineering or construction project starts with "what is around the site?" The usual answer is
a paid data service, a day of hand modelling, or a screenshot from Google Maps. For most early design, coordination
and client work you need something simpler: the street, the neighbours, the slope and the lot, in the same
coordinates as your model. That is what this tool builds.

## What you get

| Output | What it is |
| --- | --- |
| `context.glb` | Terrain (USGS 3DEP), roads and paths at their real widths, draped on a smoothed profile, OSM buildings at their tagged heights, destroyed buildings as bare pads. Every mesh is named and carries its address, parcel, height and source. Opens in Blender, Rhino, three.js, Unreal, Unity, Windows 3D Viewer and anything else that imports glTF. |
| `check.png` | Top-down plot of the context, every parcel, your lot and your property lines, for checking the registration. |
| `site.kmz` | Your model (and optionally the context) placed in Google Earth Pro at the correct latitude, longitude, altitude and heading. |
| `context.json` | Counts, the datum, and the offset between OSM and the parcels. |

<p align="center"><img src="https://raw.githubusercontent.com/sumanthreddyc/sitecontext/main/docs/img/palisades-3d.jpg" width="49%"> <img src="https://raw.githubusercontent.com/sumanthreddyc/sitecontext/main/docs/img/palisades-check.jpg" width="40%"></p>

## Install

```bash
pip install "sitecontext[all]"
```

Python 3.10+. The core needs only `numpy` and `shapely`. The extras add `scipy` (lot fitting), `pillow` (terrain) and
`matplotlib` (check plot).

## Quick start

```bash
sitecontext init palisades --address "861 Alma Real Dr, Pacific Palisades, CA 90272" --radius 250 --parcels la-county
sitecontext fetch palisades      # OSM, parcels, terrain, geoid heights  (~1 min, cached)
sitecontext build palisades      # -> palisades/context.glb
sitecontext check palisades      # -> palisades/check.png
```

That gives you the context in local east/north metres, with z = 0 at the ground at the address. To put it in your
model's coordinates, register the model.

## Registering your model

The context is most useful in your model's own coordinate system. There are two ways to tie the two together.

**Fit to the lot (recommended).** Read the property lines off your site plan, in model coordinates, and give the lot's
parcel number:

```json
{
  "parcels": {"source": "la-county", "lot_id": "<the lot's parcel number>"},
  "model": {
    "units": "ft",
    "property_lines": [[[0, 0], [62.5, 0]], [[62.5, 0], [64.1, 141.2]], [[0, 0], [-1.8, 139.7]]],
    "z0_elevation": 652.0
  }
}
```

```bash
sitecontext fit mysite
# prints the bearing of model +Y, the anchor, and the median / max distance of each line from the parcel
```

The fit is rigid (rotation and translation, no scale). A coarse search runs over every bearing, followed by a
robust ICP against the exact parcel boundary, so one misread line does not pull the result off. Read the residuals.
A line more than about 0.3 m off the parcel is usually a fence or setback line read as the property line, or the
wrong parcel. Fix the input rather than accepting the fit. Use lines you are sure of: the front and both sides are
enough. Don't trust the sheet's north arrow; it is often several degrees off, which is why the fit uses the parcel.

**By hand.** If you know the bearing of your model's +Y axis and where one point is:

```bash
sitecontext register mysite --bearing 23.4 --anchor 0,0 --at 34.04431,-118.52487
```

After registering, `build` writes the context in model coordinates (metres, z up, z = 0 at `model.z0_elevation`).
Import it at the origin and it sits around your building.

### Other options in `site.json`

| Key | Meaning |
| --- | --- |
| `model.cutout` | Polygons (model units) where the context is cut out: your modelled site, so terrain never pokes through it. A ground skirt is added down the cut edge. `build --cut-lot` cuts out the whole lot. |
| `model.street` | `{"osm_name": "Alma Real Drive", "centerline": [[x, y], ...]}`. Shifts OSM roads onto your model's street. OSM is traced from imagery and is often a few metres off the parcels. `earth` reuses this offset to line the model up with the aerial photo. |
| `context.default_building_height_m` | Height for OSM buildings without `height` or `building:levels` (default 6 m). |
| `terrain` | `"flat"` outside the US, or when you don't want terrain. |
| `parcels.url`, `id_field`, `address_fields` | Any county's ArcGIS parcel layer, instead of a preset. |

## Google Earth

```bash
sitecontext earth mysite --model my_house.glb --name "My house"
```

Export the model as a `.glb` in the same frame you registered (metres). Open the KMZ in Google Earth Pro and untick
**Layers > 3D Buildings**: Google's photo mesh shows what stands on the site today. A few details are handled for you:

- **Height.** USGS terrain is in NAVD88, while Google Earth's `absolute` altitude is EGM96 sea level. The difference
  (about 0.4 m in Los Angeles) is looked up for the site from NOAA GEOID18 and EGM96.
- **Heading.** The KML model heading is set to the bearing of model +Y.
- **Transparency.** Earth Pro reads COLLADA transparency inverted, so opaque materials are written without it.
- **Mesh size.** Large meshes are split at 60k vertices.

Google Earth on the web does not show KML 3D models; use Earth Pro.

## Other counties

Parcels come from any ArcGIS REST layer; most US counties publish one. Find the county's parcel map service, then:

```json
"parcels": {"url": "https://.../MapServer/0/query", "id_field": "APN", "address_fields": ["SITUS_ADDR"], "lot_id": "..."}
```

Pull requests adding verified presets are welcome (see [CONTRIBUTING](https://github.com/sumanthreddyc/sitecontext/blob/main/CONTRIBUTING.md)). Without parcels everything
except `fit` still works.

## Limitations

- Terrain comes from USGS 3DEP, so the **United States** only. Elsewhere use `"terrain": "flat"`; a global DEM source
  is on the roadmap.
- Neighbouring buildings are flat-roofed footprint prisms. They are only as good as OpenStreetMap, and footprints
  may predate recent rebuilds.
- Trees, walls and driveways are not modelled.
- Equirectangular local frame: accurate over the ~1 km a site context covers, not for city-scale areas.

## Data and attribution

Everything is downloaded from public services. Only the address or coordinates are sent, never project or owner
names. When you share outputs, credit the sources:

- Map data © [OpenStreetMap contributors](https://www.openstreetmap.org/copyright), available under the
  [Open Database License](https://opendatacommons.org/licenses/odbl/). Outputs derived from OSM must keep this
  attribution.
- Elevation: [USGS 3D Elevation Program](https://www.usgs.gov/3d-elevation-program) (public domain).
- Parcels: the county's terms apply (e.g. LA County Assessor open data).
- Geoid heights: [NOAA NGS GEOID18](https://geodesy.noaa.gov/GEOID/GEOID18/) and
  [GeographicLib EGM96](https://geographiclib.sourceforge.io/).
- Geocoding: [Nominatim](https://nominatim.org/), within its usage policy (1 request per second, identified client).

## Roadmap

- Global terrain (Copernicus GLO-30)
- IFC input for `earth` and an IFC context export
- Roof shapes from OSM `roof:*` tags, and trees from OSM
- More verified county parcel presets

## Contributing

Issues and pull requests are welcome; see [CONTRIBUTING.md](https://github.com/sumanthreddyc/sitecontext/blob/main/CONTRIBUTING.md). If you use sitecontext on a real
project, I'd like to hear how it went.

## License

[MIT](https://github.com/sumanthreddyc/sitecontext/blob/main/LICENSE) © Sumanth Reddy Challaram
