This commit is contained in:
Petr Pridal
2012-06-20 17:47:28 +02:00
18 changed files with 789 additions and 167 deletions

View File

@@ -47,4 +47,67 @@ ol.geom.point = function(opt_arg){
var p = new ol.geom.Point(x,y,z,projection);
return p;
};
goog.inherits(ol.geom.point, ol.geom.geometry);
goog.inherits(ol.geom.point, ol.geom.geometry);
/**
* @export
* @param {number=} opt_arg X.
* @return {ol.geom.Point|number} Result.
*/
ol.geom.Point.prototype.x = function(opt_arg){
if (arguments.length == 1 && goog.isDef(opt_arg)) {
this.setX(opt_arg);
return this;
}
else {
return this.getX();
}
};
/**
* @export
* @param {number=} opt_arg Y.
* @return {ol.geom.Point|number} Result.
*/
ol.geom.Point.prototype.y = function(opt_arg){
if (arguments.length == 1 && goog.isDef(opt_arg)) {
this.setY(opt_arg);
return this;
}
else {
return this.getY();
}
};
/**
* @export
* @param {number=} opt_arg Z.
* @return {ol.geom.Point|number|undefined} Result.
*/
ol.geom.Point.prototype.z = function(opt_arg){
if (arguments.length == 1 && goog.isDef(opt_arg)) {
this.setZ(opt_arg);
return this;
}
else {
return this.getZ();
}
};
/**
* @export
* @param {ol.Projection=} opt_arg Projection.
* @return {ol.geom.Point|ol.Projection|undefined} Result.
*/
ol.geom.Point.prototype.projection = function(opt_arg){
if (arguments.length == 1 && goog.isDef(opt_arg)) {
this.setProjection(ol.projection(opt_arg));
return this;
}
else {
return this.getProjection();
}
};

View File

