-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
225 lines (184 loc) · 5.31 KB
/
index.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
var mymap = L.map('mapid').setView([42, -93], 4);
L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
attribution: '© <a href="https://www.mapbox.com/about/maps/">Mapbox</a> © <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> <strong><a href="https://www.mapbox.com/map-feedback/" target="_blank">Improve this map</a></strong>',
tileSize: 512,
maxZoom: 18,
zoomOffset: -1,
id: 'mapbox/streets-v11',
}).addTo(mymap);
var data = new L.GeoJSON.AJAX("newfile.geojson",{
onEachFeature:onEachFeature,
});
var legend;
var stations = new L.GeoJSON.AJAX("stations.geojson",{
pointToLayer: function (feature, latlng) {
return L.circleMarker(latlng, {
radius: 2,
fillColor: "#0000ff",
color: "#000",
weight: 1,
opacity: 1,
fillOpacity: 0.8
});
}
})
data.on('data:loaded', function(){
var min=1000
var max=0
var key
for(key in data['_layers'])
{
max=Math.max(data['_layers'][key]['feature']['properties']['elev_mean'],max)
min=Math.min(data['_layers'][key]['feature']['properties']['elev_mean'],min)
}
addProperties(Object.keys(data['_layers'][key]['feature']['properties']))
diff=max-min
for(var key in data['_layers'])
{
perc=100*(data['_layers'][key]['feature']['properties']['elev_mean']-min)/diff
data['_layers'][key].setStyle({color:perc2color(perc),weight:1 })
}
data.addTo(mymap);
legend = L.control({position: 'bottomright'});
legend.onAdd = function (map) {
var div = L.DomUtil.create('div', 'info legend'),
grades = [0, 10, 20, 50, 100],
labels = ['<strong> Elevation</strong>'],
from, to;
for (var i = 0; i < grades.length; i++) {
from = grades[i];
to = grades[i + 1];
labels.push(
'<i style="background:' + perc2color(from) + '"></i> ' +
Math.round(min+from/100*diff) );
}
div.innerHTML = labels.join('<br>');
return div;
};
legend.addTo(mymap);
});
stations.on('data:loaded',function(){
stations.addTo(mymap)
});
function onEachFeature(feature, layer) {
//bind click
layer.on('click', function (e) {
// e = event
// You can make your ajax call declaration here
//$.ajax(...
$(datasetList).empty()
var data=`<table class="table table-bordered">`+
`<tbody>`
Object.keys(feature["properties"]).forEach(function(k){
if(k=='geojson')
index='basin_id'
else
index=k
data+=`<tr>`+
`<th scope=row>`+
index+
`</th>`+
`<td>`+
feature["properties"][k]+
`</td> <tr>`
});
data+=`</tbody></table>`
$(data).appendTo('#datasetList')
});
}
function addProperties(keys){
console.log(keys)
$(propertylist).empty()
var dropdown=`<a class="dropdown-item dropdown-item-checked" href="#">`+`None`+`</a>`
for (k in keys){
dropdown+=`<a class="dropdown-item" onclick="recolor(this)" href="#">`+
keys[k]+
`</a>`
};
$(dropdown).appendTo('#propertylist')
}
function perc2color(perc) {
var r, g, b = 0;
if(perc < 50) {
r = 255;
g = Math.round(5.1 * perc);
}
else {
g = 255;
r = Math.round(510 - 5.10 * perc);
}
var h = r * 0x10000 + g * 0x100 + b * 0x1;
return '#' + ('000000' + h.toString(16)).slice(-6);
}
// Sidebar resizing
var minSize = 100;
var maxSize = $(window).width() * 0.95;
$('#splitbar').mousedown(function (e) {
e.preventDefault();
$(document).mousemove(function (e) {
e.preventDefault();
var x = e.pageX - $('#sidebar-wrapper').offset().left;
if (x > minSize && x < maxSize && e.pageX < ($(window).width() - minSize)) {
$('#sidebar-wrapper').css("width", x);
$('#map-container').css("width", $(window).width() - x);
map.invalidateSize();
}
})
});
$(document).mouseup(function (e) {
$(document).unbind('mousemove');
});
function recolor(input){
colorscheme=input.innerHTML
mymap.removeLayer(data)
mymap.removeControl(legend)
var min=1000
var max=0
for(var key in data['_layers'])
{
max=Math.max(data['_layers'][key]['feature']['properties'][colorscheme],max)
min=Math.min(data['_layers'][key]['feature']['properties'][colorscheme],min)
}
diff=max-min
for(var key in data['_layers'])
{
perc=100*(data['_layers'][key]['feature']['properties'][colorscheme]-min)/diff
data['_layers'][key].setStyle({color:perc2color(perc),weight:1 })
}
data.addTo(mymap);
legend = L.control({position: 'bottomright'});
legend.onAdd = function (map) {
var div = L.DomUtil.create('div', 'info legend'),
grades = [0, 10, 20, 50, 100],
labels = ['<strong>'+colorscheme+'</strong>'],
from, to;
for (var i = 0; i < grades.length; i++) {
from = grades[i];
to = grades[i + 1];
labels.push(
'<i style="background:' + perc2color(from) + '"></i> ' +
Math.round(100*(min+from/100*diff))/100 );
}
div.innerHTML = labels.join('<br>');
return div;
};
legend.addTo(mymap);
}
(function(){
function onChange(event) {
var reader = new FileReader();
reader.onload = onReaderLoad;
reader.readAsText(event.target.files[0]);
}
function onReaderLoad(event){
console.log(event.target.result);
var obj = JSON.parse(event.target.result);
geojson = L.geoJson(obj, {
onEachFeature: onEachFeature
}).addTo(mymap);
}
function alert_data(name, family){
alert(name);
}
document.getElementById('file').addEventListener('change', onChange);
}());