Merge pull request #42 from tschaub/convenience
Adding extent, location, and projection convenience. Where appropriate, literals can be used instead of creating new OpenLayers objects.
This commit is contained in:
@@ -41,12 +41,12 @@
|
||||
|
||||
var map = new OpenLayers.Map({
|
||||
div: "map",
|
||||
projection: new OpenLayers.Projection("EPSG:900913"),
|
||||
displayProjection: new OpenLayers.Projection("EPSG:4326"),
|
||||
projection: "EPSG:900913",
|
||||
displayProjection: "EPSG:4326",
|
||||
units: "m",
|
||||
numZoomLevels: 18,
|
||||
maxResolution: 156543.0339,
|
||||
maxExtent: new OpenLayers.Bounds(-20037508, -20037508, 20037508, 20037508),
|
||||
maxExtent: [-20037508, -20037508, 20037508, 20037508],
|
||||
});
|
||||
|
||||
// create Google Mercator layers
|
||||
|
||||
@@ -67,6 +67,12 @@ OpenLayers.Bounds = OpenLayers.Class({
|
||||
* top - {Number} The top bounds.
|
||||
*/
|
||||
initialize: function(left, bottom, right, top) {
|
||||
if (OpenLayers.Util.isArray(left)) {
|
||||
top = left[3];
|
||||
right = left[2];
|
||||
bottom = left[1];
|
||||
left = left[0];
|
||||
}
|
||||
if (left != null) {
|
||||
this.left = OpenLayers.Util.toFloat(left);
|
||||
}
|
||||
|
||||
@@ -38,6 +38,10 @@ OpenLayers.LonLat = OpenLayers.Class({
|
||||
* it will be the y coordinate of the map location in your map units.
|
||||
*/
|
||||
initialize: function(lon, lat) {
|
||||
if (OpenLayers.Util.isArray(lon)) {
|
||||
lat = lon[1];
|
||||
lon = lon[0];
|
||||
}
|
||||
this.lon = OpenLayers.Util.toFloat(lon);
|
||||
this.lat = OpenLayers.Util.toFloat(lat);
|
||||
},
|
||||
|
||||
@@ -464,6 +464,16 @@ OpenLayers.Layer = OpenLayers.Class({
|
||||
if (this.options == null) {
|
||||
this.options = {};
|
||||
}
|
||||
|
||||
// allow array for extents
|
||||
if (newOptions) {
|
||||
if (newOptions.maxExtent && !(newOptions.maxExtent instanceof OpenLayers.Bounds)) {
|
||||
newOptions.maxExtent = new OpenLayers.Bounds(newOptions.maxExtent);
|
||||
}
|
||||
if (newOptions.minExtent && !(newOptions.minExtent instanceof OpenLayers.Bounds)) {
|
||||
newOptions.minExtent = new OpenLayers.Bounds(newOptions.minExtent);
|
||||
}
|
||||
}
|
||||
|
||||
// update our copy for clone
|
||||
OpenLayers.Util.extend(this.options, newOptions);
|
||||
|
||||
@@ -478,6 +478,14 @@ OpenLayers.Map = OpenLayers.Class({
|
||||
|
||||
// now override default options
|
||||
OpenLayers.Util.extend(this, options);
|
||||
|
||||
// allow extents to be arrays
|
||||
if (this.maxExtent && !(this.maxExtent instanceof OpenLayers.Bounds)) {
|
||||
this.maxExtent = new OpenLayers.Bounds(this.maxExtent);
|
||||
}
|
||||
if (this.restrictedExtent && !(this.restrictedExtent instanceof OpenLayers.Bounds)) {
|
||||
this.restrictedExtent = new OpenLayers.Bounds(this.restrictedExtent);
|
||||
}
|
||||
|
||||
// initialize layers array
|
||||
this.layers = [];
|
||||
@@ -1703,6 +1711,9 @@ OpenLayers.Map = OpenLayers.Class({
|
||||
* options - {Object}
|
||||
*/
|
||||
moveTo: function(lonlat, zoom, options) {
|
||||
if (!(lonlat instanceof OpenLayers.LonLat)) {
|
||||
lonlat = new OpenLayers.LonLat(lonlat);
|
||||
}
|
||||
if (!options) {
|
||||
options = {};
|
||||
}
|
||||
@@ -2208,6 +2219,9 @@ OpenLayers.Map = OpenLayers.Class({
|
||||
*
|
||||
*/
|
||||
zoomToExtent: function(bounds, closest) {
|
||||
if (!(bounds instanceof OpenLayers.Bounds)) {
|
||||
bounds = new OpenLayers.Bounds(bounds);
|
||||
}
|
||||
var center = bounds.getCenterLonLat();
|
||||
if (this.baseLayer.wrapDateLine) {
|
||||
var maxExtent = this.getMaxExtent();
|
||||
|
||||
@@ -101,6 +101,9 @@ OpenLayers.Projection = OpenLayers.Class({
|
||||
equals: function(projection) {
|
||||
var p = projection, equals = false;
|
||||
if (p) {
|
||||
if (!(p instanceof OpenLayers.Projection)) {
|
||||
p = new OpenLayers.Projection(p);
|
||||
}
|
||||
if (window.Proj4js && this.proj.defData && p.proj.defData) {
|
||||
equals = this.proj.defData.replace(this.titleRegEx, "") ==
|
||||
p.proj.defData.replace(this.titleRegEx, "");
|
||||
@@ -183,12 +186,19 @@ OpenLayers.Projection.addTransform = function(from, to, method) {
|
||||
* point - {object} A transformed coordinate. The original point is modified.
|
||||
*/
|
||||
OpenLayers.Projection.transform = function(point, source, dest) {
|
||||
if (source.proj && dest.proj) {
|
||||
point = Proj4js.transform(source.proj, dest.proj, point);
|
||||
} else if (source && dest &&
|
||||
OpenLayers.Projection.transforms[source.getCode()] &&
|
||||
OpenLayers.Projection.transforms[source.getCode()][dest.getCode()]) {
|
||||
OpenLayers.Projection.transforms[source.getCode()][dest.getCode()](point);
|
||||
if (source && dest) {
|
||||
if (!(source instanceof OpenLayers.Projection)) {
|
||||
source = new OpenLayers.Projection(source);
|
||||
}
|
||||
if (!(dest instanceof OpenLayers.Projection)) {
|
||||
dest = new OpenLayers.Projection(dest);
|
||||
}
|
||||
if (source.proj && dest.proj) {
|
||||
point = Proj4js.transform(source.proj, dest.proj, point);
|
||||
} else if (OpenLayers.Projection.transforms[source.getCode()] &&
|
||||
OpenLayers.Projection.transforms[source.getCode()][dest.getCode()]) {
|
||||
OpenLayers.Projection.transforms[source.getCode()][dest.getCode()](point);
|
||||
}
|
||||
}
|
||||
return point;
|
||||
};
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
<script type="text/javascript">
|
||||
var bounds;
|
||||
function test_Bounds_constructor (t) {
|
||||
t.plan( 21 );
|
||||
t.plan( 26 );
|
||||
|
||||
bounds = new OpenLayers.Bounds();
|
||||
t.ok( bounds instanceof OpenLayers.Bounds, "new OpenLayers.Bounds returns Bounds object" );
|
||||
@@ -44,6 +44,15 @@
|
||||
t.eq( bounds.bottom, -20037508.34, "bounds.bottom adjusted for floating precision");
|
||||
t.eq( bounds.right, 40075016.68, "bounds.right adjusted for floating precision");
|
||||
t.eq( bounds.top, 20037508.34, "bounds.top adjusted for floating precision");
|
||||
|
||||
// allow construction from a single arg
|
||||
bounds = new OpenLayers.Bounds([-180, -90, 180, 90]);
|
||||
t.ok(bounds instanceof OpenLayers.Bounds, "(array) correct instance");
|
||||
t.eq(bounds.left, -180, "(array) left");
|
||||
t.eq(bounds.bottom, -90, "(array) bottom");
|
||||
t.eq(bounds.right, 180, "(array) right");
|
||||
t.eq(bounds.top, 90, "(array) top");
|
||||
|
||||
}
|
||||
|
||||
function test_Bounds_constructorFromStrings(t) {
|
||||
@@ -621,7 +630,7 @@
|
||||
|
||||
}
|
||||
function test_Bounds_transform(t) {
|
||||
t.plan( 3 );
|
||||
t.plan( 5 );
|
||||
bounds = new OpenLayers.Bounds(10, -10, 20, 10);
|
||||
bounds.transform(new OpenLayers.Projection("foo"), new OpenLayers.Projection("Bar"));
|
||||
t.eq(bounds.toBBOX(), "10,-10,20,10", "null transform okay");
|
||||
@@ -629,6 +638,14 @@
|
||||
t.eq(bounds.toBBOX(), "1113194.907778,-1118889.974702,2226389.815556,1118889.974702", "bounds for spherical mercator transform are correct");
|
||||
bounds.transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:4326"));
|
||||
t.eq(bounds.toBBOX(), "10,-10,20,10", "bounds for inverse spherical mercator transform are correct");
|
||||
|
||||
// transform with string
|
||||
bounds = new OpenLayers.Bounds(10, -10, 20, 10);
|
||||
bounds.transform("EPSG:4326", "EPSG:900913");
|
||||
t.eq(bounds.toBBOX(), "1113194.907778,-1118889.974702,2226389.815556,1118889.974702", "(string) bounds for spherical mercator transform are correct");
|
||||
bounds.transform("EPSG:900913", "EPSG:4326");
|
||||
t.eq(bounds.toBBOX(), "10,-10,20,10", "(string) bounds for inverse spherical mercator transform are correct");
|
||||
|
||||
}
|
||||
|
||||
function test_Bounds_add(t) {
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
var lonlat;
|
||||
|
||||
function test_LonLat_constructor (t) {
|
||||
t.plan( 6 );
|
||||
t.plan( 8 );
|
||||
lonlat = new OpenLayers.LonLat(6, 5);
|
||||
t.ok( lonlat instanceof OpenLayers.LonLat, "new OpenLayers.LonLat returns LonLat object" );
|
||||
t.eq( lonlat.CLASS_NAME, "OpenLayers.LonLat", "lonlat.CLASS_NAME is set correctly");
|
||||
@@ -17,6 +17,12 @@
|
||||
lonlat = new OpenLayers.LonLat(20037508.33999999, -20037508.33999999);
|
||||
t.eq( lonlat.lon, 20037508.34, "lonlat.lon rounds correctly");
|
||||
t.eq( lonlat.lat, -20037508.34, "lonlat.lat rounds correctly");
|
||||
|
||||
// allow construction from single arg
|
||||
lonlat = new OpenLayers.LonLat([1, 2]);
|
||||
t.eq(lonlat.lon, 1, "lon from array");
|
||||
t.eq(lonlat.lat, 2, "lat from array");
|
||||
|
||||
}
|
||||
|
||||
function test_LonLat_constructorFromStrings (t) {
|
||||
@@ -160,7 +166,7 @@
|
||||
}
|
||||
|
||||
function test_LonLat_transform(t) {
|
||||
t.plan( 6 );
|
||||
t.plan(10);
|
||||
lonlat = new OpenLayers.LonLat(10, -10);
|
||||
lonlat.transform(new OpenLayers.Projection("foo"), new OpenLayers.Projection("Bar"));
|
||||
t.eq(lonlat.lon, 10, "lon for null transform is the same")
|
||||
@@ -171,6 +177,16 @@
|
||||
lonlat.transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:4326"));
|
||||
t.eq(lonlat.lon, 10, "lon for inverse spherical mercator transform is correct");
|
||||
t.eq(Math.round(lonlat.lat), -10, "lat for inverse spherical mercator correct")
|
||||
|
||||
// transform with string
|
||||
lonlat = new OpenLayers.LonLat(10, -10);
|
||||
lonlat.transform("EPSG:4326", "EPSG:900913");
|
||||
t.eq(Math.round(lonlat.lon), 1113195, "(string) lon for spherical mercator transform is correct");
|
||||
t.eq(Math.round(lonlat.lat), -1118890, "(string) lat for spherical mercator correct")
|
||||
lonlat.transform("EPSG:900913", "EPSG:4326");
|
||||
t.eq(lonlat.lon, 10, "(string) lon for inverse spherical mercator transform is correct");
|
||||
t.eq(Math.round(lonlat.lat), -10, "(string) lat for inverse spherical mercator correct")
|
||||
|
||||
}
|
||||
|
||||
function test_LonLat_wrapDateLine(t) {
|
||||
|
||||
@@ -50,6 +50,21 @@
|
||||
new OpenLayers.Projection("EPSG:900913"));
|
||||
t.eq(point.bounds, null, "Point bounds cleared after transform");
|
||||
}
|
||||
|
||||
function test_Point_transform_string(t) {
|
||||
t.plan(4);
|
||||
|
||||
var x = 10;
|
||||
var y = 20;
|
||||
point = new OpenLayers.Geometry.Point(x, y);
|
||||
point.calculateBounds();
|
||||
t.ok( point.bounds != null, "bounds calculated by calcBounds" );
|
||||
point.transform("EPSG:4326", "EPSG:900913");
|
||||
t.eq(point.bounds, null, "Point bounds cleared after transform");
|
||||
t.eq(point.x.toFixed(2), "1113194.91", "transformed x");
|
||||
t.eq(point.y.toFixed(2), "2273030.93", "transformed y");
|
||||
|
||||
}
|
||||
|
||||
function test_Point_distanceTo(t) {
|
||||
t.plan(7);
|
||||
|
||||
@@ -46,6 +46,19 @@
|
||||
t.eq(polygon.getBounds().toBBOX(), "556597.453889,334111.171355,1113194.907778,1574216.547942", "Bounds are correct")
|
||||
}
|
||||
|
||||
function test_Polygon_transform_string (t) {
|
||||
t.plan(3);
|
||||
|
||||
var components = [new OpenLayers.Geometry.Point(10,14), new OpenLayers.Geometry.Point(5,3)];
|
||||
var linearRing = new OpenLayers.Geometry.LinearRing(components);
|
||||
polygon = new OpenLayers.Geometry.Polygon([linearRing.clone()]);
|
||||
polygon.calculateBounds();
|
||||
t.ok( polygon.bounds != null, "bounds calculated by calcBounds" );
|
||||
polygon.transform("EPSG:4326", "EPSG:900913");
|
||||
t.eq(polygon.bounds, null, "Point bounds cleared after transform");
|
||||
t.eq(polygon.getBounds().toBBOX(), "556597.453889,334111.171355,1113194.907778,1574216.547942", "Bounds are correct")
|
||||
}
|
||||
|
||||
function test_Polygon_getArea(t) {
|
||||
t.plan( 5 );
|
||||
|
||||
|
||||
@@ -197,6 +197,39 @@
|
||||
t.eq(layer.numZoomLevels, numZoomLevels, "numZoomLevels set correctly");
|
||||
}
|
||||
|
||||
function test_maxExtent(t) {
|
||||
t.plan(5);
|
||||
|
||||
var layer = new OpenLayers.Layer(
|
||||
null, {maxExtent: [-180, 0, 0, 90]}
|
||||
);
|
||||
|
||||
t.ok(layer.maxExtent instanceof OpenLayers.Bounds, "(array) bounds instance");
|
||||
t.eq(layer.maxExtent.left, -180, "(array) bounds left");
|
||||
t.eq(layer.maxExtent.bottom, 0, "(array) bounds left");
|
||||
t.eq(layer.maxExtent.right, 0, "(array) bounds right");
|
||||
t.eq(layer.maxExtent.top, 90, "(array) bounds top");
|
||||
|
||||
layer.destroy();
|
||||
}
|
||||
|
||||
function test_minExtent(t) {
|
||||
t.plan(5);
|
||||
|
||||
var layer = new OpenLayers.Layer(
|
||||
null, {minExtent: [-180, 0, 0, 90]}
|
||||
);
|
||||
|
||||
t.ok(layer.minExtent instanceof OpenLayers.Bounds, "(array) bounds instance");
|
||||
t.eq(layer.minExtent.left, -180, "(array) bounds left");
|
||||
t.eq(layer.minExtent.bottom, 0, "(array) bounds left");
|
||||
t.eq(layer.minExtent.right, 0, "(array) bounds right");
|
||||
t.eq(layer.minExtent.top, 90, "(array) bounds top");
|
||||
|
||||
layer.destroy();
|
||||
}
|
||||
|
||||
|
||||
function test_eventListeners(t) {
|
||||
t.plan(1);
|
||||
|
||||
|
||||
@@ -378,6 +378,42 @@
|
||||
|
||||
}
|
||||
|
||||
function test_maxExtent(t) {
|
||||
t.plan(5);
|
||||
|
||||
var layer = new OpenLayers.Layer.WMS(
|
||||
null, "http://example.com/wms",
|
||||
{layers: "foo"},
|
||||
{maxExtent: [-180, 0, 0, 90]}
|
||||
);
|
||||
|
||||
t.ok(layer.maxExtent instanceof OpenLayers.Bounds, "(array) bounds instance");
|
||||
t.eq(layer.maxExtent.left, -180, "(array) bounds left");
|
||||
t.eq(layer.maxExtent.bottom, 0, "(array) bounds left");
|
||||
t.eq(layer.maxExtent.right, 0, "(array) bounds right");
|
||||
t.eq(layer.maxExtent.top, 90, "(array) bounds top");
|
||||
|
||||
layer.destroy();
|
||||
}
|
||||
|
||||
function test_minExtent(t) {
|
||||
t.plan(5);
|
||||
|
||||
var layer = new OpenLayers.Layer.WMS(
|
||||
null, "http://example.com/wms",
|
||||
{layers: "foo"},
|
||||
{minExtent: [-180, 0, 0, 90]}
|
||||
);
|
||||
|
||||
t.ok(layer.minExtent instanceof OpenLayers.Bounds, "(array) bounds instance");
|
||||
t.eq(layer.minExtent.left, -180, "(array) bounds left");
|
||||
t.eq(layer.minExtent.bottom, 0, "(array) bounds left");
|
||||
t.eq(layer.minExtent.right, 0, "(array) bounds right");
|
||||
t.eq(layer.minExtent.top, 90, "(array) bounds top");
|
||||
|
||||
layer.destroy();
|
||||
}
|
||||
|
||||
function test_tileOrigin(t) {
|
||||
t.plan(4);
|
||||
|
||||
|
||||
@@ -37,6 +37,41 @@
|
||||
map.destroy();
|
||||
}
|
||||
|
||||
function test_Map_constructor_convenience(t) {
|
||||
t.plan(13);
|
||||
var map = new OpenLayers.Map({
|
||||
maxExtent: [-170, -80, 170, 80],
|
||||
restrictedExtent: [-120, -65, 120, 65],
|
||||
layers: [
|
||||
new OpenLayers.Layer(null, {isBaseLayer: true})
|
||||
],
|
||||
center: [-111, 45],
|
||||
zoom: 3
|
||||
});
|
||||
|
||||
// maxExtent from array
|
||||
t.ok(map.maxExtent instanceof OpenLayers.Bounds, "maxExtent bounds");
|
||||
t.eq(map.maxExtent.left, -170, "maxExtent left");
|
||||
t.eq(map.maxExtent.bottom, -80, "maxExtent bottom");
|
||||
t.eq(map.maxExtent.right, 170, "maxExtent right");
|
||||
t.eq(map.maxExtent.top, 80, "maxExtent top");
|
||||
|
||||
// restrictedExtent from array
|
||||
t.ok(map.restrictedExtent instanceof OpenLayers.Bounds, "restrictedExtent bounds");
|
||||
t.eq(map.restrictedExtent.left, -120, "restrictedExtent left");
|
||||
t.eq(map.restrictedExtent.bottom, -65, "restrictedExtent bottom");
|
||||
t.eq(map.restrictedExtent.right, 120, "restrictedExtent right");
|
||||
t.eq(map.restrictedExtent.top, 65, "restrictedExtent top");
|
||||
|
||||
var center = map.getCenter();
|
||||
t.eq(center.lon, -111, "center lon");
|
||||
t.eq(center.lat, 45, "center lat");
|
||||
|
||||
t.eq(map.getZoom(), 3, "zoom");
|
||||
|
||||
map.destroy();
|
||||
}
|
||||
|
||||
function test_Map_constructor_late_rendering(t) {
|
||||
t.plan( 4 );
|
||||
|
||||
@@ -163,7 +198,7 @@
|
||||
}
|
||||
|
||||
function test_Map_center(t) {
|
||||
t.plan(11);
|
||||
t.plan(14);
|
||||
var log = [];
|
||||
map = new OpenLayers.Map('map', {
|
||||
eventListeners: {
|
||||
@@ -186,7 +221,7 @@
|
||||
t.ok( map.getCenter().equals(ll), "map center is correct after calling setCenter, zoom in");
|
||||
map.zoomOut();
|
||||
t.eq( map.getZoom(), 0, "map.zoom is correct after calling setCenter,zoom in, zoom out");
|
||||
|
||||
|
||||
log = [];
|
||||
map.zoomTo(5);
|
||||
t.eq(log[0], "movestart", "zoomTo fires movestart event");
|
||||
@@ -205,6 +240,13 @@
|
||||
map.getCenter().lon = 10;
|
||||
t.ok( map.getCenter().equals(ll), "map.getCenter returns a clone of map.center");
|
||||
|
||||
// allow calling setCenter with an array
|
||||
map.setCenter([4, 2]);
|
||||
var center = map.getCenter();
|
||||
t.ok(center instanceof OpenLayers.LonLat, "(array) center is lonlat");
|
||||
t.eq(center.lon, 4, "(array) center lon");
|
||||
t.eq(center.lat, 2, "(array) center lat");
|
||||
|
||||
map.destroy();
|
||||
}
|
||||
|
||||
@@ -1386,7 +1428,7 @@
|
||||
}
|
||||
|
||||
function test_Map_zoomToExtent(t) {
|
||||
t.plan(9);
|
||||
t.plan(12);
|
||||
|
||||
var map = new OpenLayers.Map("map");
|
||||
var layer = new OpenLayers.Layer(null, {isBaseLayer: true});
|
||||
@@ -1415,6 +1457,13 @@
|
||||
t.eq(center.lon, -105, "c) correct x");
|
||||
t.eq(center.lat, 42, "c) correct y");
|
||||
t.eq(map.getZoom(), 3, "c) correct zoom");
|
||||
|
||||
// accept array
|
||||
map.zoomToExtent([-160, 15, -50, 69]);
|
||||
center = map.getCenter();
|
||||
t.eq(center.lon, -105, "(array) correct x");
|
||||
t.eq(center.lat, 42, "(array) correct y");
|
||||
t.eq(map.getZoom(), 2, "(array) correct zoom");
|
||||
|
||||
map.destroy();
|
||||
}
|
||||
|
||||
@@ -60,7 +60,24 @@
|
||||
if (!hasProj) {
|
||||
window.Proj4js = undefined;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
function test_equals_string(t) {
|
||||
|
||||
t.plan(7);
|
||||
var gg = new OpenLayers.Projection("EPSG:4326");
|
||||
var sm = new OpenLayers.Projection("EPSG:900913");
|
||||
|
||||
// allow comparison with identifier
|
||||
t.eq(gg.equals("EPSG:4326"), true, "EPSG:4326 equality with string");
|
||||
t.eq(gg.equals("EPSG:4327"), false, "EPSG:4326 inequality with string");
|
||||
t.eq(sm.equals("EPSG:900913"), true, "EPSG:900913 equality with string");
|
||||
t.eq(sm.equals("EPSG:900914"), false, "EPSG:900913 inequality with string");
|
||||
t.eq(sm.equals("EPSG:3857"), true, "EPSG:900913 equality with EPSG:3857");
|
||||
t.eq(sm.equals("EPSG:102113"), true, "EPSG:900913 equality with EPSG:102113");
|
||||
t.eq(sm.equals("EPSG:102100"), true, "EPSG:900913 equality with EPSG:102100");
|
||||
|
||||
}
|
||||
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user