Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update featuresGeojsonChange event to match new geojsonChange behavior #479

Merged
merged 2 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,8 @@ <h1 style="color:red;font-size:16px;">
ariaLabelOlFixedOverlay="Interactive example map"
zoom="20"
maxZoom="23"
basemap="MapboxSatellite"
drawMode
drawMany
drawType="Polygon"
osCopyright="© Crown copyright and database rights 2024 OS (0)100024857"
osProxyEndpoint="https://api.editor.planx.dev/proxy/ordnance-survey"
/>
Expand Down
58 changes: 33 additions & 25 deletions src/components/my-map/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import styles from "./styles.scss?inline";
import {
AreaUnitEnum,
fitToData,
formatArea,
calculateArea,
hexToRgba,
makeGeoJSON,
} from "./utils";
Expand Down Expand Up @@ -234,6 +234,9 @@ export class MyMap extends LitElement {
@property({ type: Boolean })
staticMode = false;

/**
* @deprecated - both `area.squareMetres` & `area.hectares` are calculated by default now in applicable `geojsonChange` events
*/
@property({ type: String })
areaUnit: AreaUnitEnum = "m2";

Expand Down Expand Up @@ -389,16 +392,7 @@ export class MyMap extends LitElement {

if (this.drawMode) {
drawingSource.clear();

this.dispatch("geojsonChange", {});

if (this.drawType === "Polygon") {
this.dispatch(
"areaChange",
`0 ${this.areaUnit === "m2" ? "m²" : this.areaUnit}`,
);
}

map.addInteraction(draw);
map.addInteraction(snap);
}
Expand Down Expand Up @@ -507,7 +501,7 @@ export class MyMap extends LitElement {
// log total area of static geojson data (assumes single polygon for now)
const data = geojsonSource.getFeatures()[0].getGeometry();
if (data) {
this.dispatch("geojsonDataArea", formatArea(data, this.areaUnit));
this.dispatch("geojsonDataArea", calculateArea(data, this.areaUnit));
}
}

Expand Down Expand Up @@ -541,7 +535,7 @@ export class MyMap extends LitElement {
}
map.addInteraction(modify);

// XXX: snap must be added after draw and modify
// Snap must be added after draw and modify
map.addInteraction(snap);

// 'change' listens for 'drawend' and modifications
Expand All @@ -555,10 +549,15 @@ export class MyMap extends LitElement {

if (sketches.length > 0) {
if (this.drawType === "Polygon") {
// Calculate the "area" and set on geojson `properties`
sketches.forEach((sketch) => {
const sketchGeom = sketch.getGeometry();
if (sketchGeom) {
sketch.set("area", formatArea(sketchGeom, this.areaUnit));
sketch.set(
"area.squareMetres",
calculateArea(sketchGeom, "m2"),
);
sketch.set("area.hectares", calculateArea(sketchGeom, "ha"));
Comment on lines +556 to +560
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fyi: There's a setProperties() that allows multiple values to be set at once. No better or worse, just sharing!

}
});
}
Expand Down Expand Up @@ -639,29 +638,38 @@ export class MyMap extends LitElement {
);
map.addLayer(outlineLayer);

// ensure getFeaturesAtPoint has fetched successfully
// Ensure getFeaturesAtPoint has fetched successfully
outlineSource.on("change", () => {
if (
outlineSource.getState() === "ready" &&
outlineSource.getFeatures().length > 0
) {
// fit map to extent of features
// Fit map to extent of features
fitToData(map, outlineSource, this.featureBuffer);

// write the geojson representation of the feature or merged features
// Calculate the total area of the feature or merged features and set on geojson `properties`
const osFeatures = outlineSource.getFeatures();
if (osFeatures.length > 0) {
osFeatures.forEach((osFeature) => {
const osFeatureGeom = osFeature.getGeometry();
if (osFeatureGeom) {
osFeature.set(
"area.squareMetres",
calculateArea(osFeatureGeom, "m2"),
);
osFeature.set(
"area.hectares",
calculateArea(osFeatureGeom, "ha"),
);
}
});
}

// Dispatch the geojson of the feature or merged features
this.dispatch("featuresGeojsonChange", {
"EPSG:3857": makeGeoJSON(outlineSource, "EPSG:3857"),
"EPSG:27700": makeGeoJSON(outlineSource, "EPSG:27700"),
});

// calculate the total area of the feature or merged features
const data = outlineSource.getFeatures()[0].getGeometry();
if (data) {
this.dispatch(
"featuresAreaChange",
formatArea(data, this.areaUnit),
);
}
}
});
}
Expand Down
23 changes: 12 additions & 11 deletions src/components/my-map/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,26 @@ import { ProjectionEnum } from "./projections";
export type AreaUnitEnum = "m2" | "ha";

/**
* Calculate & format the area of a polygon
* Calculate the area of a polygon
* @param polygon
* @param unit - defaults to square metres ("m2"), or supports "ha" for hectares
* @returns - the total area formatted with unit as a string
* @returns - the total area
*/
export function formatArea(polygon: Geometry, unit: AreaUnitEnum) {
export function calculateArea(
polygon: Geometry,
unit: AreaUnitEnum = "m2",
): number {
const area = getArea(polygon);

const squareMetres = Math.round(area * 100) / 100;
const hectares = squareMetres / 10000; // 0.0001 hectares in 1 square metre
const hectares = squareMetres / 10000; // 1 square metre = 0.0001 hectare

let output;
if (unit === "m2") {
output = squareMetres + " m²";
} else if (unit === "ha") {
output = hectares + " ha";
switch (unit) {
case "m2":
return squareMetres;
case "ha":
return hectares;
Comment on lines +30 to +34
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

}

return output;
}

/**
Expand Down