/
geojson.js
41 lines (35 loc) · 1.1 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
const getPointAtLength = require('point-at-length');
const cloneDeep = require('@antv/util/lib/clone');
const {
geoPath
} = require('d3-geo');
const {
GEO,
registerConnector
} = require('../data-set');
const geoPathGenerator = geoPath();
function GeoJSONConnector(data, options, dataView) {
dataView.dataType = GEO;
const features = cloneDeep(data.features);
// pre-process
features.forEach(feature => {
feature.name = feature.properties.name;
feature.longitude = [];
feature.latitude = [];
const pathData = feature.pathData = geoPathGenerator(feature);
const points = getPointAtLength(pathData);
points._path.forEach(point => {
feature.longitude.push(point[1]);
feature.latitude.push(point[2]);
});
const centroid = geoPathGenerator.centroid(feature);
feature.centroidX = centroid[0];
feature.centroidY = centroid[1];
});
// dataView.origin = features;
return features;
}
registerConnector('geo', GeoJSONConnector);
registerConnector('geojson', GeoJSONConnector);
registerConnector('GeoJSON', GeoJSONConnector);
module.exports = GeoJSONConnector;