/*
 COPYRIGHT 2009 ESRI

 TRADE SECRETS: ESRI PROPRIETARY AND CONFIDENTIAL
 Unpublished material - all rights reserved under the
 Copyright Laws of the United States and applicable international
 laws, treaties, and conventions.

 For additional information, contact:
 Environmental Systems Research Institute, Inc.
 Attn: Contracts and Legal Services Department
 380 New York Street
 Redlands, California, 92373
 USA

 email: contracts@esri.com
 */
//>>built
define("esri/layers/WMTSLayer",["dojo/_base/kernel","dojo/_base/declare","dojo/_base/lang","dojo/_base/array","dojo/sniff","dojo/string","dojox/xml/parser","esri/kernel","esri/lang","esri/request","esri/WKIDUnitConversion","esri/SpatialReference","esri/geometry/Point","esri/geometry/Extent","esri/geometry/webMercatorUtils","esri/layers/TiledMapServiceLayer","esri/layers/TileInfo","esri/layers/WMTSLayerInfo","dojo/query"],function(_1,_2,_3,_4,_5,_6,_7,_8,_9,_a,_b,_c,_d,_e,_f,_10,_11,_12){var _13=_2([_10],{declaredClass:"esri.layers.WMTSLayer",copyright:null,extent:null,tileUrl:null,spatialReference:null,tileInfo:null,constructor:function(url,_14){this.version="1.0.0";this.tileUr=this._url=url;this.serviceMode="RESTful";this._parseCapabilities=_3.hitch(this,this._parseCapabilities);this._getCapabilitiesError=_3.hitch(this,this._getCapabilitiesError);if(!_14){_14={};}if(_14.serviceMode){if(_14.serviceMode==="KVP"||_14.serviceMode==="RESTful"){this.serviceMode=_14.serviceMode;}else{console.error("WMTS mode could only be 'KVP' or 'RESTful'");return;}}this.layerInfo=new _12();if(_14.layerInfo){this.layerInfo=_14.layerInfo;this._identifier=_14.layerInfo.identifier;this._tileMatrixSetId=_14.layerInfo.tileMatrixSet;if(_14.layerInfo.format){this.format="image/"+_14.layerInfo.format;}this._style=_14.layerInfo.style;this.title=_14.layerInfo.title;}if(_14.resourceInfo){this.version=_14.resourceInfo.version;if(_14.resourceInfo.getTileUrl){this._url=this.tileUrl=_14.resourceInfo.getTileUrl;}this.copyright=_14.resourceInfo.copyright;this.layerInfos=_14.resourceInfo.layerInfos;this._parseResourceInfo();this.loaded=true;this.onLoad(this);}else{this._getCapabilities();}this._formatDictionary={"image/png":".png","image/png8":".png","image/png24":".png","image/png32":".png","image/jpg":".jpg","image/jpeg":".jpeg","image/gif":".gif","image/bmp":".bmp","image/tiff":".tif","image/jpgpng":"","image/jpegpng":""};},setActiveLayer:function(_15){this.setVisibleLayer(_15);},setVisibleLayer:function(_16){this._setActiveLayer(_16);this.refresh(true);},getTileUrl:function(_17,row,col){_17=this._levelToLevelValue[_17];var _18;if(this.resourceUrls&&this.resourceUrls.length>0){_18=this.resourceUrls[row%this.resourceUrls.length].template;_18=_18.replace(/\{Style\}/gi,this._style);_18=_18.replace(/\{TileMatrixSet\}/gi,this._tileMatrixSetId);_18=_18.replace(/\{TileMatrix\}/gi,_17);_18=_18.replace(/\{TileRow\}/gi,row);_18=_18.replace(/\{TileCol\}/gi,col);return _18;}_18=this.UrlTemplate.replace(/\{level\}/gi,_17);_18=_18.replace(/\{row\}/gi,row);_18=_18.replace(/\{col\}/gi,col);return _18;},getTileUrlTemplate:function(_19){var _1a,_1b=_19.identifier,_1c=_19.tileMatrixSet,_1d=_19.format,_1e=_19.style;if(!_1b){_1a=this.layers[0];_1b=this.layers[0].identifier;}else{_1a=_4.filter(this.layers,function(_1f){return _1f.identifier===_1b;})[0];}if(!_1a){console.error("couldn't find the layer "+_1b);this.onError(new Error("couldn't find the layer "+_1b));return;}if(!_1d){_1d=_1a.formats[0];if(_1d.indexOf("image/")===-1){_1d="image/"+_1d;}}else{if(_1d.indexOf("image/")===-1){_1d="image/"+_1d;}if(_4.indexOf(_1a.formats,_1d)===-1){console.error("The layer doesn't support the format of "+_1d);this.onError(new Error("The layer doesn't support the format of "+_1d));return;}}if(!_1e){_1e=_1a.styles[0];}else{if(_4.indexOf(_1a.styles,_1e)===-1){console.error("The layer doesn't support the style of "+_1e);this.onError(new Error("The layer doesn't support the style of "+_1e));return;}}var _20;if(!_1c){_20=_4.filter(_1a.tileMatrixSetInfos,function(_21){return _21.tileMatrixSet==="GoogleMapsCompatible";})[0];if(!_20){_20=_1a.tileMatrixSetInfos[0];}_1c=_20.tileMatrixSet;}else{_20=_4.filter(_1a.tileMatrixSetInfos,function(_22){return _22.tileMatrixSet===_1c;})[0];if(!_20){console.error("The tileMatrixSetId "+_1c+" is not supported by the layer of "+_1b);this.onError(new Error("The tileMatrixSetId "+_1c+" is not supported by the layer of "+_1b));return;}}return this._getTileUrlTemplate(_1b,_1c,_1d,_1e);},_getTileUrlTemplate:function(_23,_24,_25,_26){var _27;if(!_23){_23=this._identifier;}if(!_24){_24=this._tileMatrixSetId;}if(!_25){_25=this.format;}if(!_26){_26=this._style;}if(this.resourceUrls&&this.resourceUrls.length>0){_27=this.resourceUrls[0].template;_27=_27.replace(/\{Style\}/gi,_26);_27=_27.replace(/\{TileMatrixSet\}/gi,_24);_27=_27.replace(/\{TileMatrix\}/gi,"{level}");_27=_27.replace(/\{TileRow\}/gi,"{row}");_27=_27.replace(/\{TileCol\}/gi,"{col}");return _27;}if(this.serviceMode==="KVP"){_27=this._url+"SERVICE=WMTS&VERSION="+this.version+"&REQUEST=GetTile"+"&LAYER="+_23+"&STYLE="+_26+"&FORMAT="+_25+"&TILEMATRIXSET="+_24+"&TILEMATRIX={level}&TILEROW={row}&TILECOL={col}";}else{if(this.serviceMode==="RESTful"){var _28="";if(this._formatDictionary[_25.toLowerCase()]){_28=this._formatDictionary[_25.toLowerCase()];}_27=this._url+_23+"/"+_26+"/"+_24+"/{level}/{row}/{col}"+_28;}}return _27;},_parseResourceInfo:function(){var _29=this.layerInfos,i;if(this.serviceMode==="KVP"){this._url+=(this._url.indexOf("?")>-1)?"":"?";}for(i=0;i<_29.length;i++){if((!this._identifier||_29[i].identifier===this._identifier)&&(!this.title||_29[i].title===this.title)&&(!this._tileMatrixSetId||_29[i].tileMatrixSet===this._tileMatrixSetId)&&(!this.format||"image/"+_29[i].format===this.format)&&(!this._style||_29[i].style===this._style)){_3.mixin(this,{"description":_29[i].description,tileInfo:_29[i].tileInfo,spatialReference:_29[i].tileInfo.spatialReference,fullExtent:_29[i].fullExtent,initialExtent:_29[i].initialExtent,_identifier:_29[i].identifier,_tileMatrixSetId:_29[i].tileMatrixSet,format:"image/"+_29[i].format,_style:_29[i].style});break;}}this._setActiveLayer();this.UrlTemplate=this._getTileUrlTemplate();this._levelToLevelValue=[];_4.forEach(this.tileInfo.lods,function(_2a){this._levelToLevelValue[_2a.level]=_2a.levelValue?_2a.levelValue:_2a.level;},this);},_getCapabilities:function(){var _2b;if(this.serviceMode==="KVP"){if(this._url.indexOf("?")>-1){_2b=this._url+"&request=GetCapabilities&service=WMTS&version="+this.version;}else{_2b=this._url+"?request=GetCapabilities&service=WMTS&version="+this.version;}}else{if(this.serviceMode==="RESTful"){_2b=this._url+"/"+this.version+"/WMTSCapabilities.xml";}}_a({url:_2b,handleAs:"text",load:this._parseCapabilities,error:this._getCapabilitiesError});},_parseCapabilities:function(_2c){_2c=_2c.replace(/ows:/gi,"");var xml=_7.parse(_2c);var _2d=_1.query("Contents",xml)[0];if(!_2d){console.error("The WMTS capabilities XML is not valid");this.onError(new Error("The WMTS capabilities XML is not valid"));return;}var _2e=_1.query("OperationsMetadata",xml)[0],_2f=_1.query("[name='GetTile']",_2e)[0],_30=this._url,_31=_1.query("Get",_2f),i;for(i=0;i<_31.length;i++){var _32=_1.query("Constraint",_31[i])[0];if(!_32||this._getTagWithChildTagValue("AllowedValues","Value",this.serviceMode,_32)){_30=_31[i].attributes[0].nodeValue;break;}}if(_30.indexOf("/1.0.0/")===-1&&this.serviceMode==="RESTful"){_30+="/";}if(this.serviceMode==="KVP"){_30+=(_30.indexOf("?")>-1)?"":"?";}this._url=_30;this.copyright=this._getTagValues("Capabilities>ServiceIdentification>AccessConstraints",xml)[0];var _33=_1.query("Layer",_2d),_34,_35=[];this.layers=[];_4.forEach(_33,function(_36){_34=this._getTagValues("Identifier",_36)[0];_35.push(_34);this.layers.push(this._getWMTSLayerInfo(_34,_36,_2d));},this);this._setActiveLayer();this.loaded=true;this.onLoad(this);},_setActiveLayer:function(_37){if(!_37){_37={};}if(_37.identifier){this._identifier=_37.identifier;}if(_37.tileMatrixSet){this._tileMatrixSetId=_37.tileMatrixSet;}if(_37.format){this.format=_37.format;}if(_37.style){this._style=_37.style;}if(!this.layers){return;}var _38;if(!this._identifier){_38=this.layers[0];this._identifier=this.layers[0].identifier;}else{_38=_4.filter(this.layers,function(_39){return _39.identifier===this._identifier;},this)[0];}if(!_38){console.error("couldn't find the layer "+this._identifier);this.onError(new Error("couldn't find the layer "+this._identifier));return;}if(!this.format){this.format=_38.formats[0];if(this.format.indexOf("image/")===-1){this.format="image/"+this.format;}}else{if(this.format.indexOf("image/")===-1){this.format="image/"+this.format;}if(_4.indexOf(_38.formats,this.format)===-1){console.error("The layer doesn't support the format of "+this.format);this.onError(new Error("The layer doesn't support the format of "+this.format));return;}}if(!this._style){this._style=_38.styles[0];}else{if(_4.indexOf(_38.styles,this._style)===-1){console.error("The layer doesn't support the style of "+this._style);this.onError(new Error("The layer doesn't support the style of "+this._style));return;}}var _3a;if(!this._tileMatrixSetId){_3a=_4.filter(_38.tileMatrixSetInfos,function(_3b){return _3b.tileMatrixSet==="GoogleMapsCompatible";})[0];if(!_3a){_3a=_38.tileMatrixSetInfos[0];}this._tileMatrixSetId=_3a.tileMatrixSet;}else{_3a=_4.filter(_38.tileMatrixSetInfos,function(_3c){return _3c.tileMatrixSet===this._tileMatrixSetId;},this)[0];if(!_3a){console.error("The tileMatrixSetId "+this._tileMatrixSetId+" is not supported by the layer of "+this._identifier);this.onError(new Error("The tileMatrixSetId "+this._tileMatrixSetId+" is not supported by the layer of "+this._identifier));return;}}this.description=_38.description;this.title=_38.title;this.spatialReference=_3a.tileInfo.spatialReference;this.tileInfo=_3a.tileInfo;this._levelToLevelValue=[];_4.forEach(this.tileInfo.lods,function(_3d){this._levelToLevelValue[_3d.level]=_3d.levelValue?_3d.levelValue:_3d.level;},this);if(this.spatialReference.wkid===102100||this.spatialReference.wkid===102113){this.fullExtent=this.initialExtent=_f.geographicToWebMercator(_38.gcsExtent);}else{if(this.spatialReference.wkid===4326){this.fullExtent=this.initialExtent=_38.gcsExtent;}else{this.fullExtent=_3a.fullExtent;this.initialExtent=_3a.initialExtent;}}this.resourceUrls=_38.resourceUrls;this.UrlTemplate=this._getTileUrlTemplate();this.layerInfo={"identifier":this._identifier,"tileMatrixSet":this._tileMatrixSetId,"format":this.format,"style":this._style,"fullExtent":this.fullExtent,"initialExtent":this.initialExtent,"tileInfo":this.tileInfo,"title":this.title,"description":this.description};},_getWMTSLayerInfo:function(_3e,_3f,_40){var _41=this._getTagValues("Abstract",_3f)[0],_42=this._getTagValues("Title",_3f)[0],_43=_1.query("WGS84BoundingBox",_3f)[0],_44=_43?this._getTagValues("LowerCorner",_43)[0].split(" "):["-180","-90"],_45=_43?this._getTagValues("UpperCorner",_43)[0].split(" "):["180","90"],_46=parseFloat(_44[0]),_47=parseFloat(_44[1]),_48=parseFloat(_45[0]),_49=parseFloat(_45[1]),_4a=new _e(_46,_47,_48,_49,new _c({"wkid":4326})),_4b=this._getTagValues("Identifier",_1.query("Style",_3f)[0]),_4c=this._getTagValues("Identifier",_1.query("Dimension",_3f)[0]),_4d=this._getTagValues("Value",_1.query("Dimension",_3f)[0])||this._getTagValues("Default",_1.query("Dimension",_3f)[0]),_4e=this._getTagValues("Format",_3f),_4f=this._getLayerMatrixInfos(_3f,_40),_50={"identifier":_3e,"tileMatrixSetInfos":_4f,"formats":_4e,"styles":_4b,"title":_42,"description":_41,"gcsExtent":_4a},_51=_1.query("ResourceURL",_3f),_52=[],_53;_4.forEach(_51,function(_54){_53=_54.getAttribute("template");if(_4c&&_4d){_53=_53.replace("{"+_4c+"}",_4d);}_52.push({"template":_53,"format":_54.getAttribute("format"),"resourceType":_54.getAttribute("resourceType")});});if(_52&&_52.length>0){_50.resourceUrls=_52;}return _50;},_getLayerMatrixInfos:function(_55,_56){var i,_57=[];if(!this._allMatrixInfos){this._allMatrixInfos=[];}var _58=this._getTagValues("TileMatrixSet",_55);if(!_58||_58.length===0){return;}_4.forEach(_58,function(_59,idx){var _5a;if(this._allMatrixInfos.length>0){for(i=0;i<this._allMatrixInfos.length;i++){if(this._allMatrixInfos[i].tileMatrixSet==_59){_5a=this._allMatrixInfos[i];break;}}}if(!_5a){_5a=this._getLayerMatrixInfo(_59,_55,_56);this._allMatrixInfos.push(_5a);}_57.push(_5a);},this);return _57;},_getLayerMatrixInfo:function(_5b,_5c,_5d){var _5e,_5f,_60,i,_61,lod,_62=[];var _63=this._getTagWithChildTagValue("TileMatrixSetLink","TileMatrixSet",_5b,_5c);var _64=this._getTagValues("TileMatrix",_63);var _65=this._getTagWithChildTagValue("TileMatrixSet","Identifier",_5b,_5d);var crs=this._getTagValues("SupportedCRS",_65)[0];_5e=crs.split(":").pop();if(_5e==900913||_5e==3857){_5e=102100;}if(crs.toLowerCase().indexOf("crs84")>-1||crs.toLowerCase().indexOf("crs:84")>-1){_5e=4326;}else{if(crs.toLowerCase().indexOf("crs83")>-1||crs.toLowerCase().indexOf("crs:83")>-1){_5e=4269;}else{if(crs.toLowerCase().indexOf("crs27")>-1||crs.toLowerCase().indexOf("crs:27")>-1){_5e=4267;}}}var _66=new _c({"wkid":_5e});var _67=_1.query("TileMatrix",_65)[0];_5f=parseInt(this._getTagValues("TileWidth",_67)[0],10);_60=parseInt(this._getTagValues("TileHeight",_67)[0],10);var _68=this._getTagValues("TopLeftCorner",_67)[0].split(" "),top=_68[0],_69=_68[1];if(top.split("E").length>1){var _6a=top.split("E");top=_6a[0]*Math.pow(10,_6a[1]);}if(_69.split("E").length>1){var _6b=_69.split("E");_69=_6b[0]*Math.pow(10,_6b[1]);}for(i=0;i<this._flippingAxisForWkids.length;i++){if((crs.split(":").pop()>=this._flippingAxisForWkids[i][0]&&crs.split(":").pop()<=this._flippingAxisForWkids[i][1])||_5e===4326){_61=new _d(parseFloat(_69),parseFloat(top),_66);break;}}if(i===this._flippingAxisForWkids.length){_61=new _d(parseFloat(top),parseFloat(_69),_66);}if(_64.length===0){var _6c=_1.query("TileMatrix",_65);for(i=0;i<_6c.length;i++){lod=this._getLodFromTileMatrix(_6c[i],_5e,i);_62.push(lod);}}else{for(i=0;i<_64.length;i++){var _6d=this._getTagWithChildTagValue("TileMatrix","Identifier",_64[i],_65);lod=this._getLodFromTileMatrix(_6d,_5e,i);_62.push(lod);}}var _6e=_1.query("BoundingBox",_65)[0],_6f,_70,_71,_72,_73,_74,_75,_76,_77;if(_6e){_6f=this._getTagValues("LowerCorner",_6e)[0].split(" ");_70=this._getTagValues("UpperCorner",_6e)[0].split(" ");}if(_6f&&_6f.length>1&&_70&&_70.length>1){_71=parseFloat(_6f[0]);_73=parseFloat(_6f[1]);_72=parseFloat(_70[0]);_74=parseFloat(_70[1]);}else{var _78=this._getTagValues("MatrixWidth",_67)[0],_79=this._getTagValues("MatrixHeight",_67)[0];_71=_61.x;_74=_61.y;_72=_71+_78*_60*_62[0].resolution;_73=_74-_79*_5f*_62[0].resolution;}_75=new _e(_71,_73,_72,_74,_66);_76=_77=_75;var _7a=new _11({"dpi":90.71428571428571,"spatialReference":_66,"format":this.format,"rows":_5f,"cols":_60,"origin":_61,"lods":_62});var _7b={"tileMatrixSet":_5b,"fullExtent":_76,"initialExtent":_77,"tileInfo":_7a};return _7b;},_getCapabilitiesError:function(err){console.error("Failed to get capabilities xml");this.onError(err);},_getLodFromTileMatrix:function(_7c,_7d,_7e){var id=this._getTagValues("Identifier",_7c)[0];var _7f=this._getTagValues("ScaleDenominator",_7c)[0];if(_7f.split("E").length>1){var _80=_7f.split("E");_7f=_80[0]*Math.pow(10,_80[1]);}else{_7f=parseFloat(_7f);}var _81;if(_9.isDefined(_b[_7d])){_81=_b.values[_b[_7d]];}else{_81=111194.6519066546;}var _82=_7f*7/25000/_81;var lod={"level":_7e,"levelValue":id,"scale":_7f,"resolution":_82};return lod;},_getTag:function(_83,xml){var _84=_1.query(_83,xml);if(_84&&_84.length>0){return _84[0];}else{return null;}},_getTagValues:function(_85,xml){var _86=[],_87=_85.split(">"),tag,_88,i;tag=_1.query(_87[0],xml)[0];if(_87.length>1){for(i=1;i<_87.length-1;i++){tag=_1.query(_87[i],tag)[0];}_88=_1.query(_87[_87.length-1],tag);}else{_88=_1.query(_87[0],xml);}if(_88&&_88.length>0){_4.forEach(_88,function(_89){if(_5("ie")){_86.push(_89.childNodes[0].nodeValue);}else{_86.push(_89.textContent);}});}return _86;},_getAttributeValues:function(_8a,_8b,xml){var _8c=_1.query(_8a,xml),_8d=[];if(_8c&&_8c.length>0){_4.forEach(_8c,function(tag){_8d.push(tag.getAttribute(_8b));});}return _8d;},_getTagWithChildTagValue:function(_8e,_8f,_90,xml){var _91=xml.childNodes,_92,j;for(j=0;j<_91.length;j++){if(_91[j].nodeName.indexOf(_8e)>-1){if(_5("ie")){if(_9.isDefined(_1.query(_8f,_91[j])[0])){_92=_1.query(_8f,_91[j])[0].childNodes[0].nodeValue;}}else{if(_9.isDefined(_1.query(_8f,_91[j])[0])){_92=_1.query(_8f,_91[j])[0].textContent;}}if(_92===_90){return _91[j];}}}},_flippingAxisForWkids:[[3819,3819],[3821,3824],[3889,3889],[3906,3906],[4001,4025],[4027,4036],[4039,4047],[4052,4055],[4074,4075],[4080,4081],[4120,4176],[4178,4185],[4188,4216],[4218,4289],[4291,4304],[4306,4319],[4322,4326],[4463,4463],[4470,4470],[4475,4475],[4483,4483],[4490,4490],[4555,4558],[4600,4646],[4657,4765],[4801,4811],[4813,4821],[4823,4824],[4901,4904],[5013,5013],[5132,5132],[5228,5229],[5233,5233],[5246,5246],[5252,5252],[5264,5264],[5324,5340],[5354,5354],[5360,5360],[5365,5365],[5370,5373],[5381,5381],[5393,5393],[5451,5451],[5464,5464],[5467,5467],[5489,5489],[5524,5524],[5527,5527],[5546,5546],[2044,2045],[2081,2083],[2085,2086],[2093,2093],[2096,2098],[2105,2132],[2169,2170],[2176,2180],[2193,2193],[2200,2200],[2206,2212],[2319,2319],[2320,2462],[2523,2549],[2551,2735],[2738,2758],[2935,2941],[2953,2953],[3006,3030],[3034,3035],[3038,3051],[3058,3059],[3068,3068],[3114,3118],[3126,3138],[3150,3151],[3300,3301],[3328,3335],[3346,3346],[3350,3352],[3366,3366],[3389,3390],[3416,3417],[3833,3841],[3844,3850],[3854,3854],[3873,3885],[3907,3910],[4026,4026],[4037,4038],[4417,4417],[4434,4434],[4491,4554],[4839,4839],[5048,5048],[5105,5130],[5253,5259],[5269,5275],[5343,5349],[5479,5482],[5518,5519],[5520,5520],[20004,20032],[20064,20092],[21413,21423],[21473,21483],[21896,21899],[22171,22177],[22181,22187],[22191,22197],[25884,25884],[27205,27232],[27391,27398],[27492,27492],[28402,28432],[28462,28492],[30161,30179],[30800,30800],[31251,31259],[31275,31279],[31281,31290],[31466,31700],[900913,900913]]});if(_5("extend-esri")){_3.setObject("layers.WMTSLayer",_13,_8);}return _13;});