This repository has been archived by the owner on Mar 14, 2021. It is now read-only.
forked from calvinmetcalf/leaflet.workspace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
200 lines (175 loc) · 5.92 KB
/
script.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
require(['catiline'], function(cw) {
var worker = cw({
init: function(scope) {
importScripts('jam/require.js');
require.config({
baseUrl: this.base
});
require(['shp'], function(shp) {
scope.shp = shp;
});
},
data: function(data, cb, scope) {
this.shp(data).then(function(geoJson){
if(Array.isArray(geoJson)){
geoJson.forEach(function(geo){
scope.json([geo, geo.fileName, true],true,scope);
});
}else{
scope.json([geoJson, geoJson.fileName, true],true,scope);
}
}, function(e) {
console.log('shit', e);
});
},
color:function(s){
//from http://stackoverflow.com/a/15710692
importScripts('js/colorbrewer.js');
return colorbrewer.Spectral[11][Math.abs(JSON.stringify(s).split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return a&a},0)) % 11];
},
makeString:function(buffer) {
var array = new Uint8Array(buffer);
var len = array.length;
var outString = "";
var i = 0;
while (i < len) {
outString += String.fromCharCode(array[i++]);
}
return outString;
},
json: function(data, cb, scope) {
importScripts('js/topojson.v1.min.js');
var name = data[1];
//console.log(name);
var json = data.length === 2 ? JSON.parse(scope.makeString(data[0])) : data[0];
var nom;
if (json.type === 'Topology') {
for (nom in json.objects) {
scope.layer(topojson.feature(json, json.objects[nom]), nom, scope);
}
}
else {
scope.layer(json, name, scope);
}
},layer:function(json,name,scope){
json.features.forEach(function(feature){
feature.properties.__color__ = scope.color(feature);
});
scope.fire('json',[json,name]);
},
base: cw.makeUrl('.')
});
function readerLoad() {
if (this.readyState !== 2 || this.error) {
return;
}
else {
worker.data(this.result, [this.result]);
}
}
function handleZipFile(file) {
var reader = new FileReader();
reader.onload = readerLoad;
reader.readAsArrayBuffer(file);
}
function handleFile(file) {
m.spin(true);
if (file.name.slice(-3) === 'zip') {
return handleZipFile(file);
}
var reader = new FileReader();
reader.onload = function() {
var ext;
if (reader.readyState !== 2 || reader.error) {
return;
}
else {
ext = file.name.split('.');
ext = ext[ext.length - 1];
worker.json([reader.result, file.name.slice(0, (0 - (ext.length + 1)))], [reader.result]);
}
};
reader.readAsArrayBuffer(file);
}
function makeDiv() {
var div = L.DomUtil.create('form', 'bgroup');
div.id = "dropzone";
return div;
}
function makeUp(div, handleFile) {
var upButton = L.DomUtil.create('input', 'upStuff', div);
upButton.type = "file";
upButton.id = "input";
upButton.onchange = function() {
var file = document.getElementById("input").files[0];
handleFile(file);
};
return upButton;
}
function setWorkerEvents() {
worker.on('json', function(e) {
m.spin(false);
lc.addOverlay(L.geoJson(e[0], options).addTo(m), e[1]);
});
worker.on('error', function(e) {
console.warn(e);
});
}
function makeDone(div, upButton) {
var doneButton = L.DomUtil.create('button', "btn btn-primary span3", div);
doneButton.type = "button";
doneButton.innerHTML = "Upload File<br />(or Drag and Drop Anywhere)<br />GeoJSON, TopoJSON, or Zipped Shapefile Work";
L.DomEvent.addListener(doneButton, "click", function() {
upButton.click();
});
return doneButton;
}
function addFunction(map) {
// create the control container with a particular class name
var div = makeDiv();
var upButton = makeUp(div, handleFile);
setWorkerEvents()
var doneButton = makeDone(div, upButton);
var dropbox = document.getElementById("map");
dropbox.addEventListener("dragenter", dragenter, false);
dropbox.addEventListener("dragover", dragover, false);
dropbox.addEventListener("drop", drop, false);
dropbox.addEventListener("dragleave", function() {
m.scrollWheelZoom.enable();
}, false);
function dragenter(e) {
e.stopPropagation();
e.preventDefault();
m.scrollWheelZoom.disable();
}
function dragover(e) {
e.stopPropagation();
e.preventDefault();
}
function drop(e) {
e.stopPropagation();
e.preventDefault();
m.scrollWheelZoom.enable();
var dt = e.dataTransfer;
var files = dt.files;
var i = 0;
var len = files.length;
if (!len) {
return
}
while (i < len) {
handleFile(files[i]);
i++;
}
}
return div;
}
var NewButton = L.Control.extend({ //creating the buttons
options: {
position: 'topleft'
},
onAdd: addFunction
});
//add them to the map
m.addControl(new NewButton());
});