-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathgeojson_utils.js
135 lines (114 loc) · 2.65 KB
/
geojson_utils.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
/**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var BADNUM = require('../constants/numerical').BADNUM;
/**
* Convert calcTrace to GeoJSON 'MultiLineString' coordinate arrays
*
* @param {object} calcTrace
* gd.calcdata item.
* Note that calcTrace[i].lonlat is assumed to be defined
*
* @return {array}
* return line coords array (or array of arrays)
*
*/
exports.calcTraceToLineCoords = function(calcTrace) {
var trace = calcTrace[0].trace;
var connectgaps = trace.connectgaps;
var coords = [];
var lineString = [];
for(var i = 0; i < calcTrace.length; i++) {
var calcPt = calcTrace[i];
var lonlat = calcPt.lonlat;
if(lonlat[0] !== BADNUM) {
lineString.push(lonlat);
} else if(!connectgaps && lineString.length > 0) {
coords.push(lineString);
lineString = [];
}
}
if(lineString.length > 0) {
coords.push(lineString);
}
return coords;
};
/**
* Make line ('LineString' or 'MultiLineString') GeoJSON
*
* @param {array} coords
* results form calcTraceToLineCoords
* @param {object} trace
* (optional) full trace object to be added on to output
*
* @return {object} out
* GeoJSON object
*
*/
exports.makeLine = function(coords, trace) {
var out = {};
if(coords.length === 1) {
out = {
type: 'LineString',
coordinates: coords[0]
};
}
else {
out = {
type: 'MultiLineString',
coordinates: coords
};
}
if(trace) out.trace = trace;
return out;
};
/**
* Make polygon ('Polygon' or 'MultiPolygon') GeoJSON
*
* @param {array} coords
* results form calcTraceToLineCoords
* @param {object} trace
* (optional) full trace object to be added on to output
*
* @return {object} out
* GeoJSON object
*/
exports.makePolygon = function(coords, trace) {
var out = {};
if(coords.length === 1) {
out = {
type: 'Polygon',
coordinates: coords
};
}
else {
var _coords = new Array(coords.length);
for(var i = 0; i < coords.length; i++) {
_coords[i] = [coords[i]];
}
out = {
type: 'MultiPolygon',
coordinates: _coords
};
}
if(trace) out.trace = trace;
return out;
};
/**
* Make blank GeoJSON
*
* @return {object}
* Blank GeoJSON object
*
*/
exports.makeBlank = function() {
return {
type: 'Point',
coordinates: []
};
};