Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
495 views
in Technique[技术] by (71.8m points)

javascript - Leaflet LatLngBounds with simpler CRS & projection

I'm using Leaflet 0.7.7, latest stable release, and I'm using a custom CRS inherited from L.CRS.Simple.

CRS:

It is very similar to Simple CRS but with c set to 1 (in Simple, c is set to -1).

L.CRS.XY = L.Util.extend({}, L.CRS.Simple, {
    code: 'XY',
    projection: L.Projection.LonLat,
    transformation: new L.Transformation(1, 0, 1, 0)
});

The goal of this CRS is to have a real {x, y} map system where y becomes higher when reaching the bottom of the map (like a bitmap).

Code to test:

var southWest = L.latLng(133, 0);
var northEast = L.latLng(0, 170);
var bounds = L.latLngBounds(southWest, northEast);

document._map.setMaxBounds(bounds);
document._map.fitBounds(bounds);
document._markers[68].setLatLng(bounds.getNorthEast());

console.info('southWest', southWest);
// L.LatLng {lat: 133, lng: 0}

console.info('northEast', northEast);
// L.LatLng {lat: 0, lng: 170}

console.info('bounds', bounds);
// L.LatLngBounds (
    _northEast: L.LatLng {lat: 133, lng: 170 }
    _southWest: L.LatLng {lat: 0, lng: 0 }
)

Actually, as I have a custom CRS, I think these lines are the source of the problem because maximum and minimum values are not correct in a plane {x, y} system (even if I would prefer to use "Point" but I can only use "LatLng" objects :confused: ).

So actually, it seems it is obvious that this issue comes from my code, but actually I'd like to find a solution for this that wouldn't need me to switch to L.CRS.Simple in which y coordinates get higher on top of the map.

So what's the solution to use bounds with this simple custom projection?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

EDIT:

It sounds like Leaflet internally assumes that y points increase when going down (like in images), and it should be the opposite for latitude (i.e. decrease when going down), as the min, max, <= and >= comparisons are hard-coded.

So there is probably no possibility to make up a CRS that will give you a latitude in the same direction as the Y points. Except if you are ready to modify every function that compares latitudes within Leaflet library…

You may still be able to "manipulate" numbers like you wish, if you use an intermediate conversion function every time you have to provide coordinates, and the opposite when you have to read coordinates.

That would also give you the opportunity to revert the latitude (y) and longitude (x) order to be [x, y].

For example:

function revertLat(x, y) {
  return [-y, x];
}

Demo: http://jsfiddle.net/ve2huzxw/101/


Original answer:

Any reason for not directly customizing L.LatLngBounds as well, so that it fits your need?

First of all, if you do not exactly need southWest and northEast, but just corners for your bounds, you can use L.LatLngBounds as is. For instance, all Leaflet methods would keep working even if the corners are not exactly southWest and northEast: map.fitBounds, L.imageOverlay etc. should work fine.

Otherwise, you would have to customize many methods in L.LatLngBounds (extend, pad, contains, intersects) to revert the min / max and <= / >= comparisons.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...