Skip to content

Commit 0ff358a

Browse files
committed
Add feature picking for both Cesium and Leaflet as well as licence for pbf and vector-tile
1 parent d4555cd commit 0ff358a

5 files changed

+132
-11
lines changed

LICENSE.md

+60-3
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,66 @@ https://github.com/hammerjs/hammer.js
253253
>
254254
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL > THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
255255
256+
### pbf
257+
258+
> Copyright (c) 2015, Mapbox
259+
> All rights reserved.
260+
>
261+
> Redistribution and use in source and binary forms, with or without
262+
> modification, are permitted provided that the following conditions are met:
263+
>
264+
> * Redistributions of source code must retain the above copyright notice, this
265+
> list of conditions and the following disclaimer.
266+
>
267+
> * Redistributions in binary form must reproduce the above copyright notice,
268+
> this list of conditions and the following disclaimer in the documentation
269+
> and/or other materials provided with the distribution.
270+
>
271+
> * Neither the name of pbf nor the names of its
272+
> contributors may be used to endorse or promote products derived from
273+
> this software without specific prior written permission.
274+
>
275+
> THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
276+
> AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
277+
> IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
278+
> DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
279+
> FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
280+
> DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
281+
> SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
282+
> CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
283+
> OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
284+
285+
### vector-tile
286+
287+
> Copyright (c) 2014, Mapbox
288+
>
289+
290+
> All rights reserved.
291+
>
292+
> Redistribution and use in source and binary forms, with or without modification,
293+
> are permitted provided that the following conditions are met:
294+
>
295+
> * Redistributions of source code must retain the above copyright notice,
296+
> this list of conditions and the following disclaimer.
297+
> * Redistributions in binary form must reproduce the above copyright notice,
298+
> this list of conditions and the following disclaimer in the documentation
299+
> and/or other materials provided with the distribution.
300+
> * Neither the name of llmr nor the names of its contributors
301+
> may be used to endorse or promote products derived from this software
302+
> without specific prior written permission.
303+
>
304+
> THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
305+
> "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
306+
> LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
307+
> A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
308+
> CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
309+
> EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
310+
> PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
311+
> PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
312+
> LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
313+
> NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
314+
> SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
315+
256316
Tests
257317
=====
258318

@@ -270,6 +330,3 @@ http://pivotal.github.com/jasmine/
270330
> The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
271331
>
272332
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
273-
274-
275-

lib/MapboxVectorCanvasTileLayer.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
var L = require('leaflet');
55
var defined = require('terriajs-cesium/Source/Core/defined');
66

7-
//var CesiumTileLayer = require('../Map/CesiumTileLayer');
87
var CesiumEvent = require('terriajs-cesium/Source/Core/Event');
8+
var Cartographic = require('terriajs-cesium/Source/Core/Cartographic');
9+
var pollToPromise = require('./Core/pollToPromise');
910

