-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
142 lines (130 loc) · 3.86 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
/*
goog.require('ol.Feature');
goog.require('ol.Map');
goog.require('ol.View');
goog.require('ol.format.GeoJSON');
goog.require('ol.interaction.Select');
goog.require('ol.layer.Tile');
goog.require('ol.layer.Vector');
goog.require('ol.proj');
goog.require('ol.source.MapQuest');
goog.require('ol.source.StaticCluster');
goog.require('ol.source.Vector');
goog.require('ol.style.Circle');
goog.require('ol.style.Fill');
goog.require('ol.style.Stroke');
goog.require('ol.style.Style');
goog.require('ol.style.Text');
*/
var source = new ol.source.Vector({
features: []
});
var oReq = new XMLHttpRequest();
oReq.onload = function() {
var format = new ol.format.GeoJSON();
var features = format.readFeatures(this.responseText,
{featureProjection: 'EPSG:3857'});
source.addFeatures(features);
};
oReq.open('get', 'input/dump_geojson_bus_fragments.geojson', true);
oReq.send();
var clusterResolutions = [0, 20, 100, 250, 500, 1000, 1500, 2000, 3000];
var clusterSource = new ol.source.StaticCluster({
distance: 40, // meters
source: source
}, clusterResolutions);
var styleCache = {};
var clusters = new ol.layer.Vector({
source: clusterSource,
style: function(feature, resolution) {
var rindex = feature.get('resolution_index')
var firstAppearanceResolution = clusterResolutions[rindex];
if (resolution > firstAppearanceResolution) {
return null;
}
var childResolution = 0;
for (var i = clusterResolutions.length; i > -1; --i) {
var r = clusterResolutions[i];
if (r <= resolution) {
childResolution = r;
break;
}
}
var children = feature.get('children') || {childResolution: []};
var size = childResolution > 0 ? children[childResolution].length : 0;
var style = styleCache[size];
if (!style) {
style = [new ol.style.Style({
image: new ol.style.Circle({
radius: 10,
stroke: new ol.style.Stroke({
color: '#fff'
}),
fill: new ol.style.Fill({
color: '#3399CC'
})
}),
text: new ol.style.Text({
text: size.toString(),
fill: new ol.style.Fill({
color: '#fff'
})
})
})];
styleCache[size] = style;
}
return style;
}
});
var raster = new ol.layer.Tile({
source: new ol.source.MapQuest({layer: 'sat'})
});
var map = new ol.Map({
layers: [raster, clusters],
renderer: 'canvas',
target: 'map',
view: new ol.View({
center: [0, 0],
zoom: 2
})
});
var select = new ol.interaction.Select();
map.addInteraction(select);
select.on('select', function(e) {
var features = e.target.getFeatures();
console.log('Selected', features.getLength());
var hierarchy = function(feature) {
var hiddenFeatures = feature.get('features');
if (!hiddenFeatures || hiddenFeatures.length == 1) {
return feature.getId();
} else {
return hiddenFeatures.map(hierarchy);
}
};
features.forEach(function(feature) {
var hiddenFeatures = feature.get('features');
console.log('Hidding', hiddenFeatures.length, hierarchy(feature));
});
});
function exportClusterHierarchyAsGeoJSON() {
var features = clusterSource.getSource().getFeatures();
var epsg3857 = ol.proj.get('EPSG:3857');
var epsg4326 = ol.proj.get('EPSG:4326');
features = features.map(function(f) {
var clone = f.clone();
clone.unset('features');
clone.unset('kind');
clone.set('resolution', clusterSource.clusterResolutionsById[f.getId()]);
clone.setId(f.getId());
clone.getGeometry().transform(epsg3857, epsg4326);
return clone;
});
var format = new ol.format.GeoJSON();
var str = format.writeFeatures(features);
var blob = new Blob([str], {'type': 'plain/text'});
var a = document.getElementById('url');
a.download = "clustered.json";
a.href = window.URL.createObjectURL(blob);
a.textContent = 'Download ready';
return str;
}