Skip to content
Merged
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
34 changes: 21 additions & 13 deletions src/components/my-map/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ export class MyMap extends LitElement {

@property({ type: Object })
drawGeojsonData = {
type: "Feature",
geometry: {},
type: "FeatureCollection",
features: [],
};

@property({ type: String })
Expand Down Expand Up @@ -460,23 +460,23 @@ export class MyMap extends LitElement {
map.getViewport().style.cursor = "grab";
});

// display static geojson if features are provided
// Display static GeoJSON if features are provided
const geojsonSource = new VectorSource();

if (this.geojsonData.type === "FeatureCollection") {
let features = new GeoJSON().readFeatures(this.geojsonData, {
featureProjection: "EPSG:3857",
});
geojsonSource.addFeatures(features);
geojsonSource.setAttributions(this.geojsonDataCopyright);
} else if (this.geojsonData.type === "Feature") {
let feature = new GeoJSON().readFeature(this.geojsonData, {
featureProjection: "EPSG:3857",
});
geojsonSource.addFeature(feature);
Copy link
Contributor

Choose a reason for hiding this comment

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

Took me a second to realise the differences between these parts. Is the need for two different functions relate to one accessing the Features array of a FeatureCollection and the other just taking the Feature as an object, or is there another reason for having two different functions / if clauses?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes exactly OpenLayers provides different methods depending if GeoJSON is single Feature or a FeatureCollection, and we want to keep our public-facing prop flexible to accepting eithe, therefore we do this conditional check !

geojsonSource.setAttributions(this.geojsonDataCopyright);
}

geojsonSource.setAttributions(this.geojsonDataCopyright);

const geojsonLayer = new VectorLayer({
source: geojsonSource,
style: function (this: MyMap, feature: FeatureLike) {
Expand Down Expand Up @@ -521,26 +521,34 @@ export class MyMap extends LitElement {
this.drawMany,
);
if (this.drawMode) {
// make sure drawingSource is cleared upfront, even if drawGeojsonData is provided
// Clear drawingSource to begin, even if drawGeojsonData is provided
drawingSource.clear();

// load an initial polygon into the drawing source if provided, or start from an empty drawing source
const loadInitialDrawing =
Object.keys(this.drawGeojsonData.geometry).length > 0;

if (loadInitialDrawing) {
// Load initial drawing (or drawings if drawMany) into the drawing source
if (this.drawGeojsonData.type === "FeatureCollection") {
let features = new GeoJSON().readFeatures(this.drawGeojsonData, {
featureProjection: "EPSG:3857",
});
drawingSource.addFeatures(features);
} else if (this.drawGeojsonData.type === "Feature") {
let feature = new GeoJSON().readFeature(this.drawGeojsonData, {
featureProjection: "EPSG:3857",
});
drawingSource.addFeature(feature);
drawingSource.setAttributions(this.drawGeojsonDataCopyright);
}

drawingSource.setAttributions(this.drawGeojsonDataCopyright);
if (drawingSource.getFeatures().length > 0) {
fitToData(map, drawingSource, this.drawGeojsonDataBuffer);
}

map.addLayer(drawingLayer);
drawingLayer.setZIndex(1001); // Ensure drawing layer is on top of Mapbox Satellite style

if (!loadInitialDrawing) {
// If exactly one drawGeojsonData feature was initially provided, and we are NOT supporting drawMany, only add modify interaction to map, else add draw & modify
const modifyOnly =
drawingSource.getFeatures().length === 1 && !this.drawMany;
if (!modifyOnly) {
map.addInteraction(draw);
}
map.addInteraction(modify);
Expand Down