-
Notifications
You must be signed in to change notification settings - Fork 2
/
openlayers.js
293 lines (245 loc) · 6.7 KB
/
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
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
/* global ol */
var selectStyle =
[
new ol.style.Style({
stroke: new ol.style.Stroke({
color: 'white',
width: 5
})
}),
new ol.style.Style({
stroke: new ol.style.Stroke({
color: [0, 153, 255, 1],
width: 3
})
})
];
var defaultStyle= new ol.style.Style({
stroke: new ol.style.Stroke({
width: 6,
color: [0xff,0xff,0xff,0.01]
})
})
;
var selectedIds=[]
var osmids=[] //ret.map(d=>d.osm_id)
var roadLayer=
new ol.layer.VectorTile({
source: new ol.source.VectorTile({
format: new ol.format.GeoJSON(),
tileGrid: ol.tilegrid.createXYZ({maxZoom: 17}),
tilePixelRatio: 16,
url: '/roads/{z}/{x}/{y}.json'
})
,style:
function(f, resolution) {
// if (selectedIds.indexOf(f.getId())!=-1 )
// return selectStyle;
if(ways[f.getProperties().osm_id])
return selectStyle;
return defaultStyle ;
}
});
var route=function(frm, to){
fetch(location.protocol+'//'+location.hostname+(location.port ? ':'+location.port: '')+
"/route/?from="+encodeURIComponent(frm.val)+"&to="+encodeURIComponent(to.val)).then(r=>r.json()).then(function(dt){
if(dt.error)
throw dt.error;
let data=dt.segments;
document.getElementById("error").innerHTML="";
oldways=ways;
ways={}
resetSelected();
setCurrentText();
data.forEach(treatSelect);
roadLayer.getSource().changed();
var dominant= Object.keys(segments).map(k=>({k, n:Object.keys(segments[k])
.map(key=>segments[k][key]).reduce((a,b)=>a+b,0)})).reduce((p, {k, n}) => n>p.n?{k,n}:p, {k:undefined, n:0}).k;
ways={}
resetSelected();
data.filter(d=>d.ref && (d.ref==dominant || d.ref.endsWith(dominant) || d.ref.indexOf(dominant+";")!=-1)).forEach(treatSelect);
makeMessage();
// osmids= data.filter(d=>d.ref && d.ref.indexOf(dominant)!=-1).map(x=>x.osm_id);
zoomOn(dt.props);
roadLayer.getSource().changed();
document.getElementById("undo").disabled=oldways.length>0;
}).catch(e=> {
document.getElementById("error").innerHTML=e;
});
}
var mapnik= new ol.layer.Tile({
title:"Calitatea drumurilor",
source: new ol.source.OSM({
url:'/tiles/{z}/{x}/{y}.png'
,crossOrigin:null
})
, tileOptions: {crossOriginKeyword: null}
});
var landscape= new ol.layer.Tile({
title: 'Landscape',
type: 'base',
source: new ol.source.OSM({
url:'https://a.tile.thunderforest.com/landscape/{z}/{x}/{y}.png?apikey='+thunderforestKey
,crossOrigin:null
})
, tileOptions: {crossOriginKeyword: null}
});
var searchResults= new ol.Collection();
var searchSource = new ol.source.Vector();
var searchLayer = new ol.layer.Vector({
source: searchSource,
style: new ol.style.Style({
stroke: new ol.style.Stroke({
width: 6,
color: [255, 255, 0, 0.5]
})
})
});
var nav= new MapBrowserNav();
var map = new ol.Map({
layers: [
new ol.layer.Group({
'title': 'Base maps',
layers: [
landscape
]
}),
new ol.layer.Group({
'title': 'Overlays',
layers: [
mapnik,
searchLayer,
roadLayer
]})
],
target: 'map',
view: new ol.View({
center: ol.proj.fromLonLat(nav.center),
zoom: nav.zoom,
rotation:nav.rotation,
minZoom:7,
maxZoom:18
})
});
var layerSwitcher = new ol.control.LayerSwitcher({
tipLabel: 'Légende' // Optional label for button
});
map.addControl(layerSwitcher);
nav.attachTo(map);
map.on('singleclick', function(e) {
var lonlat=ol.proj.transform(e.coordinate, 'EPSG:3857', 'EPSG:4326');
document.getElementById('coords').innerHTML=(Math.round(lonlat[1]*10000)/10000)+", "+(Math.round(lonlat[0]*10000)/10000);
var selectedFeature = map.forEachFeatureAtPixel(e.pixel, function(f) {
return f;
});
if(selectedFeature){
if(ways[selectedFeature.getProperties().osm_id] && e.originalEvent.shiftKey ){
delete ways[selectedFeature.getProperties().osm_id]
setCurrentText();
} else{
if(!e.originalEvent.shiftKey){
oldways=ways;
ways={}
document.getElementById("undo").disabled=oldways.length>0;
}
else
document.getElementById("undo").disabled=true;
ways[selectedFeature.getProperties().osm_id]=selectedFeature.getProperties();
setCurrentText(selectedFeature);
}
/*
var x;
if((x=selectedIds.indexOf(selectedFeature.getId()))!=-1 && e.originalEvent.shiftKey)
{
selectedIds.splice(x,1);
selected.splice(x,1);
}
else{
if(!e.originalEvent.shiftKey)
{
selectedIds.length=0;
selected.length=0;
}
selectedIds.push(selectedFeature.getId());
selected.push(selectedFeature);
}
*/
resetSelected();
Object.keys(ways).forEach(s=>treatSelect(ways[s]));
makeMessage();
}
roadLayer.getSource().changed();
}, undefined, function(l) { return l == roadLayer; });
var undoSelection=function(){
ways=oldways;
resetSelected();
Object.keys(ways).forEach(s=>treatSelect(ways[s]));
makeMessage();
document.getElementById("undo").disabled=true;
roadLayer.getSource().changed();
}
var resetSelected=function(){
segments={};
roads={};
other=0;
}
var makeMessage=function(){
var log= ""+Object.keys(ways).length;
if(document.getElementById('text'))
document.getElementById('text').innerHTML=
Object.keys(segments).reduce(function(partial, key){
return partial+ ' '+key+':'+Object.keys(segments[key]).length;
}, log);
//if(document.querySelector('button[id="save"]'))
enableSave(true);
}
/*var selectClick = new ol.interaction.Select({
condition: ol.events.condition.click
});
*/
//map.addInteraction(selectClick);
var selected=[];
var segments={};
var ways={};
var oldways;
var roads={};
var other=0;
var treatSelect= function(rdprop){
var ref= rdprop.ref;
if(!ref)
ref=rdprop.name;
if(!ref)
ref=rdprop.osm_id.toString();
var x=segments[ref];
if(!x){
segments[ref]={};
}
segments[ref][rdprop.osm_id]=rdprop.len?rdprop.len:1;
ways[rdprop.osm_id]=rdprop;
if(!rdprop.ref && ! rdprop.name)
other++;
else
roads[ref]=1;
};
/*
selectClick.on('select', function(e){
if(selectClick.getFeatures().getLength()==0 && selected.length>0)
{
selectClick.getFeatures().extend(selected);
return;
}
selected=selectClick.getFeatures().getArray().slice(0);
segments={};
ways={};
roads={};
other=0;
selectClick.getFeatures().forEach(treatSelect);
var log= selectClick.getFeatures().getLength().toString();
document.getElementById('text').innerHTML=
Object.keys(segments).reduce(function(partial, key){
return partial+ ' '+key+':'+Object.keys(segments[key]).length;
}, log);
if(document.querySelector('button[id="save"]'))
enableSave(e.selected);
});
*/