UNPKG

@ngageoint/geopackage

Version:
347 lines 15.8 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.NumberFeaturesTile = void 0; var canvas_1 = require("../../../canvas/canvas"); var customFeaturesTile_1 = require("./customFeaturesTile"); /** * Draws a tile indicating the number of features that exist within the tile, * visible when zoomed in closer. The number is drawn in the center of the tile * and by default is surrounded by a colored circle with border. By default a * tile border is drawn and the tile is colored (transparently most likely). The * paint objects for each draw type can be modified to or set to null (except * for the text paint object). */ var NumberFeaturesTile = /** @class */ (function (_super) { __extends(NumberFeaturesTile, _super); function NumberFeaturesTile() { var _this = _super.call(this) || this; _this.textSize = 18; _this.textFont = 'Noto Mono'; _this.textColor = 'rgba(255, 255, 255, 1.0)'; _this.circleStrokeWidth = 3; _this.circleBorderColor = 'rgba(0, 0, 0, 0.25)'; _this.circleFillColor = 'rgba(0, 0, 0, 1.0)'; _this.circlePaddingPercentage = 0.25; _this.defaultFontRegistered = false; return _this; } /** * Get the text size * @return {Number} text size */ NumberFeaturesTile.prototype.getTextSize = function () { return this.textSize; }; /** * Set the text size * @param {Number} textSize text size */ NumberFeaturesTile.prototype.setTextSize = function (textSize) { this.textSize = textSize; }; /** * Get the text color * @return {String} text color */ NumberFeaturesTile.prototype.getTextColor = function () { return this.textColor; }; /** * Set the text color * @param {String} textColor text color */ NumberFeaturesTile.prototype.setTextColor = function (textColor) { this.textColor = textColor; }; /** * Get the circle stroke width * @return {Number} circle stroke width */ NumberFeaturesTile.prototype.getCircleStrokeWidth = function () { return this.circleStrokeWidth; }; /** * Set the circle stroke width * @param {Number} circleStrokeWidth circle stroke width */ NumberFeaturesTile.prototype.setCircleStrokeWidth = function (circleStrokeWidth) { this.circleStrokeWidth = circleStrokeWidth; }; /** * Get the circle color * @return {String} circle color */ NumberFeaturesTile.prototype.getCircleColor = function () { return this.circleBorderColor; }; /** * Set the circle color * @param {String} circleBorderColor circle color */ NumberFeaturesTile.prototype.setCircleColor = function (circleBorderColor) { this.circleBorderColor = circleBorderColor; }; /** * Get the circle fill color * @return {String} circle fill color */ NumberFeaturesTile.prototype.getCircleFillColor = function () { return this.circleFillColor; }; /** * Set the circle fill color * @param {String} circleFillColor circle fill color */ NumberFeaturesTile.prototype.setCircleFillColor = function (circleFillColor) { this.circleFillColor = circleFillColor; }; /** * Get the circle padding percentage around the text * @return {Number} circle padding percentage, 0.0 to 1.0 */ NumberFeaturesTile.prototype.getCirclePaddingPercentage = function () { return this.circlePaddingPercentage; }; /** * Set the circle padding percentage to pad around the text, value between * 0.0 and 1.0 * @param {Number} circlePaddingPercentage circle padding percentage */ NumberFeaturesTile.prototype.setCirclePaddingPercentage = function (circlePaddingPercentage) { if (circlePaddingPercentage < 0.0 || circlePaddingPercentage > 1.0) { throw new Error('Circle padding percentage must be between 0.0 and 1.0: ' + circlePaddingPercentage); } this.circlePaddingPercentage = circlePaddingPercentage; }; /** * Get the tile border stroke width * @return {Number} tile border stroke width */ NumberFeaturesTile.prototype.getTileBorderStrokeWidth = function () { return this.tileBorderStrokeWidth; }; /** * Set the tile border stroke width * * @param {Number} tileBorderStrokeWidth tile border stroke width */ NumberFeaturesTile.prototype.setTileBorderStrokeWidth = function (tileBorderStrokeWidth) { this.tileBorderStrokeWidth = tileBorderStrokeWidth; }; /** * Get the tile border color * @return {String} tile border color */ NumberFeaturesTile.prototype.getTileBorderColor = function () { return this.tileBorderColor; }; /** * Set the tile border color * @param {String} tileBorderColor tile border color */ NumberFeaturesTile.prototype.setTileBorderColor = function (tileBorderColor) { this.tileBorderColor = tileBorderColor; }; /** * Get the tile fill color * @return {String} tile fill color */ NumberFeaturesTile.prototype.getTileFillColor = function () { return this.tileFillColor; }; /** * Set the tile fill color * @param {String} tileFillColor tile fill color */ NumberFeaturesTile.prototype.setTileFillColor = function (tileFillColor) { this.tileFillColor = tileFillColor; }; /** * Is the draw unindexed tiles option enabled * @return {Boolean} true if drawing unindexed tiles */ NumberFeaturesTile.prototype.isDrawUnindexedTiles = function () { return this.drawUnindexedTiles; }; /** * Set the draw unindexed tiles option * @param {Boolean} drawUnindexedTiles draw unindexed tiles flag */ NumberFeaturesTile.prototype.setDrawUnindexedTiles = function (drawUnindexedTiles) { this.drawUnindexedTiles = drawUnindexedTiles; }; /** * Get the compression format * @return {String} the compression format (either png or jpeg) */ NumberFeaturesTile.prototype.getCompressFormat = function () { return this.compressFormat; }; /** * Set the compression format * @param {String} compressFormat either 'png' or 'jpeg' */ NumberFeaturesTile.prototype.setCompressFormat = function (compressFormat) { this.compressFormat = compressFormat; }; /** * Draw unindexed tile * @param tileWidth * @param tileHeight * @param canvas * @returns {Promise<String|Buffer>} */ NumberFeaturesTile.prototype.drawUnindexedTile = function (tileWidth, tileHeight, canvas) { if (canvas === void 0) { canvas = null; } return __awaiter(this, void 0, void 0, function () { var image; return __generator(this, function (_a) { image = null; if (this.drawUnindexedTiles) { // Draw a tile indicating we have no idea if there are features // inside. // The table is not indexed and more features exist than the max // feature count set. image = this.drawTile(tileWidth, tileHeight, '?', canvas); } return [2 /*return*/, image]; }); }); }; /** * Draw a tile with the provided text label in the middle * @param {Number} tileWidth * @param {Number} tileHeight * @param {String} text * @param tileCanvas * @return {Promise<String|Buffer>} */ NumberFeaturesTile.prototype.drawTile = function (tileWidth, tileHeight, text, tileCanvas) { return __awaiter(this, void 0, void 0, function () { var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: // eslint-disable-next-line complexity return [4 /*yield*/, canvas_1.Canvas.initializeAdapter()]; case 1: // eslint-disable-next-line complexity _a.sent(); return [2 /*return*/, new Promise(function (resolve) { var canvas; var dispose = false; if (tileCanvas !== undefined && tileCanvas !== null) { canvas = tileCanvas; } else { canvas = canvas_1.Canvas.create(tileWidth, tileHeight); dispose = true; } var context = canvas.getContext('2d'); context.clearRect(0, 0, tileWidth, tileHeight); // Draw the tile border if (_this.tileFillColor !== null) { context.fillStyle = _this.tileFillColor; context.fillRect(0, 0, tileWidth, tileHeight); } // Draw the tile border if (_this.tileBorderColor !== null) { context.strokeStyle = _this.tileBorderColor; context.lineWidth = _this.tileBorderStrokeWidth; context.strokeRect(0, 0, tileWidth, tileHeight); } var textWidth = canvas_1.Canvas.measureText(context, _this.textFont, _this.textSize, text); var textHeight = _this.textSize; // Determine the center of the tile var centerX = Math.round(tileWidth / 2.0); var centerY = Math.round(tileHeight / 2.0); // Draw the circle if (_this.circleBorderColor != null || _this.circleFillColor != null) { var diameter = Math.max(textWidth, textHeight); var radius = Math.round(diameter / 2.0); radius = Math.round(radius + diameter * _this.circlePaddingPercentage); // Draw the circle if (_this.circleFillColor != null) { context.fillStyle = _this.circleFillColor; context.beginPath(); context.arc(centerX, centerY, radius, 0, 2 * Math.PI, true); context.closePath(); context.fill(); } // Draw the circle border if (_this.circleBorderColor != null) { context.strokeStyle = _this.circleBorderColor; context.lineWidth = _this.circleStrokeWidth; context.beginPath(); context.arc(centerX, centerY, radius, 0, 2 * Math.PI, true); context.closePath(); context.stroke(); } } canvas_1.Canvas.drawText(context, text, [centerX, centerY], _this.textFont, _this.textSize, _this.textColor); canvas_1.Canvas.toDataURL(canvas, 'image/' + _this.compressFormat).then(function (result) { if (dispose) { canvas_1.Canvas.disposeCanvas(canvas); } resolve(result); }); })]; } }); }); }; return NumberFeaturesTile; }(customFeaturesTile_1.CustomFeaturesTile)); exports.NumberFeaturesTile = NumberFeaturesTile; //# sourceMappingURL=numberFeaturesTile.js.map