Get simplified transformed geometry and load features in user projection

This commit is contained in:
Tim Schaub
2019-09-24 13:32:47 +02:00
parent 3c38a38570
commit 30cbbfea9d
9 changed files with 214 additions and 24 deletions

View File

@@ -2,6 +2,8 @@
* @module ol/functions
*/
import {equals as arrayEquals} from './array.js';
/**
* Always returns true.
* @returns {boolean} true.
@@ -24,3 +26,36 @@ export function FALSE() {
* @return {void} Nothing.
*/
export function VOID() {}
/**
* Wrap a function in another function that remembers the last return. If the
* returned function is called twice in a row with the same arguments and the same
* this object, it will return the value from the first call in the second call.
*
* @template ReturnType
* @template ThisType
* @param {function(this:ThisType, ...any):ReturnType} fn The function to memoize.
* @return {function(this:ThisType, ...any):ReturnType} The memoized function.
*/
export function memoizeOne(fn) {
let called = false;
/** @type ReturnType */
let lastResult;
/** @type Array<any> */
let lastArgs;
let lastThis;
return function() {
const nextArgs = Array.prototype.slice.call(arguments);
if (!called || this !== lastThis || !arrayEquals(nextArgs, lastArgs)) {
called = true;
lastThis = this;
lastArgs = nextArgs;
lastResult = fn.apply(this, arguments);
}
return lastResult;
};
}

View File

