Repository navigation
Expand file tree
/
Copy patholScriptStrings.py
More file actions
870 lines (762 loc) · 23.1 KB
/
Copy patholScriptStrings.py
File metadata and controls
870 lines (762 loc) · 23.1 KB
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
from . import xmltodict
from qgis2web.olStyleScripts import getStrokeStyle
def measureControlScript():
measureControl = """let measuring = false;
const measureButton = document.createElement('button');
measureButton.className = 'measure-button fas fa-ruler';
measureButton.title = 'Measure';
const measureControl = document.createElement('div');
measureControl.className = 'ol-unselectable ol-control measure-control';
measureControl.appendChild(measureButton);
map.getTargetElement().appendChild(measureControl);
// Event handler
function handleMeasure() {
if (!measuring) {
selectLabel.style.display = "";
map.addInteraction(draw);
createHelpTooltip();
createMeasureTooltip();
measuring = true;
} else {
selectLabel.style.display = "none";
map.removeInteraction(draw);
map.removeOverlay(helpTooltip);
map.removeOverlay(measureTooltip);
const staticTooltips = document.getElementsByClassName("tooltip-static");
while (staticTooltips.length > 0) {
staticTooltips[0].parentNode.removeChild(staticTooltips[0]);
}
measureLayer.getSource().clear();
sketch = null;
measuring = false;
}
}
measureButton.addEventListener('click', handleMeasure);
measureButton.addEventListener('touchstart', handleMeasure);"""
return measureControl
def measuringScript():
measuring = """
map.on('pointermove', function(evt) {
if (evt.dragging) {
return;
}
if (measuring) {
/** @type {string} */
var helpMsg = 'Click to start drawing';
if (sketch) {
var geom = (sketch.getGeometry());
if (geom instanceof ol.geom.Polygon) {
helpMsg = continuePolygonMsg;
} else if (geom instanceof ol.geom.LineString) {
helpMsg = continueLineMsg;
}
}
helpTooltipElement.innerHTML = helpMsg;
helpTooltip.setPosition(evt.coordinate);
}
});
"""
return measuring
def measureScript():
measure = """
var selectLabel = document.createElement("label");
selectLabel.innerHTML = " Measure: ";
var typeSelect = document.createElement("select");
typeSelect.id = "type";
var measurementOption = [
{ value: "LineString", description: "Length" },
{ value: "Polygon", description: "Area" }
];
measurementOption.forEach(function (option) {
var optionElement = document.createElement("option");
optionElement.value = option.value;
optionElement.text = option.description;
typeSelect.appendChild(optionElement);
});
selectLabel.appendChild(typeSelect);
measureControl.appendChild(selectLabel);
selectLabel.style.display = "none";
/**
* Currently drawn feature.
* @type {ol.Feature}
*/
/**
* The help tooltip element.
* @type {Element}
*/
var helpTooltipElement;
/**
* Overlay to show the help messages.
* @type {ol.Overlay}
*/
var helpTooltip;
/**
* The measure tooltip element.
* @type {Element}
*/
var measureTooltipElement;
/**
* Overlay to show the measurement.
* @type {ol.Overlay}
*/
var measureTooltip;
/**
* Message to show when the user is drawing a line.
* @type {string}
*/
var continueLineMsg = 'Click to continue drawing the line';
/**
* Message to show when the user is drawing a polygon.
* @type {string}
*/
var continuePolygonMsg = "1click continue, 2click close";
var typeSelect = document.getElementById("type");
var typeSelectForm = document.getElementById("form_measure");
typeSelect.onchange = function (e) {
map.removeInteraction(draw);
addInteraction();
map.addInteraction(draw);
};
var measureLineStyle = new ol.style.Style({
stroke: new ol.style.Stroke({
color: "rgba(0, 0, 255)", //blu
lineDash: [10, 10],
width: 4
}),
image: new ol.style.Circle({
radius: 6,
stroke: new ol.style.Stroke({
color: "rgba(255, 255, 255)",
width: 1
}),
})
});
var measureLineStyle2 = new ol.style.Style({
stroke: new ol.style.Stroke({
color: "rgba(255, 255, 255)",
lineDash: [10, 10],
width: 2
}),
image: new ol.style.Circle({
radius: 5,
stroke: new ol.style.Stroke({
color: "rgba(0, 0, 255)",
width: 1
}),
fill: new ol.style.Fill({
color: "rgba(255, 204, 51, 0.4)",
}),
})
});
var labelStyle = new ol.style.Style({
text: new ol.style.Text({
font: "14px Calibri,sans-serif",
fill: new ol.style.Fill({
color: "rgba(0, 0, 0, 1)"
}),
stroke: new ol.style.Stroke({
color: "rgba(255, 255, 255, 1)",
width: 3
})
})
});
var labelStyleCache = [];
var styleFunction = function (feature, type) {
var styles = [measureLineStyle, measureLineStyle2];
var geometry = feature.getGeometry();
var type = geometry.getType();
var lineString;
if (!type || type === type) {
if (type === "Polygon") {
lineString = new ol.geom.LineString(geometry.getCoordinates()[0]);
} else if (type === "LineString") {
lineString = geometry;
}
}
if (lineString) {
var count = 0;
lineString.forEachSegment(function (a, b) {
var segment = new ol.geom.LineString([a, b]);
var label = formatLength(segment);
if (labelStyleCache.length - 1 < count) {
labelStyleCache.push(labelStyle.clone());
}
labelStyleCache[count].setGeometry(segment);
labelStyleCache[count].getText().setText(label);
styles.push(labelStyleCache[count]);
count++;
});
}
return styles;
};
var source = new ol.source.Vector();
var measureLayer = new ol.layer.Vector({
source: source,
displayInLayerSwitcher: false,
style: function (feature) {
labelStyleCache = [];
return styleFunction(feature);
}
});
map.addLayer(measureLayer);
var draw; // global so we can remove it later
function addInteraction() {
var type = typeSelect.value;
draw = new ol.interaction.Draw({
source: source,
type: /** @type {ol.geom.GeometryType} */ (type),
style: function (feature) {
return styleFunction(feature, type);
}
});
var listener;
draw.on('drawstart',
function(evt) {
// set sketch
sketch = evt.feature;
/** @type {ol.Coordinate|undefined} */
var tooltipCoord = evt.coordinate;
listener = sketch.getGeometry().on('change', function(evt) {
var geom = evt.target;
var output;
if (geom instanceof ol.geom.Polygon) {
output = formatArea(/** @type {ol.geom.Polygon} */ (geom));
tooltipCoord = geom.getInteriorPoint().getCoordinates();
} else if (geom instanceof ol.geom.LineString) {
output = formatLength(/** @type {ol.geom.LineString} */ (geom));
tooltipCoord = geom.getLastCoordinate();
}
measureTooltipElement.innerHTML = output;
measureTooltip.setPosition(tooltipCoord);
});
}, this);
draw.on('drawend',
function(evt) {
measureTooltipElement.className = 'tooltip tooltip-static';
measureTooltip.setOffset([0, -7]);
// unset sketch
sketch = null;
// unset tooltip so that a new one can be created
measureTooltipElement = null;
createMeasureTooltip();
ol.Observable.unByKey(listener);
}, this);
}
/**
* Creates a new help tooltip
*/
function createHelpTooltip() {
if (helpTooltipElement) {
helpTooltipElement.parentNode.removeChild(helpTooltipElement);
}
helpTooltipElement = document.createElement('div');
helpTooltipElement.className = 'tooltip hidden';
helpTooltip = new ol.Overlay({
element: helpTooltipElement,
offset: [15, 0],
positioning: 'center-left'
});
map.addOverlay(helpTooltip);
}
/**
* Creates a new measure tooltip
*/
function createMeasureTooltip() {
if (measureTooltipElement) {
measureTooltipElement.parentNode.removeChild(measureTooltipElement);
}
measureTooltipElement = document.createElement('div');
measureTooltipElement.className = 'tooltip tooltip-measure';
measureTooltip = new ol.Overlay({
element: measureTooltipElement,
offset: [0, -15],
positioning: 'bottom-center'
});
map.addOverlay(measureTooltip);
}
"""
return measure
def measureUnitFeetScript():
measureUnitFeet = """
function convertToFeet(length) {
feet_length = length * 3.2808;
return feet_length
}
/**
* format length output
* @param {ol.geom.LineString} line
* @return {string}
*/
var formatLength = function(line) {
var length;
var coordinates = line.getCoordinates();
length = 0;
var sourceProj = map.getView().getProjection();
for (var i = 0, ii = coordinates.length - 1; i < ii; ++i) {
var c1 = ol.proj.transform(coordinates[i], sourceProj, 'EPSG:4326');
var c2 = ol.proj.transform(coordinates[i + 1], sourceProj, 'EPSG:4326');
length += ol.sphere.getDistance(c1, c2);
}
feet_length = convertToFeet(length)
var output;
if (feet_length > 5280) {
output = (Math.round(feet_length / 5280 * 100) / 100) + ' miles';
} else {
output = (Math.round(feet_length * 100) / 100) + ' ft';
}
return output;
};
/**
* Format area output.
* @param {ol.geom.Polygon} polygon The polygon.
* @return {string} Formatted area.
*/
var formatArea = function (polygon) {
var sourceProj = map.getView().getProjection();
var geom = polygon.clone().transform(sourceProj, 'EPSG:3857');
var area = Math.abs(ol.sphere.getArea(geom));
var output;
if (area > 2589988) { // 1 sq mi in square meters
output = (Math.round((area / 2589988) * 1000) / 1000) + ' sq mi';
} else {
output = (Math.round(area * 10.7639 * 100) / 100) + ' sq ft';
}
return output;
};
addInteraction();
var parentElement = document.querySelector(".measure-control");
var elementToMove = document.getElementById("form_measure");
if (elementToMove && parentElement) {
parentElement.insertBefore(elementToMove, parentElement.firstChild);
}
"""
return measureUnitFeet
def measureUnitMetricScript():
measureUnitMetric = """
/**
* format length output
* @param {ol.geom.LineString} line
* @return {string}
*/
var formatLength = function(line) {
var length;
var coordinates = line.getCoordinates();
length = 0;
var sourceProj = map.getView().getProjection();
for (var i = 0, ii = coordinates.length - 1; i < ii; ++i) {
var c1 = ol.proj.transform(coordinates[i], sourceProj, 'EPSG:4326');
var c2 = ol.proj.transform(coordinates[i + 1], sourceProj, 'EPSG:4326');
length += ol.sphere.getDistance(c1, c2);
}
var output;
if (length > 100) {
output = (Math.round(length / 1000 * 100) / 100) +
' ' + 'km';
} else {
output = (Math.round(length * 100) / 100) +
' ' + 'm';
}
return output;
};
/**
* Format area output.
* @param {ol.geom.Polygon} polygon The polygon.
* @return {string} Formatted area.
*/
var formatArea = function (polygon) {
var sourceProj = map.getView().getProjection();
var geom = polygon.clone().transform(sourceProj, 'EPSG:3857');
var area = Math.abs(ol.sphere.getArea(geom));
var output;
if (area > 1000000) {
output = Math.round((area / 1000000) * 1000) / 1000 + ' ' + 'km<sup>2</sup>';
} else {
output = Math.round(area * 100) / 100 + ' ' + 'm<sup>2</sup>';
}
return output.replace('.', ',');
};
addInteraction();
var parentElement = document.querySelector(".measure-control");
var elementToMove = document.getElementById("form_measure");
if (elementToMove && parentElement) {
parentElement.insertBefore(elementToMove, parentElement.firstChild);
}
"""
return measureUnitMetric
def measureStyleScript(controlCount):
pos = 65 + (controlCount * 35)
touchPos = 80 + (controlCount * 50)
measureStyle = """
<style>
.tooltip {
position: relative;
background: rgba(0, 0, 0, 0.5);
border-radius: 4px;
color: white;
padding: 4px 8px;
opacity: 0.7;
white-space: nowrap;
}
.tooltip-measure {
opacity: 1;
font-weight: bold;
}
.tooltip-static {
background-color: #ffcc33;
color: black;
border: 1px solid white;
}
.tooltip-measure:before,
.tooltip-static:before {
border-top: 6px solid rgba(0, 0, 0, 0.5);
border-right: 6px solid transparent;
border-left: 6px solid transparent;
content: "";
position: absolute;
bottom: -6px;
margin-left: -7px;
left: 50%%;
}
.tooltip-static:before {
border-top-color: #ffcc33;
}
.measure-control {
top: %(pos)dpx;
left: .5em;
display: flex;
}
.ol-touch .measure-control {
top: %(touchPos)dpx;
}
.measure-control label {
padding: 1px;
padding-right: 4px;
}
</style>""" % {"pos": pos, "touchPos": touchPos}
return measureStyle
def layerSearchStyleScript(controlCount):
pos = 65 + (controlCount * 35)
touchPos = 80 + (controlCount * 50)
layerSearchStyle = """
<style>
.search-layer {
top: %(pos)dpx;
left: .5em;
}
.ol-touch .search-layer {
top: %(touchPos)dpx;
}
</style>""" % {"pos": pos, "touchPos": touchPos}
return (layerSearchStyle, controlCount)
def geolocation(geolocate):
if geolocate:
return """
const geolocateOverlay = new ol.layer.Vector({
source: new ol.source.Vector({
features: [accuracyFeature, positionFeature],
}),
});
const geolocation = new ol.Geolocation({
projection: map.getView().getProjection(),
});
geolocation.on('change:accuracyGeometry', function () {
accuracyFeature.setGeometry(geolocation.getAccuracyGeometry());
});
geolocation.on('change:position', function () {
const coords = geolocation.getPosition();
positionFeature.setGeometry(coords ? new ol.geom.Point(coords) : null);
});
geolocation.setTracking(true);
function handleGeolocate() {
if (isTracking) {
map.removeLayer(geolocateOverlay);
isTracking = false;
} else if (geolocation.getTracking()) {
map.addLayer(geolocateOverlay);
const pos = geolocation.getPosition();
if (pos) {
map.getView().setCenter(pos);
}
isTracking = true;
}
}
geolocateButton.addEventListener('click', handleGeolocate);
geolocateButton.addEventListener('touchstart', handleGeolocate);
"""
else:
return ""
def geolocationHead(geolocate):
if geolocate:
return """
let isTracking = false;
const geolocateButton = document.createElement('button');
geolocateButton.className = 'geolocate-button fa fa-map-marker';
geolocateButton.title = 'Geolocalizza';
const geolocateControl = document.createElement('div');
geolocateControl.className = 'ol-unselectable ol-control geolocate';
geolocateControl.appendChild(geolocateButton);
map.getTargetElement().appendChild(geolocateControl);
const accuracyFeature = new ol.Feature();
const positionFeature = new ol.Feature({
style: new ol.style.Style({
image: new ol.style.Circle({
radius: 6,
fill: new ol.style.Fill({ color: '#3399CC' }),
stroke: new ol.style.Stroke({ color: '#fff', width: 2 }),
}),
}),
});"""
else:
return ""
def geolocateStyle(geolocate, controlCount):
if geolocate:
ctrlPos = 65 + (controlCount * 35)
touchCtrlPos = 80 + (controlCount * 50)
controlCount = controlCount + 1
return ("""
<style>
.geolocate {
top: %dpx;
left: .5em;
}
.ol-touch .geolocate {
top: %dpx;
}
</style>""" % (ctrlPos, touchCtrlPos), controlCount)
else:
return ("", controlCount)
def geocodeLinks(geocode):
if geocode:
returnVal = """
<link href="resources/photon-geocoder-autocomplete.min.css" rel="stylesheet">"""
return returnVal
else:
return ""
def geocodeJS(geocode):
if geocode:
returnVal = """
<script src="resources/photon-geocoder-autocomplete.min.js"></script>"""
return returnVal
else:
return ""
def mapboxStyleJS():
return """
<script src="resources/olms.js"></script>"""
def geocodeScript(geocode):
if geocode != "None":
return f"""
//Layer to represent the point of the geocoded address
var geocoderLayer = new ol.layer.Vector({{
source: new ol.source.Vector(),
}});
map.addLayer(geocoderLayer);
var vectorSource = geocoderLayer.getSource();
//Variable used to store the coordinates of geocoded addresses
var obj2 = {{
value: '',
letMeKnow() {{
//console.log(`Geocoded position: ${{this.gcd}}`);
}},
get gcd() {{
return this.value;
}},
set gcd(value) {{
this.value = value;
this.letMeKnow();
}}
}}
var obj = {{
value: '',
get label() {{
return this.value;
}},
set label(value) {{
this.value = value;
}}
}}
// Function to handle the selected address
function onSelected(feature) {{
obj.label = feature;
input.value = typeof obj.label.properties.label === "undefined"? obj.label.properties.display_name : obj.label.properties.label;
var coordinates = ol.proj.transform(
[feature.geometry.coordinates[0], feature.geometry.coordinates[1]],
"EPSG:4326",
map.getView().getProjection()
);
vectorSource.clear(true);
obj2.gcd = [feature.geometry.coordinates[0], feature.geometry.coordinates[1]];
var marker = new ol.Feature(new ol.geom.Point(coordinates));
var zIndex = 1;
marker.setStyle(new ol.style.Style({{
image: new ol.style.Icon(({{
anchor: [0.5, 1],
anchorXUnits: 'fraction',
anchorYUnits: 'fraction',
scale: 0.7,
opacity: 1,
src: "./resources/marker.png",
zIndex: zIndex
}})),
zIndex: zIndex
}}));
vectorSource.addFeature(marker);
map.getView().setCenter(coordinates);
map.getView().setZoom(18);
}}
// Format the result in the autocomplete search bar
var formatResult = function (feature, el) {{
var title = document.createElement("strong");
el.appendChild(title);
var detailsContainer = document.createElement("small");
el.appendChild(detailsContainer);
var details = [];
title.innerHTML = feature.properties.label || feature.properties.display_name;
var types = {{
housenumber: "numéro",
street: "rue",
locality: "lieu-dit",
municipality: "commune",
}};
if (
feature.properties.city &&
feature.properties.city !== feature.properties.name
) {{
details.push(feature.properties.city);
}}
if (feature.properties.context) {{
details.push(feature.properties.context);
}}
detailsContainer.innerHTML = details.join(", ");
}};
// Define a class to create the control button for the search bar in a div tag
class AddDomControl extends ol.control.Control {{
constructor(elementToAdd, opt_options) {{
const options = opt_options || {{}};
const element = document.createElement("div");
if (options.className) {{
element.className = options.className;
}}
element.appendChild(elementToAdd);
super({{
element: element,
target: options.target,
}});
}}
}}
// Function to show you can do something with the returned elements
function myHandler(featureCollection) {{
//console.log(featureCollection);
}}
// URL for API
const url = {{"Nominatim OSM": "https://nominatim.openstreetmap.org/search?format=geojson&addressdetails=1&",
"France BAN": "https://api-adresse.data.gouv.fr/search/?"}}
var API_URL = "//api-adresse.data.gouv.fr";
// Create search by adresses component
var containers = new Photon.Search({{
resultsHandler: myHandler,
onSelected: onSelected,
placeholder: "Search an address",
formatResult: formatResult,
//url: API_URL + "/search/?",
url: url["{geocode}"],
position: "topright",
// ,includePosition: function() {{
// return ol.proj.transform(
// map.getView().getCenter(),
// map.getView().getProjection(), //'EPSG:3857',
// 'EPSG:4326'
// );
// }}
}});
// Add the created DOM element within the map
//var left = document.getElementById("top-left-container");
var controlGeocoder = new AddDomControl(containers, {{
className: "photon-geocoder-autocomplete ol-unselectable ol-control",
}});
map.addControl(controlGeocoder);
var search = document.getElementsByClassName("photon-geocoder-autocomplete ol-unselectable ol-control")[0];
search.style.display = "flex";
// Create the new button element
var button = document.createElement("button");
button.type = "button";
button.id = "gcd-button-control";
button.className = "gcd-gl-btn fa fa-search leaflet-control";
// Ajouter le bouton à l'élément parent
search.insertBefore(button, search.firstChild);
last = search.lastChild;
last.style.display = "none";
button.addEventListener("click", function (e) {{
if (last.style.display === "none") {{
last.style.display = "block";
}} else {{
last.style.display = "none";
}}
}});
input = document.getElementsByClassName("photon-input")[0];
//var searchbar = document.getElementsByClassName("photon-geocoder-autocomplete ol-unselectable ol-control")[0]
//left.appendChild(searchbar);
"""
else:
return ""
def getGrid(project):
grid = ""
if project.readBoolEntry("Grid", "/Enabled", False)[0]:
stroke = project.readEntry("Grid", "/LineSymbol", "")[0]
strokeDict = xmltodict.parse(stroke)
symbol = strokeDict["symbol"]
layer = symbol["layer"]
props = layer["prop"]
lineunits = "px"
linecap = 0
linejoin = 0
width = 1
color = "#000000"
dashed = "no"
for prop in props:
if prop["@k"] == "line_color":
color = "'rgba(%s)'" % prop["@v"]
if prop["@k"] == "line_style":
dashed = prop["@v"]
if prop["@k"] == "line_width":
width = prop["@v"]
if prop["@k"] == "capstyle":
linecap = prop["@v"]
if prop["@k"] == "joinstyle":
linejoin = prop["@v"]
strokeStyle, _ = getStrokeStyle(color, dashed, width, lineunits,
linecap, linejoin)
strokeStyle = strokeStyle.replace("stroke:", "strokeStyle:")
grid = """
var gcl = new ol.Graticule({%s});
gcl.setMap(map);""" % strokeStyle
return grid
def getM2px(mapUnitsLayers):
m2px = ""
if len(mapUnitsLayers) > 0:
m2px = """
function m2px(m) {
var centerLatLng = map.getView().getCenter();
var pointC = map.getPixelFromCoordinate(centerLatLng);
var pointX = [pointC[0] + 100, pointC[1]];
var latLngC = map.getCoordinateFromPixel(pointC);
var latLngX = map.getCoordinateFromPixel(pointX);
var lineX = new ol.geom.LineString([latLngC, latLngX]);
var distanceX = lineX.getLength() / 100;
reciprocal = 1 / distanceX;
px = Math.ceil(reciprocal);
return px;
}"""
return m2px
def getMapUnitLayers(mapUnitsLayers):
mapUnitLayers = ""
if len(mapUnitsLayers) > 0:
lyrs = []
for layer in mapUnitsLayers:
lyrs.append("""
lyr_%s.setStyle(style_%s);""" % (layer, layer))
lyrScripts = "".join(lyrs)
mapUnitLayers = """
map.getView().on('change:resolution', function(evt){
%s
});""" % lyrScripts
return mapUnitLayers