import get from 'lodash-es/get.js'
import isearr from 'wsemi/src/isearr.mjs'
import isnum from 'wsemi/src/isnum.mjs'
import isestr from 'wsemi/src/isestr.mjs'
import cdbl from 'wsemi/src/cdbl.mjs'
import turf from './importTurf.mjs'
import fixCloseMultiPolygon from './fixCloseMultiPolygon.mjs'
import distilMultiPolygon from './distilMultiPolygon.mjs'
/**
* 針對MultiPolygon基於buffer技術進行修復交疊處理
*
* Unit Test: {@link https://github.com/yuda-lyu/w-gis/blob/master/test/fixGeometryMultiPolygon.test.mjs Github}
* @memberOf w-gis
* @param {Array} pgs 輸入MultiPolygon資料陣列,為[[ [[x11,y11],[x12,y12],...], [[x21,y21],[x22,y22],...] ]]構成之陣列
* @param {Object} [opt={}] 輸入設定物件,預設{}
* @param {String} [opt.supposeType='polygons'] 輸入提取模式字串,當數據座標深度為2時,使用polygons代表每個其內多邊形為獨立polygon,若為ringStrings則表示其內多邊形為交錯的ringString(代表聯集與剔除),預設'polygons'
* @param {Number} [opt.w=0.0001] 輸入Buffer寬度數字,預設0.0001
* @param {String} [opt.units='degrees'] 輸入Buffer寬度單位,可選'degrees'、'radians'、'meters'、'kilometers'、'feet'、'miles',預設'degrees'
* @returns {Array} 回傳MultiPolygon陣列
* @example
*
* let pgs
* let r
*
* pgs = [ //polygon
* [
* [0, 0],
* [100, 0],
* [100, 1],
* [0, 1],
* ],
* [
* [0, 1],
* [100, 1],
* [100, 2],
* [0, 2],
* ],
* ]
* r = fixGeometryMultiPolygon(pgs)
* console.log(JSON.stringify(r))
* // => [[[[2.4543004643887743e-8,0.000004341000778110449],[4.478253942047132e-7,3.9563737264184023e-7],[0.000004944644202122001,4.909272681804741e-8],[99.9999950553558,4.909272681804741e-8],[99.99999955217461,3.956373738345482e-7],[99.9999999754572,0.000004341002588442417],[99.99999999348924,0.9999988486143221],[99.99999995344626,0.999999568870442],[99.99999997591864,1.999995742997783],[99.99999956723738,1.9999996309608217],[99.99999514934096,2.000000100059736],[0.000004850659040284691,2.000000100059736],[4.327626116498794e-7,1.9999996309608212],[2.4081188104139243e-8,1.9999957429959938],[6.485176439941562e-9,1.0000011469494172],[4.65536071226287e-8,1.0000004402548102],[2.4543004643887743e-8,0.000004341000778110449]]]]
*
* pgs = [ //polygon
* [
* [0, 0],
* [30, 0],
* [30, 2],
* [0, 2],
* ],
* [
* [0, 0],
* [10, 0],
* [10, 1],
* [0, 1],
* ]
* ]
* r = fixGeometryMultiPolygon(pgs, { supposeType: 'ringStrings' }) //polygon內為聯集與剔除的ringStrings
* console.log(JSON.stringify(r))
* // => [[[[0.0015371637167360477, 1.000002364293075], [9.999900189704238, 1.0000001535413956], [9.999919673591801, 0.9999982095214766], [9.999938404758197, 0.9999925119288909], [9.999955665246668, 0.9999832791495612], [9.999970793470819, 0.9999708650714649], [9.999983209572939, 0.9999557455202851], [9.999992437649679, 0.9999385000212361], [9.999998123993217, 0.9999197895861321], [10.000000050648726, 0.9999003313771063], [10.000010148025869, 0.020386546319412994], [29.99999507326125, 1.497440845759323e-8], [29.999999555920844, 4.3906304398087087e-7], [29.99999998502795, 0.000004870917164687769], [29.999999985105863, 1.9999951573833497], [29.999999560229313, 1.999999569456975], [29.999995099003108, 2.000000029875097], [0.000004900996893631207, 2.000000029875097], [4.397706744600083e-7, 1.999999569456975], [1.489414954562251e-8, 1.9999951573833894], [0.0015371637167360477, 1.000002364293075]]], [[[1.4972047690880916e-8, 0.000004870917155344889], [4.4407915560926857e-7, 4.390630445772249e-7], [0.0000049267387479317335, 1.497440845759323e-8], [0.00009806485682730713, 2.9805899505592034e-7], [0.00008057633554534632, 0.0000019843341333453064], [0.00006180997515592718, 0.000007598079494486938], [0.0000445187648792545, 0.000016727463821629017], [0.000029368046749337576, 0.000029021200606014553], [0.000016940799547362766, 0.00004400624346903811], [0.000007715206588066703, 0.00006110598835085726], [0.00000204625592147356, 0.00007966246044143109], [2.995522223425946e-7, 0.00009745909243872494], [1.4972047690880916e-8, 0.000004870917155344889]]]]
*
* pgs = [[ //multiPolygon
* [
* [0, 0],
* [30, 0],
* [30, 2],
* [0, 2],
* ],
* [
* [0, 0],
* [10, 0],
* [10, 1],
* [0, 1],
* ]
* ]]
* r = fixGeometryMultiPolygon(pgs)
* console.log(JSON.stringify(r))
* // => [[[[0.0015371637167360477, 1.000002364293075], [9.999900189704238, 1.0000001535413956], [9.999919673591801, 0.9999982095214766], [9.999938404758197, 0.9999925119288909], [9.999955665246668, 0.9999832791495612], [9.999970793470819, 0.9999708650714649], [9.999983209572939, 0.9999557455202851], [9.999992437649679, 0.9999385000212361], [9.999998123993217, 0.9999197895861321], [10.000000050648726, 0.9999003313771063], [10.000010148025869, 0.020386546319412994], [29.99999507326125, 1.497440845759323e-8], [29.999999555920844, 4.3906304398087087e-7], [29.99999998502795, 0.000004870917164687769], [29.999999985105863, 1.9999951573833497], [29.999999560229313, 1.999999569456975], [29.999995099003108, 2.000000029875097], [0.000004900996893631207, 2.000000029875097], [4.397706744600083e-7, 1.999999569456975], [1.489414954562251e-8, 1.9999951573833894], [0.0015371637167360477, 1.000002364293075]]], [[[1.4972047690880916e-8, 0.000004870917155344889], [4.4407915560926857e-7, 4.390630445772249e-7], [0.0000049267387479317335, 1.497440845759323e-8], [0.00009806485682730713, 2.9805899505592034e-7], [0.00008057633554534632, 0.0000019843341333453064], [0.00006180997515592718, 0.000007598079494486938], [0.0000445187648792545, 0.000016727463821629017], [0.000029368046749337576, 0.000029021200606014553], [0.000016940799547362766, 0.00004400624346903811], [0.000007715206588066703, 0.00006110598835085726], [0.00000204625592147356, 0.00007966246044143109], [2.995522223425946e-7, 0.00009745909243872494], [1.4972047690880916e-8, 0.000004870917155344889]]]]
*
* pgs = [[ //multiPolygon
* [
* [0, 0],
* [30, 0],
* [30, 2],
* [0, 2],
* ],
* [
* [1, 0],
* [10, 0],
* [10, 1],
* [1, 1],
* ]
* ]]
* r = fixGeometryMultiPolygon(pgs)
* console.log(JSON.stringify(r))
* // => [[[[1.4972047690880916e-8, 0.000004870917155344889], [4.4407915560926857e-7, 4.390630445772249e-7], [0.0000049267387479317335, 1.497440845759323e-8], [1.000004200923848, 0.002940711771548052], [1.0000001418723181, 0.999900985569779], [1.000002036116173, 0.9999203046467752], [1.0000077013962363, 0.9999388868022], [1.0000169199814777, 0.9999560178763972], [1.0000293375783202, 0.9999710394783875], [1.0000444769469907, 0.9999833742895019], [1.0000617562430016, 0.9999925482512149], [1.0000805113789035, 0.9999982087844425], [1.0001000215468387, 1.0000001383400934], [9.999900174166491, 1.0000001380711014], [9.999919660261853, 0.9999981966946103], [9.999938393998693, 0.9999925009017745], [9.999955657179884, 0.999983269052659], [9.999970787985113, 0.9999708550694856], [9.999983206343181, 0.99995573486829], [9.999992436170238, 0.9999384881136842], [9.999998123621497, 0.9999197759961898], [10.000000050656604, 0.9999003158841037], [10.000010148025869, 0.020386546319412994], [29.99999507326125, 1.497440845759323e-8], [29.999999555920844, 4.3906304398087087e-7], [29.99999998502795, 0.000004870917164687769], [29.999999985105863, 1.9999951573833497], [29.999999560229313, 1.999999569456975], [29.999995099003108, 2.000000029875097], [0.000004900996893631207, 2.000000029875097], [4.397706744600083e-7, 1.999999569456975], [1.489414954562251e-8, 1.9999951573833894], [1.4972047690880916e-8, 0.000004870917155344889]]]]
*
* pgs = [[ //multiPolygon
* [
* [0, 0],
* [30, 0],
* [30, 2],
* [0, 2],
* ],
* [
* [0, 0.5],
* [10, 0.5],
* [10, 1.5],
* [0, 1.5],
* ]
* ]]
* r = fixGeometryMultiPolygon(pgs)
* console.log(JSON.stringify(r))
* // => [[[[1.4972047690880916e-8, 0.000004870917155344889], [4.4407915560926857e-7, 4.390630445772249e-7], [0.0000049267387479317335, 1.497440845759323e-8], [29.99999507326125, 1.497440845759323e-8], [29.999999555920844, 4.3906304398087087e-7], [29.99999998502795, 0.000004870917164687769], [29.999999985105863, 1.9999951573833497], [29.999999560229313, 1.999999569456975], [29.999995099003108, 2.000000029875097], [0.000004900996893631207, 2.000000029875097], [4.397706744600083e-7, 1.999999569456975], [1.489414954562251e-8, 1.9999951573833894], [0.0011530813901156408, 1.5000020713424091], [9.999900172402961, 1.5000001792447202], [9.999919659692646, 1.4999982302186918], [9.999938394116294, 1.4999925278358137], [9.99995565759653, 1.499983290664326], [9.999970788436201, 1.4999708727584635], [9.999983206680765, 1.4999557500878122], [9.999992436347585, 1.4999385022937257], [9.99999812367, 1.4999197904720754], [10.00000005065697, 1.4999003318338948], [10.000000050797992, 0.5000999761110504], [9.99999811685868, 0.5000804796940861], [9.999992409269023, 0.5000617344631452], [9.999983147655499, 0.5000444617292844], [9.999970688402396, 0.5000293261422671], [9.999955510938227, 0.5000169101150291], [9.999938199287458, 0.5000076914126002], [9.999919419597397, 0.5000020247678552], [9.999899894505019, 0.5000001282315094], [0.0011527347727830106, 0.5000014752481151], [1.4972047690880916e-8, 0.000004870917155344889]]]]
*
* pgs = [[ //multiPolygon
* [
* [0, 0],
* [30, 0],
* [30, 2],
* [0, 2],
* ],
* [
* [0, 0.1],
* [10, 1],
* [0, 1.9],
* ]
* ]]
* r = fixGeometryMultiPolygon(pgs)
* console.log(JSON.stringify(r))
* // => [[[[1.4972047690880916e-8, 0.000004870917155344889], [4.4407915560926857e-7, 4.390630445772249e-7], [0.0000049267387479317335, 1.497440845759323e-8], [29.99999507326125, 1.497440845759323e-8], [29.999999555920844, 4.3906304398087087e-7], [29.99999998502795, 0.000004870917164687769], [29.999999985105863, 1.9999951573833497], [29.999999560229313, 1.999999569456975], [29.999995099003108, 2.000000029875097], [0.000004900996893631207, 2.000000029875097], [4.397706744600083e-7, 1.999999569456975], [1.489414954562251e-8, 1.9999951573833894], [0.0002922411406645481, 1.8999745622923148], [9.998906350816243, 1.0001011572306464], [9.998925696522443, 1.0000974213416731], [9.998943933308668, 1.0000899705538862], [9.998960353341364, 1.0000790940583504], [9.998974319301526, 1.000065214009869], [9.998985289121299, 1.0000488691416811], [9.998992837023545, 1.0000306938553383], [9.998996670047767, 1.0000113935973485], [9.998996639420906, 0.9999917174783173], [9.998992746331771, 0.9999724291973252], [9.998985141884864, 0.9999542774000806], [9.998974121235493, 0.9999379666213498], [9.998960112133709, 0.9999241299394874], [9.998943658321851, 0.9999133044044544], [9.99892539842995, 0.9999059101930335], [9.998906041188281, 0.9999022343003149], [0.00029208302913059816, 0.10002633650646778], [1.4972047690880916e-8, 0.000004870917155344889]]]]
*
* pgs = [[ //multiPolygon
* [
* [0, 0],
* [30, 0],
* [30, 2],
* [0, 2],
* ],
* [
* [0, 0],
* [10, 1],
* [0, 2],
* ]
* ]]
* r = fixGeometryMultiPolygon(pgs, { w: 0.1 })
* console.log(JSON.stringify(r))
* // => [[[[0.00001493562676801525, 0.004870917159739751], [0.00044407887408247094, 0.00043906331038822934], [0.0049267387507242605, 0.000014972007631596088], [29.995073261249274, 0.000014972007631596088], [29.99955592112591, 0.00043906331038842813], [29.999985064373227, 0.004870917159736172], [29.9999851419269, 1.9951573831632807], [29.999560229699426, 1.9995694566349922], [29.99509900303653, 2.0000298703100317], [0.004900996963469827, 2.0000298703100317], [0.00043977030057626383, 1.9995694566349926], [0.000014858073099092731, 1.9951573831632807], [0.0015348884924635973, 1.0386277975367435], [0.0003175207251453951, 1.8907939069468858], [0.001984832322072281, 1.9090372392814843], [0.007029565194554841, 1.9266592551841506], [0.015278720084231411, 1.9430550680328056], [0.02644931845677425, 1.957661815388661], [0.04015808041696531, 1.9699779675362736], [0.05593454467525001, 1.9795805438228191], [0.07323718582519159, 1.9861396463070775], [0.09147198071214062, 1.9894298103275292], [0.11001279027535979, 1.989337779082996], [9.016671974453434, 1.1006903809241837], [9.035853602287938, 1.0968092006025036], [9.05391072984683, 1.0892740950662076], [9.070151787761773, 1.07837352261084], [9.083954741413793, 1.064524859541747], [9.094790931645358, 1.048258420646872], [9.102245336883204, 1.03019714718097], [9.106032478086824, 1.0110327410947344], [9.106007355743246, 0.991499161311665], [9.102170999500421, 0.9723444995519088], [9.094670418473028, 0.9543023155537147], [9.083792955680968, 0.9380635321364007], [9.069955265138775, 0.9242499686190593], [9.053687336499435, 0.9133905275635273], [9.035612181926473, 0.9059009471844243], [9.016421965741754, 0.9020678941227268], [0.11024304431593406, 0.011026429282299877], [0.09168084895552824, 0.010881848524895], [0.07341663848111533, 0.014132082279345088], [0.05607968722019283, 0.02066486665262698], [0.04026722958033469, 0.030254879844295875], [0.02652390664930831, 0.0425715343608882], [0.015323033525298983, 0.05719038479346033], [0.007050327996649368, 0.07360775581914084], [0.0019906558094120565, 0.09125808606905769], [0.00031824404173219384, 0.1095333917675677], [0.00001493562676801525, 0.004870917159739751]]]]
*
*/
function fixGeometryMultiPolygon(pgs, opt = {}) {
//check pgs
if (!isearr(pgs)) {
throw new Error(`no pgs`)
}
//supposeType
let supposeType = get(opt, 'supposeType')
if (supposeType !== 'polygons' && supposeType !== 'ringStrings') {
supposeType = 'polygons'
}
//w
let w = get(opt, 'w', '')
if (!isnum(w)) {
w = 0.0001
}
w = cdbl(w)
//units
let units = get(opt, 'units', '')
if (!isestr(units)) {
units = 'degrees'
}
//fixCloseMultiPolygon裡面已有toMultiPolygon故不用另外呼叫處理
//fixCloseMultiPolygon
pgs = fixCloseMultiPolygon(pgs, { supposeType })
// console.log('fixCloseMultiPolygon pgs', JSON.stringify(pgs))
//multiPolygon
pgs = turf.multiPolygon(pgs)
// console.log('turf.multiPolygon pgs', JSON.stringify(pgs))
//buffer
let pgsBf = turf.buffer(pgs, w, { units }) //膨脹
pgsBf = turf.buffer(pgsBf, -w, { units }) //侵蝕
// console.log('turf.buffer*2 pgs', JSON.stringify(pgsBf))
//distilMultiPolygon, 因buff後可能為多種幾何類型, 故需先統一轉成MultiPolygon
let pgsNew = distilMultiPolygon(pgsBf)
return pgsNew
}
export default fixGeometryMultiPolygon