-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunmined.openlayers.js
221 lines (186 loc) · 8.69 KB
/
unmined.openlayers.js
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
class Unmined {
map(mapId, options, regions) {
const dpiScale = window.devicePixelRatio ?? 1.0;
const worldMinX = options.minRegionX * 512;
const worldMinY = options.minRegionZ * 512;
const worldWidth = (options.maxRegionX + 1 - options.minRegionX) * 512;
const worldHeight = (options.maxRegionZ + 1 - options.minRegionZ) * 512;
const worldTileSize = 256;
const worldMaxZoomFactor = Math.pow(2, options.maxZoom);
// left, bottom, right, top, Y is negated
var mapExtent = ol.extent.boundingExtent([
[worldMinX * worldMaxZoomFactor, -(worldMinY + worldHeight) * worldMaxZoomFactor],
[(worldMinX + worldWidth) * worldMaxZoomFactor, -worldMinY * worldMaxZoomFactor]]);
var viewProjection = new ol.proj.Projection({
code: 'VIEW',
units: 'pixels',
});
var dataProjection = new ol.proj.Projection({
code: 'DATA',
units: 'pixels',
});
// Coordinate transformation between view and data
// OpenLayers Y is positive up, world Y is positive down
ol.proj.addCoordinateTransforms(viewProjection, dataProjection,
function (coordinate) {
return [coordinate[0], -coordinate[1]];
},
function (coordinate) {
return [coordinate[0], -coordinate[1]];
});
const mapZoomLevels = options.maxZoom - options.minZoom;
// Resolution for each OpenLayers zoom level
var resolutions = new Array(mapZoomLevels + 1);
for (let z = 0; z < mapZoomLevels + 1; ++z) {
resolutions[mapZoomLevels - z] = Math.pow(2, z) * dpiScale / worldMaxZoomFactor;
}
var tileGrid = new ol.tilegrid.TileGrid({
extent: mapExtent,
origin: [0,0],
resolutions: resolutions,
tileSize: worldTileSize / dpiScale
});
var unminedLayer =
new ol.layer.Tile({
source: new ol.source.XYZ({
projection: viewProjection,
tileGrid: tileGrid,
tilePixelRatio: dpiScale,
tileSize: worldTileSize / dpiScale,
tileUrlFunction: function (coordinate) {
const worldZoom = -(mapZoomLevels - coordinate[0]) + options.maxZoom;
const worldZoomFactor = Math.pow(2, worldZoom);
const minTileX = Math.floor(worldMinX * worldZoomFactor / worldTileSize);
const minTileY = Math.floor(worldMinY * worldZoomFactor / worldTileSize);
const maxTileX = Math.ceil((worldMinX + worldWidth) * worldZoomFactor / worldTileSize) - 1;
const maxTileY = Math.ceil((worldMinY + worldHeight) * worldZoomFactor / worldTileSize) - 1;
const tileX = coordinate[1];
const tileY = coordinate[2];
const tileBlockSize = worldTileSize / worldZoomFactor;
const tileBlockPoint = {
x: tileX * tileBlockSize,
z: tileY * tileBlockSize
};
const hasTile = function () {
const tileRegionPoint = {
x: Math.floor(tileBlockPoint.x / 512),
z: Math.floor(tileBlockPoint.z / 512)
};
const tileRegionSize = Math.ceil(tileBlockSize / 512);
for (let x = tileRegionPoint.x; x < tileRegionPoint.x + tileRegionSize; x++) {
for (let z = tileRegionPoint.z; z < tileRegionPoint.z + tileRegionSize; z++) {
const group = {
x: Math.floor(x / 32),
z: Math.floor(z / 32)
};
const regionMap = regions.find(e => e.x == group.x && e.z == group.z);
if (regionMap) {
const relX = x - group.x * 32;
const relZ = z - group.z * 32;
const inx = relZ * 32 + relX;
var b = regionMap.m[Math.floor(inx / 32)];
var bit = inx % 32;
var found = (b & (1 << bit)) != 0;
if (found) return true;
}
}
}
return false;
};
if (tileX >= minTileX
&& tileY >= minTileY
&& tileX <= maxTileX
&& tileY <= maxTileY
&& hasTile()) {
const url = ('tiles/zoom.{z}/{xd}/{yd}/tile.{x}.{y}.' + options.imageFormat)
.replace('{z}', worldZoom)
.replace('{yd}', Math.floor(tileY / 10))
.replace('{xd}', Math.floor(tileX / 10))
.replace('{y}', tileY)
.replace('{x}', tileX);
return url;
}
else
return undefined;
}
})
});
var mousePositionControl = new ol.control.MousePosition({
coordinateFormat: ol.coordinate.createStringXY(0),
projection: dataProjection
});
var map = new ol.Map({
target: mapId,
controls: ol.control.defaults().extend([
mousePositionControl
]),
layers: [
unminedLayer,
/*
new ol.layer.Tile({
source: new ol.source.TileDebug({
tileGrid: unminedTileGrid,
projection: viewProjection
})
})
*/
],
view: new ol.View({
center: [0,0],
extent: mapExtent,
projection: viewProjection,
resolutions: tileGrid.getResolutions(),
maxZoom: mapZoomLevels,
zoom: mapZoomLevels - options.maxZoom,
constrainResolution: true,
showFullExtent: true,
constrainOnlyCenter: true
})
});
if (options.markers) {
var markersLayer = this.createMarkersLayer(options.markers, dataProjection, viewProjection);
map.addLayer(markersLayer);
}
if (options.background){
document.getElementById(mapId).style.backgroundColor = options.background;
}
this.openlayersMap = map;
}
createMarkersLayer(markers, dataProjection, viewProjection) {
var features = [];
for (var i = 0; i < markers.length; i++) {
var item = markers[i];
var longitude = item.x;
var latitude = item.z;
var feature = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.transform([longitude, latitude], dataProjection, viewProjection))
});
var style = new ol.style.Style();
if (item.image)
style.setImage(new ol.style.Icon({
src: item.image,
anchor: item.imageAnchor,
scale: item.imageScale
}));
if (item.text)
style.setText(new ol.style.Text({
text: item.text,
font: item.font,
offsetX: item.offsetX,
offsetY: item.offsetY,
fill: new ol.style.Fill({
color: item.textColor
})
}));
feature.setStyle(style);
features.push(feature);
}
var vectorSource = new ol.source.Vector({
features: features
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource
});
return vectorLayer;
}
}