-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathgeojson.js
382 lines (334 loc) · 11.5 KB
/
geojson.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
(function(GeoJSON) {
GeoJSON.version = '0.4.1';
// Allow user to specify default parameters
GeoJSON.defaults = {
doThrows: {
invalidGeometry: false
},
removeInvalidGeometries: false
};
function InvalidGeometryError() {
var args = 1 <= arguments.length ? [].slice.call(arguments, 0) : [];
var item = args.shift();
var params = args.shift();
Error.apply(this, args);
this.message = this.message || "Invalid Geometry: " + 'item: ' + JSON.stringify(item) + ', params: ' + JSON.stringify(params);
}
InvalidGeometryError.prototype = Error;
GeoJSON.errors = {
InvalidGeometryError: InvalidGeometryError
};
//exposing so this can be overriden maybe by geojson-validation or the like
GeoJSON.isGeometryValid = function(geometry){
if(!geometry || !Object.keys(geometry).length)
return false;
return !!geometry.type && !!geometry.coordinates && Array.isArray(geometry.coordinates) && !!geometry.coordinates.length;
};
// The one and only public function.
// Converts an array of objects into a GeoJSON feature collection
GeoJSON.parse = function(objects, params, callback) {
var geojson,
settings = applyDefaults(params, this.defaults),
propFunc;
geomAttrs.length = 0; // Reset the list of geometry fields
setGeom(settings);
propFunc = getPropFunction(settings);
if (Array.isArray(objects)) {
geojson = {"type": "FeatureCollection", "features": []};
objects.forEach(function(item){
var feature = getFeature({item:item, params: settings, propFunc:propFunc});
if (settings.removeInvalidGeometries !== true || GeoJSON.isGeometryValid(feature.geometry)) {
geojson.features.push(feature);
}
});
addOptionals(geojson, settings);
} else {
geojson = getFeature({item:objects, params: settings, propFunc:propFunc});
addOptionals(geojson, settings);
}
if (callback && typeof callback === 'function') {
callback(geojson);
} else {
return geojson;
}
};
// Helper functions
var geoms = ['Point', 'MultiPoint', 'LineString', 'MultiLineString', 'Polygon', 'MultiPolygon', 'GeoJSON'],
geomAttrs = [];
// Adds default settings to user-specified params
// Does not overwrite any settings--only adds defaults
// the the user did not specify
function applyDefaults(params, defaults) {
var settings = params || {};
for(var setting in defaults) {
if(defaults.hasOwnProperty(setting) && !settings[setting]) {
settings[setting] = defaults[setting];
}
}
return settings;
}
// Adds the optional GeoJSON properties crs and bbox
// if they have been specified
function addOptionals(geojson, settings){
if(settings.crs && checkCRS(settings.crs)) {
if(settings.isPostgres)
geojson.geometry.crs = settings.crs;
else
geojson.crs = settings.crs;
}
if (settings.bbox) {
geojson.bbox = settings.bbox;
}
if (settings.extraGlobal) {
geojson.properties = {};
for (var key in settings.extraGlobal) {
geojson.properties[key] = settings.extraGlobal[key];
}
}
}
// Verify that the structure of CRS object is valid
function checkCRS(crs) {
if (crs.type === 'name') {
if (crs.properties && crs.properties.name) {
return true;
} else {
throw new Error('Invalid CRS. Properties must contain "name" key');
}
} else if (crs.type === 'link') {
if (crs.properties && crs.properties.href && crs.properties.type) {
return true;
} else {
throw new Error('Invalid CRS. Properties must contain "href" and "type" key');
}
} else {
throw new Error('Invald CRS. Type attribute must be "name" or "link"');
}
}
// Moves the user-specified geometry parameters
// under the `geom` key in param for easier access
function setGeom(params) {
params.geom = {};
for(var param in params) {
if(params.hasOwnProperty(param) && geoms.indexOf(param) !== -1){
params.geom[param] = params[param];
delete params[param];
}
}
setGeomAttrList(params.geom);
}
// Adds fields which contain geometry data
// to geomAttrs. This list is used when adding
// properties to the features so that no geometry
// fields are added the properties key
function setGeomAttrList(params) {
for(var param in params) {
if(params.hasOwnProperty(param)) {
if(typeof params[param] === 'string') {
geomAttrs.push(params[param]);
} else if (typeof params[param] === 'object') { // Array of coordinates for Point
geomAttrs.push(params[param][0]);
geomAttrs.push(params[param][1]);
}
}
}
if(geomAttrs.length === 0) { throw new Error('No geometry attributes specified'); }
}
// Creates a feature object to be added
// to the GeoJSON features array
function getFeature(args) {
var item = args.item,
params = args.params,
propFunc = args.propFunc;
var feature = { "type": "Feature" };
feature.geometry = buildGeom(item, params);
feature.properties = propFunc.call(item);
return feature;
}
function isNested(val){
return (/^.+\..+$/.test(val));
}
// Assembles the `geometry` property
// for the feature output
function buildGeom(item, params) {
var geom,
attr;
for(var gtype in params.geom) {
var val = params.geom[gtype];
var coordinates = [];
var itemClone;
var paths;
// If we've already found a matching geometry, stop the loop.
if (geom !== undefined && geom !== false) {
break;
}
// Geometry parameter specified as: {Point: 'coords'}
if(typeof val === 'string' && item.hasOwnProperty(val)) {
if(gtype === 'GeoJSON') {
geom = item[val];
} else {
geom = {
type: gtype,
coordinates: item[val]
};
}
}
// Geometry parameter specified as: {Point: 'geo.coords'}
else if(typeof val === 'string' && isNested(val)) {
geom = undefined;
paths = val.split('.');
itemClone = item;
for (var m = 0; m < paths.length; m++) {
if (itemClone == undefined || !itemClone.hasOwnProperty(paths[m])) {
m = paths.length;
geom = false;
} else {
itemClone = itemClone[paths[m]]; // Iterate deeper into the object
}
}
if (geom !== false) {
geom = {
type: gtype,
coordinates: itemClone
};
}
}
/* Handle things like:
Polygon: {
northeast: ['lat', 'lng'],
southwest: ['lat', 'lng']
}
*/
else if(typeof val === 'object' && !Array.isArray(val)) {
/*jshint loopfunc: true */
var points = Object.keys(val).map(function(key){
var order = val[key];
var newItem = item[key];
return buildGeom(newItem, {geom:{ Point: order}});
});
geom = {
type: gtype,
/*jshint loopfunc: true */
coordinates: [].concat(points.map(function(p){
return p.coordinates;
}))
};
}
// Geometry parameter specified as: {Point: ['lat', 'lng', 'alt']}
else if(Array.isArray(val) && item.hasOwnProperty(val[0]) && item.hasOwnProperty(val[1]) && item.hasOwnProperty(val[2])){
geom = {
type: gtype,
coordinates: [ Number(item[val[1]]), Number(item[val[0]]), Number(item[val[2]]) ]
};
}
// Geometry parameter specified as: {Point: ['lat', 'lng']}
else if(Array.isArray(val) && item.hasOwnProperty(val[0]) && item.hasOwnProperty(val[1])){
geom = {
type: gtype,
coordinates: [Number(item[val[1]]), Number(item[val[0]])]
};
}
// Geometry parameter specified as: {Point: ['container.lat', 'container.lng', 'container.alt']}
else if(Array.isArray(val) && isNested(val[0]) && isNested(val[1]) && isNested(val[2])){
geom = undefined;
for (var i = 0; i < val.length; i++) { // i.e. 0 and 1
paths = val[i].split('.');
itemClone = item;
for (var j = 0; j < paths.length; j++) {
if (itemClone == undefined || !itemClone.hasOwnProperty(paths[j])) {
i = val.length;
j = paths.length;
geom = false;
} else {
itemClone = itemClone[paths[j]]; // Iterate deeper into the object
}
}
coordinates[i] = itemClone;
}
if (geom !== false) {
geom = {
type: gtype,
coordinates: [Number(coordinates[1]), Number(coordinates[0]), Number(coordinates[2])]
};
}
}
// Geometry parameter specified as: {Point: ['container.lat', 'container.lng']}
else if(Array.isArray(val) && isNested(val[0]) && isNested(val[1])){
for (var k = 0; k < val.length; k++) { // i.e. 0 and 1
paths = val[k].split('.');
itemClone = item;
for (var l = 0; l < paths.length; l++) {
if (itemClone == undefined || !itemClone.hasOwnProperty(paths[l])) {
k = val.length;
l = paths.length;
geom = false;
} else {
itemClone = itemClone[paths[l]]; // Iterate deeper into the object
}
}
coordinates[k] = itemClone;
}
if (geom !== false) {
geom = {
type: gtype,
coordinates: [Number(coordinates[1]), Number(coordinates[0])]
};
}
}
// Geometry parameter specified as: {Point: [{coordinates: [lat, lng]}]}
else if (Array.isArray(val) && val[0].constructor.name === 'Object' && Object.keys(val[0])[0] === 'coordinates'){
geom = {
type: gtype,
coordinates: [Number(item.coordinates[(val[0].coordinates).indexOf('lng')]), Number(item.coordinates[(val[0].coordinates).indexOf('lat')])]
};
}
}
if(params.doThrows && params.doThrows.invalidGeometry && !GeoJSON.isGeometryValid(geom)){
throw new InvalidGeometryError(item, params);
}
return geom;
}
// Returns the function to be used to
// build the properties object for each feature
function getPropFunction(params) {
var func;
if(!params.exclude && !params.include) {
func = function(properties) {
for(var attr in this) {
if(this.hasOwnProperty(attr) && (geomAttrs.indexOf(attr) === -1)) {
properties[attr] = this[attr];
}
}
};
} else if(params.include) {
func = function(properties) {
params.include.forEach(function(attr){
properties[attr] = this[attr];
}, this);
};
} else if(params.exclude) {
func = function(properties) {
for(var attr in this) {
if(this.hasOwnProperty(attr) && (geomAttrs.indexOf(attr) === -1) && (params.exclude.indexOf(attr) === -1)) {
properties[attr] = this[attr];
}
}
};
}
return function() {
var properties = {};
func.call(this, properties);
if(params.extra) { addExtra(properties, params.extra); }
return properties;
};
}
// Adds data contained in the `extra`
// parameter if it has been specified
function addExtra(properties, extra) {
for(var key in extra){
if(extra.hasOwnProperty(key)) {
properties[key] = extra[key];
}
}
return properties;
}
}(typeof module == 'object' ? module.exports : window.GeoJSON = {}));