From ffa96907c7a39e98d5eb368195ae25432ffb26cb Mon Sep 17 00:00:00 2001 From: drnextgis Date: Thu, 19 Jan 2017 11:51:00 +0700 Subject: [PATCH] geojson-vt integration example --- examples/.eslintrc | 3 +- examples/geojson-vt.html | 11 +++++ examples/geojson-vt.js | 92 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 examples/geojson-vt.html create mode 100644 examples/geojson-vt.js diff --git a/examples/.eslintrc b/examples/.eslintrc index 7e1e36cc48..29440c581d 100644 --- a/examples/.eslintrc +++ b/examples/.eslintrc @@ -11,7 +11,8 @@ "toastr": false, "saveAs": false, "topojson": false, - "turf": false + "turf": false, + "geojsonvt": false }, "rules": { "no-unused-vars": [2, {"varsIgnorePattern": "^map"}] diff --git a/examples/geojson-vt.html b/examples/geojson-vt.html new file mode 100644 index 0000000000..3c170c3b9b --- /dev/null +++ b/examples/geojson-vt.html @@ -0,0 +1,11 @@ +--- +layout: example.html +title: geojson-vt integration +shortdesc: Slice GeoJSON into vector tiles on the fly in the browser. +docs: > + Example of integration with geojson-vt library. +tags: "mapbox, vector, tiles, geojson" +resources: + - https://mapbox.github.io/geojson-vt/geojson-vt-dev.js +--- +
diff --git a/examples/geojson-vt.js b/examples/geojson-vt.js new file mode 100644 index 0000000000..955e49cd9a --- /dev/null +++ b/examples/geojson-vt.js @@ -0,0 +1,92 @@ +// NOCOMPILE +goog.require('ol.Map'); +goog.require('ol.View'); +goog.require('ol.format.GeoJSON'); +goog.require('ol.source.OSM'); +goog.require('ol.source.VectorTile'); +goog.require('ol.layer.Tile'); +goog.require('ol.layer.VectorTile'); +goog.require('ol.tilegrid'); +goog.require('ol.proj'); + + +var replacer = function(key, value) { + if (value.geometry) { + var type; + var rawType = value.type; + var geometry = value.geometry; + + if (rawType === 1) { + type = geometry.length === 1 ? 'Point' : 'MultiPoint'; + } else if (rawType === 2) { + type = geometry.length === 1 ? 'LineString' : 'MultiLineString'; + } else if (rawType === 3) { + type = geometry.length === 1 ? 'Polygon' : 'MultiPolygon'; + } + + return { + 'type': 'Feature', + 'geometry': { + 'type': type, + 'coordinates': geometry.length == 1 ? geometry : [geometry] + }, + 'properties': value.tags + }; + } else { + return value; + } +}; + +var tilePixels = new ol.proj.Projection({ + code: 'TILE_PIXELS', + units: 'tile-pixels' +}); + +var map = new ol.Map({ + layers: [ + new ol.layer.Tile({ + source: new ol.source.OSM() + }) + ], + target: 'map', + view: new ol.View({ + center: [0, 0], + zoom: 2 + }) +}); + +var url = 'data/geojson/countries.geojson'; +fetch(url).then(function(response) { + return response.json(); +}).then(function(json) { + var tileIndex = geojsonvt(json, { + extent: 4096, + debug: 1 + }); + var vectorSource = new ol.source.VectorTile({ + format: new ol.format.GeoJSON(), + tileGrid: ol.tilegrid.createXYZ(), + tilePixelRatio: 16, + tileLoadFunction: function(tile, tileCoord) { + var format = tile.getFormat(); + var data = tileIndex.getTile.apply(tileIndex, tileCoord); + + var features = format.readFeatures( + JSON.stringify({ + type: 'FeatureCollection', + features: data.features + }, replacer)); + tile.setLoader(function() { + tile.setFeatures(features); + tile.setProjection(tilePixels); + }); + }, + tileUrlFunction: function(tileCoord) { + return [tileCoord[0], tileCoord[1], -tileCoord[2] - 1]; + } + }); + var vectorLayer = new ol.layer.VectorTile({ + source: vectorSource + }); + map.addLayer(vectorLayer); +});