calvinmetcalf / leaflet.shapefile

Shapefile in Leaflet
http://calvinmetcalf.github.io/leaflet.shapefile/
MIT License
259 stars 119 forks source link

Add new property to shapefile #47

Open BLGEric opened 7 years ago

BLGEric commented 7 years ago

Hello, I'm now using your plugin for shapefiles. I want to know if there's a method to add new properties (self-defined) into the shapefile object? There's a method like "feature.properties.XXX = YYY" to add new property to the feature in GeoJson. Wondering if there's something similar to it or another way to do this. Thanks.

calvinmetcalf commented 7 years ago

so this plugin, internally, just converts the shapefile to geojson and creates a geojson layer, so anything you can do to a leaflet geojson layer you can do with this

BLGEric commented 7 years ago

OK I get it, thanks for your help.

BLGEric commented 7 years ago

I got another problem:

var test = new L.Shapefile('XX/XX/XX.zip'); var maptest = test.addTo(mymap);

these code works fine but I can not get the members under "test._layers" I tried using test._layers[i], and console.log shows "undefined" Plz help, thanks.

calvinmetcalf commented 7 years ago

this is 100% a leaflet issue you have so you may want to seek help there, but more generally some issues

BLGEric commented 7 years ago

First of all, thanks for the help.

I'm using 1.1.0 version of leaflet. I put the "console.log(test)" under the addTo function, and result as follows test object

Here's the result of console.log(test._layers) test _layers

As you can see, the "feature" is just right there, but I couldn't find a way to get the key or value in that object. I've tried to add some new property into "feature" through console during runtime, and it worked. But if I add the property in my javascript, the property won't be added. It's just strange....

calvinmetcalf commented 7 years ago

ok this is probably just because the way this works is

  1. create geojson layer
  2. make an ajax request for the shapefile
  3. add the empty geojson layer to the map
  4. wait for the shapefile to load
  5. wait some more, other stuff is happening in the app
  6. transform the shapefile into geojson and then load it into the layer

in your app you're probably somewhere in steps 3-5 when you're looking you're going to want to wait for the 'data:loaded' event to fire on the layer before doing any of that stuff.

BLGEric commented 7 years ago

OK, thanks for the help. I'll work on it. Much appreciated.