1011
var MapboxVectorCanvasTileLayer = L.TileLayer.Canvas.extend({
1112
initialize: function(imageryProvider, options) {
@@ -30,6 +31,25 @@ var MapboxVectorCanvasTileLayer = L.TileLayer.Canvas.extend({
3031
});
3132

3233

34+
},
35+
36+
pickFeatures: function(map, longitudeRadians, latitudeRadians) {
37+
var ll = new Cartographic(longitudeRadians, latitudeRadians, 0.0);
38+
39+
var level = map.getZoom();
40+
41+
var that = this;
42+
return pollToPromise(function() {
43+
return that.imageryProvider.ready;
44+
}).then(function() {
45+
var tilingScheme = that.imageryProvider.tilingScheme;
46+
var tileCoordinates = tilingScheme.positionToTileXY(ll, level);
47+
if (!defined(tileCoordinates)) {
48+
return undefined;
49+
}
50+
51+
return that.imageryProvider.pickFeatures(tileCoordinates.x, tileCoordinates.y, level, longitudeRadians, latitudeRadians);
52+
});
3353
}
3454

3555

lib/MapboxVectorTileImageryProvider.js

+48-5
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@ var Event = require('terriajs-cesium/Source/Core/Event');
99
var defined = require('terriajs-cesium/Source/Core/defined');
1010
var VectorTile = require('vector-tile').VectorTile;
1111
var Protobuf = require('pbf');
12+
var inside = require('point-in-polygon');
1213
var loadArrayBuffer = require('terriajs-cesium/Source/Core/loadArrayBuffer');
1314

15+
var ImageryLayerFeatureInfo = require('terriajs-cesium/Source/Scene/ImageryLayerFeatureInfo');
16+
1417
var defineProperties = require('terriajs-cesium/Source/Core/defineProperties');
1518

1619

17-
var MapboxVectorTileImageryProvider = function (colorFunc) {
20+
var MapboxVectorTileImageryProvider = function (csvItem, colorFunc) {
1821
this._url = 'https://b.tiles.mapbox.com/v4/steve9164.1rjjq17t/';
1922
this._layerName = '1270055001_sa4_2011_aust_shape';
2023
this._fileExtension = 'vector.pbf';
@@ -33,6 +36,8 @@ var MapboxVectorTileImageryProvider = function (colorFunc) {
3336
}
3437
}
3538

39+
this._csvItem = csvItem;
40+
3641
this._tilingScheme = new WebMercatorTilingScheme();
3742

3843
this._tileWidth = 256;
@@ -186,21 +191,59 @@ MapboxVectorTileImageryProvider.prototype._requestImage = function(x, y, level,
186191
context.lineTo(pos.x, pos.y);
187192
}
188193
context.closePath();
189-
context.fillStyle = that._colorFunc(json.id);
194+
context.strokeStyle = "black";
195+
context.lineWidth = 1;
196+
context.stroke();
197+
context.fillStyle = that._colorFunc(json.id - 1);
190198
context.fill();
191199
}
192200
}
193201

194202
}
195203

196204
return canvas;
197-
});
205+
}).otherwise(function(err) { });
198206

199207
};
200208

201209

202-
MapboxVectorTileImageryProvider.prototype.pickFeatures = function() {
203-
return undefined;
210+
MapboxVectorTileImageryProvider.prototype.pickFeatures = function(x, y, level, longitude, latitude) {
211+
var that = this;
212+
var url = buildImageUrl(this, x, y, level);
213+
214+
return loadArrayBuffer(url).then(function(data) {
215+
var layer = new VectorTile(new Protobuf(data)).layers[that._layerName];
216+
217+
var to_deg = function (rad) {
218+
return 180 * rad / Math.PI;
219+
}
220+
221+
var point = [to_deg(longitude), to_deg(latitude)];
222+
223+
var features = [];
224+
225+
for (var i = 0; i < layer.length; i++) {
226+
var json = layer.feature(i).toGeoJSON(x, y, level);
227+
if (json.geometry.type == 'Polygon') {
228+
for (var i2 = 0; i2 < json.geometry.coordinates.length; i2++) {
229+
// Check whether point is in this polygon
230+
if (inside(point, json.geometry.coordinates[i2])) {
231+
var feature = new ImageryLayerFeatureInfo();
232+
/*feature.properties = Object.assign({}, json.properties);
233+
feature.properties.FID = json.id - 1;*/
234+
feature.configureDescriptionFromProperties(that._csvItem.rowProperties(json.id - 1));
235+
feature.name = json.properties["SA4_NAME11"];
236+
//feature.data = json.geometry;
237+
features.push(feature);
238+
break; // No need to check other polygons with the same id
239+
}
240+
}
241+
}
242+
243+
}
244+
245+
return features;
246+
});
204247
};
205248

206249
module.exports = MapboxVectorTileImageryProvider;

lib/Models/CsvCatalogItem.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ CsvCatalogItem.prototype._hide = function() {
440440
* For region-mapped files, enable the WMS imagery layer, with recoloring and feature picking.
441441
*/
442442
CsvCatalogItem.prototype._createImageryProvider = function(time) {
443-
var imageryProvider = new MapboxVectorTileImageryProvider(this._colorFunc);
443+
var imageryProvider = new MapboxVectorTileImageryProvider(this, this._colorFunc);
444444

445445
// new WebMapServiceImageryProvider({
446446
// url: proxyCatalogItemUrl( this, this._regionProvider.server),

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
"urijs": "^1.16.0",
2828
"urthecast": "^1.0.0",
2929
"pbf": "^1.3.2",
30-
"vector-tile": "1.2.0"
30+
"vector-tile": "1.2.0",
31+
"point-in-polygon": "1.0.0"
3132
},
3233
"devDependencies": {
3334
"browserify": "^9.0.8",

0 commit comments

Comments
 (0)