A powerful tool to simulate shadow geometries from 2D building footprints (as GeoJSON) using sun position (via direct angles or date-time). Supports elevation-based shadow generation and handles Polygon
and MultiPolygon
types.
npm install geojson-shadow-generator
or with yarn:
yarn add geojson-shadow-generator
or with vanillajs
https://cdn.jsdelivr.net/npm/geojson-shadow-generator/bundle.js
-
Calculates shadow geometries based on:
- Direct sun azimuth/elevation angles.
- Automatic sun position using date-time and GeoJSON center.
-
Validates input GeoJSON (
Feature
orFeatureCollection
). -
Supports
Polygon
andMultiPolygon
geometry types. -
Uses
@turf
andsuncalc
libraries under the hood. -
Returns output as a valid GeoJSON
FeatureCollection
orFeature
.
const GeoJSONShadowGenerator = require('geojson-shadow-generator');
const geojson = require('./yourGeoJSON.json');
const generator = new GeoJSONShadowGenerator(geojson, 'elevation');
// Using direct sun angles
const shadows1 = generator.getShadowGeometries({
type: 'direct',
sunAzimuth: 135, // degrees from north, clockwise
sunElevation: 30 // degrees above horizon
});
// Using date-time and automatic sun position
const shadows2 = generator.getShadowGeometries({
type: 'datetime',
date: '2025-06-01T10:00:00Z'
});
new GeoJSONShadowGenerator(geojson: FeatureCollection | Feature, elevationPropertyName?: string)
Parameter | Type | Required | Description |
---|---|---|---|
geojson |
GeoJSON |
✅ | Input Feature or FeatureCollection with Polygon or MultiPolygon geometries. |
elevationPropertyName |
string |
❌ | Property name in feature properties that holds the elevation value. Default is 'elevation' . |
Generates shadow geometries.
Key | Type | Required | Description |
---|---|---|---|
type |
'direct' | 'datetime' |
❌ | Type of shadow calculation. Default: 'direct' . |
sunAzimuth |
number |
Required if type === 'direct' |
Azimuth angle (degrees from north, clockwise). |
sunElevation |
number |
Required if type === 'direct' |
Sun elevation angle (degrees above horizon). |
date |
string (ISO format) |
Required if type === 'datetime' |
ISO date-time string used to calculate sun position from GeoJSON center. |
- Input must be a valid GeoJSON Feature or FeatureCollection.
- Geometry type must be Polygon or MultiPolygon.
- Each feature must have a numeric
elevation
property (or custom property if set via constructor).
Example of a valid feature:
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[27.09, 38.45],
[27.10, 38.45],
[27.10, 38.46],
[27.09, 38.46],
[27.09, 38.45]
]
]
},
"properties": {
"elevation": 50
}
}
- Uses
suncalc
to compute sun azimuth and elevation from date and latitude/longitude. - Projects coordinates to EPSG:3857 to perform shadow translation in meters.
- Offsets geometry using the solar vector and elevation.
- Combines the original and translated geometries to form shadows.
- Uses Turf's
convex
,difference
, andunion
functions.
Example test case:
const test = new GeoJSONShadowGenerator(geojson, 'elevation');
const result1 = test.getShadowGeometries({
type: 'datetime',
date: '2025-05-31T14:30:00'
});
const result2 = test.getShadowGeometries({
type: 'direct',
sunAzimuth: 50,
sunElevation: 45
});
The returned result is a GeoJSON FeatureCollection
that represents the shadow geometries of the input features.
This package uses the following libraries:
@turf/union
@turf/difference
@turf/center
@turf/convex
@turf/helpers
geojson-validation
proj4
suncalc
MIT © 2025 - Ali Kilic | https://akilic.com
You can visualize the shadow outputs using:
- geojson.io
- Kepler.gl
- openlayers.org
- gislayer.com
- Or web map libraries like Leaflet, Mapbox GL, or OpenLayers.