-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.js
209 lines (168 loc) · 5.27 KB
/
logic.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
import {PythonPredicatesHandling, TurfPredicatesHandling, serviceCollection} from "./handling.js"
import {CustomFeaturesClass} from "./features.js";
class StyleControler {
static colors = {
0: [244, 185, 66],
1: [0, 157, 220]
}
static getColor(feature) {
return StyleControler.colors[feature.getId() % 2]
}
static getColorById(id) {
return StyleControler.colors[id]
}
static drawStyleFunction = () => {
let color;
if (MapCanevas.uidCounter == 0) {
color = StyleControler.getColorById(0);
} else {
const id = MapCanevas.uidCounter % 2
color = StyleControler.getColorById(id)
}
return StyleControler.generateStyle(color)
}
static styleFunction = (feature) => {
const color = StyleControler.getColor(feature);
return StyleControler.generateStyle(color)
}
static generateStyle = (color) => {
const colorOpaque = [...color, 1];
const colorTransparent = [...color, 0.5]
return new ol.style.Style({
fill: new ol.style.Fill({
color: colorTransparent,
}),
stroke: new ol.style.Stroke({
color: colorOpaque,
width: 2,
}),
image: new ol.style.Circle({
radius: 4,
fill: new ol.style.Fill({
color: colorTransparent,
}),
stroke: new ol.style.Stroke({
color: colorOpaque,
width: 1,
}),
}),
})
}
}
class MapCanevas extends HTMLElement {
static uidCounter = 0;
connectedCallback() {
this.innerHTML = `
<div id="map"></div>
<app-type-select></app-type-select>
`;
this.raster = new ol.layer.Tile({
source: new ol.source.OSM({
attributions: [
ol.source.OSM.ATTRIBUTION
]
}),
});
this.source = new ol.source.Vector({ wrapX: false });
this.vector = new ol.layer.Vector({
source: this.source,
style: StyleControler.styleFunction
});
this.map = new ol.Map({
target: 'map',
layers: [this.raster, this.vector],
view: new ol.View({
center: [0, 0],
zoom: 2
}),
controls: [
new ol.control.Attribution()
]
});
document.addEventListener('ClickableFeatureIcon-clicked', (event) => {
if (event.detail.value !== 'clear') {
this.map.removeInteraction(draw);
addInteractions(event.detail.value);
} else {
MapCanevas.uidCounter = 0;
this.source.clear();
}
});
let draw;
let snap;
const addInteractions = (value) => {
if (value !== 'None') {
draw = new ol.interaction.Draw({
source: this.source,
type: value,
maxPoints: 20,
style: StyleControler.drawStyleFunction
});
this.map.addInteraction(draw);
snap = new ol.interaction.Snap({
edge: true,
vertex: true,
source: this.source
});
this.map.addInteraction(snap);
}
}
const removeLastFeature = () => {
const features = this.source.getFeatures();
if (features.length > 2) {
this.source.removeFeature(features[0]);
}
}
const generateUid = (feature) => {
feature.setId(MapCanevas.uidCounter);
MapCanevas.uidCounter = ++MapCanevas.uidCounter;
}
this.source.on('addfeature', (event) => {
removeLastFeature();
generateUid(event.feature);
const features = this.source.getFeatures()
document.dispatchEvent(new CustomEvent('features-added', {
detail: {
features: features
}
}));
})
addInteractions('None');
}
}
window.customElements.define('app-map-canevas', MapCanevas)
class Results extends HTMLElement {
static updateResults = (features) => {
if (features.features.length > 1) {
const resultsObject = {};
// Turf
resultsObject.turf = TurfPredicatesHandling.getResultData(features.asArrayOfGeoJson());
resultsObject.turf.pushToDOM();
// Shapely
const shapelyService = serviceCollection.shapely
const shapelyPredicateHandling = new PythonPredicatesHandling(shapelyService.name, shapelyService.url)
shapelyPredicateHandling.fetchResultData(features.asArrayOfString())
.then( (resultData) => {
resultsObject.shapely = resultData;
resultsObject.shapely.pushToDOM();
}
);
// Postgis
const postgisService = serviceCollection.postgis
const postgisPredicateHandling = new PythonPredicatesHandling(postgisService.name, postgisService.url)
postgisPredicateHandling.fetchResultData(features.asArrayOfString())
.then( (resultData) => {
resultsObject.postgis = resultData;
resultsObject.postgis.pushToDOM();
}
);
}
}
connectedCallback() {
}
}
window.customElements.define('app-results', Results)
document.addEventListener('features-added', (event) => {
const features = new CustomFeaturesClass(event.detail.features);
Results.updateResults(features);
});