Skip to content

Commit

Permalink
fix for icons without width or height or both. bump version.
Browse files Browse the repository at this point in the history
  • Loading branch information
caldwellc committed Sep 27, 2021
1 parent d41e020 commit 4b9317e
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 26 deletions.
1 change: 1 addition & 0 deletions lib/extension/relatedTables/mediaRow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export class MediaRow extends UserRow {
getScaledDataImage(scale: number): Promise<{image: any, width: number, height: number}> {
return ImageUtils.getScaledImage(this.data, scale);
}

/**
* Get the content type column
* @return {module:user/userColumn~UserColumn}
Expand Down
48 changes: 40 additions & 8 deletions lib/extension/style/iconCache.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IconRow } from './iconRow';
import { Canvas } from '../../canvas/canvas';
import {ImageUtils} from "../../tiles/imageUtils";

/**
* @memberOf module:extension/style
Expand Down Expand Up @@ -183,17 +184,48 @@ export class IconCache {
let iconImage = null;
if (icon != null) {
const iconId = icon.id;
// get image from cache

if (iconCache != null) {
iconImage = iconCache.get(iconId);
}
const iconScaledWidth = Math.round(icon.width * scale);
const iconScaledHeight = Math.round(icon.height * scale);
if (iconImage == null || (iconImage.width !== iconScaledWidth) || iconImage.height !== iconScaledHeight) {
iconImage = await icon.getScaledDataImage(scale);
}
if (iconCache != null && iconImage != null) {
iconCache.putIconForIconRow(icon, iconImage);

if (iconImage == null) {
try {
iconImage = await ImageUtils.getImage(icon.data);
} catch (e) {
throw new Error('Failed to get the Icon Row image. Id: ' + iconId + ', Name: ' + icon.name)
}

const dataWidth = iconImage.width;
const dataHeight = iconImage.height;
let iconWidth = icon.width;
let iconHeight = icon.height;

let scaleImage = iconWidth != null || iconHeight != null;
if (!scaleImage && scale != 1.0) {
iconWidth = dataWidth;
iconHeight = dataHeight;
scaleImage = true;
}

if (scaleImage) {
if (iconWidth == null) {
iconWidth = dataWidth * (iconHeight / dataHeight);
} else if (iconHeight == null) {
iconHeight = dataHeight * (iconWidth / dataWidth);
}

const scaledWidth = Math.round(scale * iconWidth);
const scaledHeight = Math.round(scale * iconHeight);

if (scaledWidth != dataWidth || scaledHeight != dataHeight) {
iconImage = await ImageUtils.scaleImage(iconImage, scale);
}
}

if (iconCache != null) {
iconCache.putIconForIconRow(icon, iconImage);
}
}
}
return iconImage;
Expand Down
6 changes: 2 additions & 4 deletions lib/tiles/features/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ export class FeatureTiles {
*/
simplifyPoints(lineString: any, isPolygon: boolean = false): any | null {
let coords = simplify(lineString.coordinates.map(coordinate => {
return {x: coordinate[0], y: coordinate[1]}
return {x: coordinate[0], y: coordinate[1]};
}), this.simplifyToleranceInPixels, false).map(point => [point.x, point.y]);
if (isPolygon) {
if (coords.length < 4) {
Expand Down Expand Up @@ -723,9 +723,7 @@ export class FeatureTiles {
boundingBox: BoundingBox,
isPolygon: boolean = false
): void {
lineString.coordinates = lineString.coordinates.map(coordinate => {
return [TileBoundingBoxUtils.getXPixel(this.tileWidth, boundingBox, coordinate[0]), TileBoundingBoxUtils.getYPixel(this.tileHeight, boundingBox, coordinate[1])]
})
lineString.coordinates = lineString.coordinates.map(coordinate => [TileBoundingBoxUtils.getXPixel(this.tileWidth, boundingBox, coordinate[0]), TileBoundingBoxUtils.getYPixel(this.tileHeight, boundingBox, coordinate[1])]);
const simplifiedLineString = this.simplifyGeometries ? this.simplifyPoints(lineString, isPolygon) : lineString;
if (simplifiedLineString != null && simplifiedLineString.coordinates.length > 0) {
context.moveTo(simplifiedLineString.coordinates[0][0], simplifiedLineString.coordinates[0][1]);
Expand Down
23 changes: 23 additions & 0 deletions lib/tiles/imageUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,27 @@ export class ImageUtils {
});
});
}

/**
* Get a scaled image
* @param {any} image
* @param {Number} scale
* @returns {Promise<any>}
*/
public static async scaleImage(image: any, scale: number): Promise<{image: any, width: number, height: number}> {
return new Promise((resolve) => {
if (scale === 1.0) {
resolve(image);
} else {
Canvas.scaleImage(image, scale).then(scaledImage => {
Canvas.disposeImage(image);
resolve(scaledImage)
}).catch((e) => {
Canvas.disposeImage(image);
console.error(e);
resolve(null);
})
}
});
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ngageoint/geopackage",
"version": "4.0.0-beta.39",
"version": "4.0.0-beta.40",
"description": "GeoPackage JavaScript Library",
"keywords": [
"NGA",
Expand Down
44 changes: 31 additions & 13 deletions test/lib/extension/style/testIconCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@ describe('IconCache Tests', function() {
var iconImage;
var iconImageBuffer;

var randomIcon = function(featureTableStyles) {
var randomIcon = function(featureTableStyles, noWidth = false, noHeight = false, noAnchors = false) {
var iconRow = featureTableStyles.getIconDao().newRow();
iconRow.data = iconImageBuffer;
iconRow.contentType = 'image/png';
iconRow.name = "Icon Name";
iconRow.description = "Icon Description";
iconRow.width = Math.random() * iconImage.width;
iconRow.height = Math.random() * iconImage.height;
iconRow.anchorU = Math.random();
iconRow.anchorV = Math.random();
if (!noWidth) {
iconRow.width = Math.random() * iconImage.width;
}
if (!noHeight) {
iconRow.height = Math.random() * iconImage.height;
}
if (!noAnchors) {
iconRow.anchorU = Math.random();
iconRow.anchorV = Math.random();
}
return iconRow;
};

Expand Down Expand Up @@ -193,15 +199,27 @@ describe('IconCache Tests', function() {
should.exist(iconCache.getIconForIconRow(iconRow));
}));

it('should create scaled icon and cache it even when already cached', mochaAsync(async () => {
it('should automatically determine dimensions of an icon with no explicit width/height', mochaAsync(async () => {
var iconCache = new IconCache();
var iconRow = randomIcon(featureTableStyles);
var iconRow = featureTableStyles.getIconDao().newRow();
iconRow.id = 0;
iconCache.putIconForIconRow(iconRow, iconImage);
var expectedImage = await ImageUtils.getImage(path.join(__dirname, '..', '..', '..', 'fixtures', isWeb ? 'web' : '', 'point_2x.png'));
var image = await iconCache.createScaledIcon(iconRow, 2.0);
var result = await compareImages(expectedImage, image);
result.should.be.equal(true);
should.exist(iconCache.getIconForIconRow(iconRow));
iconRow.data = iconImageBuffer;
iconRow.contentType = 'image/png';
iconRow.name = "Icon Name";
iconRow.description = "Icon Description";
iconRow.width = iconImage.width;
// iconRow.height = Math.random() * iconImage.height;
// iconRow.anchorU = Math.random();
// iconRow.anchorV = Math.random();

var image = await iconCache.createScaledIcon(iconRow, 1.0);
image.width.should.be.equal(iconImage.width);
image.height.should.be.equal(iconImage.height);

iconRow.id = 1;
iconRow.width = undefined;
var image = await iconCache.createScaledIcon(iconRow, 1.0);
image.width.should.be.equal(iconImage.width);
image.height.should.be.equal(iconImage.height);
}));
});

0 comments on commit 4b9317e

Please sign in to comment.