forked from mapbox/leaflet-omnivore
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
248 lines (222 loc) · 5.29 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import * as maptalks from 'maptalks';
import csv2geojson from 'csv2geojson';
import wellknown from 'wellknown';
import polyline from '@mapbox/polyline';
import { feature as topoFeature } from 'topojson';
import toGeoJSON from '@mapbox/togeojson';
import { osm2geojson } from './src/osm2geojson';
const formats = {
geojson: geojsonLoad,
topojson: topojsonLoad,
csv: csvLoad,
gpx: gpxLoad,
kml: kmlLoad,
wkt: wktLoad,
polyline: polylineLoad,
osm: osmLoad
};
export { formats as Formats };
function geojsonLoad(url, cb) {
maptalks.Ajax.get(url, (err, response) => {
if (err) {
cb(err);
return;
}
const json = JSON.parse(response);
cb(null, json);
});
return this;
}
/**
* Load a [TopoJSON](https://github.com/mbostock/topojson) document.
*
* @param {string} url
* @param {function} callback
* @returns this
*/
function topojsonLoad(url, cb) {
maptalks.Ajax.get(url, (err, response) => {
if (err) {
cb(err);
return;
}
const json = topojsonParse(response);
cb(null, json);
});
return this;
}
topojsonLoad.parse = topojsonParse;
/**
* Load a CSV document into a layer and return the layer.
*
* @param {string} url
* @param {object} options
* @param {function} callback
* @returns this
*/
function csvLoad(url, options, cb) {
maptalks.Ajax.get(url, (err, response) => {
if (err) {
cb(err);
return;
}
if (maptalks.Util.isFunction(options)) {
cb = options;
options = {};
}
csvParse(response, options, (err, geojson) => {
if (err) {
cb(err);
return;
}
cb(null, geojson);
});
});
return this;
}
csvLoad.parse = csvParse;
/**
* Load a GPX document.
*
* @param {string} url
* @param {function} callback
* @returns this
*/
function gpxLoad(url, cb) {
maptalks.Ajax.get(url, (err, response) => {
if (err) {
cb(err);
return;
}
const geojson = gpxParse(response);
cb(null, geojson);
});
return this;
}
gpxLoad.parse = gpxParse;
/**
* Load a [KML](https://developers.google.com/kml/documentation/) document.
*
* @param {string} url
* @param {function} callback
* @returns this
*/
function kmlLoad(url, cb) {
maptalks.Ajax.get(url, (err, response) => {
if (err) {
cb(err);
return;
}
const geojson = kmlParse(response);
cb(null, geojson);
});
return this;
}
kmlLoad.parse = kmlParse;
/**
* Load a WKT (Well Known Text) string
*
* @param {string} url
* @param {object} customLayer
* @returns {object}
*/
function wktLoad(url, cb) {
maptalks.Ajax.get(url, (err, response) => {
if (err) {
cb(err);
return;
}
const geojson = wktParse(response);
cb(null, geojson);
});
return this;
}
wktLoad.parse = wktParse;
function osmLoad(url, cb) {
maptalks.Ajax.get(url, (err, response) => {
if (err) {
cb(err);
return;
}
const geojson = osm2geojson(response);
cb(null, geojson);
});
return this;
}
/**
* Load a polyline string into a layer and return the layer
*
* @param {string} url
* @param {object} options
* @param {function} callback
* @returns {object}
*/
function polylineLoad(url, options, cb) {
maptalks.Ajax.get(url, (err, response) => {
if (err) {
cb(err);
return;
}
if (maptalks.Util.isFunction(options)) {
cb = options;
options = {};
}
const geojson = polylineParse(response, options);
cb(null, geojson);
});
return this;
}
polylineLoad.parse = polylineParse;
function topojsonParse(data) {
const json = [];
const o = typeof data === 'string' ?
JSON.parse(data) : data;
for (const i in o.objects) {
const ft = topoFeature(o, o.objects[i]);
if (ft.features) {
maptalks.Util.pushIn(json, ft.features);
} else {
json.push(ft);
}
}
return json;
}
function csvParse(csv, options, cb) {
csv2geojson.csv2geojson(csv, options || {}, (err, geojson) => cb(err, geojson));
}
function gpxParse(gpx) {
const xml = parseXML(gpx);
if (!xml) {
throw new Error('Could not parse gpx');
}
const geojson = toGeoJSON.gpx(xml);
return geojson;
}
function kmlParse(kml) {
const xml = parseXML(kml);
if (!xml) {
throw new Error('Could not parse KML');
}
const geojson = toGeoJSON.kml(xml);
return geojson;
}
function wktParse(wkt) {
return wellknown(wkt);
}
function polylineParse(txt, options) {
options = options || {};
const coords = polyline.decode(txt, options.precision);
const geojson = { type: 'LineString', coordinates: [] };
for (let i = 0; i < coords.length; i++) {
// polyline returns coords in lat, lng order, so flip for geojson
geojson.coordinates[i] = [coords[i][1], coords[i][0]];
}
return geojson;
}
function parseXML(str) {
if (typeof str === 'string') {
return (new DOMParser()).parseFromString(str, 'text/xml');
} else {
return str;
}
}