1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
* full list of contributors). Published under the 2-clause BSD license.
* See license.txt in the OpenLayers distribution or repository for the
* full text of the license. */
/**
* @requires OpenLayers/Tile.js
* @requires OpenLayers/Format/JSON.js
* @requires OpenLayers/Request.js
*/
/**
* Class: OpenLayers.Tile.UTFGrid
* Instances of OpenLayers.Tile.UTFGrid are used to manage
* UTFGrids. This is an unusual tile type in that it doesn't have a
* rendered image; only a 'hit grid' that can be used to
* look up feature attributes.
*
* See the <OpenLayers.Tile.UTFGrid> constructor for details on constructing a
* new instance.
*
* Inherits from:
* - <OpenLayers.Tile>
*/
OpenLayers.Tile.UTFGrid = OpenLayers.Class(OpenLayers.Tile, {
/**
* Property: url
* {String}
* The URL of the UTFGrid file being requested. Provided by the <getURL>
* method.
*/
url: null,
/**
* Property: utfgridResolution
* {Number}
* Ratio of the pixel width to the width of a UTFGrid data point. If an
* entry in the grid represents a 4x4 block of pixels, the
* utfgridResolution would be 4. Default is 2.
*/
utfgridResolution: 2,
/**
* Property: json
* {Object}
* Stores the parsed JSON tile data structure.
*/
json: null,
/**
* Property: format
* {OpenLayers.Format.JSON}
* Parser instance used to parse JSON for cross browser support. The native
* JSON.parse method will be used where available (all except IE<8).
*/
format: null,
/**
* Constructor: OpenLayers.Tile.UTFGrid
* Constructor for a new <OpenLayers.Tile.UTFGrid> instance.
*
* Parameters:
* layer - {<OpenLayers.Layer>} layer that the tile will go in.
* position - {<OpenLayers.Pixel>}
* bounds - {<OpenLayers.Bounds>}
* url - {<String>} Deprecated. Remove me in 3.0.
* size - {<OpenLayers.Size>}
* options - {Object}
*/
/**
* APIMethod: destroy
* Clean up.
*/
destroy: function() {
this.clear();
OpenLayers.Tile.prototype.destroy.apply(this, arguments);
},
/**
* Method: draw
* Check that a tile should be drawn, and draw it.
* In the case of UTFGrids, "drawing" it means fetching and
* parsing the json.
*
* Returns:
* {Boolean} Was a tile drawn?
*/
draw: function() {
var drawn = OpenLayers.Tile.prototype.draw.apply(this, arguments);
if (drawn) {
if (this.isLoading) {
this.abortLoading();
//if we're already loading, send 'reload' instead of 'loadstart'.
this.events.triggerEvent("reload");
} else {
this.isLoading = true;
this.events.triggerEvent("loadstart");
}
this.url = this.layer.getURL(this.bounds);
if (this.layer.useJSONP) {
// Use JSONP method to avoid xbrowser policy
var ols = new OpenLayers.Protocol.Script({
url: this.url,
callback: function(response) {
this.isLoading = false;
this.events.triggerEvent("loadend");
this.json = response.data;
},
scope: this
});
ols.read();
this.request = ols;
} else {
// Use standard XHR
this.request = OpenLayers.Request.GET({
url: this.url,
callback: function(response) {
this.isLoading = false;
this.events.triggerEvent("loadend");
if (response.status === 200) {
this.parseData(response.responseText);
}
},
scope: this
});
}
} else {
this.unload();
}
return drawn;
},
/**
* Method: abortLoading
* Cancel a pending request.
*/
abortLoading: function() {
if (this.request) {
this.request.abort();
delete this.request;
}
this.isLoading = false;
},
/**
* Method: getFeatureInfo
* Get feature information associated with a pixel offset. If the pixel
* offset corresponds to a feature, the returned object will have id
* and data properties. Otherwise, null will be returned.
*
*
* Parameters:
* i - {Number} X-axis pixel offset (from top left of tile)
* j - {Number} Y-axis pixel offset (from top left of tile)
*
* Returns:
* {Object} Object with feature id and data properties corresponding to the
* given pixel offset.
*/
getFeatureInfo: function(i, j) {
var info = null;
if (this.json) {
var id = this.getFeatureId(i, j);
if (id !== null) {
info = {id: id, data: this.json.data[id]};
}
}
return info;
},
/**
* Method: getFeatureId
* Get the identifier for the feature associated with a pixel offset.
*
* Parameters:
* i - {Number} X-axis pixel offset (from top left of tile)
* j - {Number} Y-axis pixel offset (from top left of tile)
*
* Returns:
* {Object} The feature identifier corresponding to the given pixel offset.
* Returns null if pixel doesn't correspond to a feature.
*/
getFeatureId: function(i, j) {
var id = null;
if (this.json) {
var resolution = this.utfgridResolution;
var row = Math.floor(j / resolution);
var col = Math.floor(i / resolution);
var charCode = this.json.grid[row].charCodeAt(col);
var index = this.indexFromCharCode(charCode);
var keys = this.json.keys;
if (!isNaN(index) && (index in keys)) {
id = keys[index];
}
}
return id;
},
/**
* Method: indexFromCharCode
* Given a character code for one of the UTFGrid "grid" characters,
* resolve the integer index for the feature id in the UTFGrid "keys"
* array.
*
* Parameters:
* charCode - {Integer}
*
* Returns:
* {Integer} Index for the feature id from the keys array.
*/
indexFromCharCode: function(charCode) {
if (charCode >= 93) {
charCode--;
}
if (charCode >= 35) {
charCode --;
}
return charCode - 32;
},
/**
* Method: parseData
* Parse the JSON from a request
*
* Parameters:
* str - {String} UTFGrid as a JSON string.
*
* Returns:
* {Object} parsed javascript data
*/
parseData: function(str) {
if (!this.format) {
this.format = new OpenLayers.Format.JSON();
}
this.json = this.format.read(str);
},
/**
* Method: clear
* Delete data stored with this tile.
*/
clear: function() {
this.json = null;
},
CLASS_NAME: "OpenLayers.Tile.UTFGrid"
});
|