Skip to content

Commit

Permalink
feat(worker): worker Source
Browse files Browse the repository at this point in the history
  • Loading branch information
lzxue committed Jul 8, 2019
1 parent 53f7268 commit d7d66a2
Show file tree
Hide file tree
Showing 19 changed files with 824 additions and 113 deletions.
2 changes: 1 addition & 1 deletion demos/06_text.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
});
window.scene = scene;
scene.on('loaded', () => {
$.get('./data/provincePoint.json', data => {
$.get('https://gw.alipayobjects.com/os/basement_prod/abcfe339-b8bc-46ce-8ff4-c96185b6235f.json', data => {
scene.PointLayer({
zIndex: 2
})
Expand Down
2 changes: 1 addition & 1 deletion demos/08_arc_line.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
})
//.animate({enable:true})
.render();
console.log(layer);

/**
scene.LineLayer({
zIndex: 2
Expand Down
67 changes: 67 additions & 0 deletions demos/workerdemo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="geometry" content="diagram">
<link rel="stylesheet" href="./assets/common.css">
<link rel="stylesheet" href="./assets/info.css">

<title>hexagon demo</title>
<style>
body {margin: 0;}
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>

<body>
<div id="map"></div>
<script src="https://webapi.amap.com/maps?v=1.4.8&key=15cd8a57710d40c9b7c0e3cc120f1200&plugin=Map3D"></script>
<script src="./assets/jquery-3.2.1.min.js"></script>
<script src="./assets/dat.gui.min.js"></script>
<script src="../build/L7.js"></script>
<script>

const scene = new L7.Scene({
id: 'map',
mapStyle: 'light', // 样式URL
center: [104.838088,34.075889 ],
pitch: 0,
hash:true,
zoom: 3,

});

scene.on('loaded', () => {
scene.addTileSource('test',{
url:'http://alipay-rmsdeploy-image.cn-hangzhou.alipay.aliyun-inc.com/thinkgis/tile/county/{z}/{x}/{y}.pbf',
type:'mvt',
minZoom: 0,
maxZoom:9
})
const layer = scene.PolygonLayer({
zIndex:0,
})
.source('test',{
parser:{
type: 'mvt',
sourceLayer:'county',
idField:'id'
}
})
.shape('line')
.size(2)
.active(false)
.color('red')
.style({
opacity:1.0
})
.render();

});

</script>
</body>
</html>

22 changes: 18 additions & 4 deletions src/core/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export default class Layer extends Base {
this._object3D.renderOrder = this.get('zIndex') || 0;
this._mapEventHandlers = [];
const layerId = this._getUniqueId();
this.set('layerId', layerId);
this.layerId = layerId;
this._activeIds = null;
const world = scene._engine.world;
Expand Down Expand Up @@ -127,7 +128,19 @@ export default class Layer extends Base {
this.set('visible', visible);
this._object3D.visible = this.get('visible');
}
// 兼容瓦片source,非瓦片source

source(data, cfg = {}) {
// 根据Source类型判断,是不是瓦片图层
if (this.scene.getTileSource(data)) {
this.set('layerType', 'tile');
this.set('sourceOption', {
id: data,
...cfg
});
return this;
}

if (data instanceof source) {
this.layerSource = data;
return this;
Expand All @@ -136,9 +149,6 @@ export default class Layer extends Base {
cfg.mapType = this.scene.mapType;
cfg.zoom = this.scene.getZoom();
this.layerSource = new source(cfg);
// this.scene.workerPool.runTask(cfg).then(data => {
// console.log(data);
// });
return this;
}
color(field, values) {
Expand Down Expand Up @@ -281,7 +291,7 @@ export default class Layer extends Base {
options[attrName] = attrCfg;
}
_createAttrOption(attrName, field, cfg, defaultValues) {

const attrCfg = {};
attrCfg.field = field;
if (cfg) {
Expand All @@ -305,6 +315,10 @@ export default class Layer extends Base {
}

render() {
if (this.get('layerType') === 'tile') {
this.scene.style.update(this._attrs);
return this;
}
this.init();
this.scene._engine.update();
return this;
Expand Down
13 changes: 11 additions & 2 deletions src/core/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ import { LAYER_MAP } from '../layer';
import Base from './base';
import LoadImage from './image';
import FontAtlasManager from '../geom/buffer/point/text/font-manager';
import WorkerPool from '../worker/worker_pool';
// import { MapProvider } from '../map/AMap';
import { getMap } from '../map/index';
import Global from '../global';
import { getInteraction } from '../interaction/index';
import { compileBuiltinModules } from '../geom/shader';
import Style from './style';
import { epsg3857 } from '@antv/geo-coord/lib/geo/crs/crs-epsg3857';
export default class Scene extends Base {
getDefaultCfg() {
return Global.scene;
}
constructor(cfg) {
super(cfg);
this._initMap();
this.crs = epsg3857;
// this._initAttribution(); // 暂时取消,后面作为组件去加载
this.addImage();
this.fontAtlasManager = new FontAtlasManager();
Expand All @@ -27,7 +29,6 @@ export default class Scene extends Base {
this._engine = new Engine(mapContainer, this);
// this.registerMapEvent();
this._engine.run();
this.workerPool = new WorkerPool();
compileBuiltinModules();
}
// 为pickup场景添加 object 对象
Expand All @@ -54,6 +55,7 @@ export default class Scene extends Base {
const interaction = new Ctor({ layer: this });
interaction._onHashChange();
}
this.style = new Style(this, {});
this.emit('loaded');
});

Expand All @@ -68,6 +70,13 @@ export default class Scene extends Base {
}

}
// 添加 Tile Source
addTileSource(id, Sourcecfg) {
this.style.addSource(id, Sourcecfg);
}
getTileSource(id) {
return this.style.getSource(id);
}
on(type, hander) {
if (this.map) { this.map.on(type, hander); }
super.on(type, hander);
Expand Down
131 changes: 131 additions & 0 deletions src/core/style.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import Base from '../core/base';
import WorkerPool from '../worker/worker_pool';
import { toLngLat, Bounds } from '@antv/geo-coord';
import SourceCache from '../source/source_cache';
import WorkerController from '../worker/worker_controller';
// 统一管理所有的Source
// 统一管理地图样式
export default class Style extends Base {
constructor(scene, cfg) {
super(cfg);
this.scene = scene;
this._sourceCaches = {};
this.WorkerPool = new WorkerPool();
this._tileMap = {};
this.WorkerController = new WorkerController(this.WorkerPool, this);
this.layerStyles = {};
this.addMapEvent();
}
addSource(id, sourceCfg) {
if (this._sourceCaches[id] !== undefined) {
throw new Error('SourceID 已存在');
}
this._sourceCaches[id] = new SourceCache(this.scene, sourceCfg);
}
getSource(id) {
return this._sourceCaches[id];
}
// 设置
addTileStyle(layerCfg) {
const layerid = layerCfg.layerId;
this.layerStyles[layerid] = layerCfg;
this._layerStyleGroupBySourceID();
this.WorkerController.broadcast('setLayers', this.layerStyles);
// TODO 更新 style

}
removeTileStyle(id) {
delete this.layerStyles[id];
this._layerStyleGroupBySourceID();

}
_layerStyleGroupBySourceID() {
const sourceStyles = [];
// 支持VectorLayer
for (const layerId in this.layerStyles) {
const sourceID = this.layerStyles[layerId].sourceOption.id;
const sourcelayer = this.layerStyles[layerId].sourceOption.parser.sourceLayer;
if (!sourceStyles[sourceID]) sourceStyles[sourceID] = {};
if (!sourceStyles[sourceID][sourcelayer]) sourceStyles[sourceID][sourcelayer] = [];
sourceStyles[sourceID][sourcelayer].push(this.layerStyles[layerId]);
}
this.sourceStyles = sourceStyles;
}
update(parameters) {
this.addTileStyle(parameters);
for (const key in this._sourceCaches) {
this._sourceCaches[key].update(this.sourceStyles[key]);
}

}
addMapEvent() {
this.mapEventHander = () => {
for (const key in this._sourceCaches) {
this._sourceCaches[key].update(this.sourceStyles[key]);
}
};
this.scene.on('zoomchange', this.mapEventHander);
this.scene.on('dragend', this.mapEventHander);
}
clearMapEvent() {
this.scene.off('zoomchange', this.mapEventHander);
this.scene.off('dragend', this.mapEventHander);
}
// 计算视野内的瓦片坐标
_calculateTileIDs() {
this.updateTileList = [];
const pixelBounds = this._getPixelBounds();
const tileRange = this._pxBoundsToTileRange(pixelBounds);
const margin = this.get('keepBuffer');
this.noPruneRange = new Bounds(tileRange.getBottomLeft().subtract([ margin, -margin ]),
tileRange.getTopRight().add([ margin, -margin ]));
if (!(isFinite(tileRange.min.x) &&
isFinite(tileRange.min.y) &&
isFinite(tileRange.max.x) &&
isFinite(tileRange.max.y))) { throw new Error('Attempted to load an infinite number of tiles'); }
for (let j = tileRange.min.y; j <= tileRange.max.y; j++) {
for (let i = tileRange.min.x; i <= tileRange.max.x; i++) {
const coords = [ i, j, this.tileZoom ];
this.tileList.push(coords);
this._tileMap[coords.join('_')] = coords;
}
}
}
_getPixelBounds() {
const viewPort = this.scene.getBounds().toBounds();
const NE = viewPort.getNorthEast();
const SW = viewPort.getSouthWest();
const zoom = this.tileZoom;
const center = this.scene.getCenter();
const NEPoint = this.scene.crs.lngLatToPoint(toLngLat(NE.lng, NE.lat), zoom);
const SWPoint = this.scene.crs.lngLatToPoint(toLngLat(SW.lng, SW.lat), zoom);
const centerPoint = this.scene.crs.lngLatToPoint(toLngLat(center.lng, center.lat), zoom);
const topHeight = centerPoint.y - NEPoint.y;
const bottomHeight = SWPoint.y - centerPoint.y;
// 跨日界线的情况
let leftWidth;
let rightWidth;
if (center.lng - NE.lng > 0 || center.lng - SW.lng < 0) {
const width = Math.pow(2, zoom) * 256 / 360 * (180 - NE.lng) + Math.pow(2, zoom) * 256 / 360 * (SW.lng + 180);
if (center.lng - NE.lng > 0) { // 日界线在右侧
leftWidth = Math.pow(2, zoom) * 256 / 360 * (center.lng - NE.lng);
rightWidth = width - leftWidth;
} else {
rightWidth = Math.pow(2, zoom) * 256 / 360 * (SW.lng - center.lng);
leftWidth = width - rightWidth;
}
} else { // 不跨日界线
leftWidth = Math.pow(2, zoom) * 256 / 360 * (center.lng - SW.lng);
rightWidth = Math.pow(2, zoom) * 256 / 360 * (NE.lng - center.lng);
}
const pixelBounds = new Bounds(centerPoint.subtract(leftWidth, topHeight), centerPoint.add(rightWidth, bottomHeight));
return pixelBounds;
}
_pxBoundsToTileRange(pixelBounds) {
return new Bounds(
pixelBounds.min.divideBy(256).floor(),
pixelBounds.max.divideBy(256).ceil().subtract([ 1, 1 ])
);
}

}
8 changes: 4 additions & 4 deletions src/layer/tile/tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ export default class Tile extends Base {
}
requestTileAsync(done) {
// 获取数据
this.layer.workerTileSource.loadTile({
tile: this._tile,
url: this.layer.tileSource.getRequestUrl(this._tile[0], this._tile[1], this._tile[2])
});
// this.layer.workerTileSource.loadTile({
// tile: this._tile,
// url: this.layer.tileSource.getRequestUrl(this._tile[0], this._tile[1], this._tile[2])
// });
const data = this.layer.tileSource.getTileData(this._tile[0], this._tile[1], this._tile[2]);
if (data.loaded) {
done(data.data);
Expand Down
4 changes: 0 additions & 4 deletions src/layer/tile/tile_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Layer from '../../core/layer';
import Util from '../../util';
import diff from '../../util/diff';
import TileSource from '../../source/tile_source';
import TileWorkerSource from '../../source/tile_worker_source';
import * as THREE from '../../core/three';
import Controller from '../../core/controller/index';
import Global from '../../global';
Expand All @@ -27,9 +26,6 @@ export default class TileLayer extends Layer {
this.tileList = {};
this.type = this.get('layerType');
this.workerPool = this.scene.workerPool;
this.workerTileSource = new TileWorkerSource({
workerPool: this.scene.workerPool
});
}
shape(field, values) {
const layerType = this.get('layerType');
Expand Down
Loading

0 comments on commit d7d66a2

Please sign in to comment.