[OpenLayers-Commits] r11487 - in sandbox/jennier/openlayers:
examples lib lib/OpenLayers lib/OpenLayers/Layer
lib/OpenLayers/LocalTileStorage lib/OpenLayers/Protocol
commits-20090109 at openlayers.org
commits-20090109 at openlayers.org
Fri Feb 25 08:02:48 EST 2011
Author: jennier
Date: 2011-02-25 05:02:47 -0800 (Fri, 25 Feb 2011)
New Revision: 11487
Added:
sandbox/jennier/openlayers/lib/OpenLayers/LocalTileStorage.js
sandbox/jennier/openlayers/lib/OpenLayers/LocalTileStorage/
sandbox/jennier/openlayers/lib/OpenLayers/LocalTileStorage/TileSQLStorage.js
sandbox/jennier/openlayers/lib/OpenLayers/LocalTileStorage/TileWebStorage.js
Removed:
sandbox/jennier/openlayers/lib/OpenLayers/Protocol/BrowserStorage.js
Modified:
sandbox/jennier/openlayers/examples/local-storage-wms-simple.html
sandbox/jennier/openlayers/lib/OpenLayers.js
sandbox/jennier/openlayers/lib/OpenLayers/Layer/Grid.js
Log:
Refactoring to move code from Grid to LocalTileStorage.
Modified: sandbox/jennier/openlayers/examples/local-storage-wms-simple.html
===================================================================
--- sandbox/jennier/openlayers/examples/local-storage-wms-simple.html 2011-02-25 12:23:53 UTC (rev 11486)
+++ sandbox/jennier/openlayers/examples/local-storage-wms-simple.html 2011-02-25 13:02:47 UTC (rev 11487)
@@ -26,7 +26,7 @@
map.addControl( new OpenLayers.Control.LayerSwitcher() );
// Set the cache counter, checks every second.
- window.setInterval(function(){layerOSGeo.countCache(updateCacheCount)},1000);
+ window.setInterval(function(){layerOSGeo.store.count(updateCacheCount)},1000);
};
@@ -53,8 +53,8 @@
tiles currently being viewed in local storage. Also allows the store to be cleared.
<br/>
<br/>
- <a href="#" onclick="layerOSGeo.cacheAreaViewed();return false;">Cache the area being viewed.</a><br/><br/>
- <a href="#" onclick="layerOSGeo.clearCache();return false;">Clear the cache.</a><br/><br/>
+ <a href="#" onclick="layerOSGeo.store.cacheAreaViewed();return false;">Cache the area being viewed.</a><br/><br/>
+ <a href="#" onclick="layerOSGeo.store.clear();return false;">Clear the cache.</a><br/><br/>
Tiles Stored: <em id="count">0</em>.
</div>
Modified: sandbox/jennier/openlayers/lib/OpenLayers/Layer/Grid.js
===================================================================
--- sandbox/jennier/openlayers/lib/OpenLayers/Layer/Grid.js 2011-02-25 12:23:53 UTC (rev 11486)
+++ sandbox/jennier/openlayers/lib/OpenLayers/Layer/Grid.js 2011-02-25 13:02:47 UTC (rev 11487)
@@ -7,7 +7,7 @@
/**
* @requires OpenLayers/Layer/HTTPRequest.js
* @requires OpenLayers/Console.js
- * @requires OpenLayers/Protocol/BrowserStorage.js
+ * @requires OpenLayers/LocalTileStorage.js
*/
/**
@@ -135,7 +135,7 @@
if (this.params.ENABLELOCALCACHE && !this.store) {
- this.store = new OpenLayers.Protocol.BrowserStorage('OLTiles: '+name, '1.0', 'OpenLayers Tiles', 2 * 1024 * 1024);
+ this.store = this.initStore('OLTiles: '+name, '1.0', 'OpenLayers Tiles', 2 * 1024 * 1024, this);
}
},
@@ -872,89 +872,35 @@
},
- /**
- * Note the area stored will depend on the buffer value that has been set.
- */
- cacheAreaViewed:function() {
-
- if (this.params.ENABLELOCALCACHE) {
-
- // Go through all the tiles in the current view and get their URL.
- for (var i=0; i<this.grid.length; i++) {
- for (var j=0; j<this.grid[i].length; j++) {
-
- // before we can store the image we need to proxy to if it comes
- // from another domain.
- var proxiedURL = this.getProxiedURL(this.grid[i][j].url);
-
- this.grid[i][j].imgDiv.onload = OpenLayers.Function.bindAsEventListener(this.cacheTile ,this.grid[i][j]);
- this.grid[i][j].imgDiv.src = proxiedURL;
-
- }
- }
+ initStore: function(name, version, description, size, layer) {
+
+ if (this.params.ENABLELOCALCACHE) {
+ var store;
+ // Default to Web SQL Storage
+ try {
+ store = new OpenLayers.LocalTileStorage.TileSQLStorage(name, version, description, size, layer);
+ console.debug(store);
+ return store;
+ } catch (e) {
+ console.log(e);
+ /* Does not support Web SQL Storage */
+ }
+
+ // Use Web (Local) Storage if Web SQL not available.
+ try {
+ store = new TileWebStorage();
+ return store.initialize(name, version, description, size, layer);
-
- }
-
- return false;
- },
-
- getProxiedURL: function(url) {
-
- // Check if the map being requested is on the same domain/port
- var sameOrigin = !(url.indexOf("http") == 0);
- var urlParts = !sameOrigin && url.match(this.URL_SPLIT_REGEX);
- if (urlParts) {
- var location = window.location;
- sameOrigin =
- urlParts[1] == location.protocol &&
- urlParts[3] == location.hostname;
- var uPort = urlParts[4], lPort = location.port;
- if (uPort != 80 && uPort != "" || lPort != "80" && lPort != "") {
- sameOrigin = sameOrigin && uPort == lPort;
- }
- }
-
- var proxiedURL;
- // For proxy need to url encode the params.
- if (!sameOrigin && OpenLayers.ProxyHost) {
- proxiedURL = OpenLayers.ProxyHost+encodeURIComponent(url);
- } else if (!sameOrigin){
- OpenLayers.Console.warn(
- OpenLayers.i18n("proxyNeeded"), {url: url});
- }
-
- return proxiedURL;
- },
-
- cacheTile: function(e) {
-
- var canvas = document.createElement("canvas");
- canvas.width = this.size.w;
- canvas.height = this.size.h;
- var ctx = canvas.getContext("2d");
- ctx.drawImage(this.imgDiv, 0, 0);
- // Below does not work in FireFox
- //var imageData = canvas.toDataURL(this.params.FORMAT,'quality=20');
- var imageData = canvas.toDataURL();
-
- // store under original URL
- this.layer.store.set(this.url,imageData);
-
- this.imgDiv.onload = {};
- },
-
- clearCache: function() {
- if (this.store) {
- this.store.clear();
- }
- },
-
- countCache:function(resultHandler) {
- if (this.store) {
- return this.store.count(resultHandler);
- }
- return 0;
+ } catch (e) {
+ /* Does not support Web (local) Storage */
+ // TODO Throw an error
+ console.debug(e);
+ }
+ } else {
+ // TODO Throw an exception, local caching not enabled.
+ }
+
},
+
CLASS_NAME: "OpenLayers.Layer.Grid"
});
Added: sandbox/jennier/openlayers/lib/OpenLayers/LocalTileStorage/TileSQLStorage.js
===================================================================
--- sandbox/jennier/openlayers/lib/OpenLayers/LocalTileStorage/TileSQLStorage.js (rev 0)
+++ sandbox/jennier/openlayers/lib/OpenLayers/LocalTileStorage/TileSQLStorage.js 2011-02-25 13:02:47 UTC (rev 11487)
@@ -0,0 +1,118 @@
+/* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for
+ * full list of contributors). Published under the Clear BSD license.
+ * See http://svn.openlayers.org/trunk/openlayers/license.txt for the
+ * full text of the license. */
+
+/**
+ * Class: OpenLayers.LocalTileStorage.TileSQLStorage
+ * Class for access to SQL Web Storage or Web (local) Storage.
+ * Allows you to store, retrieve and get information about what
+ * is stored.
+ *
+ * Inherits from:
+ * - <OpenLayers.LocalTileStorage>
+ */
+
+/**
+ * @requires OpenLayers/Layer.js
+ */
+
+OpenLayers.LocalTileStorage.TileSQLStorage = OpenLayers.Class(OpenLayers.LocalTileStorage, {
+
+ name:null,
+ version:null,
+ description:null,
+ size:null,
+ layer: null,
+ db: null,
+
+ initialize: function(name, version, description, size, layer) {
+ try {
+ this.name = name;
+ this.version = version;
+ this.description = description;
+ this.size = size;
+ this.layer = layer,
+ this.db = openDatabase(name, version, description, size);
+
+ this.db.transaction(
+ function(tx) {
+ tx.executeSql("Create table OLTable (key TEXT, value BLOB) ",
+ [],
+ function(tx, result) {
+ /* No results */
+ }, function(tx, error) {
+ /* table may already exist */
+ console.debug(error);
+ });
+ });
+
+ } catch (e) {
+ throw new Error("Web SQL storage is not supported.");
+ }
+ return this;
+
+ },
+ set: function(key, value) {
+ // TODO Check for existing entries to prevent duplicates.
+ this.db.transaction(
+ function(tx) {
+ tx.executeSql("Insert into OLTable (key, value) values (?,?)",
+ [key,value],
+ function(tx, result) {
+ /* No results as it's an insert */
+ }, function(tx, error) {
+ console.debug(error);
+ });
+ });
+ },
+ get: function(key, tile) {
+ this.db.transaction(
+ function(tx) {
+ console.log("Select value from OLTable where key="+key);
+ tx.executeSql("Select value from OLTable where key=?",
+ [key],
+ function(tx, result) {
+ if(result.rows.length){
+ tile.src = result.rows.item(0).value;
+ return tile.src;
+ }
+ }, function(tx, error) {
+ console.debug(error);
+ });
+ });
+ },
+
+ clear: function() {
+ this.db.transaction(
+ function(tx) {
+ tx.executeSql("Delete from OLTable",
+ [],
+ function(tx, result) {
+ /* No results */
+ }, function(tx, error) {
+ console.debug(error);
+ });
+ });
+ },
+ count: function(resultHandler) {
+
+ var count;
+ var name = this.name;
+ this.db.transaction(
+ function(tx) {
+ // console.log("Select count(*) as count from OLTable");
+ tx.executeSql("Select count(*) as count from OLTable",
+ [],
+ function(tx, result) {
+ if(result.rows.length){
+ resultHandler(name,result.rows.item(0)['count']);
+ }
+ }, function(tx, error) {
+ console.debug(error);
+ });
+ });
+ },
+
+ CLASS_NAME: "OpenLayers.LocalTileStorage.TileSQLStorage"
+ });
\ No newline at end of file
Property changes on: sandbox/jennier/openlayers/lib/OpenLayers/LocalTileStorage/TileSQLStorage.js
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added: sandbox/jennier/openlayers/lib/OpenLayers/LocalTileStorage/TileWebStorage.js
===================================================================
--- sandbox/jennier/openlayers/lib/OpenLayers/LocalTileStorage/TileWebStorage.js (rev 0)
+++ sandbox/jennier/openlayers/lib/OpenLayers/LocalTileStorage/TileWebStorage.js 2011-02-25 13:02:47 UTC (rev 11487)
@@ -0,0 +1,62 @@
+/* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for
+ * full list of contributors). Published under the Clear BSD license.
+ * See http://svn.openlayers.org/trunk/openlayers/license.txt for the
+ * full text of the license. */
+
+/**
+ * Class: OpenLayers.LocalTileStorage.TileWebStorage
+ * Class for access to SQL Web Storage or Web (local) Storage.
+ * Allows you to store, retrieve and get information about what
+ * is stored.
+ *
+ * Inherits from:
+ * - <OpenLayers.LocalTileStorage>
+ */
+
+/**
+ * @requires OpenLayers/Layer.js
+ */
+
+OpenLayers.LocalTileStorage.TileWebStorage = OpenLayers.Class(OpenLayers.LocalTileStorage, {
+
+ name:null,
+ version:null,
+ description:null,
+ size:null,
+ layer:null,
+
+ initialize: function(name, version, description, size, layer) {
+ if (localStorage) {
+ this.name = name;
+ this.version = version;
+ this.description = description;
+ this.size = size;
+ this.layer = layer;
+ } else {
+ throw new Error("Local storage is not supported.");
+ }
+ return this;
+ },
+ set: function(key, value) {
+ try {
+ localStorage.setItem(key,value);
+ } catch (e) {
+ if((e.name).toUpperCase() == 'QUOTA_EXCEEDED_ERR') {
+ //TODO Replace with correct error handling.
+ alert ("Unable to cache area, cache is full.");
+ }
+ }
+ },
+ get: function(key, imgDiv) {
+ imgDiv.src = localStorage.getItem(key);
+ return imgDiv.src;
+ },
+ clear: function() {
+ localStorage.clear();
+ },
+ count: function(resultHandler) {
+ resultHandler("LocalStorage", localStorage.length);
+ },
+
+ CLASS_NAME: "OpenLayers.LocalTileStorage.TileWebStorage"
+ });
\ No newline at end of file
Property changes on: sandbox/jennier/openlayers/lib/OpenLayers/LocalTileStorage/TileWebStorage.js
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Copied: sandbox/jennier/openlayers/lib/OpenLayers/LocalTileStorage.js (from rev 11440, sandbox/jennier/openlayers/lib/OpenLayers/Protocol/BrowserStorage.js)
===================================================================
--- sandbox/jennier/openlayers/lib/OpenLayers/LocalTileStorage.js (rev 0)
+++ sandbox/jennier/openlayers/lib/OpenLayers/LocalTileStorage.js 2011-02-25 13:02:47 UTC (rev 11487)
@@ -0,0 +1,121 @@
+/* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for
+ * full list of contributors). Published under the Clear BSD license.
+ * See http://svn.openlayers.org/trunk/openlayers/license.txt for the
+ * full text of the license. */
+
+/**
+ * Class: OpenLayers.LocalTileStorage
+ * Class for access to SQL Web Storage or Web (local) Storage.
+ * Allows you to store, retrieve and get information about what
+ * is stored.
+ */
+
+
+/**
+ * @requires OpenLayers/Layer.js
+ * @requires OpenLayers/LocalTileStorage/TileWebStorage.js
+ * @requires OpenLayers/LocalTileStorage/TileSQLStorage.js
+ */
+
+OpenLayers.LocalTileStorage = OpenLayers.Class({
+
+ /**
+ * Method: getURL
+ * Return a GetMap query string for this layer or the data URI for this
+ * image from local storage. To be used as the img src.
+ *
+ * Parameters:
+ * url - {<String>} The URL we want to test.
+ *
+ * Returns:
+ * {String} a GetMap query string for this layer or the data URI for this
+ * image from local storage. To be used as the img src.
+ */
+ getCacheURL: function (url, tile) {
+
+ // If we want to use the local cache, check to see if they exist and
+ // use those values if they do.
+ if (this.params.ENABLELOCALCACHE) {
+
+ // check to see if the tile exists in the cache
+ var imageCached = this.store.get(url, tile.imgDiv);
+
+ if (imageCached) {
+ return imageCached;
+ }
+
+ }
+
+ return url;
+
+ },
+
+ /**
+ * Note the area stored will depend on the buffer value that has been set.
+ */
+ cacheAreaViewed:function() {
+
+ // Go through all the tiles in the current view and get their URL.
+ for (var i=0; i<this.layer.grid.length; i++) {
+ for (var j=0; j<this.layer.grid[i].length; j++) {
+
+ // before we can store the image we need to proxy to if it comes
+ // from another domain.
+ var proxiedURL = this.getProxiedURL(this.layer.grid[i][j].url);
+
+ this.layer.grid[i][j].imgDiv.onload = OpenLayers.Function.bindAsEventListener(this.cacheTile ,this.layer.grid[i][j]);
+ this.layer.grid[i][j].imgDiv.src = proxiedURL;
+
+ }
+ }
+
+ return false;
+ },
+
+ getProxiedURL: function(url) {
+
+ // Check if the map being requested is on the same domain/port
+ var sameOrigin = !(url.indexOf("http") == 0);
+ var urlParts = !sameOrigin && url.match(this.URL_SPLIT_REGEX);
+ if (urlParts) {
+ var location = window.location;
+ sameOrigin =
+ urlParts[1] == location.protocol &&
+ urlParts[3] == location.hostname;
+ var uPort = urlParts[4], lPort = location.port;
+ if (uPort != 80 && uPort != "" || lPort != "80" && lPort != "") {
+ sameOrigin = sameOrigin && uPort == lPort;
+ }
+ }
+
+ var proxiedURL;
+ // For proxy need to url encode the params.
+ if (!sameOrigin && OpenLayers.ProxyHost) {
+ proxiedURL = OpenLayers.ProxyHost+encodeURIComponent(url);
+ } else if (!sameOrigin){
+ OpenLayers.Console.warn(
+ OpenLayers.i18n("proxyNeeded"), {url: url});
+ }
+
+ return proxiedURL;
+ },
+
+ cacheTile: function(e) {
+
+ var canvas = document.createElement("canvas");
+ canvas.width = this.size.w;
+ canvas.height = this.size.h;
+ var ctx = canvas.getContext("2d");
+ ctx.drawImage(this.imgDiv, 0, 0);
+ // Below does not work in FireFox
+ //var imageData = canvas.toDataURL(this.params.FORMAT,'quality=20');
+ var imageData = canvas.toDataURL();
+
+ // store under original URL
+ this.layer.store.set(this.url,imageData);
+
+ this.imgDiv.onload = {};
+ },
+
+ CLASS_NAME: "OpenLayers.LocalTileStorage"
+});
\ No newline at end of file
Property changes on: sandbox/jennier/openlayers/lib/OpenLayers/LocalTileStorage.js
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Deleted: sandbox/jennier/openlayers/lib/OpenLayers/Protocol/BrowserStorage.js
===================================================================
--- sandbox/jennier/openlayers/lib/OpenLayers/Protocol/BrowserStorage.js 2011-02-25 12:23:53 UTC (rev 11486)
+++ sandbox/jennier/openlayers/lib/OpenLayers/Protocol/BrowserStorage.js 2011-02-25 13:02:47 UTC (rev 11487)
@@ -1,185 +0,0 @@
-/* Copyright (c) 2006-2011 by OpenLayers Contributors (see authors.txt for
- * full list of contributors). Published under the Clear BSD license.
- * See http://svn.openlayers.org/trunk/openlayers/license.txt for the
- * full text of the license. */
-
-/**
- * @requires OpenLayers/Protocol.js
- */
-
-/**
- * Class: OpenLayers.Protocol.BrowserStorage
- * Class for access to SQL Web Storage or Web (local) Storage.
- * Allows you to store, retrieve and get information about what
- * is stored.
- *
- * Inherits from:
- * - <OpenLayers.Protocol>
- */
-OpenLayers.Protocol.BrowserStorage = OpenLayers.Class(OpenLayers.Protocol, {
-
- initialize: function(name, version, description, size) {
- var store;
- // Default to Web SQL Storage
- try {
- /// store = this.sqlStore;
- // return store.initialize(name, version, description, size);
- } catch (e) {
- /* Does not support Web SQL Storage */
- }
-
- // Use Web (Local) Storage if Web SQL not available.
- try {
- store = this.localStore;
- return store.initialize(name, version, description, size);
-
- } catch (e) {
- /* Does not support Web (local) Storage */
- // TODO Throw an error
- console.debug(e);
- }
-
- },
-
- /* localStore ****************************************************************/
-
- localStore: {
-
- name:null,
- version:null,
- description:null,
- size:null,
-
- initialize: function(name, version, description, size) {
- if (localStorage) {
- this.name = name;
- this.version = version;
- this.description = description;
- this.size = size;
- } else {
- throw new Error("Local storage is not supported.");
- }
- return this;
- },
- set: function(key, value) {
- try {
- localStorage.setItem(key,value);
- } catch (e) {
- if((e.name).toUpperCase() == 'QUOTA_EXCEEDED_ERR') {
- //TODO Replace with correct error handling.
- alert ("Unable to cache area, cache is full.");
- }
- }
- },
- get: function(key, imgDiv) {
- imgDiv.src = localStorage.getItem(key);
- return imgDiv.src;
- },
- clear: function() {
- localStorage.clear();
- },
- count: function(resultHandler) {
- resultHandler("LocalStorage", localStorage.length);
- }
- },
-
- /* sqlStore ******************************************************************/
-
- sqlStore: {
-
- name:null,
- version:null,
- description:null,
- size:null,
- db: null,
-
- initialize: function(name, version, description, size) {
- try {
- this.name = name;
- this.version = version;
- this.description = description;
- this.size = size;
- this.db = openDatabase(name, version, description, size);
-
- this.db.transaction(
- function(tx) {
- tx.executeSql("Create table OLTable (key TEXT, value BLOB) ",
- [],
- function(tx, result) {
- /* No results */
- }, function(tx, error) {
- /* table may already exist */
- console.debug(error);
- });
- });
-
- } catch (e) {
- throw new Error("Web SQL storage is not supported.");
- }
- return this;
-
- },
- set: function(key, value) {
- // TODO Check for existing entries to prevent duplicates.
- this.db.transaction(
- function(tx) {
- tx.executeSql("Insert into OLTable (key, value) values (?,?)",
- [key,value],
- function(tx, result) {
- /* No results as it's an insert */
- }, function(tx, error) {
- console.debug(error);
- });
- });
- },
- get: function(key, tile) {
- this.db.transaction(
- function(tx) {
- console.log("Select value from OLTable where key="+key);
- tx.executeSql("Select value from OLTable where key=?",
- [key],
- function(tx, result) {
- if(result.rows.length){
- tile.src = result.rows.item(0).value;
- return tile.src;
- }
- }, function(tx, error) {
- console.debug(error);
- });
- });
- },
-
- clear: function() {
- this.db.transaction(
- function(tx) {
- tx.executeSql("Delete from OLTable",
- [],
- function(tx, result) {
- /* No results */
- }, function(tx, error) {
- console.debug(error);
- });
- });
- },
- count: function(resultHandler) {
-
- var count;
- var name = this.name;
- this.db.transaction(
- function(tx) {
- // console.log("Select count(*) as count from OLTable");
- tx.executeSql("Select count(*) as count from OLTable",
- [],
- function(tx, result) {
- if(result.rows.length){
- resultHandler(name,result.rows.item(0)['count']);
- }
- }, function(tx, error) {
- console.debug(error);
- });
- });
- }
- },
-
- CLASS_NAME: "OpenLayers.Protocol.BrowserStorage"
-});
\ No newline at end of file
Modified: sandbox/jennier/openlayers/lib/OpenLayers.js
===================================================================
--- sandbox/jennier/openlayers/lib/OpenLayers.js 2011-02-25 12:23:53 UTC (rev 11486)
+++ sandbox/jennier/openlayers/lib/OpenLayers.js 2011-02-25 13:02:47 UTC (rev 11487)
@@ -242,7 +242,6 @@
"OpenLayers/Protocol.js",
"OpenLayers/Protocol/HTTP.js",
"OpenLayers/Protocol/SQL.js",
- "OpenLayers/Protocol/BrowserStorage.js",
"OpenLayers/Protocol/SQL/Gears.js",
"OpenLayers/Protocol/WFS.js",
"OpenLayers/Protocol/WFS/v1.js",
@@ -339,6 +338,9 @@
"OpenLayers/Symbolizer/Polygon.js",
"OpenLayers/Symbolizer/Text.js",
"OpenLayers/Symbolizer/Raster.js",
+ "OpenLayers/LocalTileStorage.js",
+ "OpenLayers/LocalTileStorage/TileSQLStorage.js",
+ "OpenLayers/LocalTileStorage/TileWebStorage.js",
"OpenLayers/Lang.js",
"OpenLayers/Lang/en.js"
]; // etc.
More information about the Commits
mailing list