feat(source): hexagon

This commit is contained in:
thinkinggis 2019-02-26 16:06:43 +08:00
parent 9af7f61305
commit 3ab8aac11a
8 changed files with 1641 additions and 18 deletions

View File

@ -101,6 +101,7 @@
"@turf/invariant": "^6.1.2",
"@turf/meta": "^6.0.2",
"d3-dsv": "^1.0.10",
"d3-hexbin": "^0.2.2",
"earcut": "^2.1.3",
"fecha": "^2.3.3",
"gl-matrix": "^2.4.1",

File diff suppressed because one or more lines are too long

View File

@ -13,5 +13,20 @@ export function aProjectFlat(lnglat) {
d = 0.5;
x = scale * (a * x + b) - 215440491;
y = scale * (c * y + d) - 106744817;
return { x, y };
return { x: parseInt(x), y: parseInt(y) };
}
export function unProjectFlat(px) {
const a = 0.5 / Math.PI,
b = 0.5,
c = -0.5 / Math.PI;
let d = 0.5;
const scale = 256 << 20;
let [ x, y ] = px;
x = ((x + 215440491) / scale - b) / a;
y = ((y + 106744817) / scale - d) / c;
y = (Math.atan(Math.pow(Math.E, y)) - (Math.PI / 4)) * 2;
d = Math.PI / 180;
const lat = y / d;
const lng = x / d;
return [ lng, lat ];
}

View File

@ -7,7 +7,7 @@ import json from './parser/json';
import raster from './parser/raster';
import { registerTransform, registerParser } from './factory';
import { aggregatorToGrid } from './transform/grid-aggregator';
import { aggregatorToGrid } from './transform/grid';
import { map } from './transform/map';
registerParser('geojson', geojson);

View File

@ -0,0 +1,41 @@
import { hexbin } from 'd3-hexbin';
import { aProjectFlat, unProjectFlat } from '../../geo/project';
import * as statistics from './statistics';
const R_EARTH = 6378000;
export function pointToHexbin(data, option) {
const dataArray = data.dataArray;
const { size = 10 } = option;
const pixlSize = size / (2 * Math.PI * R_EARTH) * (256 << 20) / 2;
const screenPoints = dataArray.map(point => {
const { x, y } = aProjectFlat(point.coordinates);
return {
...point,
coordinates: [ x, y ]
};
});
const newHexbin = hexbin()
.radius(pixlSize)
.x(d => d.coordinates[0])
.y(d => d.coordinates[1]);
const hexbinBins = newHexbin(screenPoints);
const result = {
size: pixlSize
};
result.dataArray = hexbinBins.map((hex, index) => {
if (option.field && option.method) {
const columns = getColumn(hex, option.field);
hex[option.method] = statistics[option.method](columns);
}
return {
coordinates: unProjectFlat([ hex.x, hex.y ]),
id: index + 1
};
});
return result;
}
function getColumn(data, columnName) {
return data.map(item => {
return item[columnName];
});
}

1490
test/asset/data/point.js Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,21 @@
import { expect } from 'chai';
import { pointData } from '../../../asset/data/point';
import { pointToHexbin } from '../../../../src/source/transform/hexagon';
describe('hexagon Test', function() {
it('pointToHexbin', function() {
const dataArray = pointData.map(item => {
const lng = 1e-6 * (250 * item.grid_x + 125),
lat = 1e-6 * (250 * item.grid_y + 125);
return {
v: item.count * 1,
coordinates: [ lng, lat ]
};
});
const data = {
dataArray
};
const hexgonGrid = pointToHexbin(data, { size: 100, field: 'count', method: 'sum' });
});
});