Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a GeoJSON object with array of polygon features. Some of them have same geometry. They look like this:

squareData = {
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },

"features": [
{ "type": "Feature", "properties": { "gid": 2196272, "acq_date": "2014\/08\/29", "firenum": "1" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -179.5, -18.5 ], [ -178.5, -18.5 ], [ -178.5, -17.5 ], [ -179.5, -17.5 ], [ -179.5, -18.5 ] ] ] } },
{ "type": "Feature", "properties": { "gid": 2492600, "acq_date": "2014\/07\/09", "firenum": "1" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -164.5, 54.5 ], [ -163.5, 54.5 ], [ -163.5, 55.5 ], [ -164.5, 55.5 ], [ -164.5, 54.5 ] ] ] } },
{ "type": "Feature", "properties": { "gid": 2446209, "acq_date": "2014\/07\/18", "firenum": "1" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -164.5, 54.5 ], [ -163.5, 54.5 ], [ -163.5, 55.5 ], [ -164.5, 55.5 ], [ -164.5, 54.5 ] ] ] } },
{ "type": "Feature", "properties": { "gid": 1562738, "acq_date": "2014\/07\/25", "firenum": "1" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -164.5, 54.5 ], [ -163.5, 54.5 ], [ -163.5, 55.5 ], [ -164.5, 55.5 ], [ -164.5, 54.5 ] ] ] } },
{ "type": "Feature", "properties": { "gid": 476723, "acq_date": "2014\/07\/27", "firenum": "1" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -164.5, 54.5 ], [ -163.5, 54.5 ], [ -163.5, 55.5 ], [ -164.5, 55.5 ], [ -164.5, 54.5 ] ] ] } },
...
] 
}

I want to nest them based on geometry values, to get an object like this one:

nestedData = [
{
  "key": { "type": "Polygon", "coordinates": [ [ [ -164.5, 54.5 ], [ -163.5, 54.5 ], [ -163.5, 55.5 ], [ -164.5, 55.5 ], [ -164.5, 54.5 ] ] ] },
  "values": [
{ "type": "Feature", "properties": { "gid": 2492600, "acq_date": "2014\/07\/09", "firenum": "1" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -164.5, 54.5 ], [ -163.5, 54.5 ], [ -163.5, 55.5 ], [ -164.5, 55.5 ], [ -164.5, 54.5 ] ] ] } },
{ "type": "Feature", "properties": { "gid": 2446209, "acq_date": "2014\/07\/18", "firenum": "1" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -164.5, 54.5 ], [ -163.5, 54.5 ], [ -163.5, 55.5 ], [ -164.5, 55.5 ], [ -164.5, 54.5 ] ] ] } },
{ "type": "Feature", "properties": { "gid": 1562738, "acq_date": "2014\/07\/25", "firenum": "1" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -164.5, 54.5 ], [ -163.5, 54.5 ], [ -163.5, 55.5 ], [ -164.5, 55.5 ], [ -164.5, 54.5 ] ] ] } },
{ "type": "Feature", "properties": { "gid": 476723, "acq_date": "2014\/07\/27", "firenum": "1" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ -164.5, 54.5 ], [ -163.5, 54.5 ], [ -163.5, 55.5 ], [ -164.5, 55.5 ], [ -164.5, 54.5 ] ] ] } }
] },
{ "key": ..., "values": [...] },
...
]

I've nested this data by acq_date property using d3.nest() function, but it doesn't seem to work on geometry property. Every geometry is considered an "Object", so I'm getting all features inside the first array element. I'm doing it in browser, so I need a solution in JavaScript, ideally using d3.nest().

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.