Skip to content

Commit

Permalink
Merge pull request #142 from thinkWhere/version-openlayers
Browse files Browse the repository at this point in the history
version openlayers
  • Loading branch information
LindaAlblas authored Oct 17, 2017
2 parents d711f22 + 8cec0f4 commit 596ee9f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
2 changes: 1 addition & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"font-awesome": "^4.7.0",
"jquery": "^3.2.1",
"jspdf": "^1.3.3",
"openlayers": "^4.2.0",
"openlayers": "4.2.0",
"proj4": "^2.4.4",
"rxjs": "^5.1.0",
"zone.js": "^0.8.4"
Expand Down
40 changes: 32 additions & 8 deletions client/src/app/map/style.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export class StyleService {

/**
* Get the OL style based on the rule style
* Supports Font Awesome for text styles
* @param ruleStyle
* @returns {any}
*/
Expand Down Expand Up @@ -136,6 +137,9 @@ export class StyleService {
* @returns {string}
*/
getLegendImage(layerStyle) {
// Calculate the device pixel ratio
var ratio = this.getDevicePixelRatio();

// Create a canvas element
var canvas: any = document.createElement("canvas");

Expand Down Expand Up @@ -165,9 +169,9 @@ export class StyleService {
for (var i = 0; i < layerStyle.rules.length; i++) {
var ruleStyle = layerStyle.rules[i].style;
var ctx = canvas.getContext("2d");
ctx.font = "10px Arial";
ctx.font = "12px Arial";
ctx.fillStyle = 'black';
ctx.fillText(ruleStyle.label, 30, (15 + i * heightPerCategory));
ctx.fillText(ruleStyle.label, 30 * ratio, (15 * ratio + i * heightPerCategory * ratio));

if (ruleStyle) {
style = this.getOLStyle(ruleStyle);
Expand Down Expand Up @@ -220,6 +224,9 @@ export class StyleService {

/**
* Create a legend based on JSON from an ArcGIS layer
* TODO: this is hardcoded! For the Pacific Disaster Center layer only the first icon needs
* to get displayed. See: https://github.com/thinkWhere/DMIS/issues/109
* By hardcoding this, any additional ArcGIS REST layers will only show the first icon as well...
* @param legendInfo
* @param callback
*/
Expand Down Expand Up @@ -253,16 +260,33 @@ export class StyleService {
context.drawImage(this, 0, this.anchorHeight, this.width, this.height);
context.font = "12px Arial";
context.fillText(this.label, this.width + 10, this.anchorHeight + 20);
// Wait for all the legend images to be loaded before returning it
if(imagesLoaded == imageCount){
var dataURL = canvas.toDataURL();
callback(dataURL);
}

// If using multiple images, Wait for all the legend images to be loaded before returning it
// by checking if imagesLoaded == imageCount
var dataURL = canvas.toDataURL();
callback(dataURL);
};
img.src = 'data:' + legends[j].contentType + ';base64,' + legends[j].imageData;
break;
}
break;
}
canvas.height = anchorHeight;
}

/**
* Returns the device pixel ratio for all browsers
* @returns {number}
*/
getDevicePixelRatio() {
var ratio = 1;
// To account for zoom, change to use deviceXDPI instead of systemXDPI
if (window.screen.systemXDPI !== undefined && window.screen.logicalXDPI !== undefined && window.screen.systemXDPI > window.screen.logicalXDPI) {
// Only allow for values > 1
ratio = window.screen.systemXDPI / window.screen.logicalXDPI;
}
else if (window.devicePixelRatio !== undefined) {
ratio = window.devicePixelRatio;
}
return ratio;
};
}

0 comments on commit 596ee9f

Please sign in to comment.