turf-bboxPolygon

Takes a bbox and returns the equivalent polygon feature.

<!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 - BboxPolygon</title>
  </head>
  <body>
    <div id="map" class="map"></div>
    <button id="createbboxpolygon" type="button">
      Generate random bboxpolygon from existing area
    </button>
    <script type="text/javascript">
      function getRandomInt(min, max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
      }
      // Declare a source for polygons
      var vectorSourcePolygons = new ol.source.Vector();

      var vectorLayerPolygons = new ol.layer.Vector({
        source: vectorSourcePolygons,
        style: [
          new ol.style.Style({
            stroke: new ol.style.Stroke({
              color: [0, 121, 88, 1],
              width: 2
            })
          })
        ]
      });

      // Declare a vector layer for bboxPolygon
      var vectorLayerBboxPolygon = new ol.layer.Vector({
        source: new ol.source.Vector(),
        style: [
          new ol.style.Style({
            stroke: new ol.style.Stroke({
              color: [0, 0, 0, 1],
              width: 2
            })
          })
        ]
      });

      // Instanciate a map and add layers
      var map = new ol.Map({
        target: 'map',
        layers: [
          new ol.layer.Tile({
            source: new ol.source.OSM()
          }),
          vectorLayerPolygons,
          vectorLayerBboxPolygon
        ],
        view: new ol.View({
          center: ol.proj.transform(
            [-1.5603, 47.2383],
            'EPSG:4326',
            'EPSG:3857'
          ),
          zoom: 11
        })
      });

      fetch('assets/data/quartiers_nantes.geojson').then(function(response) {
        return response.json().then(function(polys_fc) {
          console.log(polys_fc);

          vectorSourcePolygons.addFeatures(geojsonToFeatures(polys_fc, {
            featureProjection: 'EPSG:3857'
          }));

          // Create a button and bind a click function
          var button = document.getElementById('createbboxpolygon');
          var draw;
          button.addEventListener('click', function(e) {
            var source = vectorLayerBboxPolygon.getSource();
            source.clear();
            // Random feature extraction
            var features = vectorLayerPolygons.getSource().getFeatures();
            var feat = features[getRandomInt(0, features.length - 1)];
            // Get extent
            var featureExtent = ol.proj.transformExtent(feat.getGeometry().getExtent(), 'EPSG:3857', 'EPSG:4326');
            // Create a GeoJSON reusing the extent
            var featureBboxPolygon = turf.bboxPolygon(featureExtent);
            source.addFeatures(geojsonToFeatures(featureBboxPolygon, {
               featureProjection: 'EPSG:3857'
            }));
          });
        });
      });

    </script>
  </body>
</html>

results matching ""

    No results matching ""