-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_functions.js
202 lines (186 loc) · 6.04 KB
/
util_functions.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
const defaultStyle = {
fillColor: 'blue',
color: 'blue',
weight: 2,
fillOpacity: 0.2,
};
const apiUrl = "https://script.google.com/macros/s/AKfycbzqHIiQ74HoUlADdX0LcNxPxC2qzNTse93QEsg8sPeg0y-w8i3opdFTD_gER8gXn78Y/exec";
let resultsLayer;
async function mapInit(){
/*function handleHeaderMeta(headerMeta) {
const header = document.getElementById('header')
const formatter = new JSONFormatter(headerMeta, 10)
header.appendChild(formatter.render())
}*/
const response = await fetch('set.fgb');
for await (let feature of flatgeobuf.deserialize(response.body, undefined)) {
sets.features.push(feature)
// Leaflet styling
map.removeLayer(setsLayer)
setsLayer.addData(feature)
// Add the feature to the map
/*setsLayer = L.geoJSON(sets, {
style: defaultStyle,
onEachFeature: onEachFeature
})*/
//.addTo(map);
if(sets.features.length == 406){
setsLayer.addTo(map);
getRandomFetaure()
}
}
}
function onEachFeature(feature, layer){
if (feature.properties && feature.properties.LocNameHeb && feature.properties.LocalityCo) {
layer.bindPopup(`<h1>${feature.properties["LocNameHeb"]}</h1>\
<center>\
<h2>בגוש דן?</h2>\
<button onclick="sendYes(${feature.properties["LocalityCo"]})">כן</button>\
<button onclick="sendNo(${feature.properties["LocalityCo"]})">לא</button>\
</center>`)
}
layer.on({
mouseover: highlightFeature,
mouseout: resetHighlight,
click: onFeatureClick
});
}
function onEachResultsFeature(feature, layer){
if (feature.properties && feature.properties.LocNameHeb && feature.properties.LocalityCo) {
result = 'ללא הכרעה'
if(feature.properties.yes > feature.properties.no){
result = "חלק מגוש דן"
}else if(feature.properties.yes < feature.properties.no){
result = "לא חלק מגוש דן"
}
layer.bindPopup(`<h1>${feature.properties["LocNameHeb"]}</h1>\
<center>\
<h2>החלטת הקהל: ${result}</h2>
<h3><b>בעד:</b> ${feature.properties.yes}<br></h3>\
<h3><b>נגד:</b> ${feature.properties.no}<br></h3>\
</center>`)
}
layer.on({
mouseover: highlightFeature,
mouseout: resetHighlight,
click: onFeatureClick
});
}
function onFeatureClick(e){
let layer = e.target;
let feature = e.target.feature;
layer.openPopup()
}
function highlightFeature(e) {
var layer = e.target;
layer.setStyle({
color: 'cyan',
weight: 4,
//fillOpacity: 0.7,
});
if (!L.Browser.ie && !L.Browser.opera && !L.Browser.edge) {
layer.bringToFront();
}
}
function resetHighlight(e) {
if(map.hasLayer(setsLayer)){
setsLayer.resetStyle(e.target);
}else{
resultsLayer.resetStyle(e.target);
};
}
function sendYes(code){
fetch(apiUrl+`?code=${code}&answer=1`, {
'method': 'post',
'contentType': 'application/json'
})
.then(response => response.json())
.then(data => {
setsLayer.eachLayer(function (layer) {
if(layer.feature.properties && layer.feature.properties.LocalityCo){
if(layer.feature.properties.LocalityCo === code ){
layer.bindPopup(`<h1>${layer.feature.properties["LocNameHeb"]}</h1>\
<center>תודה<br>
<button onclick="getRandomFetaure()">לשחק שוב?</button></center>`)
}
}
})
})
}
function sendNo(code){
fetch(apiUrl+`?code=${code}&answer=0`, {
'method': 'post',
'contentType': 'application/json'
})
.then(response => response.json())
.then(data => {
setsLayer.eachLayer(function (layer) {
if(layer.feature.properties && layer.feature.properties.LocalityCo){
if(layer.feature.properties.LocalityCo === code ){
layer.bindPopup(`<h1>${layer.feature.properties["LocNameHeb"]}</h1>\
<center>תודה<br>
<button onclick="getRandomFetaure()">לשחק שוב?</button></center>`)
}
}
})
})
}
function getRandomFetaure(){
let i = Math.floor(Math.random() * 406);
var j =-1
setsLayer.eachLayer(function (layer) {
j +=1
if(layer.feature.properties && layer.feature.properties.LocalityCo){
if(j ===i){
map.fitBounds(layer.getBounds())
layer.openPopup()
}
}
})
}
function showResults(){
fetch(apiUrl)
.then(response => response.json())
.then(data => joinAttributes(data))
}
function joinAttributes(results){
sets.features.forEach(feature =>{
for(r=0;r<results.features.length;r++){
row = results.features[r]
if(row.id === feature.properties.LocalityCo){
feature.properties['yes'] = row.properties.yes
feature.properties['no'] = row.properties.no
feature.properties.color = getColor(feature.properties)
}
}
})
if(map.hasLayer(setsLayer)){map.removeLayer(setsLayer)}
if(map.hasLayer(resultsLayer)){map.removeLayer(resultsLayer)}
resultsLayer = L.geoJSON(sets, {
style: getFeatureStyle,
onEachFeature: onEachResultsFeature,
attribution: 'גבולות: למ"ס 2013, תוצאות: אתם'
})
resultsLayer.addTo(map)
if(!(legendControl._map)){
legendControl.addTo(map);
}
}
function getFeatureStyle(feature){
return {
fillColor: feature.properties.color,
weight: 2,
opacity: 1,
color: 'gray',
fillOpacity: 0.4
};
}
function getColor(properties){
let color = "#d95f02"
if(properties.yes > properties.no){
color = "#1b9e77"
}else if(properties.yes < properties.no){
color = "#7570b3"
}
return color;
}