Skip to content

Commit 0af9e09

Browse files
committed
fix leaflet overlap
1 parent 853e1b5 commit 0af9e09

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

frontend/src/app/GN2CommonModule/map/map.service.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,90 @@ export class MapService {
193193
return geojsonLayer;
194194
}
195195

196+
/**
197+
* Create a geojson with adding layers following this order : polygon - line - point
198+
* Point are clickable even if they are beyond a polygon
199+
* the function add all geojsons in a FeatureGroup and add it to the map
200+
* @returns L.FeatureGroup
201+
*/
202+
createOrderedGeojson(geojson, asCluster: boolean, onEachFeature?, style?): FeatureGroup {
203+
const features = geojson?.features || geojson;
204+
205+
// Single loop to distribute features by geometry type
206+
const pointFeatures: any[] = [];
207+
const lineFeatures: any[] = [];
208+
const polygonFeatures: any[] = [];
209+
210+
for (const feature of features) {
211+
const geometryType = feature.geometry?.type;
212+
switch (geometryType) {
213+
case 'Point':
214+
case 'MultiPoint':
215+
pointFeatures.push(feature);
216+
break;
217+
case 'LineString':
218+
case 'MultiLineString':
219+
lineFeatures.push(feature);
220+
break;
221+
case 'Polygon':
222+
case 'MultiPolygon':
223+
polygonFeatures.push(feature);
224+
break;
225+
}
226+
}
227+
228+
// Helper function to create styled geojson layer
229+
const createStyledGeojson = (geoms) => {
230+
return L.geoJSON(geoms, {
231+
style: (feature) => {
232+
switch (feature.geometry.type) {
233+
case 'LineString':
234+
case 'MultiLineString':
235+
return style || this.lineStyle();
236+
default:
237+
return style || this.defaultStyle();
238+
}
239+
},
240+
pointToLayer: (feature, latlng) => {
241+
return L.circleMarker(latlng);
242+
},
243+
onEachFeature: onEachFeature,
244+
});
245+
};
246+
247+
// Create the main feature group
248+
const featureGroup = new L.FeatureGroup();
249+
250+
// Add layers in order: polygons → lines → points (so points are clickable and on top)
251+
// Polygons (bottom)
252+
if (polygonFeatures.length > 0) {
253+
const polygonLayer = createStyledGeojson(polygonFeatures);
254+
featureGroup.addLayer(polygonLayer);
255+
}
256+
257+
// Lines (middle)
258+
if (lineFeatures.length > 0) {
259+
const lineLayer = createStyledGeojson(lineFeatures);
260+
featureGroup.addLayer(lineLayer);
261+
}
262+
263+
// Points (top)
264+
if (pointFeatures.length > 0) {
265+
const pointLayer = createStyledGeojson(pointFeatures);
266+
if (asCluster) {
267+
const clusteredPoints = (L as any).markerClusterGroup().addLayer(pointLayer);
268+
featureGroup.addLayer(clusteredPoints);
269+
} else {
270+
featureGroup.addLayer(pointLayer);
271+
}
272+
}
273+
274+
// Add to map
275+
this.map.addLayer(featureGroup);
276+
277+
return featureGroup;
278+
}
279+
196280
createWMS(layerCfg) {
197281
return L.tileLayer.wms(layerCfg.url, {
198282
...layerCfg.params,

0 commit comments

Comments
 (0)