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

Refactor add layer operations #222

Merged
merged 4 commits into from
Jan 31, 2023
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
314 changes: 84 additions & 230 deletions public/model/documentLayerFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ import { parse } from 'wellknown';
import { DocumentLayerSpecification } from './mapLayerType';
import { convertGeoPointToGeoJSON, isGeoJSON } from '../utils/geo_formater';
import { getMaplibreBeforeLayerId, layerExistInMbSource } from './layersFunctions';
import {
addCircleLayer,
addLineLayer,
addPolygonLayer,
updateCircleLayer,
updateLineLayer,
updatePolygonLayer,
} from './map/layer_operations';

interface MaplibreRef {
current: Maplibre | null;
Expand All @@ -23,23 +31,6 @@ const openSearchGeoJSONMap = new Map<string, string>([
['geometrycollection', 'GeometryCollection'],
]);

const buildLayerSuffix = (layerId: string, mapLibreType: string) => {
if (mapLibreType.toLowerCase() === 'circle') {
return layerId;
}
if (mapLibreType.toLowerCase() === 'line') {
return layerId + '-line';
}
if (mapLibreType.toLowerCase() === 'fill') {
return layerId + '-fill';
}
if (mapLibreType.toLowerCase() === 'fill-outline') {
return layerId + '-outline';
}
// if unknown type is found, use layerId as default
return layerId;
};

const getFieldValue = (data: any, name: string) => {
if (!name) {
return null;
Expand Down Expand Up @@ -128,226 +119,62 @@ const addNewLayer = (
beforeLayerId: string | undefined
) => {
const maplibreInstance = maplibreRef.current;
if (!maplibreInstance) {
return;
}
const mbLayerBeforeId = getMaplibreBeforeLayerId(layerConfig, maplibreRef, beforeLayerId);
const addLineLayer = (
documentLayerConfig: DocumentLayerSpecification,
beforeId: string | undefined
) => {
const lineLayerId = buildLayerSuffix(documentLayerConfig.id, 'line');
maplibreInstance?.addLayer(
{
id: lineLayerId,
type: 'line',
source: documentLayerConfig.id,
filter: ['==', '$type', 'LineString'],
paint: {
'line-color': documentLayerConfig.style?.fillColor,
'line-opacity': documentLayerConfig.opacity / 100,
'line-width': documentLayerConfig.style?.borderThickness,
},
},
beforeId
);
maplibreInstance?.setLayoutProperty(lineLayerId, 'visibility', documentLayerConfig.visibility);
maplibreInstance?.setLayerZoomRange(
lineLayerId,
documentLayerConfig.zoomRange[0],
documentLayerConfig.zoomRange[1]
);
};

const addCircleLayer = (
documentLayerConfig: DocumentLayerSpecification,
beforeId: string | undefined
) => {
const circleLayerId = buildLayerSuffix(documentLayerConfig.id, 'circle');
maplibreInstance?.addLayer(
{
id: circleLayerId,
type: 'circle',
source: layerConfig.id,
filter: ['==', '$type', 'Point'],
paint: {
'circle-radius': documentLayerConfig.style?.markerSize,
'circle-color': documentLayerConfig.style?.fillColor,
'circle-opacity': documentLayerConfig.opacity / 100,
'circle-stroke-width': documentLayerConfig.style?.borderThickness,
'circle-stroke-color': documentLayerConfig.style?.borderColor,
},
},
beforeId
);
maplibreInstance?.setLayoutProperty(
circleLayerId,
'visibility',
documentLayerConfig.visibility
);
maplibreInstance?.setLayerZoomRange(
circleLayerId,
documentLayerConfig.zoomRange[0],
documentLayerConfig.zoomRange[1]
);
};

const addFillLayer = (
documentLayerConfig: DocumentLayerSpecification,
beforeId: string | undefined
) => {
const fillLayerId = buildLayerSuffix(documentLayerConfig.id, 'fill');
maplibreInstance?.addLayer(
const source = getLayerSource(data, layerConfig);
maplibreInstance.addSource(layerConfig.id, {
type: 'geojson',
data: source,
});
addCircleLayer(
maplibreInstance,
{
fillColor: layerConfig.style?.fillColor,
maxZoom: layerConfig.zoomRange[1],
minZoom: layerConfig.zoomRange[0],
opacity: layerConfig.opacity,
outlineColor: layerConfig.style?.borderColor,
radius: layerConfig.style?.markerSize,
sourceId: layerConfig.id,
visibility: layerConfig.visibility,
width: layerConfig.style?.borderThickness,
},
mbLayerBeforeId
);
const geoFieldType = getGeoFieldType(layerConfig);
if (geoFieldType === 'geo_shape') {
addLineLayer(
maplibreInstance,
{
id: fillLayerId,
type: 'fill',
source: layerConfig.id,
filter: ['==', '$type', 'Polygon'],
paint: {
'fill-color': documentLayerConfig.style?.fillColor,
'fill-opacity': documentLayerConfig.opacity / 100,
},
width: layerConfig.style?.borderThickness,
color: layerConfig.style?.fillColor,
maxZoom: layerConfig.zoomRange[1],
minZoom: layerConfig.zoomRange[0],
opacity: layerConfig.opacity,
sourceId: layerConfig.id,
visibility: layerConfig.visibility,
},
beforeId
);
maplibreInstance?.setLayoutProperty(fillLayerId, 'visibility', documentLayerConfig.visibility);
maplibreInstance?.setLayerZoomRange(
fillLayerId,
documentLayerConfig.zoomRange[0],
documentLayerConfig.zoomRange[1]
mbLayerBeforeId
);
// Due to limitations on WebGL, fill can't render outlines with width wider than 1,
// so we have to create another style layer with type=line to apply width.
const outlineId = buildLayerSuffix(documentLayerConfig.id, 'fill-outline');
maplibreInstance?.addLayer(
addPolygonLayer(
maplibreInstance,
{
id: outlineId,
type: 'line',
source: layerConfig.id,
filter: ['==', '$type', 'Polygon'],
paint: {
'line-color': layerConfig.style?.borderColor,
'line-opacity': layerConfig.opacity / 100,
'line-width': layerConfig.style?.borderThickness,
},
width: layerConfig.style?.borderThickness,
fillColor: layerConfig.style?.fillColor,
maxZoom: layerConfig.zoomRange[1],
minZoom: layerConfig.zoomRange[0],
opacity: layerConfig.opacity,
sourceId: layerConfig.id,
outlineColor: layerConfig.style?.borderColor,
visibility: layerConfig.visibility,
},
beforeId
mbLayerBeforeId
);
maplibreInstance?.setLayoutProperty(outlineId, 'visibility', layerConfig.visibility);
maplibreInstance?.setLayerZoomRange(
outlineId,
documentLayerConfig.zoomRange[0],
documentLayerConfig.zoomRange[1]
);
};

if (maplibreInstance) {
const source = getLayerSource(data, layerConfig);
maplibreInstance.addSource(layerConfig.id, {
type: 'geojson',
data: source,
});
addCircleLayer(layerConfig, mbLayerBeforeId);
const geoFieldType = getGeoFieldType(layerConfig);
if (geoFieldType === 'geo_shape') {
addLineLayer(layerConfig, mbLayerBeforeId);
addFillLayer(layerConfig, mbLayerBeforeId);
}
}
};

const updateCircleLayer = (
maplibreInstance: Maplibre,
documentLayerConfig: DocumentLayerSpecification
) => {
const circleLayerId = buildLayerSuffix(documentLayerConfig.id, 'circle');
const circleLayerStyle = documentLayerConfig.style;
maplibreInstance?.setLayerZoomRange(
circleLayerId,
documentLayerConfig.zoomRange[0],
documentLayerConfig.zoomRange[1]
);
maplibreInstance?.setPaintProperty(
circleLayerId,
'circle-opacity',
documentLayerConfig.opacity / 100
);
maplibreInstance?.setPaintProperty(circleLayerId, 'circle-color', circleLayerStyle?.fillColor);
maplibreInstance?.setPaintProperty(
circleLayerId,
'circle-stroke-color',
circleLayerStyle?.borderColor
);
maplibreInstance?.setPaintProperty(
circleLayerId,
'circle-stroke-width',
circleLayerStyle?.borderThickness
);
maplibreInstance?.setPaintProperty(circleLayerId, 'circle-radius', circleLayerStyle?.markerSize);
};

const updateLineLayer = (
maplibreInstance: Maplibre,
documentLayerConfig: DocumentLayerSpecification
) => {
const lineLayerId = buildLayerSuffix(documentLayerConfig.id, 'line');
maplibreInstance?.setLayerZoomRange(
lineLayerId,
documentLayerConfig.zoomRange[0],
documentLayerConfig.zoomRange[1]
);
maplibreInstance?.setPaintProperty(
lineLayerId,
'line-opacity',
documentLayerConfig.opacity / 100
);
maplibreInstance?.setPaintProperty(
lineLayerId,
'line-color',
documentLayerConfig.style?.fillColor
);
maplibreInstance?.setPaintProperty(
lineLayerId,
'line-width',
documentLayerConfig.style?.borderThickness
);
};

const updateFillLayer = (
maplibreInstance: Maplibre,
documentLayerConfig: DocumentLayerSpecification
) => {
const fillLayerId = buildLayerSuffix(documentLayerConfig.id, 'fill');
maplibreInstance?.setLayerZoomRange(
fillLayerId,
documentLayerConfig.zoomRange[0],
documentLayerConfig.zoomRange[1]
);
maplibreInstance?.setPaintProperty(
fillLayerId,
'fill-opacity',
documentLayerConfig.opacity / 100
);
maplibreInstance?.setPaintProperty(
fillLayerId,
'fill-color',
documentLayerConfig.style?.fillColor
);
maplibreInstance?.setPaintProperty(
fillLayerId,
'fill-outline-color',
documentLayerConfig.style?.borderColor
);
const outlineLayerId = buildLayerSuffix(documentLayerConfig.id, 'fill-outline');
maplibreInstance?.setPaintProperty(
outlineLayerId,
'line-color',
documentLayerConfig.style?.borderColor
);
maplibreInstance?.setPaintProperty(
outlineLayerId,
'line-width',
documentLayerConfig.style?.borderThickness
);
};

const updateLayerConfig = (
layerConfig: DocumentLayerSpecification,
maplibreRef: MaplibreRef,
Expand All @@ -360,11 +187,38 @@ const updateLayerConfig = (
// @ts-ignore
dataSource.setData(getLayerSource(data, layerConfig));
}
updateCircleLayer(maplibreInstance, layerConfig);
updateCircleLayer(maplibreInstance, {
fillColor: layerConfig.style?.fillColor,
maxZoom: layerConfig.zoomRange[1],
minZoom: layerConfig.zoomRange[0],
opacity: layerConfig.opacity,
outlineColor: layerConfig.style?.borderColor,
radius: layerConfig.style?.markerSize,
sourceId: layerConfig.id,
visibility: layerConfig.visibility,
width: layerConfig.style?.borderThickness,
});
const geoFieldType = getGeoFieldType(layerConfig);
if (geoFieldType === 'geo_shape') {
updateLineLayer(maplibreInstance, layerConfig);
updateFillLayer(maplibreInstance, layerConfig);
updateLineLayer(maplibreInstance, {
width: layerConfig.style?.borderThickness,
color: layerConfig.style?.fillColor,
maxZoom: layerConfig.zoomRange[1],
minZoom: layerConfig.zoomRange[0],
opacity: layerConfig.opacity,
sourceId: layerConfig.id,
visibility: layerConfig.visibility,
});
updatePolygonLayer(maplibreInstance, {
width: layerConfig.style?.borderThickness,
fillColor: layerConfig.style?.fillColor,
maxZoom: layerConfig.zoomRange[1],
minZoom: layerConfig.zoomRange[0],
opacity: layerConfig.opacity,
sourceId: layerConfig.id,
outlineColor: layerConfig.style?.borderColor,
visibility: layerConfig.visibility,
});
}
}
};
Expand Down
25 changes: 25 additions & 0 deletions public/model/map/__mocks__/layer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

export class MockLayer {
private layerProperties: Map<string, any> = new Map<string, any>();

constructor(id: string) {
this.layerProperties.set('id', id);
}

public setProperty(name: string, value: any): this {
this.layerProperties.set(name, value);
return this;
}

public getProperty(name: string): any {
return this.layerProperties.get(name);
}

public hasProperty(name: string): boolean {
return this.layerProperties.has(name);
}
}
Loading