TopoJSON

Demonstrates rendering of features from a TopoJSON topology.

This example uses a vector layer with ol/format/TopoJSON for rendering features from TopoJSON.

<!DOCTYPE html>
<html>
  <head>
    <title>TopoJSON</title>
    <link rel="stylesheet" href="https://openlayers.org/en/v5.3.0/css/ol.css" type="text/css">
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>

  </head>
  <body>
    <div id="map" class="map"></div>
    <script>
      import Map from 'ol/Map.js';
      import View from 'ol/View.js';
      import TopoJSON from 'ol/format/TopoJSON.js';
      import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer.js';
      import TileJSON from 'ol/source/TileJSON.js';
      import VectorSource from 'ol/source/Vector.js';
      import {Fill, Stroke, Style} from 'ol/style.js';


      var raster = new TileLayer({
        source: new TileJSON({
          url: 'https://api.tiles.mapbox.com/v3/mapbox.world-dark.json?secure'
        })
      });

      var style = new Style({
        fill: new Fill({
          color: 'rgba(255, 255, 255, 0.6)'
        }),
        stroke: new Stroke({
          color: '#319FD3',
          width: 1
        })
      });

      var vector = new VectorLayer({
        source: new VectorSource({
          url: 'data/topojson/world-110m.json',
          format: new TopoJSON({
            // don't want to render the full world polygon (stored as 'land' layer),
            // which repeats all countries
            layers: ['countries']
          }),
          overlaps: false
        }),
        style: style
      });

      var map = new Map({
        layers: [raster, vector],
        target: 'map',
        view: new View({
          center: [0, 0],
          zoom: 1
        })
      });
    </script>
  </body>
</html>