From d752f6ca8947b1abe6fcc5bc3889824d80c07b99 Mon Sep 17 00:00:00 2001 From: Andy Gup Date: Thu, 14 Aug 2014 09:43:52 -0600 Subject: [PATCH 1/9] added additional image type detection --- lib/tpk/TPKLayer.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/tpk/TPKLayer.js b/lib/tpk/TPKLayer.js index 877ad1c..ba3bf96 100644 --- a/lib/tpk/TPKLayer.js +++ b/lib/tpk/TPKLayer.js @@ -107,9 +107,21 @@ define([ case "JPEG": imgURL = "data:image/jpg;base64," + result; break; + case "PNG": + imgURL = "data:image/png;base64," + result; + break; case "PNG8": imgURL = "data:image/png;base64," + result; break; + case "PNG24": + imgURL = "data:image/png;base64," + result; + break; + case "PNG32": + imgURL = "data:image/png;base64," + result; + break; + default: + imgURL = "data:image/jpg;base64," + result; + } img.style.borderColor = "blue"; } From 3a94dceb78a30ad8e584e3403c333768abd6ddea Mon Sep 17 00:00:00 2001 From: Andy Gup Date: Thu, 14 Aug 2014 10:01:37 -0600 Subject: [PATCH 2/9] console logging of errors now works correctly --- lib/tpk/TPKLayer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tpk/TPKLayer.js b/lib/tpk/TPKLayer.js index ba3bf96..a43b6f3 100644 --- a/lib/tpk/TPKLayer.js +++ b/lib/tpk/TPKLayer.js @@ -305,7 +305,7 @@ define([ var reader = new FileReader(); reader.token = data.token; reader.onerror = function (event) { - console.error(new Error("_unzipTileFiles Error: " + event.target.error.code).stack); + console.error("_unzipTileFiles Error: " + JSON.stringify(event.target.error)); that.emit(that.PARSING_ERROR, {msg: "Error parsing file: ", err: event.target.error}); } reader.addEventListener("loadend", function (evt) { From 58d4c0514ed383bfa4d45e123540f14b976b26d8 Mon Sep 17 00:00:00 2001 From: Andy Gup Date: Thu, 14 Aug 2014 10:14:16 -0600 Subject: [PATCH 3/9] removed unused code --- lib/tpk/TPKLayer.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/tpk/TPKLayer.js b/lib/tpk/TPKLayer.js index a43b6f3..dcd9681 100644 --- a/lib/tpk/TPKLayer.js +++ b/lib/tpk/TPKLayer.js @@ -330,8 +330,6 @@ define([ * @private */ _parseConfCdi: function(callback){ - var that = this._self; - var m_conf_i = this._inMemTilesObject[this.TILE_PATH + "CONF.CDI"]; var x2js = new X2JS(); From 7bd00cc37f69f0a019bc36570454de3182f56aa4 Mon Sep 17 00:00:00 2001 From: Andy Gup Date: Thu, 14 Aug 2014 10:16:05 -0600 Subject: [PATCH 4/9] added spatialReference to the Extent --- lib/tpk/TPKLayer.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/tpk/TPKLayer.js b/lib/tpk/TPKLayer.js index dcd9681..59f3da5 100644 --- a/lib/tpk/TPKLayer.js +++ b/lib/tpk/TPKLayer.js @@ -340,9 +340,10 @@ define([ var ymin = parseFloat(envelopeInfo.YMin); var xmax = parseFloat(envelopeInfo.XMax); var ymax = parseFloat(envelopeInfo.YMax); + var sr = parseInt(envelopeInfo.SpatialReference.WKID); var initExtent = new Extent( - xmin,ymin,xmax,ymax + xmin,ymin,xmax,ymax, new SpatialReference({wkid:sr}) ); callback(initExtent); From 48b9129aacd62fe159dd90f45174fc1c0397a532 Mon Sep 17 00:00:00 2001 From: Andy Gup Date: Thu, 14 Aug 2014 13:21:05 -0600 Subject: [PATCH 5/9] more tweaks to error messages --- lib/tpk/TPKLayer.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/tpk/TPKLayer.js b/lib/tpk/TPKLayer.js index 59f3da5..cf342ac 100644 --- a/lib/tpk/TPKLayer.js +++ b/lib/tpk/TPKLayer.js @@ -305,7 +305,7 @@ define([ var reader = new FileReader(); reader.token = data.token; reader.onerror = function (event) { - console.error("_unzipTileFiles Error: " + JSON.stringify(event.target.error)); + console.error("_unzipTileFiles Error: " + event.target.error.message); that.emit(that.PARSING_ERROR, {msg: "Error parsing file: ", err: event.target.error}); } reader.addEventListener("loadend", function (evt) { @@ -409,10 +409,11 @@ define([ this.store.retrieve(url, function(success, offlineTile){ if( success ) { - console.log("Tile found in indexedDB: " + url) + console.log("Tile found in storage: " + url) callback(offlineTile.img,tileId,url); } else { + console.log("Tile is not in storage: " + url) var snappedRow = Math.floor(row / 128) * 128; var snappedCol = Math.floor(col / 128) * 128; From 8846bfe986446864ee40dfacfcad41c089d8f496 Mon Sep 17 00:00:00 2001 From: Andy Gup Date: Thu, 14 Aug 2014 13:49:38 -0600 Subject: [PATCH 6/9] removed unused callback value --- lib/tpk/TPKLayer.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/tpk/TPKLayer.js b/lib/tpk/TPKLayer.js index cf342ac..ae82b1f 100644 --- a/lib/tpk/TPKLayer.js +++ b/lib/tpk/TPKLayer.js @@ -62,7 +62,7 @@ define([ this._fileEntriesLength = files.length; this.emit(this.PROGRESS_EVENT,this.PROGRESS_START); - this._parseInMemFiles(files,function (buffer){ + this._parseInMemFiles(files,function (){ //Parse conf.xml and conf.cdi to get the required setup info this._parseConfCdi(function(initExtent){ this.initialExtent = (this.fullExtent = initExtent); @@ -237,7 +237,7 @@ define([ for(var i=0;i < inMemTilesLength;i++){ - var name = files[i].filename.toLocaleUpperCase(); + var name = files[i].filename.toLocaleUpperCase(); console.log("name: " + name) var index = name.indexOf("_ALLLAYERS",0); if(index != -1){ this.TILE_PATH = name.slice(0,index); @@ -286,7 +286,7 @@ define([ this._inMemTilesObject[name]= data.string; var size = this.ObjectSize(this._inMemTilesObject); if(size == this._fileEntriesLength - this._zeroLengthFileCounter - 1){ - callback(this._inMemTilesObject); + callback(); } }.bind(this)); }, @@ -315,7 +315,7 @@ define([ that._inMemTilesObject[name]= this.result; var size = that.ObjectSize(that._inMemTilesObject); if(size == that._fileEntriesLength - that._zeroLengthFileCounter - 1){ - callback(that._inMemTilesObject); + callback(); } } }); From dacf6c3cff023f5508d09edde2e5432592abd6af Mon Sep 17 00:00:00 2001 From: Andy Gup Date: Fri, 15 Aug 2014 11:31:57 -0600 Subject: [PATCH 7/9] switched to deferred promises in _parseInMemFiles() --- lib/tpk/TPKLayer.js | 47 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/lib/tpk/TPKLayer.js b/lib/tpk/TPKLayer.js index ae82b1f..cf27c45 100644 --- a/lib/tpk/TPKLayer.js +++ b/lib/tpk/TPKLayer.js @@ -11,8 +11,9 @@ define([ "dojo/_base/declare","esri/geometry/Extent","dojo/query","esri/SpatialReference", "esri/layers/TileInfo","esri/layers/TiledMapServiceLayer","tiles/TilesStore","tiles/tilingScheme", - "tpk/zip","tpk/xml2json","tpk/autoCenterMap","dojo/Evented"], - function(declare,Extent,query,SpatialReference,TileInfo,TiledMapServiceLayer,TilesStore,TilingScheme,zip,X2JS,autoCenter,Evented){ + "tpk/zip","tpk/xml2json","tpk/autoCenterMap","dojo/Deferred","dojo/promise/all","dojo/Evented"], + function(declare,Extent,query,SpatialReference,TileInfo,TiledMapServiceLayer,TilesStore, + TilingScheme,zip,X2JS,autoCenter,Deferred,all,Evented){ return declare("esri.TPKLayer",[TiledMapServiceLayer,Evented],{ // @@ -232,12 +233,16 @@ define([ * @private */ _parseInMemFiles: function(files,callback){ + var inMemTilesLength = this._fileEntriesLength; this._zeroLengthFileCounter = 0; + var promises = []; for(var i=0;i < inMemTilesLength;i++){ - var name = files[i].filename.toLocaleUpperCase(); console.log("name: " + name) + var deferred = new Deferred(); + var name = files[i].filename.toLocaleUpperCase(); + var index = name.indexOf("_ALLLAYERS",0); if(index != -1){ this.TILE_PATH = name.slice(0,index); @@ -247,13 +252,33 @@ define([ var indexCDI = name.indexOf("CONF.CDI",0); var indexXML = name.indexOf("CONF.XML",0); + var indexBUNDLE = name.indexOf("BUNDLE",0); + var indexBUNDLX = name.indexOf("BUNDLX",0); + if(indexCDI != -1 || indexXML != -1){ - this._unzipConfFiles(files,i,callback); + this._unzipConfFiles(files,i,deferred,function(/* deferred */ d, /* token */ t){ console.log("CONF FILE") + d.resolve(t); + return d.promise; + }); + } + else if(indexBUNDLE != -1 || indexBUNDLX != -1){ + this._unzipTileFiles(files,i,deferred,function(/* deferred */ d, /* token */ t){ + d.resolve(t); + return d.promise; + }); } else{ - this._unzipTileFiles(files,i,callback); + deferred.resolve(i); } + promises.push(deferred); } + + all(promises).then( function(results) + { + callback && callback(results); + }); + + return promises; }, /** @@ -279,14 +304,14 @@ define([ * @param callback * @private */ - _unzipConfFiles: function(files,token,callback){ + _unzipConfFiles: function(files,token,deferred,callback){ files[token].getData(new zip.TextWriter(token),function(data){ this._inMemTilesIndex.push("blank"); var name = files[data.token].filename.toLocaleUpperCase(); this._inMemTilesObject[name]= data.string; var size = this.ObjectSize(this._inMemTilesObject); - if(size == this._fileEntriesLength - this._zeroLengthFileCounter - 1){ - callback(); + if(size > 0){ + callback(deferred,data.token); } }.bind(this)); }, @@ -298,7 +323,7 @@ define([ * @param callback * @private */ - _unzipTileFiles: function(files,token,callback){ + _unzipTileFiles: function(files,token,deferred,callback){ var that = this; files[token].getData(new zip.BlobWriter(token),function(data){ if(data.size != 0){ @@ -314,8 +339,8 @@ define([ var name = files[this.token].filename.toLocaleUpperCase(); that._inMemTilesObject[name]= this.result; var size = that.ObjectSize(that._inMemTilesObject); - if(size == that._fileEntriesLength - that._zeroLengthFileCounter - 1){ - callback(); + if(size > 0){ + callback(deferred,data.token); } } }); From e9eb35e25ce377ac40656c3de9f353b11df74157 Mon Sep 17 00:00:00 2001 From: Andy Gup Date: Fri, 15 Aug 2014 11:45:52 -0600 Subject: [PATCH 8/9] fixed minor firefox bug --- samples/tpk-layer.html | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/samples/tpk-layer.html b/samples/tpk-layer.html index af15ab8..cf5659f 100644 --- a/samples/tpk-layer.html +++ b/samples/tpk-layer.html @@ -192,7 +192,15 @@ xhrRequest.responseType = "blob"; xhrRequest.onprogress = function(evt){ loading.style.visibility = "visible"; - var percent = (parseFloat(evt.loaded / evt.totalSize) * 100).toFixed(0); + var percent = 0; + + if(evt && evt.totalSize){ + percent = (parseFloat(evt.loaded / evt.totalSize) * 100).toFixed(0); + } + else if(evt && evt.total){ + percent = (parseFloat(evt.loaded / evt.total) * 100).toFixed(0); + } + getFileBtn.innerHTML = "Get file via url " + percent + "%"; console.log("Begin downloading remote tpk file...") } From 81d8db882373ba31a56270e7bd6e190365ffe5dc Mon Sep 17 00:00:00 2001 From: Andy Gup Date: Fri, 15 Aug 2014 11:54:52 -0600 Subject: [PATCH 9/9] updated TPKLayer how to use doc --- doc/howtousetpklibrary.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/doc/howtousetpklibrary.md b/doc/howtousetpklibrary.md index cf275a8..045cad0 100644 --- a/doc/howtousetpklibrary.md +++ b/doc/howtousetpklibrary.md @@ -3,7 +3,7 @@ How to use the TPKLayer library ## `TPKLayer` Library -The `TPKLayer` Library allows you to display at TPK file as a map. +The `TPKLayer` Library allows you to display at TPK file as a map. **Step 1** Unzip the TPK file. This creates an array of Entry objects. Depending on your operating system you may have to rename the TPK file to .zip so that it becomes a recognized MIME type for the html input element. @@ -84,6 +84,15 @@ When you need to delete all tiles from the existing data use the following patte }) ``` + +**Can I use the TPKLayer with a tiled basemap?** + +Yes for ArcGIS API for JavaScript v3.x and ONLY if the TPKs Levels of Detail (LODs) match the tiled map services LODs exactly. + +The basemap (base tiled layer) defines the LODs that the map can display. Any other operational tiled layers on the map will not display if they don’t match the basemap’s LODs. Esri.Map doesn’t union LODs of all tiled layers on the map. + +For more information on creating TPKs go [here](http://resources.arcgis.com/en/help/main/10.1/index.html#//006600000457000000). + **Additional Considerations** There are a few things to keep in mind when working with TPK files and JavaScript.