Merge multiple google map polygons into one polygon, javascript

4k views Asked by At

I have situation as shown on the picture below: multiple polygons of on area

I have coordinates (lng, lat) stored in array:

[[[57.25810745004698,-2.147463220484724],[57.24900726567703,-2.150918704035007],[57.25395911797542,-2.158019360659523],[57.2542436749531,-2.1585316191217316],[57.25562976522241,-2.16204890398501],[57.255265155779064,-2.166495336569369],[57.25798151256145,-2.167941564091734],[57.26103327914291,-2.170952645669786],[57.26195297322447,-2.1712398688392796],[57.263941808143194,-2.1758858089264095],[57.262590574341885,-2.178759042359161],[57.26221269128632,-2.1790757247168813],[57.26021300373163,-2.179682366744146],[57.2592871955657,-2.1823633430306018],[57.258418457313304,-2.1836810097764783],[57.256176098574606,-2.1856606588164595],[57.2553347850735,-2.18757815372237],[57.256918901027355,-2.190189504139994],[57.25717970880296,-2.192757589547682],[57.25701652751402,-2.194442692810071],[57.25765640927138,-2.1946879937739823],[57.26055079982741,-2.196543417199109],[57.26478881581858,-2.2006584264936464],[57.263687541299376,-2.2039507974741355],[57.25990330429585,-2.205804063286223],[57.259035389580745,-2.207399906212572],[57.24392366085095,-2.201780654062077],[57.24341463253441,-2.201438840807782],[57.243391919080906,-2.2013868230712887],[57.23896473934729,-2.2008162090943415],[57.2330877224033,-2.195634699274251],[57.23225989180574,-2.1952876484691615],[57.22970587442209,-2.1857139872646485],[57.230166308161756,-2.181112779287332],[57.23910030860912,-2.1846090305614325],[57.239379675553394,-2.185230674365471],[57.24527003782118,-2.189113719863144],[57.249165078583616,-2.1857956765362587],[57.2488130196409,-2.183334243413128],[57.254529837622705,-2.1780292935516172],[57.25476006711664,-2.1773776724736535],[57.25270014298488,-2.1754677767077055],[57.245608223622156,-2.178762057627864],[57.2434065357033,-2.1775314104510244],[57.24131892120943,-2.172730537291841],[57.23797196694906,-2.1712900074030586],[57.22888378462048,-2.1785029117943395],[57.22770637655766,-2.1781188512285325],[57.223020680101996,-2.179548323087033],[57.21999041140472,-2.1887346755856925],[57.21821077185559,-2.1883313741631127],[57.2169071916862,-2.1886449466242084],[57.21414368150896,-2.191890039815007],[57.21383513551693,-2.192974370058664],[57.21366427363764,-2.1931023016069275],[57.211581242048744,-2.19100435222515],[57.21105447849438,-2.190854585948955],[57.20983563547255,-2.1909713817542524],[57.20892645384228,-2.1914780049642104],[57.20870937128266,-2.1921246214916437],[57.20844263388141,-2.1935296563563043],[57.20867609158227,-2.194079290493619],[57.208415554975446,-2.195331127123268],[57.207087078982696,-2.1992682539010957],[57.206183154834996,-2.200612734338165],[57.202931744899544,-2.1989385069007312],[57.20066535884702,-2.1980894378966696],[57.19658481429228,-2.198096757766507],[57.19472809744357,-2.201542261508962],[57.19508529482034,-2.2031938058873948],[57.194457945669626,-2.2046302214108664],[57.19299231499937,-2.210040974178696],[57.19613997064014,-2.211935395376713],[57.1950283337509,-2.218875008504938],[57.194862729218286,-2.218971716997885],[57.194784481560426,-2.2198323934775317],[57.19747621997526,-2.228796542221289],[57.19957125772065,-2.2297874257225203],[57.204008133944555,-2.2347315017625533],[57.192044735842416,-2.2392415404639223],[57.191595573397706,-2.2479130669482856],[57.19940491062111,-2.253603110899462],[57.18781461508141,-2.2585316830325155],[57.18381625930881,-2.261955166654616],[57.189981876398896,-2.2678804346330708],[57.18640793084923,-2.2718932829943697],[57.18032391472698,-2.2757943604801767],[57.18023079798073,-2.275896161066271],[57.184267458906916,-2.2822775617319166],[57.18052942461445,-2.2882614974557782],[57.17778399725451,-2.289436159742877],[57.17615020366205,-2.2923108279285316],[57.177442469352215,-2.295630169435981],[57.171696558827676,-2.307560697432109],[57.17232869030713,-2.3104086567631157],[57.18074981744165,-2.310094046875406],[57.186329876119984,-2.3119827518669354],[57.18779022514903,-2.3179814340529674],[57.19684450908027,-2.3238135903201282],[57.19658653554036,-2.3286670990429457],[57.199109344187264,-2.332185653309125],[57.20688289426475,-2.334044277594444],[57.2080804775012,-2.323497140950849],[57.21216966490129,-2.3214254368303955],[57.21934749681765,-2.321774932541871],[57.216607840561764,-2.306326768259737],[57.21699599309105,-2.30608844181279],[57.23139012178608,-2.302701647022559],[57.2344310379044,-2.299341773528795],[57.24525298111021,-2.300591181151958],[57.25061897332306,-2.297811914048225],[57.25177117235992,-2.293244397981539],[57.26749361770455,-2.2839162784914606],[57.27188986875272,-2.2867920506716928],[57.28191952101617,-2.2855323404749015],[57.282371135131555,-2.2852815566102436],[57.27909621341579,-2.2703776561784252],[57.28511186835022,-2.263609182590926],[57.28613058036221,-2.2613846268648103],[57.296530559891885,-2.265142264742053],[57.302839985595014,-2.265236494891269],[57.30550686097516,-2.2689143990473895],[57.30627663940224,-2.2788695416078326],[57.31548136673972,-2.2786756755112947],[57.32034942932915,-2.2799802390014747],[57.32739228239798,-2.2660075487203812],[57.325473345212444,-2.264161241190095],[57.32725190637747,-2.2567499187206295],[57.32070333980549,-2.2423544212001616],[57.31955996938771,-2.2414738465782875],[57.31789651014708,-2.23811668641531],[57.323560865746515,-2.233271654463124],[57.32443896125923,-2.2319456740112855],[57.32206960585557,-2.223265432561675],[57.314597400060805,-2.2202324261647846],[57.314216116069446,-2.2151226743816324],[57.312293504238646,-2.2141155821775556],[57.30291653305257,-2.2221455407909616],[57.296787525483865,-2.22010205864035],[57.30037223516952,-2.210375272278384],[57.29447737361828,-2.2003676368794913],[57.291291940751464,-2.199808265548654],[57.290292288239634,-2.199159207357752],[57.294111964506584,-2.1792466362812775],[57.29160802710085,-2.178120260886999],[57.29012283608672,-2.162756179310122],[57.29265716743792,-2.1604217040770664],[57.28738115128695,-2.1489666032638297],[57.27614485995739,-2.1611002912486583],[57.274778026302464,-2.1604081087145914],[57.273923743679504,-2.1592229515200643],[57.27309882282732,-2.1569867151180233],[57.27189872866945,-2.151559199860799],[57.26344411200469,-2.148724568745024],[57.26020991186048,-2.146999136608315],[57.258681523694484,-2.1471809535053126]],[[57.19958874119088,-2.1862651181461388],[57.198958622004135,-2.1857662783374963],[57.197886191751316,-2.1874073133353704],[57.19960753721797,-2.1886080081865202],[57.19977586842689,-2.1885000008994666]],[[57.20276299348534,-2.1684979123057246],[57.19609435975556,-2.1712643409939574],[57.19704486438276,-2.175332918972458],[57.197117895793795,-2.1753852867170735],[57.1983121522528,-2.1758853069241013],[57.19957009271968,-2.1759021947684687],[57.20127681802609,-2.1744864269614936],[57.201802527442034,-2.1738347230373165]],[[57.19670568556834,-2.1817270142890948],[57.19616982725822,-2.1832767460875857],[57.1976934113878,-2.1843511573194974],[57.19847756144182,-2.184626092308804],[57.20011544801716,-2.1840971903513946],[57.20057558358222,-2.183049433076576],[57.199843637318935,-2.1817445107790263],[57.19835289889606,-2.18133257279635],[57.19711974917167,-2.1815338255069037],[57.19683897401431,-2.1816290589318896]]]

