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

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

Open
YannNeobards opened this issue Jun 13, 2022 · 1 comment
Open

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

YannNeobards opened this issue Jun 13, 2022 · 1 comment

Comments

@YannNeobards
Copy link

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
Copy link
Contributor

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;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants