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

feat: dispatch geojsonChange events in two projections #255

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 15 additions & 13 deletions src/components/my-map/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ import {
pointsSource,
} from "./snapping";
import styles from "./styles.scss?inline";
import { AreaUnitEnum, fitToData, formatArea, hexToRgba } from "./utils";
import {
AreaUnitEnum,
fitToData,
formatArea,
hexToRgba,
makeGeoJSON,
} from "./utils";

type MarkerImageEnum = "circle" | "pin";
type ResetControlImageEnum = "unicode" | "trash";
Expand Down Expand Up @@ -391,12 +397,10 @@ export class MyMap extends LitElement {
if (sketches.length > 0) {
const lastSketchGeom = last(sketches)?.getGeometry();

this.dispatch(
"geojsonChange",
new GeoJSON().writeFeaturesObject(sketches, {
featureProjection: "EPSG:3857",
})
);
this.dispatch("geojsonChange", {
webMercator: makeGeoJSON(sketches, "EPSG:3857"),
britishNationalGrid: makeGeoJSON(sketches, "EPSG:27700"),
});

if (lastSketchGeom && this.drawType === "Polygon") {
this.dispatch(
Expand Down Expand Up @@ -487,12 +491,10 @@ export class MyMap extends LitElement {
fitToData(map, outlineSource, this.featureBuffer);

// write the geojson representation of the feature or merged features
this.dispatch(
"featuresGeojsonChange",
new GeoJSON().writeFeaturesObject(outlineSource.getFeatures(), {
featureProjection: "EPSG:3857",
})
);
this.dispatch("featuresGeojsonChange", {
webMercator: makeGeoJSON(outlineSource, "EPSG:3857"),
britishNationalGrid: makeGeoJSON(outlineSource, "EPSG:27700"),
});

// calculate the total area of the feature or merged features
const data = outlineSource.getFeatures()[0].getGeometry();
Expand Down
23 changes: 23 additions & 0 deletions src/components/my-map/utils.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { asArray, asString } from "ol/color";
import { buffer } from "ol/extent";
import { GeoJSON } from "ol/format";
import { GeoJSONObject } from "ol/format/GeoJSON";
import Geometry from "ol/geom/Geometry";
import { Feature } from "ol/index";
import Map from "ol/Map";
import { Vector } from "ol/source";
import VectorSource from "ol/source/Vector";
import { getArea } from "ol/sphere";
import { ProjectionEnum } from "./projections";

export type AreaUnitEnum = "m2" | "ha";

Expand Down Expand Up @@ -55,3 +60,21 @@ export function hexToRgba(hexColor: string, alpha: number) {
const [r, g, b] = Array.from(asArray(hexColor));
return asString([r, g, b, alpha]);
}

/**
* Generate a geojson object from a vector source or an array of features
* @param source
* @param projection
* @returns
*/
export function makeGeoJSON(
source: VectorSource<Geometry> | Feature<Geometry>[],
projection: ProjectionEnum
): GeoJSONObject {
return new GeoJSON().writeFeaturesObject(
source instanceof VectorSource ? source.getFeatures() : source,
{
featureProjection: projection,
}
);
}