Represents 3D shapes from a GeoJSON file.
GeoJSON features with Z coordinates can be displayed as 3D shapes using the FeatureCollection
entity.
import { AmbientLight, Color, DirectionalLight, Vector3 } from "three";
import { MapControls } from "three/examples/jsm/controls/MapControls.js";
import GeoJSON from "ol/format/GeoJSON.js";
import { tile } from "ol/loadingstrategy.js";
import VectorSource from "ol/source/Vector.js";
import { createXYZ } from "ol/tilegrid.js";
import Instance from "@giro3d/giro3d/core/Instance.js";
import Coordinates from "@giro3d/giro3d/core/geographic/Coordinates.js";
import Extent from "@giro3d/giro3d/core/geographic/Extent.js";
import FeatureCollection from "@giro3d/giro3d/entities/FeatureCollection.js";
import Inspector from "@giro3d/giro3d/gui/Inspector.js";
function bindToggle(id, onChange) {
const element = document.getElementById(id);
if (!(element instanceof HTMLInputElement)) {
throw new Error(
"invalid binding element: expected HTMLButtonElement, got: " +
element.constructor.name,
);
}
element.oninput = function oninput() {
onChange(element.checked);
};
const callback = (v) => {
element.checked = v;
onChange(element.checked);
};
return [callback, element.checked, element];
}
Instance.registerCRS(
"EPSG:2154",
"+proj=lcc +lat_0=46.5 +lon_0=3 +lat_1=49 +lat_2=44 +x_0=700000 +y_0=6600000 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs +type=crs",
);
Instance.registerCRS(
"urn:ogc:def:crs:OGC:1.3:CRS84",
"+proj=longlat +datum=WGS84 +no_defs +type=crs",
);
const instance = new Instance({
target: "view",
crs: "EPSG:2154",
backgroundColor: null,
});
const center = new Coordinates("EPSG:4326", 6.63125, 45.93506).as(
instance.referenceCrs,
);
const extent = Extent.fromCenterAndSize(
"EPSG:2154",
{ x: center.x, y: center.y },
1_000,
1_000,
);
const buildingSource = new VectorSource({
format: new GeoJSON(),
url: "data/geojson_3D.geojson",
strategy: tile(createXYZ({ tileSize: 512 })),
});
const colors = {};
function colorFromId(id) {
if (colors[id] == null) {
colors[id] = new Color().setHSL(Math.random(), 0.5, 0.5, "srgb");
}
const result = colors[id];
return result;
}
const params = {
shading: true,
lines: true,
};
const featureCollection = new FeatureCollection({
source: buildingSource,
dataProjection: "EPSG:4326",
extent,
minLevel: 0,
maxLevel: 0,
style: (feature) => {
return {
fill: {
color: colorFromId(feature.get("id")),
shading: params.shading,
},
stroke: params.lines ? { color: "black", lineWidth: 2 } : null,
};
},
});
instance.add(featureCollection);
// Add a sunlight
const sun = new DirectionalLight("#ffffff", 2);
sun.position.set(1, 0, 10000);
sun.updateMatrixWorld(true);
instance.scene.add(sun);
// We can look below the floor, so let's light also a bit there
const sun2 = new DirectionalLight("#ffffff", 0.5);
sun2.position.set(0, 1, 1);
sun2.updateMatrixWorld();
instance.scene.add(sun2);
// Add an ambient light
const ambientLight = new AmbientLight(0xffffff, 0.2);
instance.scene.add(ambientLight);
instance.view.camera.position.set(center.x + 60, center.y + 60, 150);
const lookAt = new Vector3(center.x, center.y, 10);
instance.view.camera.lookAt(lookAt);
const controls = new MapControls(instance.view.camera, instance.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.4;
controls.target.copy(lookAt);
controls.saveState();
instance.view.setControls(controls);
Inspector.attach("inspector", instance);
bindToggle("toggle-shading", (v) => {
params.shading = v;
featureCollection.updateStyles();
});
bindToggle("show-lines", (v) => {
params.lines = v;
featureCollection.updateStyles();
});
<!doctype html>
<html lang="en">
<head>
<title>GeoJSON 3D</title>
<meta charset="UTF-8" />
<meta name="name" content="geojson_3d" />
<meta
name="description"
content="Represents 3D shapes from a GeoJSON file."
/>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" href="https://giro3d.org/images/favicon.svg" />
<link
href="https://giro3d.org/assets/bootstrap-custom.css"
rel="stylesheet"
/>
<script src="https://giro3d.org/assets/bootstrap.bundle.min.js"></script>
<link
rel="stylesheet"
type="text/css"
href="https://giro3d.org/latest/examples/css/example.css"
/>
<style>
#view canvas {
background: rgb(132, 170, 182);
background: radial-gradient(
circle,
rgba(132, 170, 182, 1) 0%,
rgba(37, 44, 48, 1) 100%
);
}
</style>
</head>
<body>
<div id="view" class="m-0 p-0 w-100 h-100"></div>
<div
id="inspector"
class="position-absolute top-0 start-0 mh-100 overflow-auto"
></div>
<div class="side-pane-with-status-bar">
<div class="card">
<div class="card-header">Parameters</div>
<div class="card-body">
<!-- Show/Hide lines -->
<div class="form-check form-switch">
<input
class="form-check-input"
type="checkbox"
checked="true"
role="switch"
id="show-lines"
autocomplete="off"
/>
<label class="form-check-label" for="show-lines">Show lines</label>
</div>
<!-- Toggle shading -->
<div class="form-check form-switch">
<input
class="form-check-input"
type="checkbox"
checked="true"
role="switch"
id="toggle-shading"
autocomplete="off"
/>
<label class="form-check-label" for="toggle-shading"
>Enable shading</label
>
</div>
</div>
</div>
</div>
<script type="module" src="index.js"></script>
<script>
/* activate popovers */
const popoverTriggerList = [].slice.call(
document.querySelectorAll('[data-bs-toggle="popover"]'),
);
popoverTriggerList.map(
// bootstrap is used as script in the template, disable warning about undef
// eslint-disable-next-line no-undef
(popoverTriggerEl) =>
new bootstrap.Popover(popoverTriggerEl, {
trigger: "hover",
placement: "left",
content: document.getElementById(
popoverTriggerEl.getAttribute("data-bs-content"),
).innerHTML,
html: true,
}),
);
</script>
</body>
</html>
{
"name": "geojson_3d",
"dependencies": {
"@giro3d/giro3d": "0.42.1"
},
"devDependencies": {
"vite": "^3.2.3"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}