Use object literals and use generic tile layer

This commit is contained in:
Tom Payne
2012-09-27 12:41:45 +02:00
parent 5ddbf17c8d
commit 65b8e0f915
37 changed files with 803 additions and 646 deletions

View File

@@ -16,14 +16,13 @@
"exports/ol/collection.js",
"exports/ol/coordinate.js",
"exports/ol/extent.js",
"exports/ol/layer/bingmaps.js",
"exports/ol/layer/mapquest.js",
"exports/ol/layer/openstreetmap.js",
"exports/ol/layer/tilejson.js",
"exports/ol/layer/stamen.js",
"exports/ol/layer/tilelayer.js",
"exports/ol/map.js",
"exports/ol/object.js",
"exports/ol/projection.js"
"exports/ol/projection.js",
"exports/ol/source/mapquest.js",
"exports/ol/source/openstreetmap.js",
"exports/ol/source/stamen.js"
],
"output-wrapper": "(function(){%output%})();"

View File

@@ -18,7 +18,9 @@
<script src="../../build/ol.js" type="text/javascript"></script>
<script type="text/javascript">
var layer = new ol.layer.MapQuestOpenAerial();
var layer = new ol.layer.TileLayer({
source: new ol.source.MapQuestOpenAerial()
});
var map = new ol.Map(document.getElementById('map'), {
center: new ol.Coordinate(0, 0),
layers: new ol.Collection([layer]),

View File

@@ -6,7 +6,7 @@ goog.require('ol.Coordinate');
goog.require('ol.Map');
goog.require('ol.MapOptions'); // FIXME this should not be required
goog.require('ol.control.Zoom');
goog.require('ol.layer.MapQuestOpenAerial');
goog.require('ol.source.MapQuestOpenAerial');
if (goog.DEBUG) {
@@ -15,7 +15,9 @@ if (goog.DEBUG) {
}
var layer = new ol.layer.MapQuestOpenAerial();
var layer = new ol.layer.TileLayer({
source: new ol.source.MapQuestOpenAerial()
});
var map = new ol.Map(document.getElementById('map'), {
center: new ol.Coordinate(0, 0),
layers: new ol.Collection([layer]),

View File

@@ -7,7 +7,8 @@ goog.require('ol.RendererHint');
goog.require('ol.control.Attribution');
goog.require('ol.control.MousePosition');
goog.require('ol.interaction.Keyboard');
goog.require('ol.layer.MapQuestOpenAerial');
goog.require('ol.layer.TileLayer');
goog.require('ol.source.MapQuestOpenAerial');
if (goog.DEBUG) {
@@ -16,7 +17,9 @@ if (goog.DEBUG) {
}
var layer = new ol.layer.MapQuestOpenAerial();
var layer = new ol.layer.TileLayer({
source: new ol.source.MapQuestOpenAerial()
});
var domMap = new ol.Map(document.getElementById('domMap'), {
center: new ol.Coordinate(0, 0),

View File

@@ -4,16 +4,23 @@ goog.require('ol.Map');
goog.require('ol.Projection');
goog.require('ol.RendererHint');
goog.require('ol.control.Attribution');
goog.require('ol.layer.BingMaps');
goog.require('ol.layer.TileJSON');
goog.require('ol.layer.TileLayer');
goog.require('ol.source.BingMaps');
goog.require('ol.source.TileJSON');
var layers = new ol.Collection([
new ol.layer.BingMaps(
ol.BingMapsStyle.AERIAL,
'AheP841R-MsLErKQChaTba_xDoOCl40-EeTubD9uNhNAyQTePwFY9iVD1_pyqqlE'),
new ol.layer.TileJSON(
'http://api.tiles.mapbox.com/v3/mapbox.va-quake-aug.jsonp')
new ol.layer.TileLayer({
source: new ol.source.BingMaps({
key: 'AheP841R-MsLErKQChaTba_xDoOCl40-EeTubD9uNhNAyQTePwFY9iVD1_pyqqlE',
style: ol.BingMapsStyle.AERIAL
})
}),
new ol.layer.TileLayer({
source: new ol.source.TileJSON({
uri: 'http://api.tiles.mapbox.com/v3/mapbox.va-quake-aug.jsonp'
})
})
]);
var webglMap = new ol.Map(document.getElementById('webglMap'), {

View File

@@ -1,3 +0,0 @@
goog.require('ol.layer.BingMaps');
goog.exportSymbol('ol.layer.BingMaps', ol.layer.BingMaps);

View File

@@ -1,5 +0,0 @@
goog.require('ol.layer.MapQuestOSM');
goog.require('ol.layer.MapQuestOpenAerial');
goog.exportSymbol('ol.layer.MapQuestOSM', ol.layer.MapQuestOSM);
goog.exportSymbol('ol.layer.MapQuestOpenAerial', ol.layer.MapQuestOpenAerial);

View File

@@ -1,3 +0,0 @@
goog.require('ol.layer.OpenStreetMap');
goog.exportSymbol('ol.layer.OpenStreetMap', ol.layer.OpenStreetMap);

View File

@@ -1,3 +0,0 @@
goog.require('ol.layer.Stamen');
goog.exportSymbol('ol.layer.Stamen', ol.layer.Stamen);

View File

@@ -1,3 +0,0 @@
goog.require('ol.layer.TileJSON');
goog.exportSymbol('ol.layer.TileJSON', ol.layer.TileJSON);

View File

@@ -0,0 +1,3 @@
goog.require('ol.layer.TileLayer');
goog.exportSymbol('ol.layer.TileLayer', ol.layer.TileLayer);

View File

@@ -0,0 +1,3 @@
goog.require('ol.source.BingMaps');
goog.exportSymbol('ol.source.BingMaps', ol.source.BingMaps);

View File

@@ -0,0 +1,5 @@
goog.require('ol.source.MapQuestOSM');
goog.require('ol.source.MapQuestOpenAerial');
goog.exportSymbol('ol.source.MapQuestOSM', ol.source.MapQuestOSM);
goog.exportSymbol('ol.source.MapQuestOpenAerial', ol.source.MapQuestOpenAerial);

View File

@@ -0,0 +1,3 @@
goog.require('ol.source.OpenStreetMap');
goog.exportSymbol('ol.source.OpenStreetMap', ol.source.OpenStreetMap);

View File

@@ -0,0 +1,3 @@
goog.require('ol.source.Stamen');
goog.exportSymbol('ol.source.Stamen', ol.source.Stamen);

View File

@@ -0,0 +1,3 @@
goog.require('ol.source.TileJSON');
goog.exportSymbol('ol.source.TileJSON', ol.source.TileJSON);

View File

@@ -181,8 +181,8 @@ ol.control.Attribution.prototype.getLayerAttributionVisiblities_ =
}
var mapZ;
if (source instanceof ol.TileSource) {
var tileSource = /** @type {ol.TileSource} */ source;
if (source instanceof ol.source.TileSource) {
var tileSource = /** @type {ol.source.TileSource} */ source;
var tileGrid = tileSource.getTileGrid();
mapZ = tileGrid.getZForResolution(mapResolution);
}
@@ -214,7 +214,7 @@ ol.control.Attribution.prototype.getLayerAttributionVisiblities_ =
}
if (!goog.isNull(coverageAreas)) {
if (source instanceof ol.TileSource) {
if (source instanceof ol.source.TileSource) {
attributionVisible = goog.array.some(
coverageAreas,
function(coverageArea, index) {

View File

@@ -1,9 +1,24 @@
goog.provide('ol.layer.Layer');
goog.provide('ol.layer.LayerOptions');
goog.provide('ol.layer.LayerProperty');
goog.require('goog.events');
goog.require('goog.events.EventType');
goog.require('goog.math');
goog.require('ol.Object');
goog.require('ol.Source');
goog.require('ol.source.Source');
/**
* @typedef {{brightness: (number|undefined),
* contrast: (number|undefined),
* hue: (number|undefined),
* opacity: (number|undefined),
* saturation: (number|undefined),
* source: ol.source.Source,
* visible: (boolean|undefined)}}
*/
ol.layer.LayerOptions;
/**
@@ -23,34 +38,47 @@ ol.layer.LayerProperty = {
/**
* @constructor
* @extends {ol.Object}
* @param {ol.Source} source Source.
* @param {Object.<string, *>=} opt_values Values.
* @param {ol.layer.LayerOptions} layerOptions LayerOptions.
*/
ol.layer.Layer = function(source, opt_values) {
ol.layer.Layer = function(layerOptions) {
goog.base(this);
/**
* @private
* @type {ol.Source}
* @type {ol.source.Source}
*/
this.source_ = source;
this.source_ = layerOptions.source;
this.setBrightness(0);
this.setContrast(0);
this.setHue(0);
this.setOpacity(1);
this.setSaturation(0);
this.setVisible(true);
this.setBrightness(
goog.isDef(layerOptions.brightness) ? layerOptions.brightness : 0);
this.setContrast(
goog.isDef(layerOptions.contrast) ? layerOptions.contrast : 0);
this.setHue(
goog.isDef(layerOptions.hue) ? layerOptions.hue : 0);
this.setOpacity(
goog.isDef(layerOptions.opacity) ? layerOptions.opacity : 1);
this.setSaturation(
goog.isDef(layerOptions.saturation) ? layerOptions.saturation : 0);
this.setVisible(
goog.isDef(layerOptions.visible) ? layerOptions.visible : true);
if (goog.isDef(opt_values)) {
this.setValues(opt_values);
if (!this.source_.isReady()) {
goog.events.listenOnce(this.source_, goog.events.EventType.LOAD,
this.handleSourceLoad_, false, this);
}
};
goog.inherits(ol.layer.Layer, ol.Object);
/**
*/
ol.layer.Layer.prototype.dispatchLoadEvent = function() {
this.dispatchEvent(goog.events.EventType.LOAD);
};
/**
* @return {number} Brightness.
*/
@@ -112,7 +140,7 @@ goog.exportProperty(
/**
* @return {ol.Source} Source.
* @return {ol.source.Source} Source.
*/
ol.layer.Layer.prototype.getSource = function() {
return this.source_;
@@ -131,6 +159,14 @@ goog.exportProperty(
ol.layer.Layer.prototype.getVisible);
/**
* @private
*/
ol.layer.Layer.prototype.handleSourceLoad_ = function() {
this.dispatchLoadEvent();
};
/**
* @return {boolean} Is ready.
*/

View File

@@ -1,40 +0,0 @@
goog.provide('ol.layer.OpenStreetMap');
goog.provide('ol.source.OpenStreetMap');
goog.require('ol.TileUrlFunction');
goog.require('ol.layer.TileLayer');
goog.require('ol.source.XYZ');
/**
* @constructor
* @extends {ol.layer.TileLayer}
* @param {Object.<string, *>=} opt_values Values.
*/
ol.layer.OpenStreetMap = function(opt_values) {
var tileSource = new ol.source.OpenStreetMap();
goog.base(this, tileSource, opt_values);
};
goog.inherits(ol.layer.OpenStreetMap, ol.layer.TileLayer);
/**
* @constructor
* @extends {ol.source.XYZ}
*/
ol.source.OpenStreetMap = function() {
var tileUrlFunction = ol.TileUrlFunction.createFromTemplate(
'http://{a-c}.tile.openstreetmap.org/{z}/{x}/{y}.png');
var attribution = new ol.Attribution(
'&copy; <a href="http://www.openstreetmap.org">OpenStreetMap</a> ' +
'contributors, ' +
'<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC BY-SA</a>');
goog.base(this, 18, tileUrlFunction, [attribution]);
};
goog.inherits(ol.source.OpenStreetMap, ol.source.XYZ);

View File

@@ -1,25 +1,25 @@
goog.provide('ol.layer.TileLayer');
goog.require('ol.TileSource');
goog.require('ol.layer.Layer');
goog.require('ol.layer.LayerOptions');
goog.require('ol.source.TileSource');
/**
* @constructor
* @extends {ol.layer.Layer}
* @param {ol.TileSource} tileSource Tile source.
* @param {Object.<string, *>=} opt_values Values.
* @param {ol.layer.LayerOptions} layerOptions Layer options.
*/
ol.layer.TileLayer = function(tileSource, opt_values) {
goog.base(this, tileSource, opt_values);
ol.layer.TileLayer = function(layerOptions) {
goog.base(this, layerOptions);
};
goog.inherits(ol.layer.TileLayer, ol.layer.Layer);
/**
* @return {ol.TileSource} Source.
* @return {ol.source.TileSource} Source.
*/
ol.layer.TileLayer.prototype.getTileSource = function() {
return /** @type {ol.TileSource} */ this.getSource();
return /** @type {ol.source.TileSource} */ this.getSource();
};

View File

@@ -1,118 +0,0 @@
goog.provide('ol.layer.XYZ');
goog.provide('ol.source.XYZ');
goog.provide('ol.tilegrid.XYZ');
goog.require('goog.math');
goog.require('ol.Attribution');
goog.require('ol.Coordinate');
goog.require('ol.Projection');
goog.require('ol.Size');
goog.require('ol.TileCoord');
goog.require('ol.TileGrid');
goog.require('ol.TileSource');
goog.require('ol.TileUrlFunction');
goog.require('ol.layer.Layer');
goog.require('ol.layer.TileLayer');
/**
* @constructor
* @extends {ol.TileGrid}
* @param {number} maxZoom Maximum zoom.
* @param {ol.Size=} opt_tileSize Tile size.
*/
ol.tilegrid.XYZ = function(maxZoom, opt_tileSize) {
var resolutions = new Array(maxZoom + 1);
var z;
for (z = 0; z <= maxZoom; ++z) {
resolutions[z] = ol.Projection.EPSG_3857_HALF_SIZE / (128 << z);
}
var extent = ol.Projection.EPSG_3857_EXTENT;
var origin = new ol.Coordinate(
-ol.Projection.EPSG_3857_HALF_SIZE, ol.Projection.EPSG_3857_HALF_SIZE);
goog.base(this, resolutions, extent, origin, opt_tileSize);
};
goog.inherits(ol.tilegrid.XYZ, ol.TileGrid);
/**
* @inheritDoc
*/
ol.tilegrid.XYZ.prototype.forEachTileCoordParentTileRange =
function(tileCoord, callback, opt_obj) {
var x = tileCoord.x;
var y = tileCoord.y;
var z = tileCoord.z;
var tileRange;
while (true) {
z -= 1;
if (z < 0) {
break;
}
x = Math.floor(x / 2);
y = Math.floor(y / 2);
tileRange = new ol.TileRange(x, y, x, y);
if (callback.call(opt_obj, z, tileRange)) {
break;
}
}
};
/**
* @constructor
* @extends {ol.layer.TileLayer}
* @param {number} maxZoom Maximum zoom.
* @param {ol.TileUrlFunctionType} tileUrlFunction Tile URL function.
* @param {Array.<ol.Attribution>=} opt_attributions Attributions.
* @param {string=} opt_crossOrigin Cross origin.
* @param {Object.<string, *>=} opt_values Values.
*/
ol.layer.XYZ = function(
maxZoom, tileUrlFunction, opt_attributions, opt_crossOrigin, opt_values) {
var tileSource = new ol.source.XYZ(
maxZoom, tileUrlFunction, opt_attributions, opt_crossOrigin);
goog.base(this, tileSource, opt_values);
};
goog.inherits(ol.layer.XYZ, ol.layer.TileLayer);
/**
* @constructor
* @extends {ol.TileSource}
* @param {number} maxZoom Maximum zoom.
* @param {ol.TileUrlFunctionType} tileUrlFunction Tile URL function.
* @param {Array.<ol.Attribution>=} opt_attributions Attributions.
* @param {string=} opt_crossOrigin Cross origin.
*/
ol.source.XYZ =
function(maxZoom, tileUrlFunction, opt_attributions, opt_crossOrigin) {
var projection = ol.Projection.getFromCode('EPSG:3857');
var tileGrid = new ol.tilegrid.XYZ(maxZoom);
var tileUrlFunction2 = ol.TileUrlFunction.withTileCoordTransform(
function(tileCoord) {
var n = 1 << tileCoord.z;
var y = -tileCoord.y - 1;
if (y < 0 || n <= y) {
return null;
} else {
var x = goog.math.modulo(tileCoord.x, n);
return new ol.TileCoord(tileCoord.z, x, y);
}
},
tileUrlFunction);
var extent = projection.getExtent();
goog.base(this, projection, tileGrid, tileUrlFunction2, extent,
opt_attributions, opt_crossOrigin);
};
goog.inherits(ol.source.XYZ, ol.TileSource);

View File

@@ -1,96 +0,0 @@
goog.provide('ol.Source');
goog.require('goog.functions');
goog.require('ol.Attribution');
goog.require('ol.Extent');
goog.require('ol.Projection');
/**
* @constructor
* @param {ol.Projection} projection Projection.
* @param {ol.Extent=} opt_extent Extent.
* @param {Array.<ol.Attribution>=} opt_attributions Attributions.
*/
ol.Source = function(projection, opt_extent, opt_attributions) {
/**
* @private
* @type {ol.Projection}
*/
this.projection_ = projection;
/**
* @private
* @type {ol.Extent}
*/
this.extent_ = opt_extent || projection.getExtent();
/**
* @private
* @type {Array.<ol.Attribution>}
*/
this.attributions_ = opt_attributions || null;
};
/**
* @return {Array.<ol.Attribution>} Attributions.
*/
ol.Source.prototype.getAttributions = function() {
return this.attributions_;
};
/**
* @return {ol.Extent} Extent.
*/
ol.Source.prototype.getExtent = function() {
return this.extent_;
};
/**
* @return {ol.Projection} Projection.
*/
ol.Source.prototype.getProjection = function() {
return this.projection_;
};
/**
* @return {Array.<number>|undefined} Resolutions.
*/
ol.Source.prototype.getResolutions = goog.abstractMethod;
/**
* @return {boolean} Is ready.
*/
ol.Source.prototype.isReady = goog.functions.TRUE;
/**
* @param {Array.<ol.Attribution>} attributions Attributions.
*/
ol.Source.prototype.setAttributions = function(attributions) {
this.attributions_ = attributions;
};
/**
* @param {ol.Extent} extent Extent.
*/
ol.Source.prototype.setExtent = function(extent) {
this.extent_ = extent;
};
/**
* @param {ol.Projection} projection Projetion.
*/
ol.Source.prototype.setProjection = function(projection) {
this.projection_ = projection;
};

View File

@@ -1,4 +1,3 @@
goog.provide('ol.layer.BingMaps');
goog.provide('ol.source.BingMaps');
goog.require('goog.Uri');
@@ -6,8 +5,7 @@ goog.require('goog.events');
goog.require('goog.events.EventType');
goog.require('goog.net.Jsonp');
goog.require('ol.TileCoverageArea');
goog.require('ol.TileSource');
goog.require('ol.layer.TileLayer');
goog.require('ol.source.TileSource');
goog.require('ol.tilegrid.XYZ');
@@ -23,43 +21,32 @@ ol.BingMapsStyle = {
};
/**
* @constructor
* @extends {ol.layer.TileLayer}
* @param {ol.BingMapsStyle} style Bing Maps style.
* @param {string} key Key.
* @param {string=} opt_culture Culture.
* @param {Object.<string, *>=} opt_values Values.
* @typedef {{culture: (string|undefined),
* key: string,
* style: ol.BingMapsStyle}}
*/
ol.layer.BingMaps = function(style, key, opt_culture, opt_values) {
var tileSource = new ol.source.BingMaps(style, key, opt_culture,
function(tileSource) {
this.dispatchEvent(goog.events.EventType.LOAD);
}, this);
goog.base(this, tileSource, opt_values);
};
goog.inherits(ol.layer.BingMaps, ol.layer.TileLayer);
ol.source.BingMapsOptions;
/**
* @constructor
* @extends {ol.TileSource}
* @param {ol.BingMapsStyle} style Bing Maps style.
* @param {string} key Key.
* @param {string=} opt_culture Culture.
* @param {?function(ol.source.BingMaps)=} opt_callback Callback.
* @param {*=} opt_obj Object.
* @extends {ol.source.TileSource}
* @param {ol.source.BingMapsOptions} bingMapsOptions Bing Maps options.
*/
ol.source.BingMaps =
function(style, key, opt_culture, opt_callback, opt_obj) {
ol.source.BingMaps = function(bingMapsOptions) {
goog.base(this, {
projection: ol.Projection.getFromCode('EPSG:3857')
});
/**
* @private
* @type {string}
*/
this.culture_ = opt_culture || 'en-us';
this.culture_ = goog.isDef(bingMapsOptions.culture) ?
bingMapsOptions.culture : 'en-us';
/**
* @private
@@ -67,34 +54,17 @@ ol.source.BingMaps =
*/
this.ready_ = false;
/**
* @private
* @type {?function(ol.source.BingMaps)}
*/
this.callback_ = opt_callback || null;
/**
* @private
* @type {*}
*/
this.object_ = opt_obj;
var uri = new goog.Uri(
'http://dev.virtualearth.net/REST/v1/Imagery/Metadata/' + style);
'http://dev.virtualearth.net/REST/v1/Imagery/Metadata/' +
bingMapsOptions.style);
var jsonp = new goog.net.Jsonp(uri, 'jsonp');
jsonp.send({
'include': 'ImageryProviders',
'key': key
'key': bingMapsOptions.key
}, goog.bind(this.handleImageryMetadataResponse, this));
var projection = ol.Projection.getFromCode('EPSG:3857');
var extent = projection.getExtent();
goog.base(
this, projection, null, ol.TileUrlFunction.nullTileUrlFunction, extent);
};
goog.inherits(ol.source.BingMaps, ol.TileSource);
goog.inherits(ol.source.BingMaps, ol.source.TileSource);
/**
@@ -118,7 +88,10 @@ ol.source.BingMaps.prototype.handleImageryMetadataResponse =
var zoomMin = resource.zoomMin;
var zoomMax = resource.zoomMax;
var tileSize = new ol.Size(resource.imageWidth, resource.imageHeight);
var tileGrid = new ol.tilegrid.XYZ(zoomMax, tileSize);
var tileGrid = new ol.tilegrid.XYZ({
maxZoom: zoomMax,
tileSize: tileSize
});
this.tileGrid = tileGrid;
this.tileUrlFunction = ol.TileUrlFunction.withTileCoordTransform(
@@ -172,11 +145,7 @@ ol.source.BingMaps.prototype.handleImageryMetadataResponse =
this.ready_ = true;
if (!goog.isNull(this.callback_)) {
this.callback_.call(this.object_, this);
this.callback_ = null;
this.object_ = null;
}
this.dispatchLoadEvent();
};

View File

@@ -1,21 +1,16 @@
goog.provide('ol.layer.MapQuestOSM');
goog.provide('ol.layer.MapQuestOpenAerial');
goog.provide('ol.source.MapQuestOSM');
goog.provide('ol.source.MapQuestOpenAerial');
goog.require('ol.Attribution');
goog.require('ol.TileUrlFunction');
goog.require('ol.layer.XYZ');
goog.require('ol.source.XYZ');
/**
* @constructor
* @extends {ol.layer.XYZ}
* @param {Object.<string, *>=} opt_values Values.
* @extends {ol.source.XYZ}
*/
ol.layer.MapQuestOSM = function(opt_values) {
var tileUrlFunction = ol.TileUrlFunction.createFromTemplate(
'http://otile{1-4}.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.jpg');
ol.source.MapQuestOSM = function() {
var attributions = [
new ol.Attribution(
@@ -29,22 +24,22 @@ ol.layer.MapQuestOSM = function(opt_values) {
'<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC BY-SA</a>')
];
goog.base(this, 18, tileUrlFunction, attributions);
goog.base(this, {
attributions: attributions,
maxZoom: 28,
url: 'http://otile{1-4}.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.jpg'
});
};
goog.inherits(ol.layer.MapQuestOSM, ol.layer.XYZ);
goog.inherits(ol.source.MapQuestOSM, ol.source.XYZ);
/**
* @constructor
* @extends {ol.layer.XYZ}
* @param {Object.<string, *>=} opt_values Values.
* @extends {ol.source.XYZ}
*/
ol.layer.MapQuestOpenAerial = function(opt_values) {
var tileUrlFunction = ol.TileUrlFunction.createFromTemplate(
'http://oatile{1-4}.mqcdn.com/tiles/1.0.0/sat/{z}/{x}/{y}.jpg');
ol.source.MapQuestOpenAerial = function() {
var attributions = [
new ol.Attribution(
@@ -56,7 +51,11 @@ ol.layer.MapQuestOpenAerial = function(opt_values) {
'U.S. Depart. of Agriculture, Farm Service Agency')
];
goog.base(this, 18, tileUrlFunction, attributions);
goog.base(this, {
attributions: attributions,
maxZoom: 18,
url: 'http://oatile{1-4}.mqcdn.com/tiles/1.0.0/sat/{z}/{x}/{y}.jpg'
});
};
goog.inherits(ol.layer.MapQuestOpenAerial, ol.layer.XYZ);
goog.inherits(ol.source.MapQuestOpenAerial, ol.source.XYZ);

View File

@@ -0,0 +1,25 @@
goog.provide('ol.source.OpenStreetMap');
goog.require('ol.source.XYZ');
/**
* @constructor
* @extends {ol.source.XYZ}
*/
ol.source.OpenStreetMap = function() {
var attribution = new ol.Attribution(
'&copy; <a href="http://www.openstreetmap.org">OpenStreetMap</a> ' +
'contributors, ' +
'<a href="http://creativecommons.org/licenses/by-sa/2.0/">CC BY-SA</a>');
goog.base(this, {
attributions: [attribution],
maxZoom: 18,
url: 'http://{a-c}.tile.openstreetmap.org/{z}/{x}/{y}.png'
});
};
goog.inherits(ol.source.OpenStreetMap, ol.source.XYZ);

119
src/ol/source/source.js Normal file
View File

@@ -0,0 +1,119 @@
goog.provide('ol.source.Source');
goog.require('goog.events.EventTarget');
goog.require('goog.events.EventType');
goog.require('goog.functions');
goog.require('ol.Attribution');
goog.require('ol.Extent');
goog.require('ol.Projection');
goog.require('ol.TileUrlFunction');
/**
* @typedef {{attribtions: (Array.<ol.Attribution>|undefined),
* extent: (ol.Extent|undefined),
* projection: (ol.Projection|undefined)}}
*/
ol.source.SourceOptions;
/**
* @constructor
* @extends {goog.events.EventTarget}
* @param {ol.source.SourceOptions} sourceOptions Source options.
*/
ol.source.Source = function(sourceOptions) {
goog.base(this);
/**
* @private
* @type {ol.Projection}
*/
this.projection_ = goog.isDef(sourceOptions.projection) ?
sourceOptions.projection : null;
/**
* @private
* @type {ol.Extent}
*/
this.extent_ = goog.isDef(sourceOptions.extent) ?
sourceOptions.extent : sourceOptions.projection.getExtent();
/**
* @private
* @type {Array.<ol.Attribution>}
*/
this.attributions_ = goog.isDef(sourceOptions.attributions) ?
sourceOptions.attributions : null;
};
goog.inherits(ol.source.Source, goog.events.EventTarget);
/**
*/
ol.source.Source.prototype.dispatchLoadEvent = function() {
this.dispatchEvent(goog.events.EventType.LOAD);
};
/**
* @return {Array.<ol.Attribution>} Attributions.
*/
ol.source.Source.prototype.getAttributions = function() {
return this.attributions_;
};
/**
* @return {ol.Extent} Extent.
*/
ol.source.Source.prototype.getExtent = function() {
return this.extent_;
};
/**
* @return {ol.Projection} Projection.
*/
ol.source.Source.prototype.getProjection = function() {
return this.projection_;
};
/**
* @return {Array.<number>|undefined} Resolutions.
*/
ol.source.Source.prototype.getResolutions = goog.abstractMethod;
/**
* @return {boolean} Is ready.
*/
ol.source.Source.prototype.isReady = goog.functions.TRUE;
/**
* @param {Array.<ol.Attribution>} attributions Attributions.
*/
ol.source.Source.prototype.setAttributions = function(attributions) {
this.attributions_ = attributions;
};
/**
* @param {ol.Extent} extent Extent.
*/
ol.source.Source.prototype.setExtent = function(extent) {
this.extent_ = extent;
};
/**
* @param {ol.Projection} projection Projetion.
*/
ol.source.Source.prototype.setProjection = function(projection) {
this.projection_ = projection;
};

View File

@@ -1,26 +1,14 @@
// FIXME Configure minZoom when supported by TileGrid
goog.provide('ol.layer.Stamen');
goog.provide('ol.source.Stamen');
goog.require('ol.TileUrlFunction');
goog.require('ol.layer.XYZ');
goog.require('ol.source.XYZ');
/**
* @enum {string}
*/
ol.StamenProvider = {
TERRAIN: 'terrain',
TONER: 'toner',
WATERCOLOR: 'watercolor'
};
/**
* @enum {string}
*/
ol.StamenFlavor = {
ol.source.StamenFlavor = {
TERRAIN_BACKGROUND: 'background',
TERRAIN_LABELS: 'labels',
TERRAIN_LINES: 'lines',
@@ -38,21 +26,38 @@ ol.StamenFlavor = {
/**
* @type {Object.<ol.StamenProvider,
* @typedef {{flavor: ol.source.StamenFlavor,
* provider: ol.source.StamenProvider}}
*/
ol.source.StamenOptions;
/**
* @enum {string}
*/
ol.source.StamenProvider = {
TERRAIN: 'terrain',
TONER: 'toner',
WATERCOLOR: 'watercolor'
};
/**
* @type {Object.<ol.source.StamenProvider,
* {type: string, minZoom: number, maxZoom: number}>}
*/
ol.StamenProviderConfig = {};
ol.StamenProviderConfig[ol.StamenProvider.TERRAIN] = {
ol.source.StamenProviderConfig = {};
ol.source.StamenProviderConfig[ol.source.StamenProvider.TERRAIN] = {
type: 'jpg',
minZoom: 4,
maxZoom: 18
};
ol.StamenProviderConfig[ol.StamenProvider.TONER] = {
ol.source.StamenProviderConfig[ol.source.StamenProvider.TONER] = {
type: 'png',
minZoom: 0,
maxZoom: 20
};
ol.StamenProviderConfig[ol.StamenProvider.WATERCOLOR] = {
ol.source.StamenProviderConfig[ol.source.StamenProvider.WATERCOLOR] = {
type: 'jpg',
minZoom: 3,
maxZoom: 16
@@ -62,21 +67,10 @@ ol.StamenProviderConfig[ol.StamenProvider.WATERCOLOR] = {
/**
* @constructor
* @extends {ol.layer.XYZ}
* @param {ol.StamenProvider} provider Provider.
* @param {ol.StamenFlavor=} opt_flavor Flavor.
* @param {Object.<string, *>=} opt_values Values.
* @extends {ol.source.XYZ}
* @param {ol.source.StamenOptions} stamenOptions Stamen options.
*/
ol.layer.Stamen = function(provider, opt_flavor, opt_values) {
var config = ol.StamenProviderConfig[provider];
var layer = provider;
if (goog.isDef(opt_flavor)) {
layer += '-' + opt_flavor;
}
var tileUrlFunction = ol.TileUrlFunction.createFromTemplate(
'http://{a-d}.tile.stamen.com/' + layer + '/{z}/{x}/{y}.' + config.type);
ol.source.Stamen = function(stamenOptions) {
var attribution = new ol.Attribution(
'Map tiles by <a href="http://stamen.com">Stamen Design</a>, ' +
@@ -86,7 +80,18 @@ ol.layer.Stamen = function(provider, opt_flavor, opt_values) {
'under ' +
'<a href="http://creativecommons.org/licenses/by-sa/3.0">CC BY SA</a>.');
goog.base(this, config.maxZoom, tileUrlFunction, [attribution]);
var layer = stamenOptions.provider;
if (goog.isDef(stamenOptions.flavor)) {
layer += '-' + stamenOptions.flavor;
}
var config = ol.source.StamenProviderConfig[stamenOptions.provider];
goog.base(this, {
attributions: [attribution],
maxZoom: config.maxZoom,
url: 'http://{a-d}.tile.stamen.com/' + layer + '/{z}/{x}/{y}.' + config.type
});
};
goog.inherits(ol.layer.Stamen, ol.layer.XYZ);
goog.inherits(ol.source.Stamen, ol.source.XYZ);

View File

@@ -1,12 +1,11 @@
// FIXME add some error checking
// FIXME check order of async callbacks
// FIXME use minzoom when supported by ol.TileGrid
// FIXME use minzoom when supported by ol.tilegrid.TileGrid
/**
* @see http://mapbox.com/developers/api/
*/
goog.provide('ol.layer.TileJSON');
goog.provide('ol.source.TileJSON');
goog.provide('ol.tilejson');
@@ -14,10 +13,16 @@ goog.require('goog.asserts');
goog.require('goog.events.EventType');
goog.require('goog.net.jsloader');
goog.require('goog.string');
goog.require('ol.Projection');
goog.require('ol.TileCoverageArea');
goog.require('ol.TileSource');
goog.require('ol.TileUrlFunction');
goog.require('ol.layer.TileLayer');
goog.require('ol.source.TileSource');
/**
* @typedef {{uri: string}}
*/
ol.source.TileJSONOptions;
/**
@@ -39,46 +44,14 @@ goog.exportSymbol('grid', grid);
/**
* @constructor
* @extends {ol.layer.TileLayer}
* @param {string} url URL.
* @param {Object.<string, *>=} opt_values Values.
* @extends {ol.source.TileSource}
* @param {ol.source.TileJSONOptions} tileJsonOptions TileJSON optios.
*/
ol.layer.TileJSON = function(url, opt_values) {
goog.asserts.assert(goog.string.endsWith(url, '.jsonp'));
var tileSource = new ol.source.TileJSON(url, function(tileSource) {
this.dispatchEvent(goog.events.EventType.LOAD);
}, this);
goog.base(this, tileSource, opt_values);
};
goog.inherits(ol.layer.TileJSON, ol.layer.TileLayer);
ol.source.TileJSON = function(tileJsonOptions) {
/**
* @constructor
* @extends {ol.TileSource}
* @param {string} uri URI.
* @param {?function(ol.source.TileJSON)=} opt_callback Callback.
* @param {*=} opt_obj Object.
*/
ol.source.TileJSON = function(uri, opt_callback, opt_obj) {
var projection = ol.Projection.getFromCode('EPSG:3857');
goog.base(
this, projection, null, ol.TileUrlFunction.nullTileUrlFunction, null);
/**
* @private
* @type {?function(ol.source.TileJSON)}
*/
this.callback_ = opt_callback || null;
/**
* @private
* @type {*}
*/
this.object_ = opt_obj;
goog.base(this, {
projection: ol.Projection.getFromCode('EPSG:3857')
});
/**
* @private
@@ -90,11 +63,12 @@ ol.source.TileJSON = function(uri, opt_callback, opt_obj) {
* @private
* @type {!goog.async.Deferred}
*/
this.deferred_ = goog.net.jsloader.load(uri, {cleanupWhenDone: true});
this.deferred_ =
goog.net.jsloader.load(tileJsonOptions.uri, {cleanupWhenDone: true});
this.deferred_.addCallback(this.handleTileJSONResponse, this);
};
goog.inherits(ol.source.TileJSON, ol.TileSource);
goog.inherits(ol.source.TileJSON, ol.source.TileSource);
/**
@@ -126,7 +100,9 @@ ol.source.TileJSON.prototype.handleTileJSONResponse = function() {
var minzoom = tileJSON.minzoom || 0;
goog.asserts.assert(minzoom === 0); // FIXME
var maxzoom = tileJSON.maxzoom || 22;
var tileGrid = new ol.tilegrid.XYZ(maxzoom);
var tileGrid = new ol.tilegrid.XYZ({
maxZoom: maxzoom
});
this.tileGrid = tileGrid;
this.tileUrlFunction = ol.TileUrlFunction.withTileCoordTransform(
@@ -164,11 +140,7 @@ ol.source.TileJSON.prototype.handleTileJSONResponse = function() {
this.ready_ = true;
if (!goog.isNull(this.callback_)) {
this.callback_.call(this.object_, this);
this.callback_ = null;
this.object_ = null;
}
this.dispatchLoadEvent();
};

118
src/ol/source/tilesource.js Normal file
View File

@@ -0,0 +1,118 @@
goog.provide('ol.source.TileSource');
goog.provide('ol.source.TileSourceOptions');
goog.require('ol.Attribution');
goog.require('ol.Extent');
goog.require('ol.Projection');
goog.require('ol.Tile');
goog.require('ol.TileCoord');
goog.require('ol.TileUrlFunction');
goog.require('ol.TileUrlFunctionType');
goog.require('ol.source.Source');
goog.require('ol.tilegrid.TileGrid');
/**
* @typedef {{attributions: (Array.<ol.Attribution>|undefined),
* crossOrigin: (string|undefined),
* extent: (ol.Extent|undefined),
* projection: (ol.Projection|undefined),
* tileGrid: (ol.tilegrid.TileGrid|undefined),
* tileUrlFunction: (ol.TileUrlFunctionType|undefined)}}
*/
ol.source.TileSourceOptions;
/**
* @constructor
* @extends {ol.source.Source}
* @param {ol.source.TileSourceOptions} tileSourceOptions Tile source options.
*/
ol.source.TileSource = function(tileSourceOptions) {
goog.base(this, {
attributions: tileSourceOptions.attributions,
extent: tileSourceOptions.extent,
projection: tileSourceOptions.projection
});
/**
* @protected
* @type {ol.tilegrid.TileGrid}
*/
this.tileGrid = goog.isDef(tileSourceOptions.tileGrid) ?
tileSourceOptions.tileGrid : null;
/**
* @protected
* @type {ol.TileUrlFunctionType}
*/
this.tileUrlFunction = goog.isDef(tileSourceOptions.tileUrlFunction) ?
tileSourceOptions.tileUrlFunction :
ol.TileUrlFunction.nullTileUrlFunction;
/**
* @private
* @type {?string}
*/
this.crossOrigin_ = goog.isDef(tileSourceOptions.crossOrigin) ?
tileSourceOptions.crossOrigin : 'anonymous';
/**
* @private
* @type {Object.<string, ol.Tile>}
* FIXME will need to expire elements from this cache
* FIXME see elemoine's work with goog.structs.LinkedMap
*/
this.tileCache_ = {};
};
goog.inherits(ol.source.TileSource, ol.source.Source);
/**
* @inheritDoc
*/
ol.source.TileSource.prototype.getResolutions = function() {
return this.tileGrid.getResolutions();
};
/**
* @param {ol.TileCoord} tileCoord Tile coordinate.
* @return {ol.Tile} Tile.
*/
ol.source.TileSource.prototype.getTile = function(tileCoord) {
var key = tileCoord.toString();
if (goog.object.containsKey(this.tileCache_, key)) {
return this.tileCache_[key];
} else {
var tileUrl = this.getTileCoordUrl(tileCoord);
var tile;
if (goog.isDef(tileUrl)) {
tile = new ol.Tile(tileCoord, tileUrl, this.crossOrigin_);
} else {
tile = null;
}
this.tileCache_[key] = tile;
return tile;
}
};
/**
* @param {ol.TileCoord} tileCoord Tile coordinate.
* @return {string|undefined} Tile URL.
*/
ol.source.TileSource.prototype.getTileCoordUrl = function(tileCoord) {
return this.tileUrlFunction(tileCoord);
};
/**
* @return {ol.tilegrid.TileGrid} Tile grid.
*/
ol.source.TileSource.prototype.getTileGrid = function() {
return this.tileGrid;
};

109
src/ol/source/xyz.js Normal file
View File

@@ -0,0 +1,109 @@
// FIXME add minZoom support
goog.provide('ol.source.XYZ');
goog.provide('ol.source.XYZOptions');
goog.require('goog.math');
goog.require('ol.Attribution');
goog.require('ol.Coordinate');
goog.require('ol.Projection');
goog.require('ol.Size');
goog.require('ol.TileCoord');
goog.require('ol.TileUrlFunction');
goog.require('ol.TileUrlFunctionType');
goog.require('ol.source.TileSource');
goog.require('ol.tilegrid.XYZ');
/**
* @typedef {{attributions: (Array.<ol.Attribution>|undefined),
* crossOrigin: (string|undefined),
* extent: (ol.Extent|undefined),
* maxZoom: number,
* tileUrlFunction: (ol.TileUrlFunctionType|undefined),
* url: (string|undefined),
* urls: (Array.<string>|undefined)}}
*/
ol.source.XYZOptions;
/**
* @constructor
* @extends {ol.source.TileSource}
* @param {ol.source.XYZOptions} xyzOptions XYZ options.
*/
ol.source.XYZ = function(xyzOptions) {
/**
* @type {ol.TileUrlFunctionType}
*/
var tileUrlFunction = ol.TileUrlFunction.nullTileUrlFunction;
// FIXME use goog.nullFunction ?
if (goog.isDef(xyzOptions.tileUrlFunction)) {
tileUrlFunction = xyzOptions.tileUrlFunction;
} else if (goog.isDef(xyzOptions.urls)) {
tileUrlFunction = ol.TileUrlFunction.createFromTemplates(xyzOptions.urls);
} else if (goog.isDef(xyzOptions.url)) {
tileUrlFunction = ol.TileUrlFunction.createFromTemplate(xyzOptions.url);
}
var tileGrid = new ol.tilegrid.XYZ({
maxZoom: xyzOptions.maxZoom
});
// FIXME factor out common code
var extent = xyzOptions.extent;
if (goog.isDefAndNotNull(extent)) {
tileUrlFunction = ol.TileUrlFunction.withTileCoordTransform(
function(tileCoord) {
if (xyzOptions.maxZoom < tileCoord.z) {
return null;
}
var n = 1 << tileCoord.z;
var y = -tileCoord.y - 1;
if (y < 0 || n <= y) {
return null;
}
var x = goog.math.modulo(tileCoord.x, n);
var tileExtent = tileGrid.getTileCoordExtent(
new ol.TileCoord(tileCoord.z, x, tileCoord.y));
// FIXME we shouldn't need a typecast here
if (!tileExtent.intersects(/** @type {ol.Extent} */ (extent))) {
return null;
}
return new ol.TileCoord(tileCoord.z, x, y);
},
tileUrlFunction);
} else {
tileUrlFunction = ol.TileUrlFunction.withTileCoordTransform(
function(tileCoord) {
if (xyzOptions.maxZoom < tileCoord.z) {
return null;
}
var n = 1 << tileCoord.z;
var y = -tileCoord.y - 1;
if (y < 0 || n <= y) {
return null;
} else {
var x = goog.math.modulo(tileCoord.x, n);
return new ol.TileCoord(tileCoord.z, x, y);
}
},
tileUrlFunction);
}
goog.base(this, {
attributions: xyzOptions.attributions,
crossOrigin: xyzOptions.crossOrigin,
extent: xyzOptions.extent,
projection: ol.Projection.getFromCode('EPSG:3857'),
tileGrid: tileGrid,
tileUrlFunction: tileUrlFunction
});
};
goog.inherits(ol.source.XYZ, ol.source.TileSource);

View File

@@ -2,14 +2,14 @@ goog.provide('ol.TileCoverageArea');
goog.require('ol.CoverageArea');
goog.require('ol.Extent');
goog.require('ol.TileGrid');
goog.require('ol.tilegrid.TileGrid');
/**
* @constructor
* @extends {ol.CoverageArea}
* @param {ol.TileGrid} tileGrid Tile grid.
* @param {ol.tilegrid.TileGrid} tileGrid Tile grid.
* @param {ol.Extent} extent Extent.
* @param {number} minZ Minimum Z.
* @param {number} maxZ Maximum Z.
@@ -20,7 +20,7 @@ ol.TileCoverageArea = function(tileGrid, extent, minZ, maxZ) {
/**
* @private
* @type {ol.TileGrid}
* @type {ol.tilegrid.TileGrid}
*/
this.tileGrid_ = tileGrid;

View File

@@ -1,6 +1,7 @@
// FIXME cope with tile grids whose minium zoom is not zero
goog.provide('ol.TileGrid');
goog.provide('ol.tilegrid.TileGrid');
goog.provide('ol.tilegrid.TileGridOptions');
goog.require('goog.array');
goog.require('goog.asserts');
@@ -13,22 +14,29 @@ goog.require('ol.TileRange');
goog.require('ol.array');
/**
* @typedef {{extent: (ol.Extent|undefined),
* origin: (ol.Coordinate|undefined),
* origins: (Array.<ol.Coordinate>|undefined),
* resolutions: !Array.<number>,
* tileSize: (ol.Size|undefined)}}
*/
ol.tilegrid.TileGridOptions;
/**
* @constructor
* @param {!Array.<number>} resolutions Resolutions.
* @param {ol.Extent} extent Extent.
* @param {ol.Coordinate|!Array.<ol.Coordinate>} origin Origin.
* @param {ol.Size=} opt_tileSize Tile size.
* @param {ol.tilegrid.TileGridOptions} tileGridOptions Tile grid options.
*/
ol.TileGrid = function(resolutions, extent, origin, opt_tileSize) {
ol.tilegrid.TileGrid = function(tileGridOptions) {
/**
* @private
* @type {Array.<number>}
* @type {!Array.<number>}
*/
this.resolutions_ = resolutions;
goog.asserts.assert(goog.array.isSorted(resolutions, function(a, b) {
this.resolutions_ = tileGridOptions.resolutions;
goog.asserts.assert(goog.array.isSorted(this.resolutions_, function(a, b) {
return b - a;
}, true));
@@ -42,34 +50,32 @@ ol.TileGrid = function(resolutions, extent, origin, opt_tileSize) {
* @private
* @type {ol.Extent}
*/
this.extent_ = extent;
this.extent_ = goog.isDef(tileGridOptions.extent) ?
tileGridOptions.extent : null;
/**
* @private
* @type {ol.Coordinate}
*/
this.origin_ = null;
this.origin_ = goog.isDef(tileGridOptions.origin) ?
tileGridOptions.origin : null;
/**
* @private
* @type {Array.<ol.Coordinate>}
*/
this.origins_ = null;
if (origin instanceof ol.Coordinate) {
this.origin_ = origin;
} else if (goog.isArray(origin)) {
goog.asserts.assert(origin.length == this.numResolutions_);
this.origins_ = origin;
} else {
goog.asserts.assert(false);
if (goog.isDef(tileGridOptions.origins)) {
this.origins_ = tileGridOptions.origins;
goog.asserts.assert(this.origins_.length == this.resolutions_.length);
}
/**
* @private
* @type {ol.Size}
*/
this.tileSize_ = opt_tileSize || new ol.Size(256, 256);
this.tileSize_ = goog.isDef(tileGridOptions.tileSize) ?
tileGridOptions.tileSize : new ol.Size(256, 256);
};
@@ -81,7 +87,7 @@ ol.TileGrid = function(resolutions, extent, origin, opt_tileSize) {
* @param {T=} opt_obj Object.
* @template T
*/
ol.TileGrid.prototype.forEachTileCoordParentTileRange =
ol.tilegrid.TileGrid.prototype.forEachTileCoordParentTileRange =
function(tileCoord, callback, opt_obj) {
var tileCoordExtent = this.getTileCoordExtent(tileCoord);
var z = tileCoord.z - 1;
@@ -98,7 +104,7 @@ ol.TileGrid.prototype.forEachTileCoordParentTileRange =
/**
* @return {ol.Extent} Extent.
*/
ol.TileGrid.prototype.getExtent = function() {
ol.tilegrid.TileGrid.prototype.getExtent = function() {
return this.extent_;
};
@@ -107,7 +113,7 @@ ol.TileGrid.prototype.getExtent = function() {
* @param {number} z Z.
* @return {ol.Coordinate} Origin.
*/
ol.TileGrid.prototype.getOrigin = function(z) {
ol.tilegrid.TileGrid.prototype.getOrigin = function(z) {
if (!goog.isNull(this.origin_)) {
return this.origin_;
} else {
@@ -123,8 +129,8 @@ ol.TileGrid.prototype.getOrigin = function(z) {
* @param {number} resolution Resolution.
* @return {ol.PixelBounds} Pixel bounds.
*/
ol.TileGrid.prototype.getPixelBoundsForTileCoordAndResolution = function(
tileCoord, resolution) {
ol.tilegrid.TileGrid.prototype.getPixelBoundsForTileCoordAndResolution =
function(tileCoord, resolution) {
var scale = resolution / this.getResolution(tileCoord.z);
var tileSize = this.getTileSize();
tileSize = new ol.Size(tileSize.width / scale,
@@ -142,7 +148,7 @@ ol.TileGrid.prototype.getPixelBoundsForTileCoordAndResolution = function(
* @param {number} z Z.
* @return {number} Resolution.
*/
ol.TileGrid.prototype.getResolution = function(z) {
ol.tilegrid.TileGrid.prototype.getResolution = function(z) {
goog.asserts.assert(0 <= z && z < this.numResolutions_);
return this.resolutions_[z];
};
@@ -151,7 +157,7 @@ ol.TileGrid.prototype.getResolution = function(z) {
/**
* @return {Array.<number>} Resolutions.
*/
ol.TileGrid.prototype.getResolutions = function() {
ol.tilegrid.TileGrid.prototype.getResolutions = function() {
return this.resolutions_;
};
@@ -161,7 +167,7 @@ ol.TileGrid.prototype.getResolutions = function() {
* @param {ol.TileRange} tileRange Tile range.
* @return {ol.Extent} Extent.
*/
ol.TileGrid.prototype.getTileRangeExtent = function(z, tileRange) {
ol.tilegrid.TileGrid.prototype.getTileRangeExtent = function(z, tileRange) {
var origin = this.getOrigin(z);
var resolution = this.getResolution(z);
var tileSize = this.tileSize_;
@@ -178,7 +184,7 @@ ol.TileGrid.prototype.getTileRangeExtent = function(z, tileRange) {
* @param {number} resolution Resolution.
* @return {ol.TileRange} Tile range.
*/
ol.TileGrid.prototype.getTileRangeForExtentAndResolution = function(
ol.tilegrid.TileGrid.prototype.getTileRangeForExtentAndResolution = function(
extent, resolution) {
var min = this.getTileCoordForCoordAndResolution(
new ol.Coordinate(extent.minX, extent.minY), resolution);
@@ -193,7 +199,7 @@ ol.TileGrid.prototype.getTileRangeForExtentAndResolution = function(
* @param {number} z Z.
* @return {ol.TileRange} Tile range.
*/
ol.TileGrid.prototype.getTileRangeForExtentAndZ = function(extent, z) {
ol.tilegrid.TileGrid.prototype.getTileRangeForExtentAndZ = function(extent, z) {
var resolution = this.getResolution(z);
return this.getTileRangeForExtentAndResolution(extent, resolution);
};
@@ -203,7 +209,7 @@ ol.TileGrid.prototype.getTileRangeForExtentAndZ = function(extent, z) {
* @param {ol.TileCoord} tileCoord Tile coordinate.
* @return {ol.Coordinate} Tile center.
*/
ol.TileGrid.prototype.getTileCoordCenter = function(tileCoord) {
ol.tilegrid.TileGrid.prototype.getTileCoordCenter = function(tileCoord) {
var origin = this.getOrigin(tileCoord.z);
var resolution = this.getResolution(tileCoord.z);
var tileSize = this.tileSize_;
@@ -217,7 +223,7 @@ ol.TileGrid.prototype.getTileCoordCenter = function(tileCoord) {
* @param {ol.TileCoord} tileCoord Tile coordinate.
* @return {ol.Extent} Extent.
*/
ol.TileGrid.prototype.getTileCoordExtent = function(tileCoord) {
ol.tilegrid.TileGrid.prototype.getTileCoordExtent = function(tileCoord) {
var origin = this.getOrigin(tileCoord.z);
var resolution = this.getResolution(tileCoord.z);
var tileSize = this.tileSize_;
@@ -234,7 +240,7 @@ ol.TileGrid.prototype.getTileCoordExtent = function(tileCoord) {
* @param {number} resolution Resolution.
* @return {ol.TileCoord} Tile coordinate.
*/
ol.TileGrid.prototype.getTileCoordForCoordAndResolution = function(
ol.tilegrid.TileGrid.prototype.getTileCoordForCoordAndResolution = function(
coordinate, resolution) {
var z = this.getZForResolution(resolution);
var scale = resolution / this.getResolution(z);
@@ -278,7 +284,8 @@ ol.TileGrid.prototype.getTileCoordForCoordAndResolution = function(
* @param {number} z Z.
* @return {ol.TileCoord} Tile coordinate.
*/
ol.TileGrid.prototype.getTileCoordForCoordAndZ = function(coordinate, z) {
ol.tilegrid.TileGrid.prototype.getTileCoordForCoordAndZ =
function(coordinate, z) {
var resolution = this.getResolution(z);
return this.getTileCoordForCoordAndResolution(coordinate, resolution);
};
@@ -288,7 +295,7 @@ ol.TileGrid.prototype.getTileCoordForCoordAndZ = function(coordinate, z) {
* @param {ol.TileCoord} tileCoord Tile coordinate.
* @return {number} Tile resolution.
*/
ol.TileGrid.prototype.getTileCoordResolution = function(tileCoord) {
ol.tilegrid.TileGrid.prototype.getTileCoordResolution = function(tileCoord) {
goog.asserts.assert(0 <= tileCoord.z && tileCoord.z < this.numResolutions_);
return this.resolutions_[tileCoord.z];
};
@@ -297,7 +304,7 @@ ol.TileGrid.prototype.getTileCoordResolution = function(tileCoord) {
/**
* @return {ol.Size} Tile size.
*/
ol.TileGrid.prototype.getTileSize = function() {
ol.tilegrid.TileGrid.prototype.getTileSize = function() {
return this.tileSize_;
};
@@ -306,6 +313,6 @@ ol.TileGrid.prototype.getTileSize = function() {
* @param {number} resolution Resolution.
* @return {number} Z.
*/
ol.TileGrid.prototype.getZForResolution = function(resolution) {
ol.tilegrid.TileGrid.prototype.getZForResolution = function(resolution) {
return ol.array.linearFindNearest(this.resolutions_, resolution);
};

64
src/ol/tilegrid/xyz.js Normal file
View File

@@ -0,0 +1,64 @@
goog.provide('ol.tilegrid.XYZ');
goog.provide('ol.tilegrid.XYZOptions');
goog.require('ol.Coordinate');
goog.require('ol.Projection');
goog.require('ol.Size');
goog.require('ol.TileRange');
goog.require('ol.tilegrid.TileGrid');
/**
* @typedef {{maxZoom: number}}
*/
ol.tilegrid.XYZOptions;
/**
* @constructor
* @extends {ol.tilegrid.TileGrid}
* @param {ol.tilegrid.XYZOptions} xyzOptions XYZ options.
*/
ol.tilegrid.XYZ = function(xyzOptions) {
var resolutions = new Array(xyzOptions.maxZoom + 1);
var z;
for (z = 0; z <= xyzOptions.maxZoom; ++z) {
resolutions[z] = ol.Projection.EPSG_3857_HALF_SIZE / (128 << z);
}
goog.base(this, {
extent: ol.Projection.EPSG_3857_EXTENT,
origin: new ol.Coordinate(-ol.Projection.EPSG_3857_HALF_SIZE,
ol.Projection.EPSG_3857_HALF_SIZE),
resolutions: resolutions,
tileSize: new ol.Size(256, 256)
});
};
goog.inherits(ol.tilegrid.XYZ, ol.tilegrid.TileGrid);
/**
* @inheritDoc
*/
ol.tilegrid.XYZ.prototype.forEachTileCoordParentTileRange =
function(tileCoord, callback, opt_obj) {
var x = tileCoord.x;
var y = tileCoord.y;
var z = tileCoord.z;
var tileRange;
while (true) {
z -= 1;
if (z < 0) {
break;
}
x = Math.floor(x / 2);
y = Math.floor(y / 2);
tileRange = new ol.TileRange(x, y, x, y);
if (callback.call(opt_obj, z, tileRange)) {
break;
}
}
};

View File

@@ -1,101 +0,0 @@
goog.provide('ol.TileSource');
goog.require('ol.Attribution');
goog.require('ol.Source');
goog.require('ol.Tile');
goog.require('ol.TileCoord');
goog.require('ol.TileGrid');
goog.require('ol.TileUrlFunctionType');
/**
* @constructor
* @extends {ol.Source}
* @param {ol.Projection} projection Projection.
* @param {ol.TileGrid} tileGrid Tile grid.
* @param {ol.TileUrlFunctionType} tileUrlFunction Tile URL.
* @param {ol.Extent=} opt_extent Extent.
* @param {Array.<string>=} opt_attributions Attributions.
* @param {?string=} opt_crossOrigin Cross origin.
*/
ol.TileSource = function(projection, tileGrid, tileUrlFunction, opt_extent,
opt_attributions, opt_crossOrigin) {
goog.base(this, projection, opt_extent, opt_attributions);
/**
* @protected
* @type {ol.TileGrid}
*/
this.tileGrid = tileGrid;
/**
* @protected
* @type {ol.TileUrlFunctionType}
*/
this.tileUrlFunction = tileUrlFunction;
/**
* @private
* @type {?string}
*/
this.crossOrigin_ = opt_crossOrigin || 'anonymous';
/**
* @private
* @type {Object.<string, ol.Tile>}
* FIXME will need to expire elements from this cache
* FIXME see elemoine's work with goog.structs.LinkedMap
*/
this.tileCache_ = {};
};
goog.inherits(ol.TileSource, ol.Source);
/**
* @inheritDoc
*/
ol.TileSource.prototype.getResolutions = function() {
return this.tileGrid.getResolutions();
};
/**
* @param {ol.TileCoord} tileCoord Tile coordinate.
* @return {ol.Tile} Tile.
*/
ol.TileSource.prototype.getTile = function(tileCoord) {
var key = tileCoord.toString();
if (goog.object.containsKey(this.tileCache_, key)) {
return this.tileCache_[key];
} else {
var tileUrl = this.getTileCoordUrl(tileCoord);
var tile;
if (goog.isDef(tileUrl)) {
tile = new ol.Tile(tileCoord, tileUrl, this.crossOrigin_);
} else {
tile = null;
}
this.tileCache_[key] = tile;
return tile;
}
};
/**
* @param {ol.TileCoord} tileCoord Tile coordinate.
* @return {string|undefined} Tile URL.
*/
ol.TileSource.prototype.getTileCoordUrl = function(tileCoord) {
return this.tileUrlFunction(tileCoord);
};
/**
* @return {ol.TileGrid} Tile grid.
*/
ol.TileSource.prototype.getTileGrid = function() {
return this.tileGrid;
};

View File

@@ -81,7 +81,8 @@
<script type="text/javascript" src="spec/ol/tilerange.test.js"></script>
<script type="text/javascript" src="spec/ol/tileurlfunction.test.js"></script>
<script type="text/javascript" src="spec/ol/resolutionconstraint.test.js"></script>
<script type="text/javascript" src="spec/ol/layer/xyz.test.js"></script>
<script type="text/javascript" src="spec/ol/source/xyz.test.js"></script>
<script type="text/javascript" src="spec/ol/tilegrid/xyz.test.js"></script>
<script type="text/javascript">

View File

@@ -5,8 +5,10 @@ describe('ol.source.XYZ', function() {
var xyzTileSource, tileGrid;
beforeEach(function() {
xyzTileSource = new ol.source.XYZ(
6, ol.TileUrlFunction.createFromTemplate('{z}/{x}/{y}'));
xyzTileSource = new ol.source.XYZ({
maxZoom: 6,
url: '{z}/{x}/{y}'
});
tileGrid = xyzTileSource.getTileGrid();
});
@@ -72,7 +74,7 @@ describe('ol.source.XYZ', function() {
describe('forEachTileCoordParentTileRange', function() {
it('iterates as expected', function() {
var xyzTileGrid = new ol.tilegrid.XYZ(6);
var xyzTileGrid = new ol.tilegrid.XYZ({maxZoom: 6});
var tileCoord = new ol.TileCoord(5, 11, 21);
var zs = [], tileRanges = [];

View File

@@ -1,4 +1,4 @@
describe('ol.TileGrid', function() {
describe('ol.tilegrid.TileGrid', function() {
var extent;
var resolutions;
var origin;
@@ -16,16 +16,25 @@ describe('ol.TileGrid', function() {
describe('create valid', function() {
it('does not throw an exception', function() {
expect(function() {
return new ol.TileGrid(resolutions, extent, origin, tileSize);
return new ol.tilegrid.TileGrid({
resolutions: resolutions,
extent: extent,
origin: origin,
tileSize: tileSize
});
}).not.toThrow();
});
});
describe('create with duplicate resolutions', function() {
it('throws an exception', function() {
var resolutions = [100, 50, 50, 25, 10];
expect(function() {
return new ol.TileGrid(resolutions, extent, origin, tileSize);
return new ol.tilegrid.TileGrid({
resolutions: [100, 50, 50, 25, 10],
extent: extent,
origin: origin,
tileSize: tileSize
});
}).toThrow();
});
});
@@ -34,37 +43,51 @@ describe('ol.TileGrid', function() {
it('throws an exception', function() {
var resolutions = [100, 25, 50, 10];
expect(function() {
return new ol.TileGrid(resolutions, extent, origin, tileSize);
return new ol.tilegrid.TileGrid({
resolutions: resolutions,
extent: extent,
origin: origin,
tileSize: tileSize
});
}).toThrow();
});
});
describe('create with multiple origins', function() {
it('does not throw an exception', function() {
var resolutions = [100, 50, 25, 10];
var origins = [origin, origin, origin, origin];
expect(function() {
return new ol.TileGrid(resolutions, extent, origins, tileSize);
return new ol.tilegrid.TileGrid({
resolutions: [100, 50, 25, 10],
extent: extent,
origins: [origin, origin, origin, origin],
tileSize: tileSize
});
}).not.toThrow();
});
});
describe('create with too few origins', function() {
it('throws an exception', function() {
var resolutions = [100, 50, 25, 10];
var origins = [origin, origin, origin];
expect(function() {
return new ol.TileGrid(resolutions, extent, origins, tileSize);
return new ol.tilegrid.TileGrid({
resolutions: [100, 50, 25, 10],
extent: extent,
origins: [origin, origin, origin],
tileSize: tileSize
});
}).toThrow();
});
});
describe('create with too many origins', function() {
it('throws an exception', function() {
var resolutions = [100, 50, 25, 10];
var origins = [origin, origin, origin, origin, origin];
expect(function() {
return new ol.TileGrid(resolutions, extent, origins, tileSize);
return new ol.tilegrid.TileGrid({
resolutions: [100, 50, 25, 10],
extent: extent,
origins: [origin, origin, origin, origin, origin],
tileSize: tileSize
});
}).toThrow();
});
});
@@ -74,7 +97,12 @@ describe('ol.TileGrid', function() {
describe('Y North, X East', function() {
it('returns the expected TileCoord', function() {
origin = new ol.Coordinate(0, 0);
var tileGrid = new ol.TileGrid(resolutions, extent, origin, tileSize);
var tileGrid = new ol.tilegrid.TileGrid({
resolutions: resolutions,
extent: extent,
origin: origin,
tileSize: tileSize
});
var tileCoord;
tileCoord = tileGrid.getTileCoordForCoordAndZ(
@@ -106,7 +134,12 @@ describe('ol.TileGrid', function() {
describe('Y South, X East', function() {
it('returns the expected TileCoord', function() {
origin = new ol.Coordinate(0, 100000);
var tileGrid = new ol.TileGrid(resolutions, extent, origin, tileSize);
var tileGrid = new ol.tilegrid.TileGrid({
resolutions: resolutions,
extent: extent,
origin: origin,
tileSize: tileSize
});
var tileCoord;
tileCoord = tileGrid.getTileCoordForCoordAndZ(
@@ -139,7 +172,12 @@ describe('ol.TileGrid', function() {
describe('getTileCoordForCoordAndResolution', function() {
it('returns the expected TileCoord', function() {
var tileSize = new ol.Size(256, 256);
var tileGrid = new ol.TileGrid([10], extent, origin, tileSize);
var tileGrid = new ol.tilegrid.TileGrid({
resolutions: [10],
extent: extent,
origin: origin,
tileSize: tileSize
});
var coordinate;
var tileCoord;
@@ -221,7 +259,12 @@ describe('ol.TileGrid', function() {
describe('getTileCoordForCoordAndResolution fractional', function() {
it('returns the expected TileCoord', function() {
var tileSize = new ol.Size(256, 256);
var tileGrid = new ol.TileGrid([1 / 3], extent, origin, tileSize);
var tileGrid = new ol.tilegrid.TileGrid({
resolutions: [1 / 3],
extent: extent,
origin: origin,
tileSize: tileSize
});
var coordinate;
var tileCoord;
@@ -315,7 +358,12 @@ describe('ol.TileGrid', function() {
describe('getTileCoordCenter', function() {
it('returns the expected center', function() {
var tileGrid = new ol.TileGrid(resolutions, extent, origin, tileSize);
var tileGrid = new ol.tilegrid.TileGrid({
resolutions: resolutions,
extent: extent,
origin: origin,
tileSize: tileSize
});
var center;
center = tileGrid.getTileCoordCenter(new ol.TileCoord(0, 0, 0));
@@ -334,7 +382,12 @@ describe('ol.TileGrid', function() {
describe('getTileCoordExent', function() {
it('returns the expected extend', function() {
var tileGrid = new ol.TileGrid(resolutions, extent, origin, tileSize);
var tileGrid = new ol.tilegrid.TileGrid({
resolutions: resolutions,
extent: extent,
origin: origin,
tileSize: tileSize
});
var tileCoordExtent;
tileCoordExtent = tileGrid.getTileCoordExtent(new ol.TileCoord(0, 0, 0));
@@ -359,7 +412,12 @@ describe('ol.TileGrid', function() {
describe('getTileRangeForExtentAndZ', function() {
it('returns the expected TileRange', function() {
var tileGrid = new ol.TileGrid(resolutions, extent, origin, tileSize);
var tileGrid = new ol.tilegrid.TileGrid({
resolutions: resolutions,
extent: extent,
origin: origin,
tileSize: tileSize
});
var e = new ol.Extent(45000, 5000, 55000, 15000);
var tileRange;
@@ -391,7 +449,12 @@ describe('ol.TileGrid', function() {
describe('forEachTileCoordParentTileRange', function() {
it('iterates as expected', function() {
var tileGrid = new ol.TileGrid(resolutions, extent, origin, tileSize);
var tileGrid = new ol.tilegrid.TileGrid({
resolutions: resolutions,
extent: extent,
origin: origin,
tileSize: tileSize
});
var zs = [], tileRanges = [];
tileGrid.forEachTileCoordParentTileRange(
@@ -427,8 +490,12 @@ describe('ol.TileGrid', function() {
describe('getZForResolution (exact)', function() {
it('returns the expected z value', function() {
var tileGrid =
new ol.TileGrid(resolutions, extent, origin, tileSize);
var tileGrid = new ol.tilegrid.TileGrid({
resolutions: resolutions,
extent: extent,
origin: origin,
tileSize: tileSize
});
expect(tileGrid.getZForResolution(1000)).toEqual(0);
expect(tileGrid.getZForResolution(500)).toEqual(1);
@@ -439,9 +506,12 @@ describe('ol.TileGrid', function() {
describe('getZForResolution (approcimate)', function() {
it('returns the expected z value', function() {
var tileGrid =
new ol.TileGrid(resolutions, extent, origin, tileSize);
var tileGrid = new ol.tilegrid.TileGrid({
resolutions: resolutions,
extent: extent,
origin: origin,
tileSize: tileSize
});
expect(tileGrid.getZForResolution(2000)).toEqual(0);
expect(tileGrid.getZForResolution(1000)).toEqual(0);
@@ -465,4 +535,4 @@ goog.require('ol.Coordinate');
goog.require('ol.Extent');
goog.require('ol.Size');
goog.require('ol.TileCoord');
goog.require('ol.TileGrid');
goog.require('ol.tilegrid.TileGrid');