Skip to content

Commit

Permalink
feat: set labelingInfo via metadata (#1015)
Browse files Browse the repository at this point in the history
* feat: set labelingInfo via metadata

* chore: increase test coverage
  • Loading branch information
rgwozdz authored May 22, 2024
1 parent 7e47717 commit 11f19a7
Show file tree
Hide file tree
Showing 13 changed files with 260 additions and 217 deletions.
5 changes: 5 additions & 0 deletions .changeset/violet-fans-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@koopjs/featureserver": minor
---

- set labelingInfo via metadata
22 changes: 2 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions packages/featureserver/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion packages/featureserver/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"@esri/proj-codes": "^3.3.0",
"@koopjs/logger": "5.0.0",
"@koopjs/winnow": "5.0.2",
"@terraformer/spatial": "^2.2.1",
"@turf/envelope": "^6.5.0",
"chroma-js": "^2.4.2",
"esri-extent": "^1.1.3",
"geojson-validation": "^1.0.2",
Expand Down
50 changes: 33 additions & 17 deletions packages/featureserver/src/helpers/feature-layer-metadata.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const _ = require('lodash');
const TableLayerMetadata = require('./table-layer-metadata');
const { PointRenderer, LineRenderer, PolygonRenderer } = require('./renderers');
const { calculateBounds } = require('@terraformer/spatial');
const envelope = require('@turf/envelope');
const logManager = require('../log-manager');
const getSpatialReference = require('./get-spatial-reference');
const getGeometryTypeFromGeojson = require('./get-geometry-type-from-geojson');
Expand All @@ -22,47 +22,59 @@ class FeatureLayerMetadata extends TableLayerMetadata {
return this;
}

mixinOverrides(geojson = {}, options = {}) {
mixinOverrides(geojson, options = {}) {
super.mixinOverrides(geojson, options);

const { renderer, extent, inputCrs, sourceSR, capabilities = {} } = options;
const { renderer, labelingInfo, extent, inputCrs, sourceSR, capabilities = {} } = options;

this.geometryType = getGeometryTypeFromGeojson({ ...geojson, ...options });

this.supportsCoordinatesQuantization = !!capabilities.quantization;

this._setExtent(geojson, { inputCrs, sourceSR, extent });
this.#_setExtent(geojson, { inputCrs, sourceSR, extent });

this._setRenderer(renderer);
this.#_setRenderer(renderer);

this._setDirectOverrides(options);
this.#_setLabelingInfo(labelingInfo);

this.#_setDirectOverrides(options);

return this;
}

_setExtent(geojson, options) {
#_setExtent(geojson, options) {
const extent = getLayerExtent(geojson, options);
if (extent) {
this.extent = extent;
}
}

_setRenderer(renderer) {
#_setRenderer(renderer) {
if (renderer) {
this.drawingInfo.renderer = renderer;
return;
}

if (this.geometryType === 'esriGeometryPolygon') {
this.drawingInfo.renderer = new PolygonRenderer();
} else if (this.geometryType === 'esriGeometryPolyline') {
return;
}

if (this.geometryType === 'esriGeometryPolyline') {
this.drawingInfo.renderer = new LineRenderer();
} else {
this.drawingInfo.renderer = new PointRenderer();
return;
}

this.drawingInfo.renderer = new PointRenderer();
}

_setDirectOverrides(options) {
#_setLabelingInfo(labelingInfo) {
if (labelingInfo) {
this.drawingInfo.labelingInfo = labelingInfo;
}
}

#_setDirectOverrides(options) {
super._setDirectOverrides(options);
const { minScale, maxScale } = options;

Expand All @@ -74,10 +86,7 @@ class FeatureLayerMetadata extends TableLayerMetadata {
}

function getLayerExtent(geojson, options) {
const spatialReference = getSpatialReference(geojson, options) || {
wkid: 4326,
latestWkid: 4326,
};
const spatialReference = getSpatialReference(geojson, options);

const { extent } = options;

Expand All @@ -94,8 +103,15 @@ function calculateExtentFromFeatures(geojson, spatialReference) {
}

try {
const [xmin, ymin, xmax, ymax] = calculateBounds(geojson);
const { bbox } = envelope(geojson);

bbox.forEach((coordinate) => {
if (!isFinite(coordinate)) {
throw new Error(`Feature does not contain valid geometry`);
}
});

const [xmin, ymin, xmax, ymax] = bbox;
return {
xmin,
xmax,
Expand Down
Loading

0 comments on commit 11f19a7

Please sign in to comment.