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

Mesure-path with CRS.Simple #22

Open
robjlg opened this issue Mar 28, 2018 · 5 comments
Open

Mesure-path with CRS.Simple #22

robjlg opened this issue Mar 28, 2018 · 5 comments

Comments

@robjlg
Copy link

robjlg commented Mar 28, 2018

Hellow,

I'm very new to Leaflet.js and probably doing something wrong.

I'm using L.CRS.Simple to show a tiled image (non-geographical map). The L.control.scale shows the expected distance (2 m) but, mesure-path shows completed wrong values ( 119 km ! ).

Is it compatible with L.CRS.Simple ? Does mesure-path use the same approach L.control.scale uses to calculate distances?

Could you give me some tips on how to make mesure-path works correctly in this situation?

Thank You

Roberto

@perliedman
Copy link
Contributor

Hi there!
You're right in your observations - this plugin hasn't been written with non-geographic projections in mind, so it assumes coordinates are really lat/lng.

It would be interesting to convert it to support other projections, and I think it would be fairly easy to fix this for line lengths. Polygon area is a bit trickier, since there's no built-in function in Leaflet for this, so Leaflet Measure Path has its own implementation: https://github.com/ProminentEdge/leaflet-measure-path/blob/master/leaflet-measure-path.js#L121 - we would need to make that possible to swap out.

Given that I don't actively work on this plugin, I'm not in a position where I would make these changes, but I would be happy to guide you (other anyone else) if you would like fix this.

@robjlg
Copy link
Author

robjlg commented Mar 31, 2018

Thank you, I will take a look at your code

@robjlg
Copy link
Author

robjlg commented Apr 3, 2018

Hi, I did change the 291 line from:

   dist = ll1.distanceTo(ll2);

to

   dist = this._map.distance(ll1, ll2);

For the distribution example, the values result identical. For my map, using L.CRS.Simple, now shows the expected result.

Explanation from Leaflet docs:

distanceTo() - Returns the distance (in meters) to the given LatLng calculated using the Spherical Law of Cosines.

distance() - Returns the distance between two geographical coordinates according to the map's CRS. By default this measures distance in meters.

Maybe you can test this change in others maps and configurations

Thank you

@perliedman
Copy link
Contributor

@robjlg yes, sounds like a proper fix. Would you like to submit a PR?

The only issue is that polygon areas will still be broken, but we could open a separate issue for that.

@beig
Copy link

beig commented Mar 3, 2020

var ringArea = function ringArea(coords) {
    let area = 0;

    if (coords.length > 2) {
      for (let i = 0; i < coords.length; i++) {
        let z = i + 1;
        if (i === coords.length - 1) {
          z = 0;
        }
        area += (coords[i].lat * coords[z].lng) - (coords[i].lng * coords[z].lat)
      }
    }

    area = area / 2;

    return Math.abs(area);
}

For a simple polygon (https://en.wikipedia.org/wiki/Shoelace_formula)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants