-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path_snippet.js
215 lines (151 loc) · 32.5 KB
/
_snippet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
const turf = require('@turf/turf')
// ALGO. TO FIND COMBINATIONS OF ALLOCATIONS THAT ARE <= TO AREA OF DISCARDED POLYGON
function getBestFitCombinations(combinations, currentCombination, allocations, remainingSum) {
// Check if currentCombination should be added to the solutions list
if (remainingSum < 0) {
// Sum is too large; terminate recursion
return
} else if (currentCombination.length > 0) {
// Sort all combinations so comparison can be made sequentially
currentCombination.sort();
var uniquePermutation = true;
for (var i=0; i<combinations.length; i++) {
if (currentCombination.length == combinations[i].length) {
// Pass
for (var j=0; currentCombination[j]==combinations[i][j] && j<combinations[i].length; j++);
if (j == currentCombination.length) {
// For loop got all the way through combinations[i], so currentCombination = combinations[i]
uniquePermutation = false;
break;
}
}
}
if (uniquePermutation) {
combinations.push(currentCombination);
}
}
for (var i=0; i<allocations.length; i++) {
// Copy allocations
var copiedAllocations = allocations.slice();
// Cut out the i'th element and add to the current combination
var newCombination = currentCombination.concat(copiedAllocations.splice(i,1));
var newRemainingSum = remainingSum - allocations[i];
getBestFitCombinations(combinations, newCombination, copiedAllocations, newRemainingSum);
}
}
const SUCCESSFUL_CLUMPS = [];
// GET THE ALLOCATION(S) THAT BEST FIT THE LAND AREA
function _getBestFit(polygonArea, allocations) {
let pendingFeatures = []; // features yet to be clumped after each recursive pass
const combinations = [];
const currentCombination = [];
const results = []; // to store combos. and their sums
const allCombinations = []; // to store combos. only
const limitedCombinations = []; // to store combos. with only 2 elements
const successfulClumps = [];
// TODO > INSERT _geojson-clumps.js CODE HERE >
getBestFitCombinations(combinations, currentCombination, allocations, polygonArea);
for (var i=0; i<combinations.length; i++) {
const combinationSum = combinations[i].reduce((combItem, sum) => sum + combItem);
const combinationObj = {
combo: combinations[i],
sum: combinationSum
}
results.push(combinationObj);
// STORE COMBOS. WITH ONLY TWO ELEMENTS
if (combinations[i].length === 2 ) {
limitedCombinations.push(combinations[i]);
};
// STORE COMBOS. WITH MORE THAN ONE ELEMENT
if (combinations[i].length > 1 ) {
allCombinations.push(combinations[i]);
};
};
// SORT THE RESULTS IN ASC. ORDER
const sortedReesults = results.sort((a,b)=>parseFloat(a.sum)-parseFloat(b.sum));
// THE LAST RESULT IN ASC. ORDER IS THE BEST FIT
const bestFit = sortedReesults[sortedReesults.length - 1];
// // TRY TO UNITE THE FEATURES
// // limitedCombinations.forEach(resultCombo => {
// allCombinations.forEach(resultCombo => {
// try {
// // ATTEMPT TO UNITE COMBO. WITH MAX NO. OF MEMBERS
// // ATTEMPT TO UNITE COBOS. WITH 2 MEMBERS ONLY
// // const comboMembers = [];
// // for (let idx = 0; idx < resultCombo.length; idx++) {
// // const member = resultCombo[idx];
// // comboMembers.push(member);
// // }
// // const clump = turf.union(resultCombo[0], resultCombo[1], resultCombo[2]);
// const clump = turf.union(...resultCombo)
// // successfulClumps.push(clump)
// // successfulClumps.push(JSON.stringify(clump));
// if (turf.getType(clump) === "Polygon") {
// successfulClumps.push(turf.area(clump)/10000);
// return;
// // FILTER OUT ALL REFS. OF THE COMBO. MEMBERS FROM THE LIMITED RESULTS
// // console.log(resultCombo[0])
// // console.log(resultCombo[1])
// }
// } catch (_err) {
// console.error(_err.message)
// }
// })
// TRY TO UNITE THE FEATURES
for (let idx = 0; idx < allCombinations.length; idx++) {
const resultCombo = allCombinations[idx];
try {
const clumpAttempt = turf.union(...resultCombo);
// const clumpAttempt = _getPolygonUnion(...resultCombo);
if (turf.getType(clumpAttempt) === "Polygon") {
// UPDATE THE CUSTOM PROPERTIES OF THE FEAT. CLUMPS
clumpAttempt.properties = {
"feature_clump_id": `${turf.centerOfMass(clumpAttempt).geometry.coordinates[0].toFixed(8)}+${turf.centerOfMass(clumpAttempt).geometry.coordinates[1].toFixed(8)}`,
"clump_area": turf.area(clumpAttempt)/10000
};
successfulClumps.push(turf.area(clumpAttempt)/10000);
// UNION WAS SUCCESSFUL
SUCCESSFUL_CLUMPS.push(clumpAttempt);
allocations.push(clumpAttempt)
// FILTER OUT ALL REFS. OF THE SUCCESSFUL clumpAttempt MEMBERS FROM DISCARDED_POLYGON_FEATURES
for (const featMember of resultCombo) {
allocations = allocations.filter( feature => {
return feature.properties.discarded_feat_id !== featMember.properties.discarded_feat_id;
});
};
// STOP AT THE FIRST SUCCESSFUL UNION
break;
}
} catch (_err) {
console.error(_err.message)
}
}
// console.warn({sortedResults});
// console.warn({bestFit});
// console.warn({limitedCombinations});
console.warn({allCombinations});
console.warn({successfulClumps});
console.warn({allocations});
// RECURSIVE CALL TO ATTEMPT CLUMPING OF REMAINING FEATS.
// successfulClumps WILL == 0 WHEN THERE ARE NO MORE SUCCESSFUL UNIONS
if (successfulClumps.length !== 0) {
_getBestFit("", allocations);
}
}
// const polygonArea = 0;
// const allocations = [3, 1.5, 4.99, 0.75, 4.5, 0.5, 0.1, 0.69];
// _getBestFit(polygonArea, allocations);
const polygonArea = "";
// const allocations = ["feat3", "feat1", "feat2", "feat5"];
// _getBestFit(polygonArea, allocations);
// const DISCARDED_POLYGON_FEATURES = [{"type":"Feature","properties":{"chunk_index":1,"plot_id":"unique-plot-id-91360","chunk_size":"5.0","actual_chunk_size":"5.1","farmer_id":"unique-farmer-id-92692","center_lng":-74.01911945422337,"center_lat":40.71669322349614},"geometry":{"type":"Polygon","coordinates":[[[-74.02029439493052,40.71820583887709],[-74.020044,40.717957],[-74.017942,40.717631],[-74.01779010697271,40.71558],[-74.02029501584299,40.71558],[-74.02029439493052,40.71820583887709]]]}},{"type":"Feature","properties":{"chunk_index":5,"plot_id":"unique-plot-id-446077","chunk_size":"3.2","actual_chunk_size":"3.3","farmer_id":"unique-farmer-id-340902","center_lng":-74.02060935562271,"center_lat":40.71557934655602},"geometry":{"type":"Polygon","coordinates":[[[-74.01779,40.715617],[-74.017781,40.715616],[-74.017772,40.715615],[-74.017772,40.715614],[-74.017764,40.715611],[-74.017763,40.715611],[-74.017757,40.715607],[-74.017756,40.715607],[-74.01775,40.715601],[-74.017746,40.715596],[-74.017746,40.715595],[-74.017743,40.715589],[-74.017741,40.715582],[-74.017722,40.715324],[-74.017723,40.715316],[-74.017726,40.715308],[-74.01780605463858,40.715159],[-74.02253028866555,40.715159],[-74.022531,40.712611],[-74.02253030052724,40.716193],[-74.02029572765957,40.716193],[-74.020296,40.715617],[-74.01779,40.715617]]]}},{"type":"Feature","properties":{"chunk_index":7,"plot_id":"unique-plot-id-430693","chunk_size":"3.4","actual_chunk_size":"3.5","farmer_id":"unique-farmer-id-424840","center_lng":-74.02052724892557,"center_lat":40.71388133710174},"geometry":{"type":"Polygon","coordinates":[[[-74.0182460597015,40.71434],[-74.01867,40.713551],[-74.018674,40.713544],[-74.018675,40.713544],[-74.018681,40.713538],[-74.018682,40.713538],[-74.01869,40.713533],[-74.018699,40.71353],[-74.0187,40.71353],[-74.01923897727272,40.713395],[-74.02253054655871,40.713395],[-74.022531,40.712611],[-74.02253032142858,40.71434],[-74.0182460597015,40.71434]]]}},{"type":"Feature","properties":{"chunk_size":6.531039304833136,"id":"customPolygon1985"},"geometry":{"type":"Polygon","coordinates":[[[-74.02520838684778,40.71194103217246],[-74.02729297297583,40.711419332650415],[-74.02785053170885,40.71356463478504],[-74.02520797382452,40.71540134084847],[-74.02520838684778,40.71194103217246]]]}},{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-74.02031442675795,40.713105549979595],[-74.02041080008162,40.713105549979595],[-74.022517,40.712579],[-74.022525,40.712578],[-74.022534,40.712577],[-74.022542,40.712578],[-74.02255,40.71258],[-74.022557,40.712584],[-74.022563,40.712588],[-74.022569,40.712593],[-74.022572,40.712599],[-74.022573,40.7126],[-74.022574,40.7126],[-74.025208,40.711941],[-74.025208,40.713105549979595],[-74.02520844025149,40.71310556637342],[-74.0252172028824,40.71310622054783],[-74.02521806646672,40.713106350752696],[-74.0252264923546,40.71310828813628],[-74.02522730583328,40.71310854354232],[-74.02523507117607,40.71311168968244],[-74.02523580328753,40.71311206047453],[-74.02524260966739,40.713116294466595],[-74.02524323227699,40.71311676639536],[-74.0252488181286,40.71312192552923],[-74.02524930730982,40.7131224804587],[-74.025253457972,40.7131283664715],[-74.0252537949259,40.71312898307603],[-74.02525635089106,40.71313536977148],[-74.02525652266866,40.713136024355286],[-74.02525738571246,40.7131426662961],[-74.02525740734063,40.71314299999999],[-74.02525740734063,40.71329599999996],[-74.02525738571246,40.713296333703084],[-74.02525652266866,40.71330297562791],[-74.02525635089106,40.71330363021008],[-74.0252537949259,40.71331001688877],[-74.025253457972,40.713310633491616],[-74.02524930730983,40.713316519487805],[-74.02524881812859,40.713317074415656],[-74.02524323227698,40.71332223353399],[-74.02524260966739,40.71332270546128],[-74.02523580328753,40.71332693943995],[-74.02523507117607,40.713327310230845],[-74.02522730583327,40.71333045636061],[-74.02522649235462,40.713330711765785],[-74.02521806646673,40.71333264914286],[-74.02521720288239,40.71333277934731],[-74.02520844025149,40.713333433519466],[-74.025208,40.713333449913236],[-74.01947950957855,40.713333449913236],[-74.01947905758846,40.713333432631316],[-74.01947006367365,40.713332743109106],[-74.01946917800178,40.71333260590467],[-74.01946054839529,40.71333056526777],[-74.01945971690687,40.71333029641652],[-74.01945180116019,40.71332698732299],[-74.01945105753556,40.713326597715024],[-74.01944417628462,40.713322154203134],[-74.01944355064512,40.71332165961991],[-74.01943798262245,40.713316261678706],[-74.01943750031025,40.713315682153834],[-74.01943347105467,40.71330954843254],[-74.01943315160636,40.71330890744028],[-74.01943082432719,40.71330228639173],[-74.01943068068238,40.71330160989616],[-74.0194301496486,40.71329476971302],[-74.01943018762577,40.71329408511632],[-74.01943147434751,40.713287302867535],[-74.01943169240836,40.713286637900026],[-74.0194347447655,40.7132801883081],[-74.01943513407724,40.71327956990502],[-74.0194398284308,40.71327371421772],[-74.01944037322396,40.713273167428206],[-74.01944651942402,40.71326814283676],[-74.01944719763111,40.713267689809115],[-74.01945454671922,40.71326369984023],[-74.01945533086875,40.71326335892485],[-74.0194635851621,40.7132605651968],[-74.0194640071728,40.713260441290636],[-74.02025049759428,40.713063441185426],[-74.02025092237382,40.71306335205022],[-74.02025951276917,40.71306188921158],[-74.02026038658795,40.71306180745111],[-74.02026918842411,40.713061642953576],[-74.02027006649692,40.71306169197294],[-74.02027874156411,40.713062832137275],[-74.02027959015115,40.71306301005288],[-74.02028780511063,40.713065411068364],[-74.02028859160495,40.713065711043804],[-74.02029603079757,40.7130692806514],[-74.02029672497827,40.713069691160165],[-74.02030310255371,40.713074292197895],[-74.02030367774688,40.71307479746614],[-74.02030874864737,40.71308025313925],[-74.0203091827513,40.713080833752116],[-74.02031275212791,40.71308693442668],[-74.0203130284622,40.713087568074116],[-74.02031495916191,40.713094079332016],[-74.02031506710841,40.7130947416662],[-74.02031528494427,40.71310141331284],[-74.02031522035516,40.71310207888359],[-74.02031442675795,40.713105549979595]]]},"properties":{"chunk_size":3.8165495543766554}}]
// const DISCARDED_POLYGON_FEATURES = [{"type":"Feature","properties":{"chunk_size":10.953348669674291,"id":"customPolygon1985"},"geometry":{"type":"Polygon","coordinates":[[[-74.02364156241893,40.71941095775042],[-74.02365032572645,40.71941161196487],[-74.02365875221247,40.719413549463184],[-74.02366651800133,40.71941669577666],[-74.0236733246116,40.719420929975215],[-74.02367891042825,40.71942608931541],[-74.02368306075802,40.719431975495596],[-74.02368561608118,40.71943836227772],[-74.0236864781828,40.71944500418293],[-74.02368591576386,40.72291400418272],[-74.0236849814113,40.722920907106364],[-74.02368221941883,40.722927523005715],[-74.02367774456059,40.72293357695839],[-74.02367174278851,40.72293881739358],[-74.0236644635052,40.72294302654616],[-74.02365620920001,40.72294602950572],[-74.02364732287917,40.722947701484976],[-74.02363817381219,40.72294797300522],[-74.02334517381219,40.72293397301235],[-74.02333616063083,40.72293283661317],[-74.02332763739034,40.722930341358214],[-74.02331995680237,40.72292659050686],[-74.02331343670767,40.722921739277915],[-74.02330834692305,40.72291598842641],[-74.02330489807571,40.722909575935866],[-74.02330323288705,40.722902767170005],[-74.02300460619621,40.72012782138529],[-74.02070752895825,40.7195804750487],[-74.02069885247764,40.719577615955096],[-74.02069115866742,40.71957344282195],[-74.02068478889667,40.719568140808086],[-74.02068002578778,40.719561945159995],[-74.02067708067663,40.71955513077414],[-74.02067608423579,40.719547999999996],[-74.02067608423579,40.71944499999999],[-74.02067694727961,40.71943835868794],[-74.02067950324476,40.71943197259714],[-74.02068365390694,40.71942608714161],[-74.02068923975855,40.71942092849628],[-74.02069604613841,40.719416694905156],[-74.0207038114812,40.719413549062985],[-74.0207122373691,40.71941161186285],[-74.020721,40.71941095775042],[-74.02364156241893,40.71941095775042]]]}},{"type":"Feature","properties":{"chunk_size":10.953348669674291,"id":"customPolygon1985"},"geometry":{"type":"Polygon","coordinates":[[[-74.02004473314801,40.71941095775042],[-74.02005343589983,40.719411602867645],[-74.02006180880895,40.71941351376873],[-74.02006953453403,40.71941661802856],[-74.02007632026262,40.71942079799254],[-74.02008190880909,40.719425895235815],[-74.02008608836208,40.71943171656776],[-74.02008870051255,40.71943804135411],[-74.02008964625742,40.71944462987925],[-74.02008888975207,40.71945123243166],[-74.0200864596688,40.719457598768265],[-74.0200824481101,40.71946348759887],[-74.0176369157642,40.72232922071001],[-74.0176369157642,40.72236499999999],[-74.01763656167915,40.72236926590179],[-74.01763556167916,40.7223752659014],[-74.01763454764588,40.722379337654544],[-74.01763373581029,40.72238177316104],[-74.01763189839312,40.722388204120364],[-74.01763098403265,40.72239087569073],[-74.01762898403264,40.722395875690005],[-74.01762688699881,40.72240009083528],[-74.01762414386792,40.722404662719136],[-74.01762398310323,40.72240509868196],[-74.01762040772829,40.72241064853896],[-74.01761640772828,40.72241564853764],[-74.01761570241074,40.72241650147805],[-74.01678591867105,40.723387736705185],[-74.01678404862615,40.7233908210995],[-74.01678104862614,40.72339482109848],[-74.01677779696405,40.72339856049095],[-74.01677279696406,40.72340356048942],[-74.01677022917066,40.72340590276753],[-74.01676422917066,40.723410902765806],[-74.01676066615525,40.723413558753435],[-74.01675466615526,40.72341755875189],[-74.01675204106337,40.72341917996544],[-74.01674504106337,40.72342317996382],[-74.01674010938723,40.72342563060525],[-74.01673310938723,40.7234286306039],[-74.01673091959188,40.723429509574736],[-74.01672291959188,40.723432509573364],[-74.01672108346975,40.723433159463205],[-74.01671508346973,40.723435159462284],[-74.01671107069457,40.72343632678019],[-74.01670307069459,40.72343832677921],[-74.01669630948793,40.723439586429095],[-74.01668830948792,40.72344058642859],[-74.01668751545527,40.72344068016082],[-74.0166800914576,40.72344150504904],[-74.01667564713331,40.723442245769384],[-74.016666,40.7234430402049],[-74.016634,40.7234430402049],[-74.01662567995727,40.72344245110371],[-74.01661918421478,40.723441523140956],[-74.01661169051198,40.72344058642857],[-74.01660369051208,40.723439586429095],[-74.01660267995786,40.723439451105286],[-74.01659567995786,40.72343845110581],[-74.01658591653005,40.723436159461734],[-74.01657991653003,40.72343415946267],[-74.01657808040792,40.723433509572835],[-74.01657008040792,40.72343050957419],[-74.01656344511052,40.72342745389785],[-74.01655444511054,40.723422453899914],[-74.01655133384403,40.72342055875029],[-74.01655035216459,40.72341990429759],[-74.01654994807711,40.723419748386405],[-74.0165432704825,40.72341582665427],[-74.0130427947421,40.72092720030648],[-74.01304033447022,40.720925560125835],[-74.01303677139151,40.72092290409042],[-74.01303077139153,40.72091790409215],[-74.01302656734298,40.72091381388333],[-74.01301956734297,40.72090581388559],[-74.01301691089202,40.72090235164722],[-74.01301291089202,40.7208963516486],[-74.01301091089249,40.72089335164998],[-74.01300899883199,40.720890062889474],[-74.01300599883199,40.72088406289056],[-74.01300386940551,40.72087833723577],[-74.0130018694055,40.720870337236526],[-74.01300134520845,40.72086766429319],[-74.01300034520845,40.72086066429359],[-74.0130000842358,40.720857],[-74.0130000842358,40.720848999999994],[-74.01300034520857,40.72084533570497],[-74.01300134520856,40.720838335704585],[-74.01300186940614,40.7208356627582],[-74.01300286940614,40.72083166275781],[-74.0130034524145,40.720829661971244],[-74.01300545241449,40.7208236619705],[-74.01300628123167,40.72082148370911],[-74.01300928123167,40.72081448370799],[-74.01301191089962,40.72080964832352],[-74.01301591089963,40.72080364832214],[-74.01302020353896,40.72079843820916],[-74.01302202795588,40.72079661379169],[-74.0130749909509,40.720732862020455],[-74.01307502426857,40.72073282197759],[-74.01308002426858,40.720726821975965],[-74.01308220355354,40.720724438171736],[-74.01308720355352,40.720719438170164],[-74.01309333451117,40.720714439775584],[-74.01309933451118,40.72071043977406],[-74.01310195962569,40.720708818545084],[-74.01310895962568,40.72070481854346],[-74.01311389128388,40.72070236790798],[-74.01312089128389,40.720699367906626],[-74.01312308105082,40.72069848894636],[-74.01313108105082,40.720695488945],[-74.01313692981624,40.720693671775734],[-74.01314492981625,40.72069167177476],[-74.01315292981678,40.72068967177367],[-74.01315735323799,40.720688752810005],[-74.01316335323799,40.720687752809475],[-74.0131656907994,40.72068741216315],[-74.01317318452068,40.720686475447536],[-74.01317968028194,40.720685547481175],[-74.013188,40.720684958401954],[-74.01741409827729,40.720684958401954],[-74.01741460981555,40.71944498935609],[-74.01741547525614,40.71943834955373],[-74.01741803285417,40.719431965222235],[-74.01742218436164,40.71942608161067],[-74.01742777030168,40.719420924733384],[-74.01743457609498,40.719416692687815],[-74.01744234030248,40.71941354804477],[-74.0174507646684,40.71941161160332],[-74.01745952557756,40.71941095775042],[-74.02004473314801,40.71941095775042]]]}},{"type":"Feature","properties":{"chunk_size":5.861501486616815},"geometry":{"type":"Polygon","coordinates":[[[-74.02552244554987,40.71146240524705],[-74.02729253222527,40.71141939821509],[-74.02785004227847,40.713564513046705],[-74.02552213633882,40.71518252338816],[-74.02552244554987,40.71146240524705]]]}},{"type":"Feature","properties":{"chunk_size":6.639671510296072,"id":"customPolygon1985"},"geometry":{"type":"Polygon","coordinates":[[[-74.02552187109374,40.719402957746325],[-74.02553063474355,40.719403612012464],[-74.0255390615321,40.71940554966185],[-74.0255468275466,40.71940869621413],[-74.0255536342734,40.71941293072048],[-74.02555922007234,40.71941809041281],[-74.02556337023391,40.71942397696027],[-74.02556592523226,40.719430364092865],[-74.02556678685718,40.719437006298904],[-74.02556591576344,40.72300500629858],[-74.02556497994416,40.723011912460535],[-74.02556221478396,40.72301853109879],[-74.02555773529934,40.723024586911926],[-74.02555172781399,40.723029828009246],[-74.02554444220861,40.72303403638812],[-74.02553618152713,40.72303703700168],[-74.0255272893717,40.72303870503992],[-74.02551813561055,40.72303897112105],[-74.02363913561054,40.7229479711675],[-74.02363079245305,40.722946963714236],[-74.02362284581208,40.72294479002412],[-74.0236155768352,40.7229415270009],[-74.02360924269458,40.72293729008831],[-74.02360406748846,40.7229322291857],[-74.02360023431264,40.72292652334472],[-74.02359787878265,40.722920374434544],[-74.02359708423579,40.722914],[-74.02359708423579,40.71943699999999],[-74.0235979472796,40.71943035868712],[-74.02360050324475,40.71942397259557],[-74.02360465390693,40.71941808713933],[-74.02361023975854,40.71941292849338],[-74.0236170461384,40.719408694901745],[-74.02362481148117,40.719405549059196],[-74.02363323736908,40.719403611858844],[-74.023642,40.719402957746325],[-74.02552187109374,40.719402957746325]]]}},{"type":"Feature","properties":{"chunk_size":4.846267256719175},"geometry":{"type":"Polygon","coordinates":[[[-74.02772543925234,40.719451],[-74.027636,40.719973],[-74.02605,40.72303],[-74.025521,40.723004],[-74.02552199663394,40.719451],[-74.02772543925234,40.719451]]]}}]
const DISCARDED_POLYGON_FEATURES = [{"type":"Feature","properties":{"chunk_size":0.07593725156886033,"id":"customPolygon1985"},"geometry":{"type":"Polygon","coordinates":[[[-74.02072,40.719387957738675],[-74.0207287626309,40.71938861185134],[-74.02073718851881,40.71939054905213],[-74.0207449538616,40.71939369489539],[-74.02075176024145,40.71939792848798],[-74.02075734609305,40.7194030871351],[-74.02076149675524,40.71940897259265],[-74.0207640527204,40.71941535868565],[-74.0207649157642,40.71942200000001],[-74.0207649157642,40.719547999999996],[-74.0207641290245,40.71955434363872],[-74.0207617963663,40.71956046504787],[-74.02075799950677,40.719566149783674],[-74.0207528714567,40.71957119869979],[-74.0207465918608,40.71957543492387],[-74.02073938070457,40.719578710053575],[-74.02073149060764,40.7195809093554],[-74.02072319797422,40.719581955784015],[-74.02071479331006,40.719581812681184],[-74.02070657104554,40.719580485060064],[-74.0201759623499,40.71945448512157],[-74.02016760820581,40.71945177086512],[-74.02016014611529,40.71944783744308],[-74.02015388166318,40.71944284593532],[-74.02014907138904,40.71943700075206],[-74.02014591228149,40.71943054126291],[-74.02014453371115,40.71942373199442],[-74.02014499213274,40.71941685179722],[-74.02014726877314,40.71941018242668],[-74.02015127040022,40.71940399700455],[-74.02015683314075,40.71939854883416],[-74.02016372919142,40.71939406102722],[-74.02017167614767,40.71939071736699],[-74.02018034856859,40.71938865478204],[-74.02018939130436,40.719387957738675],[-74.02072,40.719387957738675]]]}},{"type":"Feature","properties":{"chunk_size":10.953348669674291,"id":"customPolygon1985"},"geometry":{"type":"Polygon","coordinates":[[[-74.02364156241893,40.71941095775042],[-74.02365032572645,40.71941161196487],[-74.02365875221247,40.719413549463184],[-74.02366651800133,40.71941669577666],[-74.0236733246116,40.719420929975215],[-74.02367891042825,40.71942608931541],[-74.02368306075802,40.719431975495596],[-74.02368561608118,40.71943836227772],[-74.0236864781828,40.71944500418293],[-74.02368591576386,40.72291400418272],[-74.0236849814113,40.722920907106364],[-74.02368221941883,40.722927523005715],[-74.02367774456059,40.72293357695839],[-74.02367174278851,40.72293881739358],[-74.0236644635052,40.72294302654616],[-74.02365620920001,40.72294602950572],[-74.02364732287917,40.722947701484976],[-74.02363817381219,40.72294797300522],[-74.02334517381219,40.72293397301235],[-74.02333616063083,40.72293283661317],[-74.02332763739034,40.722930341358214],[-74.02331995680237,40.72292659050686],[-74.02331343670767,40.722921739277915],[-74.02330834692305,40.72291598842641],[-74.02330489807571,40.722909575935866],[-74.02330323288705,40.722902767170005],[-74.02300460619621,40.72012782138529],[-74.02070752895825,40.7195804750487],[-74.02069885247764,40.719577615955096],[-74.02069115866742,40.71957344282195],[-74.02068478889667,40.719568140808086],[-74.02068002578778,40.719561945159995],[-74.02067708067663,40.71955513077414],[-74.02067608423579,40.719547999999996],[-74.02067608423579,40.71944499999999],[-74.02067694727961,40.71943835868794],[-74.02067950324476,40.71943197259714],[-74.02068365390694,40.71942608714161],[-74.02068923975855,40.71942092849628],[-74.02069604613841,40.719416694905156],[-74.0207038114812,40.719413549062985],[-74.0207122373691,40.71941161186285],[-74.020721,40.71941095775042],[-74.02364156241893,40.71941095775042]]]}},{"type":"Feature","properties":{"chunk_size":10.953348669674291,"id":"customPolygon1985"},"geometry":{"type":"Polygon","coordinates":[[[-74.02004473314801,40.71941095775042],[-74.02005343589983,40.719411602867645],[-74.02006180880895,40.71941351376873],[-74.02006953453403,40.71941661802856],[-74.02007632026262,40.71942079799254],[-74.02008190880909,40.719425895235815],[-74.02008608836208,40.71943171656776],[-74.02008870051255,40.71943804135411],[-74.02008964625742,40.71944462987925],[-74.02008888975207,40.71945123243166],[-74.0200864596688,40.719457598768265],[-74.0200824481101,40.71946348759887],[-74.0176369157642,40.72232922071001],[-74.0176369157642,40.72236499999999],[-74.01763656167915,40.72236926590179],[-74.01763556167916,40.7223752659014],[-74.01763454764588,40.722379337654544],[-74.01763373581029,40.72238177316104],[-74.01763189839312,40.722388204120364],[-74.01763098403265,40.72239087569073],[-74.01762898403264,40.722395875690005],[-74.01762688699881,40.72240009083528],[-74.01762414386792,40.722404662719136],[-74.01762398310323,40.72240509868196],[-74.01762040772829,40.72241064853896],[-74.01761640772828,40.72241564853764],[-74.01761570241074,40.72241650147805],[-74.01678591867105,40.723387736705185],[-74.01678404862615,40.7233908210995],[-74.01678104862614,40.72339482109848],[-74.01677779696405,40.72339856049095],[-74.01677279696406,40.72340356048942],[-74.01677022917066,40.72340590276753],[-74.01676422917066,40.723410902765806],[-74.01676066615525,40.723413558753435],[-74.01675466615526,40.72341755875189],[-74.01675204106337,40.72341917996544],[-74.01674504106337,40.72342317996382],[-74.01674010938723,40.72342563060525],[-74.01673310938723,40.7234286306039],[-74.01673091959188,40.723429509574736],[-74.01672291959188,40.723432509573364],[-74.01672108346975,40.723433159463205],[-74.01671508346973,40.723435159462284],[-74.01671107069457,40.72343632678019],[-74.01670307069459,40.72343832677921],[-74.01669630948793,40.723439586429095],[-74.01668830948792,40.72344058642859],[-74.01668751545527,40.72344068016082],[-74.0166800914576,40.72344150504904],[-74.01667564713331,40.723442245769384],[-74.016666,40.7234430402049],[-74.016634,40.7234430402049],[-74.01662567995727,40.72344245110371],[-74.01661918421478,40.723441523140956],[-74.01661169051198,40.72344058642857],[-74.01660369051208,40.723439586429095],[-74.01660267995786,40.723439451105286],[-74.01659567995786,40.72343845110581],[-74.01658591653005,40.723436159461734],[-74.01657991653003,40.72343415946267],[-74.01657808040792,40.723433509572835],[-74.01657008040792,40.72343050957419],[-74.01656344511052,40.72342745389785],[-74.01655444511054,40.723422453899914],[-74.01655133384403,40.72342055875029],[-74.01655035216459,40.72341990429759],[-74.01654994807711,40.723419748386405],[-74.0165432704825,40.72341582665427],[-74.0130427947421,40.72092720030648],[-74.01304033447022,40.720925560125835],[-74.01303677139151,40.72092290409042],[-74.01303077139153,40.72091790409215],[-74.01302656734298,40.72091381388333],[-74.01301956734297,40.72090581388559],[-74.01301691089202,40.72090235164722],[-74.01301291089202,40.7208963516486],[-74.01301091089249,40.72089335164998],[-74.01300899883199,40.720890062889474],[-74.01300599883199,40.72088406289056],[-74.01300386940551,40.72087833723577],[-74.0130018694055,40.720870337236526],[-74.01300134520845,40.72086766429319],[-74.01300034520845,40.72086066429359],[-74.0130000842358,40.720857],[-74.0130000842358,40.720848999999994],[-74.01300034520857,40.72084533570497],[-74.01300134520856,40.720838335704585],[-74.01300186940614,40.7208356627582],[-74.01300286940614,40.72083166275781],[-74.0130034524145,40.720829661971244],[-74.01300545241449,40.7208236619705],[-74.01300628123167,40.72082148370911],[-74.01300928123167,40.72081448370799],[-74.01301191089962,40.72080964832352],[-74.01301591089963,40.72080364832214],[-74.01302020353896,40.72079843820916],[-74.01302202795588,40.72079661379169],[-74.0130749909509,40.720732862020455],[-74.01307502426857,40.72073282197759],[-74.01308002426858,40.720726821975965],[-74.01308220355354,40.720724438171736],[-74.01308720355352,40.720719438170164],[-74.01309333451117,40.720714439775584],[-74.01309933451118,40.72071043977406],[-74.01310195962569,40.720708818545084],[-74.01310895962568,40.72070481854346],[-74.01311389128388,40.72070236790798],[-74.01312089128389,40.720699367906626],[-74.01312308105082,40.72069848894636],[-74.01313108105082,40.720695488945],[-74.01313692981624,40.720693671775734],[-74.01314492981625,40.72069167177476],[-74.01315292981678,40.72068967177367],[-74.01315735323799,40.720688752810005],[-74.01316335323799,40.720687752809475],[-74.0131656907994,40.72068741216315],[-74.01317318452068,40.720686475447536],[-74.01317968028194,40.720685547481175],[-74.013188,40.720684958401954],[-74.01741409827729,40.720684958401954],[-74.01741460981555,40.71944498935609],[-74.01741547525614,40.71943834955373],[-74.01741803285417,40.719431965222235],[-74.01742218436164,40.71942608161067],[-74.01742777030168,40.719420924733384],[-74.01743457609498,40.719416692687815],[-74.01744234030248,40.71941354804477],[-74.0174507646684,40.71941161160332],[-74.01745952557756,40.71941095775042],[-74.02004473314801,40.71941095775042]]]}},{"type":"Feature","properties":{"chunk_size":5.861501486616815},"geometry":{"type":"Polygon","coordinates":[[[-74.02552244554987,40.71146240524705],[-74.02729253222527,40.71141939821509],[-74.02785004227847,40.713564513046705],[-74.02552213633882,40.71518252338816],[-74.02552244554987,40.71146240524705]]]}},{"type":"Feature","properties":{"chunk_size":6.639671510296072,"id":"customPolygon1985"},"geometry":{"type":"Polygon","coordinates":[[[-74.02552187109374,40.719402957746325],[-74.02553063474355,40.719403612012464],[-74.0255390615321,40.71940554966185],[-74.0255468275466,40.71940869621413],[-74.0255536342734,40.71941293072048],[-74.02555922007234,40.71941809041281],[-74.02556337023391,40.71942397696027],[-74.02556592523226,40.719430364092865],[-74.02556678685718,40.719437006298904],[-74.02556591576344,40.72300500629858],[-74.02556497994416,40.723011912460535],[-74.02556221478396,40.72301853109879],[-74.02555773529934,40.723024586911926],[-74.02555172781399,40.723029828009246],[-74.02554444220861,40.72303403638812],[-74.02553618152713,40.72303703700168],[-74.0255272893717,40.72303870503992],[-74.02551813561055,40.72303897112105],[-74.02363913561054,40.7229479711675],[-74.02363079245305,40.722946963714236],[-74.02362284581208,40.72294479002412],[-74.0236155768352,40.7229415270009],[-74.02360924269458,40.72293729008831],[-74.02360406748846,40.7229322291857],[-74.02360023431264,40.72292652334472],[-74.02359787878265,40.722920374434544],[-74.02359708423579,40.722914],[-74.02359708423579,40.71943699999999],[-74.0235979472796,40.71943035868712],[-74.02360050324475,40.71942397259557],[-74.02360465390693,40.71941808713933],[-74.02361023975854,40.71941292849338],[-74.0236170461384,40.719408694901745],[-74.02362481148117,40.719405549059196],[-74.02363323736908,40.719403611858844],[-74.023642,40.719402957746325],[-74.02552187109374,40.719402957746325]]]}},{"type":"Feature","properties":{"chunk_size":4.846267256719175},"geometry":{"type":"Polygon","coordinates":[[[-74.02772543925234,40.719451],[-74.027636,40.719973],[-74.02605,40.72303],[-74.025521,40.723004],[-74.02552199663394,40.719451],[-74.02772543925234,40.719451]]]}}]
let BUFFERED_FEATURES = [];
for (const feature of DISCARDED_POLYGON_FEATURES) {
BUFFERED_FEATURES.push(turf.buffer(feature, 0.0005, {unit: 'kilometers'})); // remove any tails
feature.properties["discarded_feat_id"] = `${turf.centerOfMass(feature).geometry.coordinates[0].toFixed(8)}+${turf.centerOfMass(feature).geometry.coordinates[1].toFixed(8)}`
}
// _getBestFit(polygonArea, DISCARDED_POLYGON_FEATURES);
_getBestFit(polygonArea, BUFFERED_FEATURES);
console.log({SUCCESSFUL_CLUMPS});