turf-union

Combines two polygons into one.

<!doctype html>
<html lang="en">
  <head>
    <link rel="stylesheet" href="https://rawcdn.githack.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" type="text/css">
    <link rel="stylesheet" href="assets/css/styles.css" type="text/css">
    <script src="//cdn.jsdelivr.net/npm/@turf/turf@5/turf.min.js"></script>
    <script src="https://rawcdn.githack.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
    <script src="assets/js/es6-promise.min.js"></script>
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL,fetch"></script>
    <script src="assets/js/helpers.js"></script>
    <title>Turf and OpenLayers 3 - Union</title>
  </head>
  <body>
    <div id="map" class="map"></div>
    <button id="switchlayer" type="button">Show polygons union</button>
    <script type="text/javascript">

      // Declare a source for points and
      // for future convex polygon deduced from them
      var vectorUnionPolygon = new ol.source.Vector();

      var vectorLayerUnionPolygon = new ol.layer.Vector({
        source: vectorUnionPolygon
      });

      // Instanciate a map and add layers
      var map = new ol.Map({
        target: 'map',
        layers: [
          new ol.layer.Tile({
            source: new ol.source.OSM()
          }),
          vectorLayerUnionPolygon
        ],
        view: new ol.View({
          center: ol.proj.fromLonLat([-1.555493, 47.216151]),
          zoom: 12
        })
      });

      var polygons, union;

      fetch('assets/data/union.geojson').then(function(response) {
        return response.json().then(function(polygons_fc) {
          console.log(polygons_fc.features);
          polygons = polygons_fc;
          var features = polygons_fc.features;

          // Simple way but we could also use recursive function
          for (var i = 1, len = features.length; i < len; i++) {
            if (i == 1) {
              union = turf.union(features[i - 1], features[i]);
            } else {
              union = turf.union(union, features[i]);
            }
          }

          console.log(union);

          vectorUnionPolygon.addFeatures(geojsonToFeatures(polygons_fc, {
            featureProjection: 'EPSG:3857'
          }));
        });
      });
      var button = document.getElementById('switchlayer');
      button.addEventListener('click', function(e) {
        if (this.innerHTML === 'Show polygons union') {
          this.innerHTML = 'Show polygons before union';
          vectorUnionPolygon.clear();
          vectorUnionPolygon.addFeatures(geojsonToFeatures(union, {
            featureProjection: 'EPSG:3857'
          }));
        } else {
          this.innerHTML = 'Show polygons union';
          vectorUnionPolygon.clear();
          vectorUnionPolygon.addFeatures(geojsonToFeatures(polygons, {
            featureProjection: 'EPSG:3857'
          }));
        }
      })
    </script>
  </body>
</html>

results matching ""

    No results matching ""