-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1094 lines (909 loc) · 45.2 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
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
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Openlayer Grid</title>
<!-- Openlayers -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@latest/ol.css" />
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/ol@latest/dist/ol.js"></script>
<script
src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL,Object.assign"></script>
<script src="https://unpkg.com/@turf/turf"></script>
<!-- ol-ext -->
<link rel="stylesheet" href="./dist/ol-ext.css" />
<script type="text/javascript" src="./dist/ol-ext.js"></script>
<!-- Pointer events polyfill for old browsers, see https://caniuse.com/#feat=pointer -->
<script src="https://unpkg.com/elm-pep"></script>
</head>
<body style="width: 100%;height: 100%;">
<!-- Map div -->
<div id="map" style="width:100%; height:100vh; margin: 0px; padding: 0px;"></div>
<div className="rectangle" style="display: grid; position: absolute; top: 80px; left: 20px;">
<button onClick="handle()" id="handle">Activate Draw</button>
<button onClick="handleButtonValid()" style="margin-top: 5px; "> Valide</button>
<button onClick="handleButtonValidtest()" style="margin-top: 5px; "> GeoJsonTest</button>
</div>
<div className="rectangle" style="display: grid; position: absolute; top: 30px; left: 500px;">
<div>
<span>Grid Size</span>
<input type="number" style="text-align: center; margin-right: 5px;" id="myInput" value="10"
onchange="onInputChange(this)" />
<span>m</span>
</div>
<div style="margin-top: 2px;">
<span style="margin-right: 5px;">Grid mode</span>
<select id="select" value={selectedOption} onchange='handleSelectChange(this)'>
<option value="up_right">Top->Right align</option>
<option value="up_left">Top->Left align</option>
<option selected value="down_left">Bottom->Left align</option>
<option value="down_right">Bottom->Right align</option>
</select>
</div>
</div>
<script type="text/javascript">
var drawActive = false;
var drawFeature = null;
var newDrawFeature = null;
var rectangle = null;
var rectCoordinates = null;
var rectgridLayer = null;
var extent = null;
var interaction = null;
var value = 10;
var draw = null;
var startangle = 0;
var startRadius = 10;
var d = [0, 0];
// var map_grid_anchor = 'down_left';
// var map_grid_anchor = 'down_right';
var map_grid_anchor = 'down_left';
const map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM(),
}),
],
view: new ol.View({
center: [726376.0586457778, 5865060.915527701],
// center: [261720, 5951081],
zoom: 18,
}),
});
const source = new ol.source.Vector({ wrapX: false });
const vector = new ol.layer.Vector({
source,
style: new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(255, 255, 255, 0.2)',
}),
stroke: new ol.style.Stroke({
color: '#ffcc33',
width: 2,
}),
}),
name: 'RectLayer'
});
map.addLayer(vector);
function buttonTextChange(flag = false) {
drawActive = !drawActive;
if (drawActive) {
document.getElementById('handle').textContent = "Deactivat Draw";
}
else {
document.getElementById('handle').textContent = "Activat Draw";
map.removeInteraction(draw);
draw = null;
}
}
function handle() {
// let draw;
buttonTextChange(false);
if (!drawActive) return;
if (rectgridLayer) {
map.removeLayer(rectgridLayer);
rectgridLayer = null;
vector.getSource().removeFeature(drawFeature);
drawFeature = null;
}
if (interaction) {
map.removeInteraction(interaction);
interaction = null;
}
draw = new ol.interaction.Draw({
source: source,
type: 'Circle',
geometryFunction: ol.interaction.Draw.createBox(),
});
map.addInteraction(draw);
draw.on('drawend', (event) => {
const feature = event.feature;
const geometry = feature.getGeometry();
const extent = geometry.getExtent();
const width = extent[2] - extent[0];
const height = extent[3] - extent[1];
drawFeature = feature;
if (width < 300 && height < 300) {
drawFeature = feature;
buttonTextChange(true);
} else {
alert('Rectangle must be less than 300x300m');
vector.getSource().removeFeature(feature);
return;
}
interaction = new ol.interaction.Transform({
enableRotatedTransform: false,
addCondition: ol.events.condition.shiftKeyOnly,
hitTolerance: 2,
translateFeature: false,
scale: true,
rotate: true,
// keepAspectRatio: undefined,
translate: true,
keepRectangle: true,
stretch: true,
pointRadius: function (f) {
var radius = f.get('radius') || 10;
return [radius, radius];
}
});
map.addInteraction(interaction);
if (!interaction instanceof ol.interaction.Transform) return;
interaction.set('translate', interaction.get('translate'));
interaction.on(['rotatestart', 'translatestart', 'scalestart'], function (e) {
startangle = e.feature.get('angle') || 0;
startRadius = e.feature.get('radius') || 10;
d = [0, 0];
});
interaction.on('rotating', function (e) {
e.feature.set('angle', startangle - e.angle);
});
interaction.on('translating', function (e) {
d[0] += e.delta[0];
d[1] += e.delta[1];
});
interaction.on('scaling', function (e) {
if (e.features.getLength() === 1) {
var feature = e.features.item(0);
feature.set('radius', startRadius * Math.abs(e.scale[0]));
}
});
interaction.on(['rotateend', 'translateend', 'scaleend'], function (e) {
console.log(e.features.getArray()[0]);
drawFeature = e.features.getArray()[0];
});
// Refresh
interaction.set('translate', interaction.get('translate'));
});
};
function getFeaturefromGeoJSON(map_grid_geo_json) {
var format = new ol.format.GeoJSON();
var feature = format.readFeature(map_grid_geo_json, {
dataProjection: 'EPSG:4326',
featureProjection: 'EPSG:3857',
});
const rectangleCoordinates = feature.getGeometry().getCoordinates()[0];
const polygon = new ol.geom.Polygon([[
rectangleCoordinates[1],
rectangleCoordinates[2],
rectangleCoordinates[3],
rectangleCoordinates[0],
rectangleCoordinates[1]
]]);
return new ol.Feature({
geometry: polygon,
});
}
function handleButtonValidtest() {
var map_grid_geo_json = '{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[6.525147155,46.52619121599999],[6.524717199,46.52589173700002],[6.525159396,46.525446587000005],[6.525589356000001,46.525650067000015],[6.525147155,46.52609521599999]]]},"properties":null}';
drawFeature = getFeaturefromGeoJSON(map_grid_geo_json);
if (drawFeature) {
const layers = map.getLayers();
const gridStyle = new ol.style.Style({
image: new ol.style.Circle({
fill: new ol.style.Fill({
color: 'red',
}),
stroke: new ol.style.Stroke({
color: 'black',
width: 2,
}),
radius: 8,
}),
stroke: new ol.style.Stroke({
color: 'blue',
width: 2,
}),
text: new ol.style.Text({
fill: new ol.style.Fill({
color: 'rgba(255, 0, 0, 1)'
}),
font: '12px sans-serif',
textBaseline: 'bottom',
text: 'label'
})
});
const gridSource = new ol.source.Vector();
const gridLayer = new ol.layer.Vector({
source: gridSource,
style: function (feature) {
const fill = new ol.style.Fill({
color: 'rgba(255, 0, 0, 0.2)'
});
const stroke = new ol.style.Stroke({
color: 'blue',
width: 2
});
const text = new ol.style.Text({
fill: new ol.style.Fill({
color: 'rgba(255, 0, 0, 1)'
}),
font: '12px sans-serif',
textBaseline: 'center',
text: feature.get('text')
});
return new ol.style.Style({
fill: fill,
stroke: stroke,
text: text,
image: new ol.style.Circle({
radius: 6,
fill: new ol.style.Fill({
color: 'red'
}),
stroke: new ol.style.Stroke({
color: 'black',
width: 2
})
})
});
}
});
map.addLayer(gridLayer);
rectgridLayer = (gridLayer);
getNewRectangleFromPolygon(drawFeature.getGeometry().getCoordinates()[0], gridSource);
}
};
function isValidPoint(pt1, pt2, pt3) {
var minX = pt1[0];
var maxX = pt2[0];
if (pt1[0] >= pt2[0]) {
minX = pt2[0];
maxX = pt1[0];
}
if (pt3[0] > minX && pt3[0] < maxX) return false;
return true;
}
function truncateToDecimalPlaces(number, decimalPlaces = 7) {
const factor = 10 ** decimalPlaces;
return Math.floor(number * factor) / factor;
}
function isVectorDirection(vect1, vect2) {
var sc = vect1[0] *vect2[0] + vect1[1] * vect2[1];
if (sc > 0) return true
return false;
}
function getBaseLinePointsArray(array, source) {
console.log("array_array", array);
var newArray = [];
if (array.length == 2) newArray = [array[0], array[0], array[1]];
var Vx = array[1][0] - array[0][0];
var Vy = array[1][1] - array[0][1];
if (array.length == 3) {
var Vx1 = array[2][0] - array[0][0];
var Vy1 = array[2][1] - array[0][1];
if (isVectorDirection([Vx, Vy], [Vx1, Vy1])) {
newArray = [array[0], array[0], array[2]];
} else {
newArray = [array[0], array[2], array[1]];
}
} else if (array.length == 4) {
console.log(array)
var Vx1 = array[2][0] - array[0][0];
var Vy1 = array[2][1] - array[0][1];
var Vx2 = array[3][0] - array[0][0];
var Vy2 = array[3][1] - array[0][1];
if (isVectorDirection([Vx, Vy], [Vx1, Vy1]) && isVectorDirection([Vx, Vy], [Vx2, Vy2])) {
newArray = [array[0], array[2], array[1]];
} else if (isVectorDirection([Vx, Vy], [Vx1, Vy1])) {
newArray = [array[0], array[3], array[2]];
} else if (isVectorDirection([Vx, Vy], [Vx2, Vy2])) {
newArray = [array[0], array[2], array[3]];
} else {
newArray = [array[0], array[0], array[1]];
}
}
// newArray = [array[0], array[0], array[1]];
if (newArray[0] != newArray[1]){
var length = getLength(newArray[0], newArray[1]);
length = (parseInt(length / value) + 1 ) * value - length;
const Ax = newArray[1][0] - newArray[2][0];
const Ay = newArray[1][1] - newArray[2][1];
const ta = length * 1.0 / Math.sqrt(Ax * Ax + Ay * Ay);
newArray[1] = [newArray[1][0] + Ax * ta, newArray[1][1] + Ay * ta]
return newArray;
}
return newArray;
}
function getNewCoords(baseLinePointsArray, pt1, pt2, source) {
var pointToLineDistance1 = getPointToLineDistance(baseLinePointsArray[1], baseLinePointsArray[2], pt1);
var pointToLineDistance2 = getPointToLineDistance(baseLinePointsArray[1], baseLinePointsArray[2], pt2);
var tempPoint = [0, 0];
if (pointToLineDistance1 >= pointToLineDistance2) {
tempPoint = pt1;
} else {
tempPoint = pt2;
}
var points = getAnotherPoints(baseLinePointsArray[1], baseLinePointsArray[2], tempPoint);
var pointArray = [baseLinePointsArray[1], baseLinePointsArray[2], points[0], points[1]];
if ( !isCounterclockwiseDirection(pointArray)) {
console.log('counterclock')
pointArray = pointArray.reverse();
}
var startPointIndex = 0;
// var endPointIndex = 1;
var minLength = 9999999999999999.999999;
for (var i = 0; i < pointArray.length; i++) {
if (pointArray[i][0] <= minLength) {
minLength = pointArray[i][0];
startPointIndex = i;
}
}
console.log(startPointIndex);
var newPointArray = [];
for (var i = 0; i < pointArray.length; i++) {
newPointArray.push(pointArray[(startPointIndex + i)%4])
}
return [newPointArray[0], newPointArray[1], newPointArray[2], newPointArray[3], newPointArray[0]];
}
function getPointToLineDistance(pt1, pt2, pt0) {
var lineCoeff = getLineCoeff(pt1, pt2);
var a = lineCoeff[0];
var b = lineCoeff[1];
var distance = Math.abs(a * pt0[0] - pt0[1] + b) / Math.sqrt(a * a + 1);
return distance;
}
function getAnotherPoints(pt1, pt2, pt0) {
var lineCoeff = getLineCoeff(pt1, pt2);
var a = lineCoeff[0];
var b = lineCoeff[1];
var b1 = pt0[1] - a * pt0[0];
var b2 = pt1[1] + pt1[0] / a;
var x = (b2 - b1) / (a + 1 / a);
var y = - x / a + b2;
var b3 = pt2[1] + pt2[0] / a;
var x1 = (b3 - b1) / (a + 1 / a);
var y1 = - x1 / a + b3;
return [[x1, y1], [x, y]];
}
function getLineCoeff(pt1, pt2) {
let a = 1;
if (Math.abs(pt1[0] - pt2[0]) == 0) a = 1;
else a = (pt1[1] - pt2[1]) / (pt1[0] - pt2[0]);
let b = 0;
b = pt1[1] - a * pt1[0];
return [a, b]
}
function getClosestPointFromLine(pt1, pt2, pt0) {
let lineCoeff = getLineCoeff(pt1, pt2);
var b1 = pt0[1] + pt0[0] / lineCoeff[0];
var x = (b1 - lineCoeff[1]) / (lineCoeff[0] + 1 / lineCoeff[0]);
var y = - x / lineCoeff[0] + b1;
return [x, y];
}
function getCoordsByLongerLine(coordinates) {
var pointArray = [];
if (map_grid_anchor == 'down_left') {
pointArray.push(coordinates[0]);
var length1 = getLength(coordinates[0], coordinates[1]);
var length2 = getLength(coordinates[0], coordinates[3]);
if (length1 >= length2) {
pointArray.push(coordinates[1]);
pointArray.push(coordinates[2]);
pointArray.push(coordinates[3]);
} else {
pointArray.push(coordinates[3]);
pointArray.push(coordinates[2]);
pointArray.push(coordinates[1]);
}
} else if (map_grid_anchor == 'down_right') {
pointArray.push(coordinates[1]);
var length1 = getLength(coordinates[1], coordinates[0]);
var length2 = getLength(coordinates[1], coordinates[2]);
if (length1 >= length2) {
pointArray.push(coordinates[0]);
pointArray.push(coordinates[3]);
pointArray.push(coordinates[2]);
} else {
pointArray.push(coordinates[2]);
pointArray.push(coordinates[3]);
pointArray.push(coordinates[0]);
}
} else if (map_grid_anchor == 'up_left') {
pointArray.push(coordinates[3]);
var length1 = getLength(coordinates[3], coordinates[0]);
var length2 = getLength(coordinates[3], coordinates[2]);
if (length1 >= length2) {
pointArray.push(coordinates[0]);
pointArray.push(coordinates[1]);
pointArray.push(coordinates[2]);
} else {
pointArray.push(coordinates[2]);
pointArray.push(coordinates[1]);
pointArray.push(coordinates[0]);
}
} else if (map_grid_anchor == 'up_right') {
pointArray.push(coordinates[2]);
var length1 = getLength(coordinates[2], coordinates[3]);
var length2 = getLength(coordinates[2], coordinates[1]);
if (length1 >= length2) {
pointArray.push(coordinates[3]);
pointArray.push(coordinates[0]);
pointArray.push(coordinates[1]);
} else {
pointArray.push(coordinates[1]);
pointArray.push(coordinates[0]);
pointArray.push(coordinates[3]);
}
}
return pointArray;
}
function reorderArray(arr, index) {
if (index < 0 || index >= arr.length) {
// Handle invalid index
console.error('Invalid index');
return arr;
}
// Reorder the array
const reorderedArray = arr.slice(index).concat(arr.slice(0, index));
return reorderedArray;
}
function isCounterclockwiseDirection(coordinates) {
if (coordinates.length < 3) {
// Not enough points to determine direction
console.error('Insufficient number of coordinates.');
return false;
}
let sum = 0;
for (let i = 1; i < coordinates.length - 1; i++) {
const x1 = coordinates[i][0] - coordinates[i - 1][0];
const y1 = coordinates[i][1] - coordinates[i - 1][1];
const x2 = coordinates[i + 1][0] - coordinates[i][0];
const y2 = coordinates[i + 1][1] - coordinates[i][1];
// Calculate the cross product
const crossProduct = x1 * y2 - x2 * y1;
// Add the result to the sum
sum += crossProduct;
}
// Determine the overall direction based on the sign of the sum
if (sum > 0) {
return true;
} else if (sum < 0) {
return false;
}
return false;
}
function getNewPolygon(pointArray, source) {
const Ax = pointArray[0][0] - pointArray[1][0];
const Ay = pointArray[0][1] - pointArray[1][1];
const Bx = pointArray[0][0] - pointArray[3][0];
const By = pointArray[0][1] - pointArray[3][1];
const ta = value * 1.0 / (Math.sqrt(Ax * Ax + Ay * Ay));
const tb = value * 1.0 / (Math.sqrt(Bx * Bx + By * By));
const newPoint1 = getNewPoint([Ax, Ay], [Bx, By], ta, tb, pointArray[0]);
const newPoint2 = getNewPoint([-Ax, -Ay], [Bx, By], ta, tb, pointArray[1]);
const newPoint3 = getNewPoint([-Ax, -Ay], [-Bx, -By], ta, tb, pointArray[2]);
const newPoint4 = getNewPoint([Ax, Ay], [-Bx, -By], ta, tb, pointArray[3]);
return [newPoint1, newPoint2, newPoint3, newPoint4, newPoint1];
}
function getNewPoint(VectorA, VectorB, ta, tb, pt) {
const newX1 = ta * VectorA[0] + pt[0];
const newY1 = ta * VectorA[1] + pt[1];
const newX2 = tb * VectorB[0] + pt[0];
const newY2 = tb * VectorB[1] + pt[1];
const X = newX1 + newX2 - pt[0];
const Y = newY1 + newY2 - pt[1];
return [X, Y];
}
function isRectangle(points) {
if (points.length !== 4) {
console.error('Invalid number of points. A rectangle needs exactly 4 points.');
return false;
}
// Calculate vectors
const vector1 = [points[1][0] - points[0][0], points[1][1] - points[0][1]];
const vector2 = [points[2][0] - points[1][0], points[2][1] - points[1][1]];
const vector3 = [points[3][0] - points[2][0], points[3][1] - points[2][1]];
const vector4 = [points[0][0] - points[3][0], points[0][1] - points[3][1]];
// Calculate dot products
const dotProduct1 = vector1[0] * vector2[0] + vector1[1] * vector2[1];
const dotProduct2 = vector2[0] * vector3[0] + vector2[1] * vector3[1];
const dotProduct3 = vector3[0] * vector4[0] + vector3[1] * vector4[1];
const dotProduct4 = vector4[0] * vector1[0] + vector4[1] * vector1[1];
// Check if dot products are zero (indicating perpendicular vectors)
if (dotProduct1 === 0 && dotProduct2 === 0 && dotProduct3 === 0 && dotProduct4 === 0) {
// Check if opposite sides are equal in length
const length1 = Math.sqrt(vector1[0] * vector1[0] + vector1[1] * vector1[1]);
const length2 = Math.sqrt(vector2[0] * vector2[0] + vector2[1] * vector2[1]);
const length3 = Math.sqrt(vector3[0] * vector3[0] + vector3[1] * vector3[1]);
const length4 = Math.sqrt(vector4[0] * vector4[0] + vector4[1] * vector4[1]);
if (length1 === length3 && length2 === length4) {
return true;
}
}
return false;
}
function drawGridLinestring(poly1, line1, source) {
const intersection1 = turf.lineIntersect(poly1, line1);
let pointsTurf = [];
if (intersection1) {
turf.coordEach(intersection1, function (currentCoord, coordIndex, featureIndex, multiFeatureIndex, geometryIndex) {
pointsTurf.push(currentCoord);
});
if (pointsTurf.length < 2) return;
source.addFeature(new ol.Feature({
geometry: new ol.geom.LineString([
pointsTurf[0],
pointsTurf[1],
]),
}));
}
}
function drawCellPolygon(drawFeature, coordinates, clickedCoordinates, source) {
const newFeature = new ol.Feature({
geometry: new ol.geom.Polygon(
[coordinates]
),
});
const poly1 = turf.polygon(drawFeature.getGeometry().getCoordinates());
const cellPolygon = turf.polygon(newFeature.getGeometry().getCoordinates());
const intersection1 = turf.intersect(cellPolygon, poly1);
if (intersection1) {
var coords = turf.getCoords(intersection1);
tempolygon = new ol.geom.Polygon(coords);
if (tempolygon.intersectsCoordinate(clickedCoordinates)) {
source.addFeature(new ol.Feature({
geometry: tempolygon,
}));
}
}
}
function getNewRectangleFromPolygon(coordinates, source) {
source.addFeature(drawFeature);
var newCoords = [];
newCoords = coordinates;
if (!isRectangle(coordinates.slice(0,4))) {
var alignCoords = getCoordsByLongerLine(coordinates);
var pointArray = [alignCoords[0], alignCoords[1]];
var nearestPoint = getClosestPointFromLine(alignCoords[0], alignCoords[1], alignCoords[2]);
if (isValidPoint(alignCoords[0], alignCoords[1], nearestPoint)) {
pointArray.push(nearestPoint);
}
nearestPoint = [];
nearestPoint = getClosestPointFromLine(alignCoords[0], alignCoords[1], alignCoords[3]);
if (isValidPoint(alignCoords[0], alignCoords[1], nearestPoint)) {
pointArray.push(nearestPoint);
}
var baseLinePointsArray = getBaseLinePointsArray(pointArray, source);
newCoords = getNewCoords(baseLinePointsArray, alignCoords[2], alignCoords[3], source);
}
var newPolygon = getNewPolygon(newCoords.slice(0, 4), source);
newFeature = new ol.Feature({
geometry: new ol.geom.Polygon(
[newPolygon]
),
});
const rectangleCoordinates = newFeature.getGeometry().getCoordinates()[0];
const rectwidth = getLength(rectangleCoordinates[0], rectangleCoordinates[1]);
const rectheight = getLength(rectangleCoordinates[0], rectangleCoordinates[3]);
const numCols1 = (rectwidth / value);
const numRows1 = (rectheight / value);
const Ax = rectangleCoordinates[0][0] - rectangleCoordinates[1][0];
const Ay = rectangleCoordinates[0][1] - rectangleCoordinates[1][1];
const Bx = rectangleCoordinates[0][0] - rectangleCoordinates[3][0];
const By = rectangleCoordinates[0][1] - rectangleCoordinates[3][1];
const ta = value * 1.0 / (Math.sqrt(Ax * Ax + Ay * Ay));
const tb = value * 1.0 / (Math.sqrt(Bx * Bx + By * By));
for (let i = 1; i <= parseInt(numCols1); i++) {
if (map_grid_anchor == 'down_left') {
const lineFeature = new ol.Feature({
geometry: new ol.geom.LineString([
[rectangleCoordinates[0][0] - ta * Ax * i, rectangleCoordinates[0][1] - ta * Ay * i],
[rectangleCoordinates[3][0] - ta * Ax * i, rectangleCoordinates[3][1] - ta * Ay * i],
]),
});
const poly1 = turf.polygon(drawFeature.getGeometry().getCoordinates());
const line1 = turf.lineString(lineFeature.getGeometry().getCoordinates());
drawGridLinestring(poly1, line1, source);
}
else if (map_grid_anchor == 'down_right') {
const lineFeature = new ol.Feature({
geometry: new ol.geom.LineString([
[rectangleCoordinates[1][0] + ta * Ax * i, rectangleCoordinates[1][1] + ta * Ay * i],
[rectangleCoordinates[2][0] + ta * Ax * i, rectangleCoordinates[2][1] + ta * Ay * i],
]),
});
const poly1 = turf.polygon(drawFeature.getGeometry().getCoordinates());
const line1 = turf.lineString(lineFeature.getGeometry().getCoordinates());
drawGridLinestring(poly1, line1, source);
}
else if (map_grid_anchor == 'up_left') {
const lineFeature = new ol.Feature({
geometry: new ol.geom.LineString([
[rectangleCoordinates[0][0] - ta * Ax * i, rectangleCoordinates[0][1] - ta * Ay * i],
[rectangleCoordinates[3][0] - ta * Ax * i, rectangleCoordinates[3][1] - ta * Ay * i],
]),
});
const poly1 = turf.polygon(drawFeature.getGeometry().getCoordinates());
const line1 = turf.lineString(lineFeature.getGeometry().getCoordinates());
drawGridLinestring(poly1, line1, source);
}
else if (map_grid_anchor === 'up_right') {
const lineFeature = new ol.Feature({
geometry: new ol.geom.LineString([
[rectangleCoordinates[2][0] + ta * Ax * i, rectangleCoordinates[2][1] + ta * Ay * i],
[rectangleCoordinates[1][0] + ta * Ax * i, rectangleCoordinates[1][1] + ta * Ay * i],
]),
});
const poly1 = turf.polygon(drawFeature.getGeometry().getCoordinates());
const line1 = turf.lineString(lineFeature.getGeometry().getCoordinates());
drawGridLinestring(poly1, line1, source);
}
}
for (let j = 1; j <= parseInt(numRows1); j++) {
if (map_grid_anchor === 'down_left' || map_grid_anchor === 'down_right') {
const line = new ol.geom.LineString([
[rectangleCoordinates[0][0] - tb * Bx * j, rectangleCoordinates[0][1] - tb * By * j],
[rectangleCoordinates[1][0] - tb * Bx * j, rectangleCoordinates[1][1] - tb * By * j],
]);
const lineFeature = new ol.Feature({
geometry: line,
});
const poly1 = turf.polygon(drawFeature.getGeometry().getCoordinates());
const line1 = turf.lineString(lineFeature.getGeometry().getCoordinates());
drawGridLinestring(poly1, line1, source);
}
else if (map_grid_anchor === 'up_left' || map_grid_anchor === 'up_right') {
const line = new ol.geom.LineString([
[rectangleCoordinates[2][0] + tb * Bx * j, rectangleCoordinates[2][1] + tb * By * j],
[rectangleCoordinates[3][0] + tb * Bx * j, rectangleCoordinates[3][1] + tb * By * j],
]);
const lineFeature = new ol.Feature({
geometry: line,
});
const poly1 = turf.polygon(drawFeature.getGeometry().getCoordinates());
const line1 = turf.lineString(lineFeature.getGeometry().getCoordinates());
drawGridLinestring(poly1, line1, source);
}
}
newDrawFeature = newFeature;
map.on('click', function (event) {
const clickedCoordinates = event.coordinate;
console.log(clickedCoordinates);
if (!newDrawFeature) return;
const rectangleCoordinates = newDrawFeature.getGeometry().getCoordinates()[0];
const rectwidth = getLength(rectangleCoordinates[0], rectangleCoordinates[1]);
const rectheight = getLength(rectangleCoordinates[0], rectangleCoordinates[3]);
const numCols1 = (rectwidth / value);
const numRows1 = (rectheight / value);
const Ax = rectangleCoordinates[0][0] - rectangleCoordinates[1][0];
const Ay = rectangleCoordinates[0][1] - rectangleCoordinates[1][1];
const Bx = rectangleCoordinates[0][0] - rectangleCoordinates[3][0];
const By = rectangleCoordinates[0][1] - rectangleCoordinates[3][1];
const ta = value * 1.0 / (Math.sqrt(Ax * Ax + Ay * Ay));
const tb = value * 1.0 / (Math.sqrt(Bx * Bx + By * By));
for (let i = 0; i <= Math.ceil(numCols1)+1; i++) {
for (let j = 0; j <= Math.ceil(numRows1)+1; j++) {
if (map_grid_anchor == 'down_left') {
let cellTopLeft = [
rectangleCoordinates[0][0] - tb * Bx * (j-1) - ta * Ax * (i),
rectangleCoordinates[0][1] - tb * By * (j-1) - ta * Ay * (i)
];
let cellBottomRight = [
rectangleCoordinates[0][0] - tb * Bx * (j) - ta * Ax * (i-1),
rectangleCoordinates[0][1] - tb * By * (j) - ta * Ay * (i-1)
];
let cellTopRight = [
rectangleCoordinates[0][0] - tb * Bx * (j) - ta * Ax * i,
rectangleCoordinates[0][1] - tb * By * (j) - ta * Ay * i
];
let cellBottomLeft = [
rectangleCoordinates[0][0] - tb * Bx * (j-1) - ta * Ax * (i-1),
rectangleCoordinates[0][1] - tb * By * (j-1) - ta * Ay * (i-1)
];
var coordinates = [
cellBottomLeft,
cellBottomRight,
cellTopRight,
cellTopLeft,
cellBottomLeft
];
let tempolygon = new ol.geom.Polygon( [ coordinates ] );
let isInside = tempolygon.intersectsCoordinate(clickedCoordinates);
if (isInside) {
drawCellPolygon(drawFeature, coordinates, clickedCoordinates, source);
break;
}
}
if (map_grid_anchor == 'down_right') {
// Calculate the coordinates of the current grid cell
let cellTopLeft = [
rectangleCoordinates[1][0] - tb * Bx * j + ta * Ax * (i - 1),
rectangleCoordinates[1][1] - tb * By * j + ta * Ay * (i - 1)
];
let cellBottomRight = [
rectangleCoordinates[1][0] - tb * Bx * (j - 1) + ta * Ax * i,
rectangleCoordinates[1][1] - tb * By * (j - 1) + ta * Ay * i
];
let cellTopRight = [
rectangleCoordinates[1][0] - tb * Bx * j + ta * Ax * (i),
rectangleCoordinates[1][1] - tb * By * j + ta * Ay * (i)
];
let cellBottomLeft = [
rectangleCoordinates[1][0] - tb * Bx * (j - 1) + ta * Ax * (i - 1),
rectangleCoordinates[1][1] - tb * By * (j - 1) + ta * Ay * (i - 1)
];
var coordinates = [
cellBottomLeft,
cellBottomRight,
cellTopRight,
cellTopLeft,
cellBottomLeft
];
let tempolygon = new ol.geom.Polygon( [ coordinates ] );
let isInside = tempolygon.intersectsCoordinate(clickedCoordinates);
if (isInside) {
drawCellPolygon(drawFeature, coordinates, clickedCoordinates, source);
break;
}
}
else if (map_grid_anchor === 'up_left') {
let cellTopLeft = [
rectangleCoordinates[3][0] + tb * Bx * (j-1) - ta * Ax * (i-1),
rectangleCoordinates[3][1] + tb * By * (j-1) - ta * Ay * (i-1)
];
let cellBottomRight = [
rectangleCoordinates[3][0] + tb * Bx * (j) - ta * Ax * i,
rectangleCoordinates[3][1] + tb * By * (j) - ta * Ay * i
];
let cellTopRight = [
rectangleCoordinates[3][0] + tb * Bx * (j - 1) - ta * Ax * (i),
rectangleCoordinates[3][1] + tb * By * (j - 1) - ta * Ay * (i) ];
let cellBottomLeft = [
rectangleCoordinates[3][0] + tb * Bx * (j) - ta * Ax * (i-1),
rectangleCoordinates[3][1] + tb * By * (j) - ta * Ay * (i-1)
];
var coordinates = [
cellBottomLeft,
cellBottomRight,
cellTopRight,
cellTopLeft,
cellBottomLeft
];
let tempolygon = new ol.geom.Polygon( [ coordinates ] );
let isInside = tempolygon.intersectsCoordinate(clickedCoordinates);
if (isInside) {
drawCellPolygon(drawFeature, coordinates, clickedCoordinates, source);
break;
}
}
else if (map_grid_anchor === 'up_right') {
let cellTopLeft = [
rectangleCoordinates[2][0] + tb * Bx * (j-1) + ta * Ax * (i),
rectangleCoordinates[2][1] + tb * By * (j-1) + ta * Ay * (i)
];
let cellBottomRight = [
rectangleCoordinates[2][0] + tb * Bx * (j) + ta * Ax * (i-1),
rectangleCoordinates[2][1] + tb * By * (j) + ta * Ay * (i-1)
];
let cellTopRight = [
rectangleCoordinates[2][0] + tb * Bx * (j-1) + ta * Ax * (i-1),
rectangleCoordinates[2][1] + tb * By * (j-1) + ta * Ay * (i-1)
];
let cellBottomLeft = [
rectangleCoordinates[2][0] + tb * Bx * (j) + ta * Ax * (i),
rectangleCoordinates[2][1] + tb * By * (j) + ta * Ay * (i)
];
var coordinates = [
cellBottomLeft,
cellBottomRight,
cellTopRight,
cellTopLeft,
cellBottomLeft
];
let tempolygon = new ol.geom.Polygon( [ coordinates ] );
let isInside = tempolygon.intersectsCoordinate(clickedCoordinates);
if (isInside) {
drawCellPolygon(drawFeature, coordinates, clickedCoordinates, source);
break;
}
}
}
}
});
}
function handleButtonValid() {
if (interaction) {
map.removeInteraction(interaction);
interaction = (null);