Here's the current issue:
I have successfully implemented a MultiPolygon Layer in Leaflet, but I am encountering an error when trying to convert it to a GeoJSON object.
This is my code snippet:
let colecccionPoligonos=[];
const multiPolygonOptions = {color:'red', weight:2, fillOpacity: 0.5};
function swapCoords(coords){
for (const val of coords[0]){
const aux=val[0];
val[0]=val[1];
val[1]=aux;
}
return coords;
}
function addPolygon(polygon){
colecccionPoligonos.push([[swapCoords(polygon.geometry.coordinates)]]);
if(multipolygon){
map.removeLayer(multipolygon);
}
multipolygon=L.polygon(colecccionPoligonos,multiPolygonOptions);
console.log(multipolygon);
map.addLayer(multipolygon);
console.log(colecccionPoligonos);
console.log(multipolygon.toGeoJSON(8));
}
I introduced the 'swapCoords' function because I am working with a GeoJSON file that has inverted lat/lng values.
The output includes the layer addition, the array structure passed to L.polygon, and the error shown in this image link:
The error details are as follows:
GeoJSON.js:272 Uncaught TypeError: Cannot read properties of null (reading 'alt')
at Mi (GeoJSON.js:272:16)
at zi (GeoJSON.js:288:4)
at zi (GeoJSON.js:287:4)
at zi (GeoJSON.js:287:4)
at e.toGeoJSON (GeoJSON.js:368:16)
at addPolygon (busqueda:477:28)
at <anonymous>:1:1
Any insights on what might be causing this error? Let me know if you need more information.
Thanks a lot! Leandro