Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

geoInterruptedHomolosine is missing an invert() method #97

Closed
apezel opened this issue Mar 7, 2017 · 6 comments
Closed

geoInterruptedHomolosine is missing an invert() method #97

apezel opened this issue Mar 7, 2017 · 6 comments

Comments

@apezel
Copy link
Contributor

apezel commented Mar 7, 2017

I don't know if it's normal but d3.geoInterruptedHomolosine() misses an .invert() method.

@Fil
Copy link
Member

Fil commented Mar 7, 2017

Not all projections have an invert — see #85 for a general solution.
Of course if we can invert the equations it's better, but it's not always possible.

@apezel
Copy link
Contributor Author

apezel commented Mar 7, 2017

Ok I understand. But in this case, if i use d3.geoHomolosine() with manually defined lobes i can use the invert() method and it seems to work correctly. Do you mean that this method returns wrong coordinates ?

@apezel
Copy link
Contributor Author

apezel commented Mar 7, 2017

Oups sorry. Forget my comment. Defining lobes on d3.geoHomolosine doesn't work at all.

@apezel apezel closed this as completed Mar 7, 2017
@apezel
Copy link
Contributor Author

apezel commented Mar 7, 2017

@Fil Actually, i think it may be a bug :

If i defined the projection projection as :

d3.geoInterrupt(d3.geoHomolosineRaw, [[ [[-180, 0], [-100, 90], [ -40, 0]], [[ -40, 0], [ 30, 90], [ 180, 0]] ], [ [[-180, 0], [-160, -90], [-100, 0]], [[-100, 0], [ -60, -90], [ -20, 0]], [[ -20, 0], [ 20, -90], [ 80, 0]], [[ 80, 0], [ 140, -90], [ 180, 0]] ]])

there's no invert() method neither. When i look into interrupted/index.js, it seems that the projection is initialized before that the invert() method get attached to forward :

var p = projection(forward),
      stream_ = p.stream;

  function forward(lambda, phi) {
    var sign = phi < 0 ? -1 : +1, lobe = lobes[+(phi < 0)];
    for (var i = 0, n = lobe.length - 1; i < n && lambda > lobe[i][2][0]; ++i);
    var p = project(lambda - lobe[i][1][0], phi);
    p[0] += project(lobe[i][1][0], sign * phi > sign * lobe[i][0][1] ? lobe[i][0][1] : phi)[0];
    return p;
  }

  // Assumes mutually exclusive bounding boxes for lobes.
  if (project.invert) forward.invert = function(x, y) {
    var bound = bounds[+(y < 0)], lobe = lobes[+(y < 0)];
    for (var i = 0, n = bound.length; i < n; ++i) {
      var b = bound[i];
      if (b[0][0] <= x && x < b[1][0] && b[0][1] <= y && y < b[1][1]) {
        var p = project.invert(x - project(lobe[i][1][0], 0)[0], y);
        p[0] += lobe[i][1][0];
        return pointEqual(forward(p[0], p[1]), [x, y]) ? p : null;
      }
    }
};

If i move the initialization to the end of the block (like this), i can get the projection.invert method to work perfectly :

  function forward(lambda, phi) {
    var sign = phi < 0 ? -1 : +1, lobe = lobes[+(phi < 0)];
    for (var i = 0, n = lobe.length - 1; i < n && lambda > lobe[i][2][0]; ++i);
    var p = project(lambda - lobe[i][1][0], phi);
    p[0] += project(lobe[i][1][0], sign * phi > sign * lobe[i][0][1] ? lobe[i][0][1] : phi)[0];
    return p;
  }

  // Assumes mutually exclusive bounding boxes for lobes.
  if (project.invert) forward.invert = function(x, y) {
    var bound = bounds[+(y < 0)], lobe = lobes[+(y < 0)];
    for (var i = 0, n = bound.length; i < n; ++i) {
      var b = bound[i];
      if (b[0][0] <= x && x < b[1][0] && b[0][1] <= y && y < b[1][1]) {
        var p = project.invert(x - project(lobe[i][1][0], 0)[0], y);
        p[0] += lobe[i][1][0];
        return pointEqual(forward(p[0], p[1]), [x, y]) ? p : null;
      }
    }
};

var p = projection(forward),
      stream_ = p.stream;

@apezel apezel reopened this Mar 7, 2017
@Fil
Copy link
Member

Fil commented Mar 7, 2017

Yes I just fixed it the same way you did in http://blockbuilder.org/Fil/cb3718040261307ef04b4031615e0c5c

Would you like to submit a PR?

apezel pushed a commit to apezel/d3-geo-projection that referenced this issue Mar 7, 2017
Moved projection initialization after forward declaration. Related to
issue d3#97
mbostock pushed a commit that referenced this issue Mar 7, 2017
Moved projection initialization after forward declaration. Related to
issue #97
@mbostock
Copy link
Member

mbostock commented Mar 7, 2017

Fixed by #98.

@mbostock mbostock closed this as completed Mar 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants