-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
295 lines (269 loc) · 9.49 KB
/
index.html
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>GIS file formats</title>
<link rel="stylesheet" href="libs/ol.css" type="text/css">
<link rel="stylesheet" href="libs/ol-layerswitcher.css" type="text/css">
<link rel="stylesheet" href="libs/highlight.css">
<style>
#map { position: absolute; top: 0; right: 40%; bottom: 0; left: 0; height: 100%; }
pre { position: absolute; top: 0; right: 0; bottom: 0; left: 60%; height: 100%; margin: 0; padding: 0; }
#code { height: 100%; margin: 0; padding: 0; }
#stylebtn { position: absolute; bottom: 0.5em; left: 0.5em }
#stylebtn button { width: auto; padding: 0 0.5em; font-weight: normal; font-size: initial; }
#stylebtn button.active { background-color: rgba(0, 0, 0, 0.7); }
</style>
</head>
<body>
<div id="map" class="map"></div>
<pre><code id="code" class="javascript"></code></pre>
<div id="stylebtn" class="ol-unselectable ol-control">
<button type="button" class="active" onclick="toggleStyle(this)">Toggle optimized styles</button>
</div>
<script src="libs/ol.js"></script>
<script src="libs/ol-layerswitcher.js"></script>
<script src="libs/geotiff.js"></script>
<script src="libs/shapefile.js"></script>
<script src="libs/highlight.pack.js"></script>
<script id="source">
// ol/Image~LoadFunction with GeoTIFF file support
// depends on: https://github.com/geotiffjs/geotiff.js
let GeoTIFFloader = function (img, src) {
const self = this;
const canvas = document.createElement('canvas');
GeoTIFF.fromUrl(src)
.then(tiff => tiff.getImage()
.then(image => {
const width = image.getWidth();
const height = image.getHeight();
const bbox = image.getBoundingBox();
// world files (TFW) not supported!
canvas.width = width;
canvas.height = height;
self.extent = bbox;
image.readRGB().then(raster => {
// render raw image data (rgb) to canvas
let ctx = canvas.getContext("2d");
let imageData = ctx.createImageData(width, height);
let o = 0;
for (let i = 0; i < raster.length; i += 3) {
imageData.data[o] = raster[i];
imageData.data[o + 1] = raster[i + 1];
imageData.data[o + 2] = raster[i + 2];
imageData.data[o + 3] = 255;
o += 4;
}
ctx.putImageData(imageData, 0, 0);
img.getImage().src = canvas.toDataURL();
})
}))
.catch(error => console.error(error));
};
// ol/featureloader~FeatureLoader with ESRI Shape file support
// depends on: https://github.com/mbostock/shapefile
let SHPloader = function (extent, resolution, projection) {
const self = this;
shapefile.open(self.getUrl())
.then(source => source.read()
.then(function load(result) {
if (result.done) return;
self.addFeatures(
self.getFormat().readFeatures(result.value)
);
return source.read().then(load);
}))
.catch(error => self.removeLoadedExtent(extent))
};
// client side styling
let strokeCache = {
'2': new ol.style.Stroke({ // állam
color: [255, 35, 35],
width: 4
}),
'5': new ol.style.Stroke({ // régió
color: [255, 35, 35],
width: 2
}),
'6': new ol.style.Stroke({ // megye
color: [255, 145, 1],
width: 1
}),
'7': new ol.style.Stroke({ // járás
color: [183, 64, 0],
width: 1
}),
'8': new ol.style.Stroke({ // település
color: [225, 198, 76],
width: 1
}),
'9': new ol.style.Stroke({ // kerület
color: [35, 35, 35],
width: 1
})
};
let defaultStyle = new ol.style.Style({
fill: new ol.style.Fill({
color: [255, 255, 255, 0.2]
}),
stroke: new ol.style.Stroke({
color: [255, 35, 35],
width: 1.5
}),
text: new ol.style.Text({
text: '',
fill: new ol.style.Fill({
color: [33, 33, 33]
})
})
});
// its very important for performance to cache styles
let vectorStyle = (feature, resolution) => {
let level = feature.get('level');
let text = feature.get('name');
if (level == 9 && resolution > 110) return null;
if (level == 8 && resolution > 110) return null;
if (level == 7 && resolution > 250) return null;
if (level == 6 && resolution > 350) return null;
if (level == 5 && resolution > 450) return null;
//if (level == 2 && resolution > 700) return null;
defaultStyle.text_.text_ = text;
defaultStyle.stroke_ = strokeCache[level];
return [defaultStyle];
};
// define the map
let map = new ol.Map({
target: 'map',
controls: [
new ol.control.Zoom(),
new ol.control.Rotate(),
new ol.control.Attribution(),
new ol.control.LayerSwitcher()
],
view: new ol.View({
projection: 'EPSG:3857',
center: [2117211.2877144828, 6009019.550011185],
zoom: 9
}),
layers: [
// Base layer
new ol.layer.Tile({
title: 'OpenStreetMap',
source: new ol.source.OSM(),
visible: true
}),
// GeoJPEG file
new ol.layer.Image({
title: 'GeoJPEG',
source: new ol.source.ImageStatic({
imageExtent: [ // need to set manually
1793743.4113663190510124, 5738323.4000000003725290,
2549007.5886336811818182, 6204778.9599999999627471
],
url: 'data/boundaries.jpg'
}),
visible: false
}),
// GeoTIFF file
new ol.layer.Image({
title: 'GeoTIFF',
source: new ol.source.ImageStatic({
imageLoadFunction: GeoTIFFloader,
imageExtent: ol.proj.get('EPSG:3857').getExtent(),
url: 'data/boundaries.tiff'
// world files (TFW) not supported!
}),
visible: false
}),
// ESRI Shape file
new ol.layer.Vector({
title: 'ESRI Shape file',
source: new ol.source.Vector({
format: new ol.format.GeoJSON(),
loader: SHPloader,
url: 'data/boundaries.shp'
// parsed with DBF and SHX files
}),
style: vectorStyle,
visible: false
}),
// MVT file
new ol.layer.VectorTile({
title: 'MVT',
source: new ol.source.VectorTile({
format: new ol.format.MVT(),
tileGrid: ol.tilegrid.createXYZ({ maxZoom: 10 }),
// Filesystem
url: 'data/boundaries.mvt/{z}/{x}/{y}.pbf',
// TMS service
//url: 'http://localhost:8080/geoserver/gwc/service/'
// + 'tms/1.0.0/boundaries@EPSG%3A3857@pbf/{z}/{x}/{y}.pbf',
// WMTS service
//url: 'http://localhost:8080/geoserver/gwc/service/'
// + 'wmts?SERVICE=WMTS&VERSION=1.0.0&REQUEST=GetTile&LAYER=boundaries'
// + '&STYLE=&FORMAT=application%2Fvnd.mapbox-vector-tile'
// + '&TILEMATRIX=EPSG:3857:{z}&TILEMATRIXSET=EPSG:3857&TILECOL={x}&TILEROW={y}',
// WMS service
//tileUrlFunction: (coordinate, projection) => {
// let size = map.getSize();
// return 'http://localhost:8080/geoserver/gwc/service/'
// + 'wms?service=WMS&version=1.1.0&request=GetMap&layers=boundaries'
// + '&format=application%2Fvnd.mapbox-vector-tile'
// + '&width=' + size[0] + '&height=' + size[0] + '&srs=EPSG%3A3857'
// + '&bbox=' + map.getView().calculateExtent().join(',');
//},
transition: 0,
}),
style: vectorStyle,
visible: false
}),
// GeoJSON file
new ol.layer.Vector({
title: 'GeoJSON',
source: new ol.source.Vector({
format: new ol.format.GeoJSON(),
url: 'data/boundaries.geojson'
}),
style: vectorStyle,
visible: false
}),
// KML file
new ol.layer.Vector({
title: 'KML',
source: new ol.source.Vector({
format: new ol.format.KML(),
url: 'data/boundaries.kml'
}),
style: vectorStyle,
// KML has own style defeinition
visible: false
}),
// WKT file
new ol.layer.Vector({
title: 'WKT',
source: new ol.source.Vector({
format: new ol.format.WKT(),
url: 'data/boundaries.wkt'
}),
style: vectorStyle,
visible: false
}),
]
});
</script>
<script>
let source = document.getElementById('source').innerText;
let code = document.getElementById('code');
code.innerHTML = source.replace(/url: '(data\/.*?)'/g,
'url: \'<a href="$1" class="hljs-string">$1</a>\'');
hljs.initHighlightingOnLoad();
function toggleStyle(this_) {
let active = (this_.className == 'active') ? true : false;
this_.className = !active ? 'active' : '';
for (let i = 3; i <= 7; i++) {
map.getLayers().item(i).setStyle(active ? undefined : vectorStyle);
}
}
</script>
</body>
</html>