Search code examples
javascriptd3.jsleafletgeojson

geoJson properties add/update


Is it somehow possible to dynamically add properties to features through d3 / leaflet.

I have this kind of GeoJson file:

        {"type":"Feature","id":"01","properties":
{"name":"Alabama","density":94.65, "news":22},
"geometry":{"type":"Polygon","coordinates":
[[[-87.359296,35.00118],[-85.606675,34.984749],[-85.431413,34.124869]...

And I would like to either update the news property when e.g. the user clicks on the feature or if news is not in this feature then add it. Is that possible?


Solution

  • It's certainly possible: GeoJSON is a superset of JSON, which is represented in JavaScript as a normal object. To update a property of this GeoJSON feature if it was stored as the variable feature, you would write

    feature.properties.news = 23;