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

L.Proj.CRS is missing distance method #109

Closed
perliedman opened this issue Dec 14, 2015 · 5 comments
Closed

L.Proj.CRS is missing distance method #109

perliedman opened this issue Dec 14, 2015 · 5 comments

Comments

@perliedman
Copy link
Contributor

In Leaflet 1.0, a CRS is expected to implement the method distance, which calculates the distance between two L.LatLngs, see L.CRS.Earth for example.

I think the easy and "correct enough" fix is to just set L.Proj.CRS.distance = L.CRS.Earth.distance, which will use the haversine formula for all projections.

An alternative, more complicated, but at least in some sense more correct solution, which uses the fact that distances are preserved by some projections, would be to project each LatLng, finding the differences dx and dy and doing d = Math.sqrt(dx*dx+dy*dy), but some problems need to be solved:

  • This doesn't apply to all projections, both EPSG:3857 and EPSG:4326 can for example be used with Proj4Leaflet, where this is not true, just to give an example.
  • Some projections where this is true still use weird units, like feet, and the result must be scaled so that distance still returns its result in meters

I'd vote for just using L.CRS.Earth.distance until we find some use case were it would cause a problem.

@emacgillavry
Copy link
Contributor

Updated the gist http://bl.ocks.org/emacgillavry/1a654b76d35deceb9bec. The scalebar does appear and no error message. However, I simply get the stock image it seems: no lengths indicated. Am I missing something?

@perliedman
Copy link
Contributor Author

Yeah, L.CRS.Earth.distance uses a constant, R, which it expects to be part of this, which it isn't with my hack.

Again, this is super-ugly, but works as a workaround:

RD.distance = L.CRS.Earth.distance;
RD.R = 6378137;

@emacgillavry
Copy link
Contributor

Updated the gist and it works beatifully. Have added 100 meter grid layer
as a reference. Thank you very much for looking into this.

On 15 December 2015 at 13:58, Per Liedman notifications@github.com wrote:

Yeah, L.CRS.Earth.distance uses a constant, R, which it expects to be
part of this, which it isn't with my hack.

Again, this is super-ugly, but works as a workaround:

RD.distance = L.CRS.Earth.distance;
RD.R = 6378137;


Reply to this email directly or view it on GitHub
#109 (comment)
.

@sivesind
Copy link

Any hope of fixing this? It holds back the release of a branch compatible with leaflet 1.0.0 beta, #90.

@pthorin
Copy link

pthorin commented Jan 25, 2017

Closed in #126.

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

No branches or pull requests

4 participants