# geojson

GeoJSON-to-Three.js geometry builders. Each class converts GeoJSON `Feature<Polygon | MultiPolygon>` arrays into a `SubsetGeometry` with position, color, and feature-id attributes ready for GPU rendering.

---

## Geometry classes

All classes extend `FeatureGeometry` (itself a `SubsetGeometry`).

```typescript
import {
    GeoJSONFillGeometry,
    GeoJSONFillExtrudeGeometry,
    GeoJSONEdgeGeometry,
    GeoJSONEdgeExtrudeGeometry,
} from '@yaoc1996/js-tools/three-utils'
```

---

### `GeoJSONFillGeometry`

Flat polygon fill — one triangle mesh per polygon ring (triangulated via `earcut`).

```typescript
new GeoJSONFillGeometry(features, options?)
```

### `GeoJSONFillExtrudeGeometry`

Extruded polygon fill — adds a top face and closed side walls.

```typescript
new GeoJSONFillExtrudeGeometry(features, options?)
```

### `GeoJSONEdgeGeometry`

Polygon outline — one `LineSegments`-compatible index buffer per ring.

```typescript
new GeoJSONEdgeGeometry(features, options?)
```

### `GeoJSONEdgeExtrudeGeometry`

Extruded polygon outline — base edges, top edges, and vertical pillars.

```typescript
new GeoJSONEdgeExtrudeGeometry(features, options?)
```

---

## Common options

All constructors accept the same `GeometryOptions` bag as a second argument:

| Name | Type | Default | Description |
|------|------|---------|-------------|
| `defaultColor` | `number` | `0` | Hex color applied to all features unless overridden |
| `projection` | `{ forward([lon, lat]): [x, y] }` | — | Coordinate projection function |
| `translate` | `[number, number]` | `[0, 0]` | XZ translation applied after projection |
| `getBaseHeight` | `(f, i, arr) => number` | `() => 0` | Per-feature base Y elevation |
| `getExtrudeHeight` | `(f, i, arr) => number` | `() => 0` | Per-feature extrusion height (extrude variants only) |
| `getColor` | `(f, i, arr) => number` | — | Per-feature color override (fill variants only) |

### Example

```typescript
import { GeoJSONFillExtrudeGeometry } from '@yaoc1996/js-tools/three-utils'

const geo = new GeoJSONFillExtrudeGeometry(features, {
    getBaseHeight: (f) => f.properties.base_height ?? 0,
    getExtrudeHeight: (f) => f.properties.height ?? 10,
    projection: proj4('EPSG:4326', 'EPSG:3857'),
})

// show only a subset of features
geo.addId(42)
geo.addAllIds()
geo.clearIds()
```

### Notes

- Feature ids start at `1`. Id `0` is reserved for "no feature".
- The `SubsetGeometry` methods (`addId`, `removeId`, `addAllIds`, `clearIds`, `hasId`, `getIds`) are available on every geometry instance.
