Adjust resolution for user extent

This commit is contained in:
Andreas Hocevar
2021-09-10 01:36:33 +02:00
parent d7f7cbbc75
commit c9593b0cf6
4 changed files with 77 additions and 4 deletions

View File

@@ -631,6 +631,44 @@ export function fromUserExtent(extent, destProjection) {
return transformExtent(extent, userProjection, destProjection);
}
/**
* Return the resolution in user projection units per pixel. If no user projection
* is set, or source or user projection are missing units, the original resolution
* is returned.
* @param {number} resolution Resolution in input projection units per pixel.
* @param {ProjectionLike} sourceProjection The input projection.
* @return {number} Resolution in user projection units per pixel.
*/
export function toUserResolution(resolution, sourceProjection) {
if (!userProjection) {
return resolution;
}
const sourceUnits = get(sourceProjection).getUnits();
const userUnits = userProjection.getUnits();
return sourceUnits && userUnits
? (resolution * METERS_PER_UNIT[sourceUnits]) / METERS_PER_UNIT[userUnits]
: resolution;
}
/**
* Return the resolution in user projection units per pixel. If no user projection
* is set, or source or user projection are missing units, the original resolution
* is returned.
* @param {number} resolution Resolution in user projection units per pixel.
* @param {ProjectionLike} destProjection The destination projection.
* @return {number} Resolution in destination projection units per pixel.
*/
export function fromUserResolution(resolution, destProjection) {
if (!userProjection) {
return resolution;
}
const sourceUnits = get(destProjection).getUnits();
const userUnits = userProjection.getUnits();
return sourceUnits && userUnits
? (resolution * METERS_PER_UNIT[userUnits]) / METERS_PER_UNIT[sourceUnits]
: resolution;
}
/**
* Creates a safe coordinate transform function from a coordinate transform function.
* "Safe" means that it can handle wrapping of x-coordinates for global projections,

View File

@@ -36,6 +36,7 @@ import {
getTransformFromProjections,
getUserProjection,
toUserExtent,
toUserResolution,
} from '../../proj.js';
import {getUid} from '../../util.js';
import {wrapX as wrapCoordinateX} from '../../coordinate.js';
@@ -642,9 +643,11 @@ class CanvasVectorLayerRenderer extends CanvasLayerRenderer {
let userTransform;
if (userProjection) {
for (let i = 0, ii = loadExtents.length; i < ii; ++i) {
const extent = loadExtents[i];
const userExtent = toUserExtent(extent, projection);
vectorSource.loadFeatures(
toUserExtent(loadExtents[i], projection),
resolution,
userExtent,
toUserResolution(resolution, projection),
userProjection
);
}

View File

@@ -26,7 +26,7 @@ import {xhr} from '../featureloader.js';
* returns an array of {@link module:ol/extent~Extent} with the extents to load. Usually this
* is one of the standard {@link module:ol/loadingstrategy} strategies.
*
* @typedef {function(import("../extent.js").Extent, number): Array<import("../extent.js").Extent>} LoadingStrategy
* @typedef {function(import("../extent.js").Extent, number, import("../proj/Projection.js").default): Array<import("../extent.js").Extent>} LoadingStrategy
* @api
*/
@@ -939,7 +939,7 @@ class VectorSource extends Source {
*/
loadFeatures(extent, resolution, projection) {
const loadedExtentsRtree = this.loadedExtentsRtree_;
const extentsToLoad = this.strategy_(extent, resolution);
const extentsToLoad = this.strategy_(extent, resolution, projection);
for (let i = 0, ii = extentsToLoad.length; i < ii; ++i) {
const extentToLoad = extentsToLoad[i];
const alreadyLoaded = loadedExtentsRtree.forEachInExtent(

View File

@@ -12,6 +12,7 @@ import {
fromLonLat,
fromUserCoordinate,
fromUserExtent,
fromUserResolution,
getPointResolution,
get as getProjection,
getTransform,
@@ -21,6 +22,7 @@ import {
toLonLat,
toUserCoordinate,
toUserExtent,
toUserResolution,
transform,
transformExtent,
useGeographic,
@@ -151,6 +153,36 @@ describe('ol/proj.js', function () {
});
});
describe('fromUserResolution()', function () {
it("adjusts a resolution for the user projection's units", function () {
useGeographic();
const user = 1 / METERS_PER_UNIT['degrees'];
const resolution = fromUserResolution(user, 'EPSG:3857');
expect(resolution).to.roughlyEqual(1, 1e-9);
});
it('returns the original if no user projection is set', function () {
const user = METERS_PER_UNIT['meters'];
const resolution = fromUserResolution(user, 'EPSG:3857');
expect(resolution).to.eql(user);
});
});
describe('toUserResolution()', function () {
it("adjusts a resolution for the user projection's units", function () {
useGeographic();
const dest = 1;
const resolution = toUserResolution(dest, 'EPSG:3857');
expect(resolution).to.eql(1 / METERS_PER_UNIT['degrees']);
});
it('returns the original if no user projection is set', function () {
const dest = METERS_PER_UNIT['degrees'];
const resolution = toUserResolution(dest, 'EPSG:3857');
expect(resolution).to.eql(dest);
});
});
describe('toLonLat()', function () {
const cases = [
{