Skip to content

Commit

Permalink
using latest leaflet crs fixes, pixelToGeo and geoToPixel can now pro…
Browse files Browse the repository at this point in the history
…jects buildings using a crs like ESPG:4326
  • Loading branch information
elb committed Jun 1, 2014
1 parent 02af20e commit 3c5cf5d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 28 deletions.
36 changes: 27 additions & 9 deletions src/engines/Leaflet.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
var crs;

function pixelToGeo(x, y) {
var res = {};

x /= size;
y /= size;

var coord = crs.pointToLatLng(L.point(x, y));

res[LON] = coord.lng;
res[LAT] = coord.lat;

return res;
}

function geoToPixel(lat, lon) {
var point = crs.latLngToPoint(L.latLng(lat, lon));

return {
x: point.x*size << 0,
y: point.y*size << 0
};
}

var osmb = function(map) {
this.offset = { x:0, y:0 };
crs = map.options.crs;
map.addLayer(this);
};

var proto = osmb.prototype;
var proto = osmb.prototype = L.Layer.prototype;

proto.onAdd = function(map) {
this.map = map;
Expand Down Expand Up @@ -88,14 +114,6 @@ proto.onZoomStart = function(e) {
};

proto.onZoom = function(e) {
// var map = this.map,
// scale = map.getZoomScale(e.zoom),
// offset = map._getCenterOffset(e.center).divideBy(1 - 1/scale),
// viewportPos = map.containerPointToLayerPoint(map.getSize().multiplyBy(-1)),
// origin = viewportPos.add(offset).round();
//
// this.container.style[L.DomUtil.TRANSFORM] = L.DomUtil.getTranslateString((origin.multiplyBy(-1).add(this.getOffset().multiplyBy(-1)).multiplyBy(scale).add(origin))) + ' scale(' + scale + ') ';
// isZooming = true;
};

proto.onZoomEnd = function(e) {
Expand Down
20 changes: 19 additions & 1 deletion src/engines/OpenLayers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
function pixelToGeo(x, y) {
var res = {};
x /= size;
y /= size;
res[LAT] = y <= 0 ? 90 : y >= 1 ? -90 : RAD * (2 * atan(exp(PI * (1 - 2*y))) - HALF_PI),
res[LON] = (x === 1 ? 1 : (x%1 + 1) % 1) * 360 - 180;
return res;
}

function geoToPixel(lat, lon) {
var latitude = min(1, max(0, 0.5 - (log(tan(QUARTER_PI + HALF_PI * lat / 180)) / PI) / 2)),
longitude = lon/360 + 0.5;
return {
x: longitude*size <<0,
y: latitude *size <<0
};
}

// based on a pull request from Jérémy Judéaux (https://github.com/Volune)

var parent = OpenLayers.Layer.prototype;
Expand Down Expand Up @@ -85,4 +103,4 @@ proto.moveByPx = function(dx, dy) {
setCamOffset(this.offset);
Buildings.render();
return res;
};
};
18 changes: 0 additions & 18 deletions src/functions.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
function pixelToGeo(x, y) {
var res = {};
x /= size;
y /= size;
res[LAT] = y <= 0 ? 90 : y >= 1 ? -90 : RAD * (2 * atan(exp(PI * (1 - 2*y))) - HALF_PI),
res[LON] = (x === 1 ? 1 : (x%1 + 1) % 1) * 360 - 180;
return res;
}

function geoToPixel(lat, lon) {
var latitude = min(1, max(0, 0.5 - (log(tan(QUARTER_PI + HALF_PI * lat / 180)) / PI) / 2)),
longitude = lon/360 + 0.5;
return {
x: longitude*size <<0,
y: latitude *size <<0
};
}

function fromRange(sVal, sMin, sMax, dMin, dMax) {
sVal = min(max(sVal, sMin), sMax);
var rel = (sVal-sMin) / (sMax-sMin),
Expand Down

0 comments on commit 3c5cf5d

Please sign in to comment.