I'm using OpenLayers and give some try on Turf.js, but with no result. I've an array with several GeoJSON string and I want to merge them all. (Below an example of just one element of my array that will be in union with itself):
var geojsonObject = "{\"type\":\"Polygon\",\"coordinates\":[[[13.2469617908357,38.106343739784],[13.2421500373403,38.1005855534108],[13.261749614562,38.105202678408],[13.2699877484575,38.1044321069354],[13.2811739681279,38.1059660299754],[13.2858187628462,38.108277772332],[13.2914064168058,38.1078274679509],[13.2898212673315,38.104884642929],[13.294039707404,38.1010168903083],[13.2985401972433,38.1007036504097],[13.3000939965039,38.0991040420218],[13.3031067559942,38.100120842302],[13.3052842404983,38.0971874323546],[13.3033290649018,38.0954379950413],[13.3040623374565,38.0925738615984],[13.3018080037223,38.0890988550067],[13.3026472435628,38.0859659087279],[13.301158760409,38.0852505042815],[13.304746330868,38.0793726270222],[13.3018450766421,38.0776365502474],[13.3027802779906,38.0747843446728],[13.3090603059182,38.0747216597232],[13.3131141910783,38.0783935971233],[13.3159734450347,38.071035997103],[13.314759444812,38.0702886503457],[13.3189856003767,38.0710430664526],[13.3250648617413,38.065772386101],[13.3177332688661,38.0569083028952],[13.3143917069584,38.0545173898417],[13.3147646911864,38.0531619670472],[13.3181804495011,38.0508137622306],[13.3240839791233,38.050429457195],[13.3332978528508,38.0585989169735],[13.3365263111369,38.0564547919162],[13.3414695203176,38.0565967288599],[13.3428836698147,38.0597167125994],[13.3488011690506,38.0620348930438],[13.3535680432633,38.0614168597544],[13.3584306159383,38.0646750248368],[13.366656294683,38.0633206872671],[13.3681633886838,38.0647474534927],[13.3683413804636,38.0721305568516],[13.3764053262138,38.0678084265649],[13.3838030705909,38.0688566073722],[13.3913952868969,38.0662391879728],[13.3957917696274,38.0663983973538],[13.4047300846727,38.059499837113],[13.4115475919905,38.0588982412235],[13.4192215559142,38.0603354292766],[13.4212066024513,38.0591905665266],[13.4217802895702,38.0616675108177],[13.424444377558,38.0628747835572],[13.4252418332733,38.0618310844217],[13.4340724649788,38.06732009725],[13.4339096041729,38.0765640612955],[13.4297325269908,38.0783737800724],[13.4337949670713,38.0824742846771],[13.4333854204232,38.0861726798461],[13.4414890835367,38.0857847630205],[13.4428289061594,38.0881996636549],[13.4395669608011,38.0903868242603],[13.4447930545785,38.0923665968281],[13.4512538340848,38.0888066306222],[13.452482993715,38.0927024086589],[13.4498277274273,38.0942444839585],[13.4507229952426,38.0954548597333],[13.4446300504395,38.0941397657281],[13.4353505652916,38.0950799863993],[13.4285074760643,38.0972596542719],[13.4168084450264,38.0977334603605],[13.4045997764948,38.100136709582],[13.4009555603427,38.1035657158419],[13.3930839147447,38.1041973049596],[13.3816412062306,38.1095374285443],[13.3776926387171,38.1158272247935],[13.3713861527642,38.120903685252],[13.3715518078764,38.1221766295278],[13.3667100940808,38.1193248144545],[13.3694886691201,38.1226166247665],[13.3687346953998,38.1252285591441],[13.3647779019346,38.1256601869912],[13.3642303797786,38.1266798964418],[13.367864264098,38.1269196782852],[13.3638629621179,38.1272965859828],[13.3633520426316,38.1282086605145],[13.3670386086942,38.1286474592112],[13.3629691809649,38.1290053652408],[13.3625158673767,38.129891203327],[13.3656288344797,38.130475249323],[13.3629371922582,38.1304377748901],[13.3623340531062,38.1339439185058],[13.3686994328707,38.1355284132348],[13.3679729401092,38.13382412981],[13.3702917965105,38.1306211285177],[13.3703039571958,38.1326399122253],[13.3724852396683,38.13305765159],[13.3752123011482,38.1263546594866],[13.3726301136012,38.1332128568988],[13.3697010455685,38.1330731193218],[13.3682345456029,38.1435964051801],[13.3709068369333,38.1450393124015],[13.3749465726856,38.152746159934],[13.3708258790961,38.1640707782542],[13.3667690013919,38.1678714105925],[13.3664417681263,38.1733278983744],[13.3685542484812,38.1753488283541],[13.3656339318988,38.1824093675137],[13.3584206522582,38.1919781865759],[13.3512029686573,38.1910209647056],[13.3497802384994,38.1913253958624],[13.3504619418123,38.1924884546823],[13.3487563763589,38.1911578061794],[13.3464496037654,38.1911704036111],[13.3376317966638,38.1973269541164],[13.3318707759574,38.1975786780103],[13.3271074016157,38.2017643526321],[13.3255127418784,38.2064456367296],[13.3279470972475,38.2068678254468],[13.3303984231979,38.2120482882127],[13.3266458940937,38.2118236450909],[13.3229382228747,38.213627112309],[13.3220015714621,38.2191017337074],[13.317424584413,38.2244070719516],[13.3003424937784,38.219673278507],[13.2848417484041,38.2094631315682],[13.2805750373307,38.2112030146855],[13.2821221481451,38.209522495601],[13.281604820147,38.2059013254166],[13.2761603713693,38.2023791322625],[13.2742087758073,38.1984575066368],[13.2692260019256,38.2009163848817],[13.2683005599703,38.2039486134892],[13.2664010356207,38.2035060543175],[13.2649187775774,38.2001589302253],[13.2600994298876,38.2000515665767],[13.2583089336096,38.2013902551609],[13.2584859398609,38.1997122446346],[13.2692574478697,38.1991055436043],[13.273964607061,38.1923531465611],[13.2722345261929,38.1900658201751],[13.2757635837705,38.1861526380461],[13.2693010934335,38.1837506270283],[13.271326455897,38.1769136995452],[13.2636630929579,38.1698347552142],[13.2555742323133,38.1685794371604],[13.2581765787317,38.1633102875176],[13.2622467583381,38.1601354472202],[13.2645540329717,38.1504281044151],[13.2554085283633,38.1400825655654],[13.2481371771866,38.1350810905456],[13.2480958770686,38.1304845909667],[13.2524305316261,38.1223756748012],[13.2469786368354,38.1142291119982],[13.2469617908357,38.106343739784]]]}";
var union = turf.union(turf.polygon(turf.multiPolygon(geojsonObject)), turf.polygon(turf.multiPolygon(geojsonObject)));
The union doesn't work and it give me the error: Cannot read property 'length' of undefined
If I try with just: turf.polygon(geojsonObject) I get the error: Each LinearRing of a Polygon must have 4 or more Positions.
Edit 1:
The complete code doesn't work: Each LinearRing of a Polygon must have 4 or more Positions.
I suppose because I try to merge a MultiPolygon
, union
, with the current Polygon
:
var union = turf.polygon(JSON.parse(listaMappe[0].geoJSON).coordinates);
for (var i = 0; i < listaMappe.length; ++i) {
if (i>=1)
{
union = turf.union(turf.polygon(JSON.parse(listaMappe[i].geoJSON).coordinates), union);
}
var vectorSource = new ol.source.Vector({
features: reader.readFeatures(listaMappe[i].geoJSON, projector)
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource,
style: styleFunction
});
map.addLayer(vectorLayer);
}
Edit 2:
I discovered another problem, the geoJSON values of my array listaMappe[i].geoJSON
are mixed geoJSON, some Polygon, others Multipolygon. How can I handle the union?
var union = turf.union(turf.polygon(JSON.parse(geojsonObject).coordinates), ....