UNPKG

thematic-earth

Version:

HTML-based, whole-Earth thematic maps using locally hosted data layers

2 lines 3.68 kB
/* Copyright (c) 2023 Read Write Tools. Legal use subject to the Thematic Earth Software License Agreement. */ import ProxyBase from'../labels/proxy-base.class.js';import LabelPosition from'../enum/label-position.enum.js';import LabelLocation from'../labels/label-location.class.js';import*as LabelGraphics from'../graphics/label-graphics.js';import LabelPoint from'../labels/label-point.class.js';import aver from'../dev/aver.js';import expect from'../dev/expect.js';export default class ProxyPoint extends ProxyBase{constructor(e,i,t){super(e,i,t),this.referencePoint=null,this.labelOffset=null,this.possibleLocations=new Map,this.chosenLocation=null,Object.seal(this)}getFeatureData(e,i){expect(e,'Catalog'),expect(i,'HTMLCanvasElement'),super.getFeatureData(e,i),this.referencePoint=this.getReferencePoint(this.feature);var t=.5*this.canvasParams.getSymbolOverallDiameter(''),s=this.canvasParams.getLabelOffset(),o=this.canvasParams.scaleSymbolsCoefficient();this.labelOffset=(t+s)*o}getFeatureBoundingBox(e,i){aver(null!=this.canvasParams);var t=.7071*(t=.5*this.canvasParams.getSymbolOverallDiameter(''))*this.canvasParams.scaleSymbolsCoefficient();return e.getFeatureBoundingBox(t)}getReferencePoint(e){return aver(null!=this.featureBoundingBox),new LabelPoint(this.featureBoundingBox.centerX,this.featureBoundingBox.centerY)}disqualificationCriteria(){aver(null!=this.labelBoundingBox),aver(null!=this.featureBoundingBox),0!=this.featureBoundingBox.isValid()||(this.isDisqualified=!0)}possiblePlacements(e){this.onePossiblePlacement(LabelPosition.NW),this.onePossiblePlacement(LabelPosition.N),this.onePossiblePlacement(LabelPosition.NE),this.onePossiblePlacement(LabelPosition.W),this.onePossiblePlacement(LabelPosition.C),this.onePossiblePlacement(LabelPosition.E),this.onePossiblePlacement(LabelPosition.SW),this.onePossiblePlacement(LabelPosition.S),this.onePossiblePlacement(LabelPosition.SE)}onePossiblePlacement(e){var i=new LabelLocation(e,this.referencePoint,this.labelOffset,this.labelBoundingBox);this.possibleLocations.set(e,i)}reserveSymbols(e){expect(e,'CollisionMatrix'),'C'!=this.canvasParams.getLabelPosition()&&e.reserveSymbolBoundingBox(this.featureBoundingBox)}collisionAdjustment(e,i){expect(e,'HTMLCanvasElement'),expect(i,'CollisionMatrix');var t=this.canvasParams.getLabelPosition();for(let e=0;e<9;e++){if(this.chosenLocation=this.possibleLocations.get(t),1==i.reserveLabelLocation(this.chosenLocation))return;t=this.getNextLabelPosition(t)}this.isDisqualified=!0}getNextLabelPosition(e){switch(e){case LabelPosition.NW:return LabelPosition.E;case LabelPosition.N:return LabelPosition.SE;case LabelPosition.NE:return LabelPosition.S;case LabelPosition.W:return LabelPosition.NE;case LabelPosition.C:return LabelPosition.NW;case LabelPosition.E:return LabelPosition.SW;case LabelPosition.SW:return LabelPosition.N;case LabelPosition.S:return LabelPosition.NW;case LabelPosition.SE:return LabelPosition.W;default:return LabelPosition.NE}}drawBoundingBox(e){if('show'==(this.canvasParams['label-debug-alt-positions']??'hide')){var i=[LabelPosition.NW,LabelPosition.N,LabelPosition.NE,LabelPosition.W,LabelPosition.C,LabelPosition.E,LabelPosition.SW,LabelPosition.S,LabelPosition.SE];const o=['#f00','#0f0','#00f','#ff0','#0ff','#f0f','#000','#777','#fff'];for(let a=0;a<i.length;a++){var t=this.possibleLocations.get(i[a]),s=e.getContext('2d');s.strokeStyle=o[a%9],s.lineWidth=.5;s.strokeRect(t.x,t.y,t.width,t.height,'left','top')}}}drawLabel(e){if(expect(e,'HTMLCanvasElement'),''!=this.labelText){var i=this.labelBoundingBox.top;LabelGraphics.draw(e,this.canvasParams,this.chosenLocation.x,this.chosenLocation.y+i,this.labelText,'left','top')}}}