-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
204 lines (167 loc) · 6.83 KB
/
Copy pathscript.js
File metadata and controls
204 lines (167 loc) · 6.83 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
// Initialize OpenLayers Map
var olMap = new ol.Map({
target: 'map',
layers: [new ol.layer.Tile({source: new ol.source.OSM()})],
view: new ol.View({
center: ol.proj.fromLonLat([126.9780, 37.5665]),
zoom: 10
})
});
// VectorLayer Class
class VectorLayer {
constructor(title, map) {
this.layer = new ol.layer.Vector({
title: title,
source: new ol.source.Vector(),
style: new ol.style.Style({
stroke: new ol.style.Stroke({
color: '#0e97fa',
width: 4
})
})
});
map.addLayer(this.layer);
}
}
// Overlay Class
class Overlay {
constructor(map, element = document.getElementById("popup"), offset = [0, -15], positioning = 'bottom-center', className = 'ol-tooltip-measure ol-tooltip .ol-tooltip-static') {
this.overlay = new ol.Overlay({
element: element,
offset: offset,
positioning: positioning,
className: className
});
map.addOverlay(this.overlay);
}
}
// Draw Class
class Draw {
constructor(type, map, vector_layer) {
this.map = map;
this.vector_layer = vector_layer;
this.draw = new ol.interaction.Draw({
type: type,
stopClick: true
});
this.map.addInteraction(this.draw);
this.draw.on('drawstart', (e) => this.onDrawStart(e));
this.draw.on('drawend', (e) => this.onDrawEnd(e));
}
onDrawStart = (e) => {
//It will store the coordinates length of geometry
this.coordinates_length = 0;
//partDistanceOverlay is used to display the label of distance measurements on each segment of Line and Polygon geomtry
this.partDistanceOverlay = null;
//totalAreaDistanceOverlay is used to display the total distance if geomtery is LineString or it will display the area if geomtry is Polygon
this.totalAreaDistanceOverlay = new Overlay(this.map).overlay;
//lastPartLineOverlay is used to display the distance measurement of last segment of Polygon which is its last two coordinates
this.lastPartLineOverlay = new Overlay(this.map).overlay;
//Binding onGeomChange function with drawing feature
e.feature.getGeometry().on('change', this.onGeomChange);
}
/*
This function will be called when drawing is finished
*/
onDrawEnd = (e) => {
//Add drawn geometry to vector layer
this.vector_layer.getSource().addFeature(e.feature);
}
/*
This function will called when ever there will be a change in geometry like increase in length, area, position,
*/
onGeomChange = (e) => {
let geomType = e.target.getType();
let coordinates = e.target.getCoordinates();
if(geomType == "Polygon"){
coordinates = e.target.getCoordinates()[0];
}
//This logic will check if the new coordinates are added to geometry. If yes, then It will create a overlay for the new segment
if (coordinates.length > this.coordinates_length) {
this.partDistanceOverlay = new Overlay(this.map).overlay;
this.coordinates_length = coordinates.length;
}
else {
this.coordinates_length = coordinates.length;
}
let partLine = new ol.geom.LineString([coordinates[this.coordinates_length-2], coordinates[this.coordinates_length-1]]);
if(geomType == "Polygon") {
partLine = new ol.geom.LineString([coordinates[this.coordinates_length-3], coordinates[this.coordinates_length-2]]);
}
//the calculates the length of a segment and position the overlay at the midpoint of it
this.calDistance(this.partDistanceOverlay, partLine.getFlatMidpoint(), partLine.getLength());
//if geometry is LineString and coordinates_length is greater than 2, then calculate the total length of the line and set the position of the overlay at last coordninates
if (geomType == "LineString" && this.coordinates_length > 2 && e.target.getLength() > new ol.geom.LineString([coordinates[0], coordinates[1]]).getLength()) {
this.calDistance(this.totalAreaDistanceOverlay, coordinates[this.coordinates_length-1], ol.sphere.getLength(e.target));
}
//If geometry is Polygon, then it will create the overlay for area measurement and last segment of it which is its first and last coordinates.
if (geomType == "Polygon" && this.coordinates_length > 3) {
this.calArea(this.totalAreaDistanceOverlay, e.target.getFlatInteriorPoint(), ol.sphere.getArea(e.target));
partLine = new ol.geom.LineString([coordinates[this.coordinates_length-2], coordinates[this.coordinates_length-1]]);
this.calDistance(this.lastPartLineOverlay, partLine.getFlatMidpoint(), ol.sphere.getLength(partLine));
}
}
//Calculates the length of a segment and position the overlay at the midpoint of it.
calDistance = (overlay, overlayPosition, distance) => {
if(parseInt(distance) == 0) {
overlay.setPosition([0,0]);
}
else {
overlay.setPosition(overlayPosition);
if (distance >= 1000) {
overlay.element.innerHTML = (distance/1000).toFixed(2) + ' km';
}
else {
overlay.element.innerHTML = distance.toFixed(2) + ' m';
}
}
}
//Calculates the area of Polygon and position the overlay at the center of polygon
calArea = (overlay, overlayPosition, area) => {
if(parseInt(area) == 0) {
overlay.setPosition([0,0]);
}
else {
overlay.setPosition(overlayPosition);
if (area >= 10000) {
overlay.element.innerHTML = Math.round((area / 1000000) * 100) / 100 + ' km<sup>2<sup>';
}
else {
overlay.element.innerHTML = Math.round(area * 100) / 100 + ' m<sup>2<sup>';
}
}
}
}
let vector_layer = new VectorLayer('Temp Layer', olMap);
// Function to post map view changes to the iframe
function postMapViewToIframe() {
const mapView = olMap.getView();
const center = ol.proj.toLonLat(mapView.getCenter());
const zoom = mapView.getZoom();
const iframeWindow = document.querySelector('#cadastralIframe iframe').contentWindow;
iframeWindow.postMessage({ center, zoom }, '*');
}
// Add event listeners to post messages when the map view changes
olMap.on('moveend', postMapViewToIframe);
// Interaction management
let draw = null;
let btnClick = (geomType) => {
removeInteractions();
draw = new Draw(geomType, olMap, vector_layer.layer);
}
// Remove interactions
let removeInteractions = () => {
olMap.getInteractions().getArray().slice(9).forEach(interaction => {
olMap.removeInteraction(interaction);
});
}
// Clear vector features and overlays
let clear = () => {
removeInteractions();
olMap.getOverlays().clear();
vector_layer.layer.getSource().clear();
}
// Bind methods to button click events
document.getElementById('btn1').onclick = () => btnClick('LineString');
document.getElementById('btn2').onclick = () => btnClick('Polygon');
document.getElementById('btn3').onclick = clear;