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

@@ -1,22 +1,33 @@
import {useGeographic} from '../src/ol/proj.js'; import {useGeographic} from '../src/ol/proj.js';
import Map from '../src/ol/Map.js'; import {Map, View, Feature} from '../src/ol/index.js';
import View from '../src/ol/View.js'; import {Point} from '../src/ol/geom.js';
import TileLayer from '../src/ol/layer/Tile.js'; import {Vector as VectorLayer, Tile as TileLayer} from '../src/ol/layer.js';
import OSM from '../src/ol/source/OSM.js'; import {OSM, Vector as VectorSource} from '../src/ol/source.js';
useGeographic(); useGeographic();
const place = [-110, 45];
const point = new Point(place);
const map = new Map({ const map = new Map({
target: 'map',
view: new View({
center: place,
zoom: 8
}),
layers: [ layers: [
new TileLayer({ new TileLayer({
source: new OSM() source: new OSM()
}),
new VectorLayer({
source: new VectorSource({
features: [
new Feature(point)
]
}) })
],
target: 'map',
view: new View({
center: [-110, 45],
zoom: 8
}) })
]
}); });
const info = document.getElementById('info'); const info = document.getElementById('info');

Binary file not shown.

After

Width:  |  Height:  |  Size: 118 KiB

View File

@@ -0,0 +1,43 @@
import {Map, View, Feature} from '../../../src/ol/index.js';
import {Point} from '../../../src/ol/geom.js';
import {Tile as TileLayer, Vector as VectorLayer} from '../../../src/ol/layer.js';
import {useGeographic} from '../../../src/ol/proj.js';
import {XYZ, Vector as VectorSource} from '../../../src/ol/source.js';
import {Style, Circle, Fill} from '../../../src/ol/style.js';
useGeographic();
const center = [8.6, 50.1];
const point = new Point(center);
new Map({
layers: [
new TileLayer({
source: new XYZ({
url: '/data/tiles/satellite/{z}/{x}/{y}.jpg',
transition: 0
})
}),
new VectorLayer({
source: new VectorSource({
features: [
new Feature(point)
]
}),
style: new Style({
image: new Circle({
radius: 5,
fill: new Fill({color: 'red'})
})
})
})
],
target: 'map',
view: new View({
center: center,
zoom: 3
})
});
render();

View File

@@ -2,6 +2,8 @@
* @module ol/functions * @module ol/functions
*/ */
import {equals as arrayEquals} from './array.js';
/** /**
* Always returns true. * Always returns true.
* @returns {boolean} true. * @returns {boolean} true.
@@ -24,3 +26,36 @@ export function FALSE() {
* @return {void} Nothing. * @return {void} Nothing.
*/ */
export function VOID() {} 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 BaseObject from '../Object.js';
import {createEmpty, getHeight, returnOrUpdate} from '../extent.js'; import {createEmpty, getHeight, returnOrUpdate} from '../extent.js';
import {transform2D} from './flat/transform.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 Units from '../proj/Units.js';
import {create as createTransform, compose as composeTransform} from '../transform.js'; import {create as createTransform, compose as composeTransform} from '../transform.js';
import {memoizeOne} from '../functions.js';
/** /**
* @type {import("../transform.js").Transform} * @type {import("../transform.js").Transform}
@@ -63,6 +63,24 @@ class Geometry extends BaseObject {
*/ */
this.simplifiedGeometryRevision = 0; 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; 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. * Get the feature properties.
* @return {Object<string, *>} Feature properties. * @return {Object<string, *>} Feature properties.

View File

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

View File

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

View File

@@ -0,0 +1,61 @@
import {memoizeOne} from '../../../src/ol/functions.js';
describe('ol/functions', function() {
describe('memoizeOne()', function() {
it('returns the result from the first call when called a second time with the same args', function() {
const arg1 = {};
const arg2 = {};
const arg3 = {};
function call(a1, a2, a3) {
return {};
}
const memoized = memoizeOne(call);
const result = memoized(arg1, arg2, arg3);
expect(memoized(arg1, arg2, arg3)).to.be(result);
});
it('returns the result from the first call when called a second time with the same this object', function() {
const arg1 = {};
const arg2 = {};
const arg3 = {};
function call(a1, a2, a3) {
return {};
}
const memoized = memoizeOne(call);
const thisObj = {};
const result = memoized.call(thisObj, arg1, arg2, arg3);
expect(memoized.call(thisObj, arg1, arg2, arg3)).to.be(result);
});
it('returns a different result when called a second time with the different args', function() {
const arg1 = {};
const arg2 = {};
const arg3 = {};
function call(a1, a2, a3) {
return {};
}
const memoized = memoizeOne(call);
const result = memoized(arg1, arg2, arg3);
expect(memoized(arg3, arg2, arg1)).not.to.be(result);
});
it('returns a different result when called a second time with a different this object', function() {
const arg1 = {};
const arg2 = {};
const arg3 = {};
function call(a1, a2, a3) {
return {};
}
const firstThis = {};
const secondThis = {};
const memoized = memoizeOne(call);
const result = memoized.call(firstThis, arg1, arg2, arg3);
expect(memoized.call(secondThis, arg1, arg2, arg3)).not.to.be(result);
});
});
});