How-to guide·3D viewerJavaScript

Geo mode

Switch the viewer between LOCAL and GEO placement, know why the switch can fail, and add basemaps, terrain and 3D Tiles as GIS layers.

The viewer places models in one of two modes. In LOCAL mode every model sits where its own coordinates put it, which is right for the discipline models of one project. In GEO mode every model is placed by its Geo reference, so models from different sites land at their true relative positions and GIS layers (basemaps, terrain, 3D Tiles) can be drawn under and around them.

Before you start#

Every model you load in GEO mode needs a geo reference (Set the geo reference). Loading a model without one switches the viewer back to LOCAL.

Steps#

  1. Switch the mode. setMode returns true when the mode changed and false when it did not: when the viewer already was in that mode, when a loaded model has no geo reference, or when the viewer is offline (GEO mode needs the map services).

    JavaScript#
    if (!viewer.setMode("GEO")) {
    	console.warn("Still in", viewer.getMode());   // "LOCAL" | "GEO"
    }
    viewer.addModeChangeListener((mode) => console.log("now in", mode));
  2. Add GIS layers. A layer is registered once with an id and shown or hidden with toggleGisLayer; it is loaded when the first geo-referenced model is in the viewer.

    JavaScript#
    viewer.addGisLayer("osm", {
    	name: "OpenStreetMap",
    	type: "TMS",
    	url: "https://tile.openstreetmap.org/",   // the viewer appends zoom/x/y.png
    	maxZoomLevel: 18,
    	visible: true
    });
    viewer.addGisLayer("terrain", {
    	name: "Terrain",
    	type: "TERRAIN",
    	url: "https://example.org/terrain/",     // a quantized-mesh service with layer.json
    	heightDatum: "orthometric"
    });
    viewer.toggleGisLayer("terrain", true);

    OpenStreetMap's tile servers are for light use: their tile usage policy forbids bulk downloading and requires the attribution "© OpenStreetMap contributors" on the map. The viewer draws no attribution, so the page around it shows it; an application with many users takes its tiles from its own or a commercial tile service.

    type is one of TMS, WMS, WMTS (imagery), TERRAIN (a quantized-mesh height service) and 3DTILES (url is the set's tileset.json). The imagery types work on the Web Mercator tile grid (EPSG:3857) and need maxZoomLevel, the zoom level of their tiles; the viewer lowers it when the area around the models does not fit in one texture.

    • TMS requests url followed by zoom/x/y.png, with row 0 at the north edge (the OpenStreetMap scheme), so url ends with a /. For a service whose tile URLs have another shape, give a urlBuilder(settings, zoom, x, y) that returns the URL of one tile; it replaces the default URL of every imagery type.
    • WMS asks the service for one 256 × 256 pixel GetMap image per tile, in EPSG:3857, which the service must offer. url is the service address and params.layers (required) the comma-separated layer names. params.styles (default: the default style), params.version ("1.1.1", the default, or "1.3.0"), params.format (default "image/png") and params.transparent (default true, so that where the service draws nothing the layer underneath shows) are optional, and any other key in params is passed on as a GetMap parameter.
    • WMTS needs a urlBuilder; without one the layer fails to load. The service's tile matrix set must be Web Mercator (GoogleMapsCompatible or EPSG:3857): the builder gets the zoom, the column and the row of a tile and turns the zoom into the TileMatrix identifier that the service's capabilities list.
    JavaScript#
    viewer.addGisLayer("parcels", {                // the Dutch cadastral map, registered hidden
    	name: "Kadastrale kaart",
    	type: "WMS",
    	url: "https://service.pdok.nl/kadaster/kadastralekaart/wms/v5_0",
    	params: {layers: "Kadastralekaart"},
    	maxZoomLevel: 18
    });
    viewer.addGisLayer("brt", {
    	name: "BRT achtergrondkaart",
    	type: "WMTS",
    	url: "https://service.pdok.nl/kadaster/brt-achtergrondkaart/wmts/v2_0",
    	maxZoomLevel: 18,
    	urlBuilder: (settings, zoom, x, y) => settings.url
    		+ "?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=standaard&STYLE=default&FORMAT=image/png"
    		+ "&TILEMATRIXSET=EPSG:3857&TILEMATRIX=" + String(zoom).padStart(2, "0")   // PDOK names them 00 to 19
    		+ "&TILECOL=" + x + "&TILEROW=" + y
    });

    Imagery drapes over a TERRAIN layer named in its terrain property, or over whichever terrain is visible; a terrain that imagery drapes over is not drawn on its own. heightDatum says what the service's heights are measured from: "orthometric" (national datum, the default) or "ellipsoid" (the Cesium convention, for which the viewer subtracts the geoid height where it has a table); heightOffsetMeters adds a constant. snapToZero puts a flat layer at the model's lowest point instead of the geo reference's elevation, and translationMm shifts a layer, for instance to draw a basemap slightly below the IfcSite.

  3. After changing a geo reference, tell the viewer: viewer.updateModel(versionUuid) re-reads the version and moves the model without reloading it.

Note

In LOCAL mode two models can be too far apart to view together (one near the origin, one 500 km away). Compare im_metadata.transformedBoundsMm of the versions before loading them; in GEO mode compare their geo references with Get the geo reference.

Check#

viewer.getMode() returns "GEO", the basemap tiles appear under the model, and setMode("LOCAL") moves the model back to its own origin.