@@ -22,11 +22,20 @@ ol.loc = function(opt_arg){
return opt_arg;
}
var x = 0;
var y = 0;
/** @type {number|undefined} */
var x;
/** @type {number|undefined} */
var y;
/** @type {number|undefined} */
var z;
/** @type {Object|undefined} */
var projection;
var usage = 'ol.loc accepts a coordinate array or an object with x, y, and (optional) z properties';
if (arguments.length == 1 && goog.isDef(opt_arg)) {
if (goog.isArray(opt_arg)) {
x = opt_arg[0];
@@ -39,9 +48,13 @@ ol.loc = function(opt_arg){
z = opt_arg['z'];
projection = opt_arg['projection'];
} else {
throw new Error('ol.loc');
throw new Error(usage);
}
}
if (!goog.isNumber(x) || !goog.isNumber(y)) {
throw new Error(usage);
}
if (goog.isDef(projection)) {
projection = ol.projection(projection);

View File

@@ -31,6 +31,8 @@ ol.map = function(opt_arg){
var userProjection;
/** @type {ol.Bounds|undefined} */
var maxExtent;
/** @type {ol.Bounds|undefined} */
var maxRes;
/** @type {Array.<number>|undefined} */
var resolutions;
/** @type {Array|undefined} */
@@ -47,6 +49,7 @@ ol.map = function(opt_arg){
projection = opt_arg['projection'];
userProjection = opt_arg['userProjection'];
maxExtent = opt_arg['maxExtent'];
maxRes = opt_arg['maxRes'];
resolutions = opt_arg['resolutions'];
layers = opt_arg['layers'];
}
@@ -74,6 +77,9 @@ ol.map = function(opt_arg){
if (goog.isDef(maxExtent)) {
map.setMaxExtent(ol.bounds(maxExtent));
}
if (goog.isDef(maxRes)) {
map.setMaxRes(maxRes);
}
if (goog.isDef(resolutions)) {
map.setResolutions(resolutions);
}
@@ -187,3 +193,24 @@ ol.Map.prototype.maxExtent = function(opt_arg) {
return this.getMaxExtent();
}
};
/**
* @param {number=} opt_arg
* @returns {ol.Map|number|undefined} Map maximum resolution
*/
ol.Map.prototype.maxRes = function(opt_arg) {
if (arguments.length == 1 && goog.isDef(opt_arg)) {
this.setMaxRes(opt_arg);
return this;
} else {
return this.getMaxRes();
}
};
/**
* @param {number} arg
* @returns {number} resolution for a given zoom level
*/
ol.Map.prototype.getResForZoom = function(arg) {
return this.getResolutionForZoom(arg);
};

View File

@@ -21,6 +21,9 @@ ol.projection = function(opt_arg){
/** @type {undefined|number} */
var units;
/** @type {undefined|Array|ol.UnreferencedBounds} */
var extent;
if (arguments.length == 1 && goog.isDefAndNotNull(opt_arg)) {
if (opt_arg instanceof ol.Projection) {
return opt_arg;
@@ -35,13 +38,21 @@ ol.projection = function(opt_arg){
throw new Error('Projection requires a string code.');
}
units = opt_arg['units'];
extent = opt_arg['maxExtent'];
}
else {
throw new Error('ol.projection');
}
}
var proj = new ol.Projection(code);
proj.setUnits(units);
if (goog.isDef(units)) {
proj.setUnits(units);
}
if (goog.isDef(extent)) {
proj.setExtent(
new ol.UnreferencedBounds(extent[0],extent[1],extent[2],extent[3])
);
}
return proj;
};

View File

@@ -33,9 +33,9 @@ ol.Loc = function(x, y, opt_z, opt_projection) {
/**
* @private
* @type {ol.Projection|undefined}
* @type {ol.Projection}
*/
this.projection_ = opt_projection;
this.projection_ = goog.isDef(opt_projection) ? opt_projection : null;
};
@@ -73,7 +73,7 @@ ol.Loc.prototype.getZ = function() {
/**
* @param {ol.Projection|undefined} projection Projection.
* @param {ol.Projection} projection Projection.
*/
ol.Loc.prototype.setProjection = function(projection) {
this.projection_ = projection;
@@ -118,7 +118,7 @@ ol.Loc.prototype.transform = function(proj) {
if (goog.isString(proj)) {
proj = new ol.Projection(proj);
}
return this.transform_(proj);
return this._transform(proj);
};
/**
@@ -127,8 +127,8 @@ ol.Loc.prototype.transform = function(proj) {
* @param {!ol.Projection} proj The destination projection.
* @returns {!ol.Loc}
*/
ol.Loc.prototype.transform_ = function(proj) {
var point = {x: this.x_, y: this.y_};
ol.Loc.prototype._transform = function(proj) {
var point = {'x': this.x_, 'y': this.y_};
var sourceProj = this.projection_;
if (!goog.isDefAndNotNull(sourceProj)) {
throw new Error("Cannot transform a location without a source projection.");

View File

@@ -42,16 +42,28 @@ ol.Map = function() {
/**
* @private
* @type {Array|undefined}
* @type {Array}
*/
this.resolutions_ = null;
/**
* @private
* @type {Array|undefined}
* @type {Array}
*/
this.layers_ = null;
/**
* @private
* @type {ol.UnreferencedBounds}
*/
this.maxExtent_ = null;
/**
* @private
* @type {number|undefined}
*/
this.maxRes_ = undefined;
};
/**
@@ -64,12 +76,23 @@ ol.Map.prototype.DEFAULT_PROJECTION = "EPSG:3857";
@type {string}
*/
ol.Map.prototype.DEFAULT_USER_PROJECTION = "EPSG:4326";
/**
@const
@type {number}
*/
ol.Map.ZOOM_FACTOR = 2;
/**
@const
@type {number}
*/
ol.Map.DEFAULT_TILE_SIZE = 256;
/**
* @return {ol.Loc} Location.
*/
ol.Map.prototype.getCenter = function() {
var proj = this.getUserProjection();
this.center_ = this.center_.transform(proj);
return this.center_;
};
@@ -145,11 +168,47 @@ ol.Map.prototype.getMaxExtent = function() {
};
/**
* @return {number} the max resolution for the map
*/
ol.Map.prototype.getMaxRes = function() {
if (goog.isDefAndNotNull(this.maxRes_)) {
return this.maxRes_;
} else {
var extent = this.getMaxExtent();
var dim = Math.max(
(extent.getMaxX()-extent.getMinX()),
(extent.getMaxY()-extent.getMinY())
);
return dim/ol.Map.DEFAULT_TILE_SIZE;
}
};
/**
* @param {number} zoom the zoom level being requested
* @return {number} the resolution for the map at the given zoom level
*/
ol.Map.prototype.getResolutionForZoom = function(zoom) {
if (goog.isDefAndNotNull(this.resolutions_)) {
return this.resolutions_[zoom];
} else {
var maxRes = this.getMaxRes();
return maxRes/Math.pow(ol.Map.ZOOM_FACTOR, zoom);
}
};
/**
* @param {ol.Loc} center Center.
*/
ol.Map.prototype.setCenter = function(center) {
this.center_ = center;
var proj = center.getProjection();
if (goog.isNull(proj)) {
proj = this.getUserProjection();
center.setProjection(proj);
}
this.center_ = center.transform(this.getProjection());
};
@@ -199,12 +258,19 @@ ol.Map.prototype.setLayers = function(layers) {
};
/**
* @param {ol.Bounds} extent the maxExtent for the map
* @param {ol.UnreferencedBounds} extent the maxExtent for the map
*/
ol.Map.prototype.setMaxExtent = function(extent) {
this.maxExtent_ = extent;
};
/**
* @param {number} res the max resolution for the map
*/
ol.Map.prototype.setMaxRes = function(res) {
this.maxRes_ = res;
};
/**
*/
ol.Map.prototype.destroy = function() {

View File

@@ -22,15 +22,15 @@ ol.Projection = function(code) {
/**
* @private
* @type {!Object|undefined}
* @type {Object}
*/
this.proj_ = undefined;
this.proj_ = null;
/**
* @private
* @type {!ol.UnreferencedBounds|undefined}
* @type {ol.UnreferencedBounds}
*/
this.extent_ = undefined;
this.extent_ = null;
};
@@ -66,9 +66,18 @@ ol.Projection.prototype.setUnits = function(units) {
/**
* Get the validity extent of the coordinate reference system.
*
* @return {!ol.UnreferencedBounds|undefined} The valididty extent.
* @return {ol.UnreferencedBounds} The valididty extent.
*/
ol.Projection.prototype.getExtent = function() {
if (goog.isNull(this.extent_)) {
var defs = ol.Projection['defaults'][this.code_];
if (goog.isDef(defs)) {
var ext = defs['maxExtent'];
if (goog.isDef(ext)) {
this.setExtent(new ol.UnreferencedBounds(ext[0],ext[1],ext[2],ext[3]));
}
}
}
return this.extent_;
};

View File

@@ -11,21 +11,21 @@ goog.require('goog.style');
/**
* Determine whether event was caused by a single touch
*
* @param {Event} evt
* @param {!Event} evt
* @return {boolean}
*/
ol.event.isSingleTouch = function(evt) {
return evt.touches && evt.touches.length == 1;
return !!(evt.touches && evt.touches.length == 1);
};
/**
* Determine whether event was caused by a multi touch
*
* @param {Event} evt
* @param {!Event} evt
* @return {boolean}
*/
ol.event.isMultiTouch = function(evt) {
return evt.touches && evt.touches.length > 1;
return !!(evt.touches && evt.touches.length > 1);
};
@@ -83,7 +83,7 @@ ol.event.Events.prototype.getObject = function() {
* @param {boolean} includeXY
*/
ol.event.Events.prototype.setIncludeXY = function(includeXY) {
this._includeXY = includeXY;
this.includeXY_ = includeXY;
};
/**
@@ -215,7 +215,7 @@ ol.event.Events.prototype.un = function(object) {
};
/**
* Unregister a listener for an egent
* Unregister a listener for an event
*
* @param {string} type Name of the event to unregister
* @param {Function} listener The callback function.
@@ -242,6 +242,9 @@ ol.event.Events.prototype.triggerEvent = function(type, evt) {
var returnValue,
listeners = goog.events.getListeners(this, type, true)
.concat(goog.events.getListeners(this, type, false));
if (arguments.length === 1) {
evt = {type: type};
}
for (var i=0, ii=listeners.length; i<ii; ++i) {
returnValue = listeners[i].handleEvent(evt);
if (returnValue === false) {
@@ -283,7 +286,7 @@ ol.event.Events.prototype.handleBrowserEvent = function(evt) {
evt.clientX = x / num;
evt.clientY = y / num;
}
if (this.includeXY) {
if (this.includeXY_) {
var element = /** @type {!Element} */ this.element_;
evt.xy = goog.style.getRelativePosition(evt, element);
}
@@ -294,6 +297,7 @@ ol.event.Events.prototype.handleBrowserEvent = function(evt) {
* Destroy this Events instance.
*/
ol.event.Events.prototype.destroy = function() {
this.setElement();
for (var p in this) {
delete this[p];
}

View File

@@ -106,3 +106,37 @@ ol.geom.Point.prototype.setZ = function(z) {
this.z_ = z;
};
/**
* Transform this point to another coordinate reference system. This
* requires that this point has a projection set already (if not, an error
* will be thrown). Returns a new point object and does not modify this
* point.
*
* @param {string|!ol.Projection} proj The destination projection. Can be
* supplied as a projection instance of a string identifier.
* @returns {!ol.geom.Point} A new location.
*/
ol.geom.Point.prototype.transform = function(proj) {
if (goog.isString(proj)) {
proj = new ol.Projection(proj);
}
return this.transform_(proj);
};
/**
* Transform this point to a new location given a projection object.
*
* @param {!ol.Projection} proj The destination projection.
* @returns {!ol.geom.Point}
* @private
*/
ol.geom.Point.prototype.transform_ = function(proj) {
var point = {x: this.x_, y: this.y_};
var sourceProj = this.projection_;
if (!goog.isDefAndNotNull(sourceProj)) {
throw new Error("Cannot transform a point without a source projection.");
}
ol.Projection.transform(point, sourceProj, proj);
return new ol.geom.Point(point.x, point.y, this.z_, proj);
};

View File

@@ -8,7 +8,7 @@ goog.require('ol.TileSet');
* Class for XYZ layers.
* @constructor
* @param {string} url URL template. E.g.
* http://a.tile.openstreetmap.org/${z}/${x}/${y}.png.
* http://a.tile.openstreetmap.org/{z}/{x}/{y}.png.
*/
ol.layer.XYZ = function(url) {
@@ -108,9 +108,10 @@ ol.layer.XYZ.prototype.setResolutions = function(resolutions) {
*/
ol.layer.XYZ.prototype.getData = function(bounds, resolution) {
var me = this,
zoom = me.zoomForResolution_(resolution);
zoom = me.zoomForResolution(resolution);
resolution = me.resolutions_[zoom];
// define some values used for the actual tiling
var boundsMinX = bounds.getMinX(),
boundsMaxX = bounds.getMaxX(),
boundsMinY = bounds.getMinY(),
@@ -130,26 +131,27 @@ ol.layer.XYZ.prototype.getData = function(bounds, resolution) {
offsetY = Math.floor(
(tileOriginY - boundsMaxY) / tileHeightGeo),
gridWidth = Math.ceil(
(boundsMaxX - boundsMinX) / tileWidthGeo),
gridHeight = Math.ceil(
(boundsMaxY - boundsMinY) / tileHeightGeo);
gridLeft = tileOriginX + tileWidthGeo * offsetX,
gridTop = tileOriginY - tileHeightGeo * offsetY;
// now tile
var tiles = [],
tile,
url,
i, ii,
j, jj;
for (i=0, ii=gridWidth; i<ii; i++) {
tiles[i] = [];
for (j=0, jj=gridHeight; j<jj; j++) {
url = me.url_.replace('{x}', offsetX + i + '')
.replace('{y}', offsetY + j + '')
x = 0,
y = 0;
while (gridTop - (y * tileHeightGeo) > boundsMinY) {
tiles[y] = [];
while (gridLeft + (x * tileWidthGeo) < boundsMaxX) {
url = me.url_.replace('{x}', offsetX + x + '')
.replace('{y}', offsetY + y + '')
.replace('{z}', zoom);
tile = new ol.Tile(url);
tiles[i][j] = tile;
tiles[y][x] = tile;
x++;
}
y++;
x = 0;
}
return new ol.TileSet(tiles, tileWidth, tileHeight, resolution);
@@ -157,10 +159,9 @@ ol.layer.XYZ.prototype.getData = function(bounds, resolution) {
/**
* Get the zoom level (z) for the given resolution.
* @private
* @param {number} resolution
*/
ol.layer.XYZ.prototype.zoomForResolution_ = function(resolution) {
ol.layer.XYZ.prototype.zoomForResolution = function(resolution) {
var delta = Number.POSITIVE_INFINITY,
currentDelta,
resolutions = this.resolutions_;

View File

@@ -1,31 +0,0 @@
goog.provide('ol.mixins.coordinate');
goog.require('goog.object');
goog.object.extend(ol.mixins.coordinate, {
getX : function() {
return this.x_;
},
getY : function() {
return this.y_;
},
getZ : function() {
return this.z_;
},
setX : function(x) {
this.x_ = x;
return this;
},
setY : function(y) {
this.y_ = y;
return this;
},
setZ: function(z) {
this.z_ = z;
return this;
}
});