0

I'm looking for a Waterman Butterfly map with imagery projection (Esri.WorldImagery). Waterman Butterfly map

I'm a beginner in javascript and after searching for some projects and simply replacing the projection used didn't work : Test1 Test2

I have also try to adapt Mbostock converter used in this post -> index.html source.jpeg

<!DOCTYPE html>
<meta charset="utf-8">
<style>
  body {
    background: #222;
  }
</style>
<body>
  <script src="https://d3js.org/d3.v5.min.js"></script>
  <script src="https://d3js.org/d3-geo.v1.min.js"></script>
  <script src="https://d3js.org/d3-geo-projection.v2.min.js"></script>
  <script>
 
    var width = 960,
        height = 500;
 
    var projection = d3.geoPolyhedralWaterman();
 
    var path = d3.geoPath()
        .projection(projection);
 
    var canvas = d3.select("body").append("canvas")
        .attr("width", width)
        .attr("height", height);
 
    var context = canvas.node().getContext("2d");
 
    var image = new Image;
    image.onload = onload;
    image.src = "source.jpg";
 
    function onload() {
      var dx = image.width,
          dy = image.height;
 
      context.drawImage(image, 0, 0, dx, dy);
 
      var sourceData = context.getImageData(0, 0, dx, dy).data,
          target = context.createImageData(width, height),
          targetData = target.data;
 
      for (var y = 0, i = -1; y < height; ++y) {
        for (var x = 0; x < width; ++x) {
          var p = projection.invert([x, y]), λ = p[0], φ = p[1];
          if (λ > 180 || λ < -180 || φ > 90 || φ < -90) { i += 4; continue; }
          var q = ((90 - φ) / 180 * dy | 0) * dx + ((180 + λ) / 360 * dx | 0) << 2;
          targetData[++i] = sourceData[q];
          targetData[++i] = sourceData[++q];
          targetData[++i] = sourceData[++q];
          targetData[++i] = 255;
        }
      }
 
      context.clearRect(0, 0, width, height);
      context.putImageData(target, 0, 0);
    }
  </script>
</body>
</html>

But again without success,

If anyone would like to look into this, I'd be very grateful.

Xab Ion
  • 1,105
  • 1
  • 11
  • 20
Capt_
  • 1
  • 2

1 Answers1

0

ChatGPT is really impressive ... I have finally obtained this code that generates a Waterman Butterfly map from a picture by reversing the pixel coordinates. Thanks a lot to Mbostock for his code !

Result.jpg

<!DOCTYPE html>
<meta charset="utf-8">
<style>

body {
  background: #222;
}

</style>
<body>
<script src="https://cdn.jsdelivr.net/npm/d3@7"></script>
<script src="https://cdn.jsdelivr.net/npm/d3-array@3"></script>
<script src="https://cdn.jsdelivr.net/npm/d3-geo@3"></script>
<script src="https://cdn.jsdelivr.net/npm/d3-geo-projection@4"></script>
<script>

var coef = 3;
var width = 1000*coef,
    height = 500*coef;

var projection = d3.geoPolyhedralWaterman().rotate([20, 0]).fitExtent([[0, 0], [width-1, height-1]], { type: "Sphere" });

var path = d3.geoPath()
    .projection(projection);

var canvas = d3.select("body").append("canvas")
    .attr("width", width)
    .attr("height", height);

var context = canvas.node().getContext("2d");

var image = new Image;
image.onload = onload;
image.src = "source.jpg";

function onload() {
  var dx = image.width,
      dy = image.height;

  context.drawImage(image, 0, 0, dx, dy);

  var sourceData = context.getImageData(0, 0, dx, dy).data,
      target = context.createImageData(width, height),
      targetData = target.data;

  for (var y = 0, i = -1; y < height; ++y) {
    for (var x = 0; x < width; ++x) {
      var p = projection.invert([x, y]);
      if (!p) {
         i += 4;
         continue;
      }

      var λ = p[0], φ = p[1];
      if (λ > 180 || λ < -180 || φ > 90 || φ < -90) { i += 4; continue; }
      var q = ((90 - φ) / 180 * dy | 0) * dx + ((180 + λ) / 360 * dx | 0) << 2;
      targetData[++i] = sourceData[q];
      targetData[++i] = sourceData[++q];
      targetData[++i] = sourceData[++q];
      targetData[++i] = 255;
    }
  }

  context.clearRect(0, 0, width, height);
  context.putImageData(target, 0, 0);
}

</script>
Capt_
  • 1
  • 2