Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a newly created polygon to GeoJSON in Leaflet

I create polygons of different sizes by clicking a button in my webapp.

I also add some values inside the object as a nested object, like {properties:{status:'active'}}. Then I run toGeoJSON() method of the polygon and get an object with properties and geometry objects. properties object is empty.

My question is how do I add my values into the object so that they are passed to the GeoJSON object on conversion?

like image 340
Sergei Basharov Avatar asked Oct 16 '25 09:10

Sergei Basharov


1 Answers

Any "extra" data on your Polygon is lost when a GeoJSON object is created for it. Only the coordinates of the polygon are carried over into the GeoJSON object. See lines 213 and 171 in layer/GeoJSON.js in the Leaflet source. On line 171 you can see that a new object is created and that object has an attribute called "properties" but that properties attribute has nothing to do with any attribute called "properties" on your Polygon.

After creating the GeoJSON object you could copy the properties from your Polygon into the properties object on the GeoJSON object by doing something like the following. However - I'm not sure what, if any, specific meaning the "properties" object has in the GeoJSON specification.

var json = polygon.toGeoJSON();
L.extend(json.properties, polygon.properties)
like image 110
InPursuit Avatar answered Oct 19 '25 01:10

InPursuit