From 0b099cac5e72241b97214639fa849bef112f6fd5 Mon Sep 17 00:00:00 2001 From: thinkinggis Date: Wed, 10 Jul 2019 11:18:40 +0800 Subject: [PATCH 1/2] fix(poylygon): json source --- demos/polygon_jsondata.html | 73 +++++++++++ src/core/controller/tile_mapping.js | 183 ++++++++++++++++++++++++++++ src/geom/buffer/factory.js | 12 ++ src/source/parser/json.js | 15 ++- 4 files changed, 278 insertions(+), 5 deletions(-) create mode 100644 demos/polygon_jsondata.html create mode 100644 src/core/controller/tile_mapping.js create mode 100644 src/geom/buffer/factory.js diff --git a/demos/polygon_jsondata.html b/demos/polygon_jsondata.html new file mode 100644 index 0000000000..14343e60e4 --- /dev/null +++ b/demos/polygon_jsondata.html @@ -0,0 +1,73 @@ + + + + + + + + + + extrude Polygon + + + + +
+
+ + + + + + + + diff --git a/src/core/controller/tile_mapping.js b/src/core/controller/tile_mapping.js new file mode 100644 index 0000000000..f8e70c1b3c --- /dev/null +++ b/src/core/controller/tile_mapping.js @@ -0,0 +1,183 @@ +import Util from '../../util'; +import Global from '../../global'; +import ScaleController from './scale'; +import Base from '../base'; +import Attr from '../../attr/index'; +export default class TileMapping extends Base { + constructor(source, cfg) { + super(cfg); + this.source = source; + this._init(); + } + _init() { + this._initControllers(); + this._initTileAttrs(); + this._mapping(); + } + update() { + this.set('scales', {}); + this._initTileAttrs(); + this._updateMaping(); + } + _initControllers() { + const scalesOption = this.get('scaleOptions'); + const scaleController = new ScaleController({ + defs: { + ...scalesOption + } + }); + this.set('scaleController', scaleController); + } + _createScale(field) { + const scales = this.get('scales'); + this._initControllers(); // scale更新 + let scale = scales[field]; + if (!scale) { + scale = this.createScale(field); + scales[field] = scale; + } + return scale; + } + createScale(field) { + const data = this.source.data.dataArray; + const scales = this.get('scales'); + let scale = scales[field]; + const scaleController = this.get('scaleController'); + if (!scale) { + scale = scaleController.createScale(field, data); + scales[field] = scale; + } + return scale; + } + // 获取属性映射的值 + _getAttrValues(attr, record) { + const scales = attr.scales; + const params = []; + for (let i = 0; i < scales.length; i++) { + const scale = scales[i]; + const field = scale.field; + if (scale.type === 'identity') { + params.push(scale.value); + } else { + params.push(record[field]); + } + } + const indexZoom = params.indexOf('zoom'); + indexZoom !== -1 ? params[indexZoom] = attr.zoom : null; + const values = attr.mapping(...params); + return values; + } + _mapping() { + const attrs = this.get('attrs'); + const mappedData = []; + const data = this.source.data.dataArray; + for (let i = 0; i < data.length; i++) { + const record = data[i]; + const newRecord = {}; + newRecord.id = data[i]._id; + for (const k in attrs) { + if (attrs.hasOwnProperty(k)) { + const attr = attrs[k]; + const names = attr.names; + const values = this._getAttrValues(attr, record); + if (names.length > 1) { // position 之类的生成多个字段的属性 + for (let j = 0; j < values.length; j++) { + const val = values[j]; + const name = names[j]; + newRecord[name] = (Util.isArray(val) && val.length === 1) ? val[0] : val; // 只有一个值时返回第一个属性值 + } + } else { + newRecord[names[0]] = values.length === 1 ? values[0] : values; + + } + } + } + newRecord.coordinates = record.coordinates; + mappedData.push(newRecord); + } + // 通过透明度过滤数据 + if (attrs.hasOwnProperty('filter')) { + mappedData.forEach(item => { + if (item.filter === false) { + (item.color[3] = 0); + item.id = -item.id; + } + }); + } + this.layerData = mappedData; + } + + /** + * 更新数据maping + * @param {*} layerSource 数据源 + * @param {*} layer map + */ + _updateMaping() { + const attrs = this.get('attrs'); + + const data = this.source.data.dataArray; + const layerData = this.layerData; + for (let i = 0; i < data.length; i++) { + const record = data[i]; + for (const attrName in attrs) { + if (attrs.hasOwnProperty(attrName) && attrs[attrName].neadUpdate) { + const attr = attrs[attrName]; + const names = attr.names; + const values = this._getAttrValues(attr, record); + if (names.length > 1) { // position 之类的生成多个字段的属性 + for (let j = 0; j < values.length; j++) { + const val = values[j]; + const name = names[j]; + layerData[i][name] = (Util.isArray(val) && val.length === 1) ? val[0] : val; // 只有一个值时返回第一个属性值 + } + } else { + layerData[i][names[0]] = values.length === 1 ? values[0] : values; + + } + attr.neadUpdate = true; + } + } + } + } + + + _initTileAttrs() { + const attrOptions = this.get('attrOptions'); + for (const type in attrOptions) { + if (attrOptions.hasOwnProperty(type)) { + this._updateTileAttr(type); + } + } + } + _updateTileAttr(type) { + const self = this; + const attrs = this.get('attrs'); + const attrOptions = this.get('attrOptions'); + const option = attrOptions[type]; + option.neadUpdate = true; + const className = Util.upperFirst(type); + const fields = this._parseFields(option.field); + const scales = []; + for (let i = 0; i < fields.length; i++) { + const field = fields[i]; + const scale = self._createScale(field); + + if (type === 'color' && Util.isNil(option.values)) { // 设置 color 的默认色值 + option.values = Global.colors; + } + scales.push(scale); + } + option.scales = scales; + const attr = new Attr[className](option); + attrs[type] = attr; + } + _parseFields(field) { + if (Util.isArray(field)) { + return field; + } + if (Util.isString(field)) { + return field.split('*'); + } + return [ field ]; + } +} diff --git a/src/geom/buffer/factory.js b/src/geom/buffer/factory.js new file mode 100644 index 0000000000..25c0afb52c --- /dev/null +++ b/src/geom/buffer/factory.js @@ -0,0 +1,12 @@ +export const Buffer_MAP = {}; +export const getBuffer = (bufferType, shapeType) => { + return Buffer_MAP[bufferType.toLowerCase()] && Buffer_MAP[bufferType.toLowerCase()][shapeType.toLowerCase()]; +}; +export const registerBuffer = (bufferType, shapeType, render) => { + if (getBuffer(bufferType, shapeType)) { + throw new Error(`Render shapeType '${shapeType}' existed.`); + } + // 存储到 map 中 + if (!Buffer_MAP[bufferType.toLowerCase()]) Buffer_MAP[bufferType.toLowerCase()] = {}; + Buffer_MAP[bufferType.toLowerCase()][shapeType.toLowerCase()] = render; +}; diff --git a/src/source/parser/json.js b/src/source/parser/json.js index 37128bebce..a325bf01ef 100644 --- a/src/source/parser/json.js +++ b/src/source/parser/json.js @@ -1,16 +1,21 @@ export default function json(data, cfg) { - const { x, y, x1, y1 } = cfg; + const { x, y, x1, y1, coordinates } = cfg; const resultdata = []; data.forEach((col, featureIndex) => { - let coordinates = []; - if (x && y) { coordinates = [ col[x], col[y] ]; } // 点数据 + let coords = []; + if (x && y) { coords = [ col[x], col[y] ]; } // 点数据 if (x1 && y1) { // 弧线 或者线段 - coordinates = [[ col[x], col[y] ], [ col[x1], col[y1] ]]; + coords = [[ col[x], col[y] ], [ col[x1], col[y1] ]]; } + if (coordinates) { + coords = col[coordinates]; + delete col[coordinates]; + } + col._id = featureIndex + 1; const dataItem = { ...col, - coordinates + coordinates: coords }; resultdata.push(dataItem); From 8e974864727132cc1a6bfe1e4105efe7c7e7ca9c Mon Sep 17 00:00:00 2001 From: thinkinggis Date: Wed, 10 Jul 2019 11:26:29 +0800 Subject: [PATCH 2/2] update version --- package.json | 4 +++- src/global.js | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 3ba6024873..06e47cf633 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@antv/l7", - "version": "1.2.0-beta.1", + "version": "1.2.0-beta.2", "description": "Large-scale WebGL-powered Geospatial Data Visualization", "main": "build/l7.js", "browser": "build/l7.js", @@ -98,6 +98,7 @@ "@antv/g": "^3.1.3", "@antv/geo-coord": "^1.0.8", "@antv/util": "~2.0.1", + "@mapbox/geojson-rewind": "^0.4.0", "@mapbox/tiny-sdf": "^1.1.0", "@mapbox/vector-tile": "^1.3.1", "@turf/bbox": "^6.0.1", @@ -108,6 +109,7 @@ "d3-hexbin": "^0.2.2", "earcut": "^2.1.3", "fecha": "^2.3.3", + "geojson-rewind": "^0.3.1", "gl-matrix": "^2.4.1", "gl-vec2": "^1.3.0", "lodash": "^4.17.5", diff --git a/src/global.js b/src/global.js index 136c0eb7e2..2256230e7c 100644 --- a/src/global.js +++ b/src/global.js @@ -5,7 +5,7 @@ // const Global = {}; const FONT_FAMILY = '"-apple-system", BlinkMacSystemFont, "Segoe UI", Roboto,"Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei",SimSun, "sans-serif"'; const Global = { - version: '1.2.0-beta.1', + version: '1.2.0-beta.2', scene: { mapType: 'AMAP', zoom: 5,