@@ -5,10 +5,10 @@ import {abstract} from '../util.js';
import BaseObject from '../Object.js';
import {createEmpty, getHeight, returnOrUpdate} from '../extent.js';
import {transform2D} from './flat/transform.js';
import {get as getProjection, getTransform} from '../proj.js';
import {get as getProjection, getTransform, getTransformFromProjections} from '../proj.js';
import Units from '../proj/Units.js';
import {create as createTransform, compose as composeTransform} from '../transform.js';
import {memoizeOne} from '../functions.js';
/**
* @type {import("../transform.js").Transform}
@@ -63,6 +63,24 @@ class Geometry extends BaseObject {
*/
this.simplifiedGeometryRevision = 0;
/**
* Get a transformed and simplified version of the geometry.
* @abstract
* @param {number} squaredTolerance Squared tolerance.
* @param {import("../proj/Projection.js").default} sourceProjection The source projection.
* @param {import("../proj/Projection.js").default} destProjection The destination projection.
* @return {Geometry} Simplified geometry.
*/
this.simplifyTransformed = memoizeOne(function(squaredTolerance, sourceProjection, destProjection) {
if (!sourceProjection || !destProjection) {
return this.getSimplifiedGeometry(squaredTolerance);
}
const transform = getTransformFromProjections(sourceProjection, destProjection);
const clone = this.clone();
clone.applyTransform(transform);
return clone.getSimplifiedGeometry(squaredTolerance);
});
}
/**

View File

@@ -200,6 +200,18 @@ class RenderFeature {
return this;
}
/**
* Get a transformed and simplified version of the geometry.
* @abstract
* @param {number} squaredTolerance Squared tolerance.
* @param {import("../proj/Projection.js").default} sourceProjection The source projection.
* @param {import("../proj/Projection.js").default} destProjection The destination projection.
* @return {RenderFeature} Simplified geometry.
*/
simplifyTransformed(squaredTolerance, sourceProjection, destProjection) {
return this;
}
/**
* Get the feature properties.
* @return {Object<string, *>} Feature properties.

View File

@@ -4,7 +4,7 @@
import {getUid} from '../../util.js';
import ViewHint from '../../ViewHint.js';
import {buffer, createEmpty, containsExtent, getWidth, intersects as intersectsExtent} from '../../extent.js';
import {fromUserExtent} from '../../proj.js';
import {fromUserExtent, toUserExtent, getUserProjection} from '../../proj.js';
import CanvasBuilderGroup from '../../render/canvas/BuilderGroup.js';
import ExecutorGroup, {replayDeclutter} from '../../render/canvas/ExecutorGroup.js';
import CanvasLayerRenderer from './Layer.js';
@@ -304,7 +304,12 @@ class CanvasVectorLayerRenderer extends CanvasLayerRenderer {
getRenderTolerance(resolution, pixelRatio), extent, resolution,
pixelRatio, vectorLayer.getDeclutter());
vectorSource.loadFeatures(extent, resolution, projection);
const userProjection = getUserProjection();
if (userProjection) {
vectorSource.loadFeatures(toUserExtent(extent, projection), resolution, userProjection);
} else {
vectorSource.loadFeatures(extent, resolution, projection);
}
const squaredTolerance = getSquaredRenderTolerance(resolution, pixelRatio);
@@ -319,15 +324,16 @@ class CanvasVectorLayerRenderer extends CanvasLayerRenderer {
styles = styleFunction(feature, resolution);
}
if (styles) {
const dirty = this.renderFeature(feature, squaredTolerance, styles, replayGroup);
const dirty = this.renderFeature(feature, squaredTolerance, styles, replayGroup, projection);
this.dirty_ = this.dirty_ || dirty;
}
}.bind(this);
const userExtent = toUserExtent(extent, projection);
if (vectorLayerRenderOrder) {
/** @type {Array<import("../../Feature.js").default>} */
const features = [];
vectorSource.forEachFeatureInExtent(extent,
vectorSource.forEachFeatureInExtent(userExtent,
/**
* @param {import("../../Feature.js").default} feature Feature.
*/
@@ -339,7 +345,7 @@ class CanvasVectorLayerRenderer extends CanvasLayerRenderer {
render(features[i]);
}
} else {
vectorSource.forEachFeatureInExtent(extent, render);
vectorSource.forEachFeatureInExtent(userExtent, render);
}
const replayGroupInstructions = replayGroup.finish();
@@ -362,9 +368,10 @@ class CanvasVectorLayerRenderer extends CanvasLayerRenderer {
* @param {number} squaredTolerance Squared render tolerance.
* @param {import("../../style/Style.js").default|Array<import("../../style/Style.js").default>} styles The style or array of styles.
* @param {import("../../render/canvas/BuilderGroup.js").default} builderGroup Builder group.
* @param {import("../../proj/Projection.js").default} projection The view projection.
* @return {boolean} `true` if an image is loading.
*/
renderFeature(feature, squaredTolerance, styles, builderGroup) {
renderFeature(feature, squaredTolerance, styles, builderGroup, projection) {
if (!styles) {
return false;
}
@@ -373,12 +380,12 @@ class CanvasVectorLayerRenderer extends CanvasLayerRenderer {
for (let i = 0, ii = styles.length; i < ii; ++i) {
loading = renderFeature(
builderGroup, feature, styles[i], squaredTolerance,
this.boundHandleStyleImageChange_) || loading;
this.boundHandleStyleImageChange_, projection) || loading;
}
} else {
loading = renderFeature(
builderGroup, feature, styles, squaredTolerance,
this.boundHandleStyleImageChange_);
this.boundHandleStyleImageChange_, projection);
}
return loading;
}

View File

@@ -5,6 +5,7 @@ import {getUid} from '../util.js';
import ImageState from '../ImageState.js';
import GeometryType from '../geom/GeometryType.js';
import BuilderType from '../render/canvas/BuilderType.js';
import {getUserProjection} from '../proj.js';
/**
@@ -92,10 +93,11 @@ function renderCircleGeometry(builderGroup, geometry, style, feature) {
* @param {import("../style/Style.js").default} style Style.
* @param {number} squaredTolerance Squared tolerance.
* @param {function(import("../events/Event.js").default): void} listener Listener function.
* @param {import("../proj/Projection.js").default} projection The view projection.
* @return {boolean} `true` if style is loading.
* @template T
*/
export function renderFeature(replayGroup, feature, style, squaredTolerance, listener) {
export function renderFeature(replayGroup, feature, style, squaredTolerance, listener, projection) {
let loading = false;
const imageStyle = style.getImage();
if (imageStyle) {
@@ -111,7 +113,7 @@ export function renderFeature(replayGroup, feature, style, squaredTolerance, lis
loading = true;
}
}
renderFeatureInternal(replayGroup, feature, style, squaredTolerance);
renderFeatureInternal(replayGroup, feature, style, squaredTolerance, projection);
return loading;
}
@@ -122,13 +124,14 @@ export function renderFeature(replayGroup, feature, style, squaredTolerance, lis
* @param {import("../Feature.js").FeatureLike} feature Feature.
* @param {import("../style/Style.js").default} style Style.
* @param {number} squaredTolerance Squared tolerance.
* @param {import("../proj/Projection.js").default} projection The view projection.
*/
function renderFeatureInternal(replayGroup, feature, style, squaredTolerance) {
function renderFeatureInternal(replayGroup, feature, style, squaredTolerance, projection) {
const geometry = style.getGeometryFunction()(feature);
if (!geometry) {
return;
}
const simplifiedGeometry = geometry.getSimplifiedGeometry(squaredTolerance);
const simplifiedGeometry = geometry.simplifyTransformed(squaredTolerance, getUserProjection(), projection);
const renderer = style.getRenderer();
if (renderer) {
renderGeometry(replayGroup, simplifiedGeometry, style, feature);