forked from caseycesari/GeoJSON.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geojson.js
194 lines (161 loc) · 5.26 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
(function(GeoJSON) {
GeoJSON.version = '0.2.0';
// Allow user to specify default parameters
GeoJSON.defaults = {};
// The one and only public function.
// Converts an array of objects into a GeoJSON feature collection
GeoJSON.parse = function(objects, params, callback) {
var geojson = {"type": "FeatureCollection", "features": []},
settings = applyDefaults(params, this.defaults),
propFunc;
geomAttrs.length = 0; // Reset the list of geometry fields
setGeom(settings);
propFunc = getPropFunction(settings);
objects.forEach(function(item){
geojson.features.push(getFeature(item, settings, propFunc));
});
addOptionals(geojson, settings);
if (callback && typeof callback === 'function') {
callback(geojson);
} else {
return geojson;
}
};
// Helper functions
var geoms = ['Point', 'MultiPoint', 'LineString', 'MultiLineString', 'Polygon', 'MultiPolygon'],
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) {
geojson.crs = {
type: "name",
properties: {
name: 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];
}
}
}
// 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(item, params, propFunc) {
var feature = { "type": "Feature" };
feature.geometry = buildGeom(item, params);
feature.properties = propFunc.call(item);
return feature;
}
// 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];
// Geometry parameter specified as: {Point: 'coords'}
if(typeof val === 'string' && item.hasOwnProperty(val)) {
geom.type = gtype;
geom.coordinates = item[val];
}
// Geometry parameter specified as: {Point: ['lat', 'lng']}
else if(Array.isArray(val) && item.hasOwnProperty(val[0]) && item.hasOwnProperty(val[1])){
geom.type = gtype;
geom.coordinates = [item[val[1]], item[val[0]]];
}
}
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 = {}));