turf-flip

Takes a point, linestring, polygon, or featurecollection, and flips all of its coordinates from [x, y] to [y, x].

<!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" type="text/javascript"></script>
    <title>Turf and OpenLayers 3 - flip Feature or FeatureCollection</title>
  </head>
  <body>
    <div id="map" class="map"></div>
    <script type="text/javascript">

      // Tower of Bretagne without properties declared
      var pt1 = turf.point([-1.5579479, 47.2175545]);

      // Declare a formatter to read GeoJSON
      var format = new ol.format.GeoJSON();

      // Declare a source
      var vectorSource = new ol.source.Vector();

      var flipped = turf.flip(pt1);

      // When reading features, reproject to EPSG 3857
      var features = format.readFeatures(pt1, {
        featureProjection: 'EPSG:3857'
      });
      var flipped_features = format.readFeatures(flipped, {
        featureProjection: 'EPSG:3857'
      });
      var all_features = features.concat(flipped_features);
      // Add features
      vectorSource.addFeatures(all_features);

      // Declare a vector layer with the already
      // created source containing added features
      var vectorLayer = new ol.layer.Vector({
        source: vectorSource
      });

      // Instanciate a map and add layers
      var map = new ol.Map({
        target: 'map',
        layers: [
          new ol.layer.Tile({
            source: new ol.source.OSM()
          }),
          vectorLayer
        ],
        view: new ol.View({
          center: ol.proj.fromLonLat([47.214751, -1.563046]),
          zoom: 2
        })
      });
    </script>
  </body>
</html>

results matching ""

    No results matching ""