Skip to content

Commit 4448eaa

Browse files
committed
util: move GeoJSON coords / type logic in own module
- to be reuse in 'scattergeo'
1 parent d0e1104 commit 4448eaa

File tree

2 files changed

+136
-60
lines changed

2 files changed

+136
-60
lines changed

src/lib/geojson_utils.js

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/**
2+
* Copyright 2012-2016, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
10+
'use strict';
11+
12+
/**
13+
* Convert calcTrace to GeoJSON 'MultiLineString' coordinate arrays
14+
*
15+
* @param {object} calcTrace
16+
* gd.calcdata item.
17+
* Note that calcTrace[i].lonlat is assumed to be defined
18+
*
19+
* @return {array}
20+
* return line coords array (or array of arrays)
21+
*
22+
*/
23+
exports.calcTraceToLineCoords = function(calcTrace) {
24+
var trace = calcTrace[0].trace,
25+
connectgaps = trace.connectgaps;
26+
27+
var coords = [],
28+
lineString = [];
29+
30+
for(var i = 0; i < calcTrace.length; i++) {
31+
var calcPt = calcTrace[i];
32+
33+
lineString.push(calcPt.lonlat);
34+
35+
if(!connectgaps && calcPt.gapAfter && lineString.length > 0) {
36+
coords.push(lineString);
37+
lineString = [];
38+
}
39+
}
40+
41+
coords.push(lineString);
42+
43+
return coords;
44+
};
45+
46+
47+
/**
48+
* Make line ('LineString' or 'MultiLineString') GeoJSON
49+
*
50+
* @param {array} coords
51+
* results form calcTraceToLineCoords
52+
* @param {object} trace
53+
* (optional) full trace object to be added on to output
54+
*
55+
* @return {object} out
56+
* GeoJSON object
57+
*
58+
*/
59+
exports.makeLine = function(coords, trace) {
60+
var out = {};
61+
62+
if(coords.length === 1) {
63+
out = {
64+
type: 'LineString',
65+
coordinates: coords[0]
66+
};
67+
}
68+
else {
69+
out = {
70+
type: 'MultiLineString',
71+
coordinates: coords
72+
};
73+
}
74+
75+
if(trace) out.trace = trace;
76+
77+
return out;
78+
};
79+
80+
/**
81+
* Make polygon ('Polygon' or 'MultiPolygon') GeoJSON
82+
*
83+
* @param {array} coords
84+
* results form calcTraceToLineCoords
85+
* @param {object} trace
86+
* (optional) full trace object to be added on to output
87+
*
88+
* @return {object} out
89+
* GeoJSON object
90+
*/
91+
exports.makePolygon = function(coords, trace) {
92+
var out = {};
93+
94+
if(coords.length === 1) {
95+
out = {
96+
type: 'Polygon',
97+
coordinates: coords
98+
};
99+
}
100+
else {
101+
var _coords = new Array(coords.length);
102+
103+
for(var i = 0; i < coords.length; i++) {
104+
_coords[i] = [coords[i]];
105+
}
106+
107+
out = {
108+
type: 'MultiPolygon',
109+
coordinates: _coords
110+
};
111+
}
112+
113+
if(trace) out.trace = trace;
114+
115+
return out;
116+
};
117+
118+
/**
119+
* Make blank GeoJSON
120+
*
121+
* @return {object}
122+
* Blank GeoJSON object
123+
*
124+
*/
125+
exports.makeBlank = function() {
126+
return {
127+
type: 'Point',
128+
coordinates: []
129+
};
130+
};

src/traces/scattermapbox/convert.js

Lines changed: 6 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
'use strict';
1111

1212
var Lib = require('../../lib');
13+
var geoJsonUtils = require('../../lib/geojson_utils');
14+
1315
var subTypes = require('../scatter/subtypes');
1416
var convertTextOpts = require('../../plots/mapbox/convert_text_opts');
1517

@@ -46,11 +48,11 @@ module.exports = function convert(calcTrace) {
4648
// fill layer and line layer use the same coords
4749
var coords;
4850
if(hasFill || hasLines) {
49-
coords = getCoords(calcTrace);
51+
coords = geoJsonUtils.calcTraceToLineCoords(calcTrace);
5052
}
5153

5254
if(hasFill) {
53-
fill.geojson = makeFillGeoJSON(calcTrace, coords);
55+
fill.geojson = geoJsonUtils.makePolygon(coords);
5456
fill.layout.visibility = 'visible';
5557

5658
Lib.extendFlat(fill.paint, {
@@ -59,7 +61,7 @@ module.exports = function convert(calcTrace) {
5961
}
6062

6163
if(hasLines) {
62-
line.geojson = makeLineGeoJSON(calcTrace, coords);
64+
line.geojson = geoJsonUtils.makeLine(coords);
6365
line.layout.visibility = 'visible';
6466

6567
Lib.extendFlat(line.paint, {
@@ -133,45 +135,12 @@ module.exports = function convert(calcTrace) {
133135

134136
function initContainer() {
135137
return {
136-
geojson: makeBlankGeoJSON(),
138+
geojson: geoJsonUtils.makeBlank(),
137139
layout: { visibility: 'none' },
138140
paint: {}
139141
};
140142
}
141143

142-
function makeBlankGeoJSON() {
143-
return {
144-
type: 'Point',
145-
coordinates: []
146-
};
147-
}
148-
149-
function makeFillGeoJSON(_, coords) {
150-
if(coords.length === 1) {
151-
return {
152-
type: 'Polygon',
153-
coordinates: coords
154-
};
155-
}
156-
157-
var _coords = new Array(coords.length);
158-
for(var i = 0; i < coords.length; i++) {
159-
_coords[i] = [coords[i]];
160-
}
161-
162-
return {
163-
type: 'MultiPolygon',
164-
coordinates: _coords
165-
};
166-
}
167-
168-
function makeLineGeoJSON(_, coords) {
169-
return {
170-
type: 'MultiLineString',
171-
coordinates: coords
172-
};
173-
}
174-
175144
// N.B. `hash` is mutated here
176145
//
177146
// The `hash` object contains mapping between values
@@ -323,29 +292,6 @@ function calcCircleRadius(trace, hash) {
323292
return out;
324293
}
325294

326-
function getCoords(calcTrace) {
327-
var trace = calcTrace[0].trace,
328-
connectgaps = trace.connectgaps;
329-
330-
var coords = [],
331-
lineString = [];
332-
333-
for(var i = 0; i < calcTrace.length; i++) {
334-
var calcPt = calcTrace[i];
335-
336-
lineString.push(calcPt.lonlat);
337-
338-
if(!connectgaps && calcPt.gapAfter && lineString.length > 0) {
339-
coords.push(lineString);
340-
lineString = [];
341-
}
342-
}
343-
344-
coords.push(lineString);
345-
346-
return coords;
347-
}
348-
349295
function getFillFunc(attr) {
350296
if(Array.isArray(attr)) {
351297
return function(v) { return v; };

0 commit comments

Comments
 (0)