Skip to content

Commit

Permalink
Add getExtent utils and constants
Browse files Browse the repository at this point in the history
  • Loading branch information
ger-benjamin committed Feb 9, 2024
1 parent d4769e5 commit a5f9baa
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# @geoblocks/geoblocks changes

## 0.2.3
- Add utility functions.
- In `BaseCustomizer`, the printExtent can be now set and get/set are dedicated methods.
- `pdfA` (allow transparency) is now a spec.map optional param.
- spec.attributes are now partial and `datasources` attribute is removed.
Expand Down
2 changes: 1 addition & 1 deletion src/VectorEncoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default class VectorEncoder {
}
console.assert(source instanceof VectorSource);

const features = source.getFeaturesInExtent(this.customizer_.printExtent);
const features = source.getFeaturesInExtent(this.customizer_.getPrintExtent());

const geojsonFeatures: GeoJSONFeature[] = [];
const mapfishStyleObject: MFPVectorStyle = {
Expand Down
13 changes: 13 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const Constants = {
/** "Standardized rendering pixel size" is defined as 0.28 mm, see http://www.opengeospatial.org/standards/wmts */
WMTS_PIXEL_SIZE: 0.28e-3,
/** Standard PPI */
POINTS_PER_INCH: 72,
/** According to the "international yard" definition 1 inch is defined as exactly 2.54 cm. */
METERS_PER_INCH: 0.0254,
};

export const CalculatedConstants = {
/** Default to PPI / METERS per Inch */
POINTS_PER_DISTANCE_UNIT: () => Constants.POINTS_PER_INCH / Constants.METERS_PER_INCH,
};
23 changes: 20 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,26 @@ import WMTSTileGrid from 'ol/tilegrid/WMTS.js';
import {toSize} from 'ol/size.js';
import type {MFPReportResponse, MFPSpec, MFPStatusResponse, MFPWmtsMatrix} from './types';
import type {WMTS} from 'ol/source.js';
import type {Extent} from 'ol/extent';
import {Constants, CalculatedConstants} from './constants';

// "Standardized rendering pixel size" is defined as 0.28 mm, see http://www.opengeospatial.org/standards/wmts
const WMTS_PIXEL_SIZE_ = 0.28e-3;
/**
* @param mapPageSize The page size (width, height)
* @param center The coordinate of the extent's center.
* @param scale The scale to calculate the extent width.
* @returns an extent that fit the page size. Calculated with POINTS_PER_DISTANCE_UNIT (by default using meters)
*/
export function getPrintExtent(mapPageSize: number[], center: number[], scale: number): Extent {
const [mapPageWidthMeters, mapPageHeightMeters] = mapPageSize.map(
(side) => ((side / CalculatedConstants.POINTS_PER_DISTANCE_UNIT()) * scale) / 2,
);
return [
center[0] - mapPageWidthMeters,
center[1] - mapPageHeightMeters,
center[0] + mapPageWidthMeters,
center[1] + mapPageHeightMeters,
];
}

/**
* Takes a hex value and prepends a zero if it's a single digit.
Expand Down Expand Up @@ -51,7 +68,7 @@ export function getWmtsMatrices(source: WMTS): MFPWmtsMatrix[] {
const resolutionMeters = tileGrid.getResolution(i) * metersPerUnit;
wmtsMatrices.push({
identifier: matrixIds[i],
scaleDenominator: resolutionMeters / WMTS_PIXEL_SIZE_,
scaleDenominator: resolutionMeters / Constants.WMTS_PIXEL_SIZE,
tileSize: toSize(tileGrid.getTileSize(i)),
topLeftCorner: tileGrid.getOrigin(i),
matrixSize: [tileRange.maxX - tileRange.minX, tileRange.maxY - tileRange.minY],
Expand Down
5 changes: 3 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ test('Empty map', async (t) => {
dpi: 300,
layout: 'landscape_a4',
format: 'pdf',
customAttributes: {},
customAttributes: {title: 'My title'},
customizer: customizer,
});
assert.deepEqual(result, {
Expand All @@ -38,14 +38,15 @@ test('Empty map', async (t) => {
rotation: 0,
scale: 1,
},
title: 'My title',
},
format: 'pdf',
layout: 'landscape_a4',
});
});

test('OSM map', async (t) => {
const MFP_URL = 'https://geomapfish-demo-2-5.camptocamp.com/printproxy';
const MFP_URL = 'https://geomapfish-demo-2-8.camptocamp.com/printproxy';
const layout = '1 A4 portrait'; // better take from MFP
const map = new Map({
target: 'map',
Expand Down

0 comments on commit a5f9baa

Please sign in to comment.