calvinmetcalf / shapefile-js

Convert a Shapefile to GeoJSON. Not many caveats.
http://calvinmetcalf.github.io/shapefile-js/
MIT License
735 stars 230 forks source link

shapefile-js in React? #158

Closed gmarshall56 closed 3 years ago

gmarshall56 commented 3 years ago

Hello again:

I am trying to use this shape-js file in my react functional component.

Here is my code:

        const shpfile = L.geoJson({features:[]},{onEachFeature:function popUp(f,l){
                var out = [];
                if (f.properties){
                    for(var key in f.properties){
                        out.push(key+": "+f.properties[key]);
                    }
                    l.bindPopup(out.join("<br />"));
                }
            }}).addTo(map);

        const base = '../../../public/shapefiles/JNC/testJNC.zip'; 
        shp(base).then(function(geojson){  <== shp is not defined here
            shpfile.addData(geojson);
        });

Would this be the correct way of implementing and using the shapefile-js in a react component? Thanks for your time. Sorry if this is a stupid question. I'm learning React as well as using Leaflet in React.

calvinmetcalf commented 3 years ago

you could just use https://github.com/calvinmetcalf/leaflet.shapefile which would allow you to do

const base = '../../../public/shapefiles/JNC/testJNC.zip'; 
    const shpfile = L.shapefile(base, {onEachFeature:function popUp(f,l){
                var out = [];
                if (f.properties){
                    for(var key in f.properties){
                        out.push(key+": "+f.properties[key]);
                    }
                    l.bindPopup(out.join("<br />"));
                }
            }}).addTo(map);