robertleeplummerjr / Leaflet.glify

fully functional, ridiculously fast web gl renderer plugin for leaflet
https://robertleeplummerjr.github.io/Leaflet.glify
MIT License
484 stars 86 forks source link

shapes() function doesn't display `MultiPolygon` in geojson #133

Open YannNeobards opened 2 years ago

YannNeobards commented 2 years ago

Hi, shapes() function doesn't display MultiPolygon using npm leaflet.glify 3.2.0

for the moment i solve it by converting in Polygon only the geojson before

const MultiPolygon2PolygonOnly = (geoJ) => {
    let polyOnly = {type: 'FeatureCollection', features:[]}
    geoJ.features.forEach((f)=>{
        let g = f.geometry
        if (!g || !g.type) return
        if (g.type=='Polygon') return polyOnly.features.push(f)
        if (g.type!='MultiPolygon') return
        for (let i=0,m=g.coordinates.length;i<m;i++) { 
            polyOnly.features.push({ ...f, geometry: {...g, type: 'Polygon', coordinates: g.coordinates[i]}})
        }
    })
    return polyOnly
}

sample of geojson that contains Polygon and MultiPolygon

https://raw.githubusercontent.com/gregoiredavid/france-geojson/master/departements-version-simplifiee.geojson

trafficonese commented 1 year ago

Nice, that works for me although I had to rewrite it a little:

const MultiPolygon2PolygonOnly = (
  geoJ: GeoJSON.FeatureCollection
): GeoJSON.FeatureCollection => {
  const polyOnly: GeoJSON.FeatureCollection = {
    type: "FeatureCollection",
    features: [],
  };
  geoJ.features.forEach((f: GeoJSON.Feature) => {
    const g = f.geometry;
    if (!g || !g.type) return;
    if (g.type == "Polygon") return polyOnly.features.push(f);
    if (g.type != "MultiPolygon") return;
    for (let i = 0, m = g.coordinates.length; i < m; i++) {
      polyOnly.features.push({
        ...f,
        geometry: { ...g, type: "Polygon", coordinates: g.coordinates[i] },
      });
    }
  });
  return polyOnly;
};