Update geojson-vt example

This commit is contained in:
Tim Schaub
2022-01-01 18:03:31 -07:00
parent 9024867893
commit 2d96c92ac8
5 changed files with 55 additions and 55 deletions

View File

@@ -1,3 +0,0 @@
#map {
background: #1a2b39;
}

View File

@@ -22,6 +22,7 @@ const map = new Map({
layers: [ layers: [
new VectorLayer({ new VectorLayer({
source: vectorSource, source: vectorSource,
background: '#1a2b39',
style: function (feature) { style: function (feature) {
const color = feature.get('COLOR_BIO') || '#eeeeee'; const color = feature.get('COLOR_BIO') || '#eeeeee';
style.getFill().setColor(color); style.getFill().setColor(color);

View File

@@ -1,60 +1,67 @@
import GeoJSON from '../src/ol/format/GeoJSON.js'; import GeoJSON from '../src/ol/format/GeoJSON.js';
import Map from '../src/ol/Map.js'; import Map from '../src/ol/Map.js';
import OSM from '../src/ol/source/OSM.js';
import Projection from '../src/ol/proj/Projection.js'; import Projection from '../src/ol/proj/Projection.js';
import VectorTileLayer from '../src/ol/layer/VectorTile.js';
import VectorTileSource from '../src/ol/source/VectorTile.js'; import VectorTileSource from '../src/ol/source/VectorTile.js';
import View from '../src/ol/View.js'; import View from '../src/ol/View.js';
import { import {Fill, Style} from '../src/ol/style.js';
Tile as TileLayer,
VectorTile as VectorTileLayer,
} from '../src/ol/layer.js';
// Converts geojson-vt data to GeoJSON // Converts geojson-vt data to GeoJSON
const replacer = function (key, value) { const replacer = function (key, value) {
if (value.geometry) { if (!value || !value.geometry) {
let type;
const rawType = value.type;
let geometry = value.geometry;
if (rawType === 1) {
type = 'MultiPoint';
if (geometry.length == 1) {
type = 'Point';
geometry = geometry[0];
}
} else if (rawType === 2) {
type = 'MultiLineString';
if (geometry.length == 1) {
type = 'LineString';
geometry = geometry[0];
}
} else if (rawType === 3) {
type = 'Polygon';
if (geometry.length > 1) {
type = 'MultiPolygon';
geometry = [geometry];
}
}
return {
'type': 'Feature',
'geometry': {
'type': type,
'coordinates': geometry,
},
'properties': value.tags,
};
} else {
return value; return value;
} }
let type;
const rawType = value.type;
let geometry = value.geometry;
if (rawType === 1) {
type = 'MultiPoint';
if (geometry.length == 1) {
type = 'Point';
geometry = geometry[0];
}
} else if (rawType === 2) {
type = 'MultiLineString';
if (geometry.length == 1) {
type = 'LineString';
geometry = geometry[0];
}
} else if (rawType === 3) {
type = 'Polygon';
if (geometry.length > 1) {
type = 'MultiPolygon';
geometry = [geometry];
}
}
return {
'type': 'Feature',
'geometry': {
'type': type,
'coordinates': geometry,
},
'properties': value.tags,
};
}; };
const style = new Style({
fill: new Fill({
color: '#eeeeee',
}),
});
const layer = new VectorTileLayer({
background: '#1a2b39',
style: function (feature) {
const color = feature.get('COLOR') || '#eeeeee';
style.getFill().setColor(color);
return style;
},
});
const map = new Map({ const map = new Map({
layers: [ layers: [layer],
new TileLayer({
source: new OSM(),
}),
],
target: 'map', target: 'map',
view: new View({ view: new View({
center: [0, 0], center: [0, 0],
@@ -62,7 +69,7 @@ const map = new Map({
}), }),
}); });
const url = 'data/geojson/countries.geojson'; const url = 'https://openlayers.org/data/vector/ecoregions.json';
fetch(url) fetch(url)
.then(function (response) { .then(function (response) {
return response.json(); return response.json();
@@ -106,8 +113,5 @@ fetch(url)
tile.setFeatures(features); tile.setFeatures(features);
}, },
}); });
const vectorLayer = new VectorTileLayer({ layer.setSource(vectorSource);
source: vectorSource,
});
map.addLayer(vectorLayer);
}); });

View File

@@ -1,3 +0,0 @@
#map {
background: #1a2b39;
}

View File

@@ -12,6 +12,7 @@ const style = new Style({
}); });
const vectorLayer = new VectorLayer({ const vectorLayer = new VectorLayer({
background: '#1a2b39',
source: new VectorSource({ source: new VectorSource({
url: 'https://openlayers.org/data/vector/ecoregions.json', url: 'https://openlayers.org/data/vector/ecoregions.json',
format: new GeoJSON(), format: new GeoJSON(),