From f4920504e5190835312bfa3e4630aeac39da6047 Mon Sep 17 00:00:00 2001
From: Aziz <119948730+aziz-access@users.noreply.github.com>
Date: Tue, 4 Jul 2023 15:25:09 +0200
Subject: [PATCH] feat(geo): Print with resolution PDF and image (#1264)
* feat(geo): Added rotate option and north direction to the map when printing
* solve lint and optimize map updateControls function
* integrate custom controle rotation-button component in the map
* adjust image in all template
* solve scale and rotate arrow color and position in pdf export
* code improvement and write documentations
* sove lint
* feat(geo): add resolution options for print module
* add resolution to image export
* solve lint
* code improvement
* code improvement eliminate repeated code
* solve lint
* code refactoring
* solve lint
* delete mapOverlayHTML from dom
* solve comments
* add type to map canvas
* delete console log
* merge branches and adjust code
* delete scaleLine from print demo component
* solve conflicts
* code improvement add resetOriginalMapSize, setMapImageResolution and setMapResolution for pdf
* add access modifiers to function
---
.../print-form/print-form.component.html | 2 +-
.../src/lib/print/print/print.component.ts | 4 +-
.../geo/src/lib/print/shared/print.service.ts | 83 ++++++++++++++-----
3 files changed, 65 insertions(+), 24 deletions(-)
diff --git a/packages/geo/src/lib/print/print-form/print-form.component.html b/packages/geo/src/lib/print/print-form/print-form.component.html
index 721cf8c7c0..4831d7cdbc 100644
--- a/packages/geo/src/lib/print/print-form/print-form.component.html
+++ b/packages/geo/src/lib/print/print-form/print-form.component.html
@@ -113,7 +113,7 @@
-
+
,
+ docSize: [number, number],
margins: Array,
legendPostion: PrintLegendPosition
) {
- const mapSize = map.ol.getSize();
+ const mapSize = map.ol.getSize() as [number, number];
const viewResolution = map.ol.getView().getResolution();
- const extent = map.ol.getView().calculateExtent(mapSize);
- const widthPixels = Math.round((size[0] * resolution) / 25.4);
- const heightPixels = Math.round((size[1] * resolution) / 25.4);
+ const dimensionPixels = this.setMapResolution(map, docSize, resolution, viewResolution);
const status$ = new Subject();
let timeout;
map.ol.once('rendercomplete', async (event: any) => {
const mapCanvas = event.target.getViewport().getElementsByTagName('canvas') as HTMLCollectionOf;
const mapResultCanvas = await this.drawMap(
- [widthPixels, heightPixels],
+ dimensionPixels,
mapCanvas
);
- // Reset original map size
- map.ol.setSize(mapSize);
- map.ol.getView().setResolution(viewResolution);
- this.renderMap(map, mapSize, extent);
+ this.resetOriginalMapSize(map, mapSize, viewResolution);
await this.drawMapControls(map, mapResultCanvas, legendPostion);
@@ -669,20 +664,43 @@ export class PrintService {
status = SubjectStatus.Error;
this.messageService.error('igo.geo.printForm.corsErrorMessageBody', 'igo.geo.printForm.corsErrorMessageHeader');
}
- // Reset original map size
- map.ol.setSize(mapSize);
- map.ol.getView().setResolution(viewResolution);
- this.renderMap(map, mapSize, extent);
+ this.resetOriginalMapSize(map, mapSize, viewResolution);
status$.next(status);
},200);
});
+ return status$;
+ }
+
+ private setMapResolution(
+ map: IgoMap,
+ initialSize: [number, number],
+ resolution: number,
+ viewResolution: number
+ ): [number, number] {
+
+ const mapSize = map.ol.getSize();
+ const widthPixels = Math.round((initialSize[0] * resolution) / 25.4);
+ const heightPixels = Math.round((initialSize[1] * resolution) / 25.4);
+
// Set print size
const printSize = [widthPixels, heightPixels];
map.ol.setSize(printSize);
const scaling = Math.min(widthPixels / mapSize[0], heightPixels / mapSize[1]);
map.ol.getView().setResolution(viewResolution / scaling);
- return status$;
+
+ return [widthPixels, heightPixels];
+ }
+
+ private resetOriginalMapSize(
+ map: IgoMap,
+ initialSize: [number, number],
+ viewResolution: number
+ ) {
+ map.ol.setSize(initialSize);
+ map.ol.getView().setResolution(viewResolution);
+ map.ol.updateSize();
+ map.ol.renderSync();
}
private async drawMap(
@@ -832,7 +850,7 @@ export class PrintService {
*/
downloadMapImage(
map: IgoMap,
- resolution: number,
+ printResolution: PrintResolution,
format = 'png',
projection = false,
scale = false,
@@ -843,15 +861,20 @@ export class PrintService {
legendPosition: PrintLegendPosition
) {
const status$ = new Subject();
- // const resolution = map.ol.getView().getResolution();
this.activityId = this.activityService.register();
const translate = this.languageService.translate;
format = format.toLowerCase();
+ const resolution = +printResolution;
+ const initialMapSize = map.ol.getSize() as [number, number];
+ const viewResolution = map.ol.getView().getResolution();
map.ol.once('rendercomplete', async (event: any) => {
const size = map.ol.getSize();
const mapCanvas = event.target.getViewport().getElementsByTagName('canvas') as HTMLCollectionOf;
const mapResultCanvas = await this.drawMap(size, mapCanvas);
+
+ this.resetOriginalMapSize(map, initialMapSize, viewResolution);
+
await this.drawMapControls(map, mapResultCanvas, legendPosition);
// Check the legendPosition
if (legendPosition !== 'none') {
@@ -1032,10 +1055,30 @@ export class PrintService {
}
}
});
- map.ol.renderSync();
+
+ this.setMapImageResolution(map, initialMapSize, resolution, viewResolution);
+
return status$;
}
+ private setMapImageResolution(
+ map: IgoMap,
+ initialMapSize: [number, number],
+ resolution: number,
+ viewResolution: number
+ ): void {
+ const scaleFactor = resolution / 96;
+ const newMapSize = [
+ Math.round(initialMapSize[0] * scaleFactor), // width
+ Math.round(initialMapSize[1] * scaleFactor) // height
+ ];
+ map.ol.setSize(newMapSize);
+ const scaling = Math.min(newMapSize[0] / initialMapSize[0], newMapSize[1] / initialMapSize[1]);
+ const view = map.ol.getView();
+ view.setResolution(viewResolution / scaling);
+ map.ol.renderSync();
+ }
+
/**
* Create and Add Legend to the map canvas
* @param canvas Canvas of the map