So, it is an array of 4 polygons, and polygons are arrays of coordinates.

I need these polygons to be merged and stored as only one array. Can anyone help?

I use javascript, google maps, node.js.

1

There are 1 answers

0
bcdan On

It is not possible to have all the polylines stored in flat array (like [[1,1],[2,2],[3,3],[4,4],[5,5]] instead of [[[1,1],[2,2],[3,3]],[[4,4],[5,5]]]) and still maintain polylines. The effect from combining distinct loops would be a bunch of crossed lines that you'd not likely want, as well as messed up boundaries.

If you need to figure out if something is inside of them, iterate through a list of polylines with a for loop.

var polylines = [.....];
for(var i=0; i<polylines.length; i++){
  //do whatever with polylines[i]
}

I cannot imagine what you would need to do with a single array instead of an array of arrays. It is probably better to stay true to the form of polylines and use iterative statements, and also take advantage of polyline functions Google offers.

If you need to find bounds of all of the points, iterate through the points, iterating through the polylines, and make a temporary array for those.

var temp_points;
for(var i=0; i<polylines.length; i++){
  for(var j=0; j<polylines[i].length; j++){
    temp_points.push(new google.maps.LatLng(polylines[i][j][0], polylines[i][j][1]));
  }
}
//calculate using temp_points;

But it is simply a bad idea to have permanently joined disjoint polylines.