UNPKG

vislite

Version:

灵活、快速、简单的数据可视化交互式跨端前端库

214 lines (206 loc) 7.92 kB
var toString = Object.prototype.toString; function getType(value) { if (value == null) { return value === undefined ? '[object Undefined]' : '[object Null]'; } return toString.call(value); } function isPlainObject(value) { if (value === null || typeof value !== 'object' || getType(value) != '[object Object]') { return false; } if (Object.getPrototypeOf(value) === null) { return true; } var proto = value; while (Object.getPrototypeOf(proto) !== null) { proto = Object.getPrototypeOf(proto); } return Object.getPrototypeOf(value) === proto; } function initOption(setOption, defaultOption) { for (var key in setOption) { defaultOption[key] = setOption[key]; } return defaultOption; } function mergeOption(oldOption, newOption) { (function doit(oldOption, newOption) { for (var key in newOption) { var value = newOption[key]; if (isPlainObject(value)) { if (!oldOption[key]) oldOption[key] = {}; doit(oldOption[key], newOption[key]); } else { oldOption[key] = value; } } })(oldOption, newOption); } function mllData(geoJson) { var minLongitude = null, maxLongitude = null, minLatitude = null, maxLatitude = null; var forPolygon = function (coordinates) { for (var j = 0; j < coordinates.length; j++) { for (var k = 0; k < coordinates[j].length; k++) { var longitude = coordinates[j][k][0], latitude = coordinates[j][k][1]; if (minLongitude == void 0 || minLongitude > longitude) minLongitude = longitude; if (maxLongitude == void 0 || maxLongitude < longitude) maxLongitude = longitude; if (minLatitude == void 0 || minLatitude > latitude) minLatitude = latitude; if (maxLatitude == void 0 || maxLatitude < latitude) maxLatitude = latitude; } } }; var features = geoJson.features; for (var i = 0; i < features.length; i++) { if (features[i].geometry.type == "Polygon") { forPolygon(features[i].geometry.coordinates); } else if (features[i].geometry.type == "MultiPolygon") { for (var j = 0; j < features[i].geometry.coordinates.length; j++) { forPolygon(features[i].geometry.coordinates[j]); } } } return [minLongitude, maxLongitude, minLatitude, maxLatitude]; } function getBoundary () { var geos = []; for (var _i = 0; _i < arguments.length; _i++) { geos[_i] = arguments[_i]; } var minLongitude = null, maxLongitude = null, minLatitude = null, maxLatitude = null; for (var _a = 0, geos_1 = geos; _a < geos_1.length; _a++) { var geoJson = geos_1[_a]; var _b = mllData(geoJson), _minLongitude = _b[0], _maxLongitude = _b[1], _minLatitude = _b[2], _maxLatitude = _b[3]; if (minLongitude == void 0 || _minLongitude < minLongitude) minLongitude = _minLongitude; if (maxLongitude == void 0 || _maxLongitude > maxLongitude) maxLongitude = _maxLongitude; if (minLatitude == void 0 || _minLatitude < minLatitude) minLatitude = _minLatitude; if (maxLatitude == void 0 || _maxLatitude > maxLatitude) maxLatitude = _maxLatitude; } return [minLongitude, maxLongitude, minLatitude, maxLatitude]; } var Mercator = (function () { function Mercator(scale, center) { if (scale === void 0) { scale = 7; } if (center === void 0) { center = [107, 36]; } this.name = 'Mercator'; var perimeter = 100 * scale * Math.PI; var help = perimeter / 180; var cx = help * center[0]; var cy = -1 * help * center[1]; this.use = function (λ, φ) { return [ (help * λ - cx) * 0.8, -1 * help * φ - cy, 0 ]; }; } return Mercator; }()); var rotateX = function (deg, x, y, z) { var cos = Math.cos(deg), sin = Math.sin(deg); return [x, y * cos - z * sin, y * sin + z * cos]; }; var rotateY = function (deg, x, y, z) { var cos = Math.cos(deg), sin = Math.sin(deg); return [z * sin + x * cos, y, z * cos - x * sin]; }; var rotateZ = function (deg, x, y, z) { var cos = Math.cos(deg), sin = Math.sin(deg); return [x * cos - y * sin, x * sin + y * cos, z]; }; var Eoap = (function () { function Eoap(scale, center) { if (scale === void 0) { scale = 7; } if (center === void 0) { center = [107, 36]; } this.name = 'Eoap'; this.__scale = scale; this.__center = center; } Eoap.prototype.use = function (λ, φ) { var p = rotateY((360 - φ) / 180 * Math.PI, 100 * this.__scale, 0, 0); p = rotateZ(λ / 180 * Math.PI, p[0], p[1], p[2]); p = rotateZ((90 - this.__center[0]) / 180 * Math.PI, p[0], p[1], p[2]); p = rotateX((90 - this.__center[1]) / 180 * Math.PI, p[0], p[1], p[2]); return [ -p[0], p[1], p[2] ]; }; return Eoap; }()); var MapCoordinate = (function () { function MapCoordinate(config) { if (config === void 0) { config = {}; } this.name = 'MapCoordinate'; this.__config = initOption(config, { api: "Mercator", width: 400, height: 300, left: 0, top: 0 }); } MapCoordinate.prototype.$$updateMap = function () { var _a = this.__boundary, minLongitude = _a[0], maxLongitude = _a[1], minLatitude = _a[2], maxLatitude = _a[3]; if (this.__config.api == "Eoap") { var xScale = this.__config.width * 0.836 / (maxLongitude - minLongitude); var yScale = this.__config.height * 0.557 / (maxLatitude - minLatitude); this.__map = new Eoap(xScale < yScale ? xScale : yScale, [ (minLongitude + maxLongitude) * 0.5 * 0.9778, (minLatitude + maxLatitude) * 0.5 * 1.025 ]); } else if (this.__config.api == "Mercator") { var xScale = this.__config.width * 0.72 / (maxLongitude - minLongitude); var yScale = this.__config.height * 0.575 / (maxLatitude - minLatitude); this.__map = new Mercator(xScale < yScale ? xScale : yScale, [ (minLongitude + maxLongitude) * 0.5, (minLatitude + maxLatitude) * 0.5 ]); } else { throw new Error("Unexpected API types:" + this.__config.api); } }; MapCoordinate.prototype.setConfig = function (config) { mergeOption(this.__config, config); this.$$updateMap(); return this; }; MapCoordinate.prototype.setGeos = function () { var geos = []; for (var _i = 0; _i < arguments.length; _i++) { geos[_i] = arguments[_i]; } this.__boundary = getBoundary.apply(void 0, geos); this.$$updateMap(); return this; }; MapCoordinate.prototype.use = function (λ, φ) { if (this.__map) { var dxyz = this.__map.use(λ, φ); return [ this.__config.left + this.__config.width * 0.5 + dxyz[0], this.__config.top + this.__config.height * 0.5 + dxyz[1], ]; } else { throw new Error("Geographic data not set"); } }; return MapCoordinate; }()); export { MapCoordinate as default };