Index: data/Join Areas Tests.osm
===================================================================
--- data/Join Areas Tests.osm	(revision 0)
+++ data/Join Areas Tests.osm	(revision 0)
@@ -0,0 +1,590 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<osm version='0.5' generator='JOSM'>
+  <bounds minlat='49.6428455937926' minlon='8.97891998291015' maxlat='49.6659612524238' maxlon='9.01926040649414' origin='JOSM' />
+  <node id='-1' lat='49.665557350679464' lon='8.980018030697323'>
+    <tag k='name' v='Multipolygon' />
+  </node>
+  <node id='-2' lat='49.66293444373006' lon='8.979904268383734' />
+  <node id='-3' lat='49.66309431357888' lon='8.98771163350794' />
+  <node id='-4' lat='49.66550322539429' lon='8.987481709734286' />
+  <node id='-5' lat='49.661140812390876' lon='8.980754551545285' />
+  <node id='-6' lat='49.66414787714609' lon='8.98167662069808' />
+  <node id='-7' lat='49.66193599760082' lon='8.983668051096597' />
+  <node id='-8' lat='49.66423062929877' lon='8.985896954118042' />
+  <node id='-9' lat='49.66122094281029' lon='8.987593944961972' />
+  <node id='-10' lat='49.665246272951265' lon='8.996673744036638'>
+    <tag k='name' v='No Additional Inner Nodes' />
+  </node>
+  <node id='-11' lat='49.66260897027721' lon='8.996995205661822' />
+  <node id='-12' lat='49.66247822670222' lon='9.006405902938653' />
+  <node id='-13' lat='49.66509579743186' lon='9.006492938770437' />
+  <node id='-14' lat='49.663673852556485' lon='9.001872836660935' />
+  <node id='-15' lat='49.66356462957665' lon='9.003786018319271' />
+  <node id='-16' lat='49.659381328509475' lon='9.002998050852923' />
+  <node id='-17' lat='49.66102960777179' lon='9.004102187558955' />
+  <node id='-18' lat='49.658143795735675' lon='9.001321365825214' />
+  <node id='-19' lat='49.65470614469758' lon='9.000967096568774' />
+  <node id='-20' lat='49.65966007592634' lon='8.995756927212474'>
+    <tag k='name' v='With Inner Nodes, resolve tag conflicts beforehand' />
+  </node>
+  <node id='-21' lat='49.65482452933729' lon='8.99800181533644' />
+  <node id='-22' lat='49.658215448844345' lon='8.998101909487305' />
+  <node id='-23' lat='49.656334599426415' lon='8.99596685442522' />
+  <node id='-24' lat='49.65622420968986' lon='8.999630518959972' />
+  <node id='-25' lat='49.65595437351232' lon='9.002670090930014' />
+  <node id='-26' lat='49.664767306664714' lon='9.00358600531296' />
+  <node id='-27' lat='49.652897713964435' lon='8.995398194580462'>
+    <tag k='name' v='Overlapping Itself' />
+  </node>
+  <node id='-28' lat='49.66298227683501' lon='9.007574342263663' />
+  <node id='-29' lat='49.657294551336726' lon='9.005523880219158' />
+  <node id='-30' lat='49.64759565941927' lon='8.995601060044594' />
+  <node id='-31' lat='49.659323888301465' lon='9.00052935158495' />
+  <node id='-32' lat='49.65131105391522' lon='9.00298658642922' />
+  <node id='-33' lat='49.64965980248303' lon='9.003039719712563' />
+  <node id='-34' lat='49.647526853143034' lon='8.99921412331189' />
+  <node id='-35' lat='49.65151745640557' lon='8.997992057795011' />
+  <node id='-36' lat='49.648042897844014' lon='9.003942985529385' />
+  <node id='-37' lat='49.6534782364111' lon='9.003730452396017' />
+  <node id='-38' lat='49.64965980248303' lon='8.997301325111557' />
+  <node id='-39' lat='49.64862774189198' lon='8.99756699152827' />
+  <node id='-40' lat='49.650123242376026' lon='8.984223634986959' />
+  <node id='-41' lat='49.64837612802103' lon='8.982669694368326' />
+  <node id='-42' lat='49.65267519581664' lon='8.982531354736969'>
+    <tag k='name' v='Joining an two areas with self-overlap' />
+  </node>
+  <node id='-43' lat='49.648702988205066' lon='8.99108732452821' />
+  <node id='-44' lat='49.6498411096278' lon='8.989584718400737' />
+  <node id='-45' lat='49.65180728142213' lon='8.98635078159476' />
+  <node id='-46' lat='49.65154365880048' lon='8.989894759403137' />
+  <node id='-47' lat='49.64821655131596' lon='8.986618619738293' />
+  <node id='-48' lat='49.65263419499493' lon='8.992015874465995' />
+  <node id='-49' lat='49.64910487989931' lon='8.98434746261959' />
+  <node id='-50' lat='49.661575265881794' lon='9.002417415068946' />
+  <node id='-51' lat='49.66111564855195' lon='8.994837077867599' />
+  <node id='-52' lat='49.664258008517386' lon='8.994834196254615' />
+  <node id='-53' lat='49.66327697326473' lon='8.996037451903568' />
+  <node id='-54' lat='49.66163205844883' lon='8.996182583343169' />
+  <node id='-55' lat='49.66336086872286' lon='9.000172520579218' />
+  <node id='-56' lat='49.66439890567706' lon='9.000253362850133' />
+  <node id='-57' lat='49.653656418352305' lon='8.980925219001294' />
+  <node id='-58' lat='49.65083240952062' lon='8.981009751040055' />
+  <node id='-59' lat='49.65093842995153' lon='8.98362536207706' />
+  <node id='-60' lat='49.65366266054767' lon='8.983765181752341' />
+  <node id='-61' lat='49.65918199608578' lon='8.984744990288629' />
+  <node id='-62' lat='49.65684188230803' lon='8.984902160770126' />
+  <node id='-63' lat='49.65687579780412' lon='8.979558364399258' />
+  <node id='-64' lat='49.65918199608578' lon='8.97945358407826'>
+    <tag k='name' v='Intersection already have nodes added' />
+  </node>
+  <node id='-65' lat='49.65545132661812' lon='8.981025288893223' />
+  <node id='-66' lat='49.65558501959522' lon='8.988437026602341' />
+  <node id='-67' lat='49.65931567881406' lon='8.986865321787379'>
+    <tag k='name' v='SOME Intersection already have nodes added' />
+  </node>
+  <node id='-68' lat='49.65700948686801' lon='8.986970102108378' />
+  <node id='-69' lat='49.65687579780412' lon='8.980815728251226' />
+  <node id='-70' lat='49.65809673992071' lon='8.980815728251226' />
+  <node id='-71' lat='49.65792716645855' lon='8.983382846115664' />
+  <node id='-72' lat='49.65701145955204' lon='8.98306850515267' />
+  <node id='-73' lat='49.65700948686801' lon='8.988227465960346' />
+  <node id='-74' lat='49.660221488976575' lon='8.977137289660517' />
+  <node id='-75' lat='49.66019710027813' lon='9.021519330497165' />
+  <node id='-76' lat='49.66824309940781' lon='8.99379608098765' />
+  <node id='-77' lat='49.65823042563044' lon='8.988227465960346' />
+  <node id='-78' lat='49.65931567881406' lon='8.992156727997747' />
+  <node id='-79' lat='49.65697557146509' lon='8.992313898479246' />
+  <node id='-80' lat='49.65806085263415' lon='8.990794583824783' />
+  <node id='-81' lat='49.6423143331186' lon='8.993645046699799' />
+  <node id='-82' lat='49.66770174614116' lon='9.008788568211484' />
+  <node id='-83' lat='49.64184704638359' lon='9.008847909558769' />
+  <node id='-84' lat='49.654142227092336' lon='8.97793106762297' />
+  <node id='-85' lat='49.65418064466472' lon='9.02166564057246' />
+  <node id='-86' lat='49.665325869211465' lon='9.010629016616129'>
+    <tag k='name' v='Member of some (i.e. not multigon) relation' />
+  </node>
+  <node id='-87' lat='49.66089950426598' lon='9.010651964271892' />
+  <node id='-88' lat='49.66084008723247' lon='9.01744447037755' />
+  <node id='-89' lat='49.66210268358402' lon='9.017513313344837' />
+  <node id='-90' lat='49.662236368338334' lon='9.011707556436962' />
+  <node id='-91' lat='49.663989090028004' lon='9.01179934706001' />
+  <node id='-92' lat='49.663929676767346' lon='9.01471369934183' />
+  <node id='-93' lat='49.66156794089354' lon='9.014644856374543' />
+  <node id='-94' lat='49.66149367061061' lon='9.016824883671967' />
+  <node id='-95' lat='49.665266457583265' lon='9.016733093048918' />
+  <node id='-96' lat='49.6657566013461' lon='9.017559208656362' />
+  <node id='-97' lat='49.66572689580407' lon='9.018683643788718' />
+  <node id='-98' lat='49.66467233731082' lon='9.018729539100242' />
+  <node id='-99' lat='49.65954150350348' lon='9.010225255249217'>
+    <tag k='name' v='One Area already part of multipolygon (as outer)' />
+  </node>
+  <node id='-100' lat='49.65948208481168' lon='9.016329331682007' />
+  <node id='-101' lat='49.65507724377221' lon='9.016274166792183' />
+  <node id='-102' lat='49.65831483593041' lon='9.011804408209608' />
+  <node id='-103' lat='49.656561909834345' lon='9.011712617586559' />
+  <node id='-104' lat='49.65825541574074' lon='9.014718760491427' />
+  <node id='-105' lat='49.655114612313845' lon='9.01024820290498' />
+  <node id='-106' lat='49.65649630973553' lon='9.01456121513066' />
+  <node id='-107' lat='49.659983770585875' lon='9.015660786026782' />
+  <node id='-108' lat='49.65777838791238' lon='9.0156778202983' />
+  <node id='-109' lat='49.6577783879124' lon='9.01859068072843' />
+  <node id='-110' lat='49.659950690584154' lon='9.018522543642344' />
+  <node id='-111' lat='49.650467504431084' lon='9.01556653387284' />
+  <node id='-112' lat='49.650467504431084' lon='9.012653673442712' />
+  <node id='-113' lat='49.649036528490285' lon='9.01626713010843' />
+  <node id='-114' lat='49.651484566652904' lon='9.01400037262895' />
+  <node id='-115' lat='49.653441916259965' lon='9.016322294998254' />
+  <node id='-116' lat='49.65267321841768' lon='9.012636639171191' />
+  <node id='-117' lat='49.652640133446276' lon='9.015498396786755' />
+  <node id='-118' lat='49.64972521590133' lon='9.013842827268181' />
+  <node id='-119' lat='49.64907390166997' lon='9.010241166221228' />
+  <node id='-120' lat='49.649790825127035' lon='9.010994229724082' />
+  <node id='-121' lat='49.65350134232707' lon='9.010218218565463'>
+    <tag k='name' v='One Area already part of multipolygon (as inner)' />
+  </node>
+  <node id='-122' lat='49.651543995109876' lon='9.011086020347133' />
+  <node id='-123' lat='49.64702795382207' lon='9.021373906176958' />
+  <node id='-124' lat='49.64698953060391' lon='8.97763933322747' />
+  <node id='-125' lat='49.6440995060445' lon='9.015089670434026' />
+  <node id='-126' lat='49.64633804682556' lon='9.016044421132413' />
+  <node id='-127' lat='49.64329477394559' lon='9.00982115694612' />
+  <node id='-128' lat='49.64474487637503' lon='9.013313148784425' />
+  <node id='-129' lat='49.646452701397266' lon='9.00974135512665'>
+    <tag k='name' v='Both part of other relations' />
+  </node>
+  <node id='-130' lat='49.645694403782294' lon='9.012131348650524' />
+  <node id='-131' lat='49.64477854294109' lon='9.010865000644996' />
+  <node id='-132' lat='49.64566131406722' lon='9.01503858959705' />
+  <node id='-133' lat='49.64333102750582' lon='9.015932402078883' />
+  <node id='-134' lat='49.6436516438452' lon='9.013337536747516' />
+  <node id='-135' lat='49.6440995060445' lon='9.012176810003897' />
+  <node id='-136' lat='49.64364363066422' lon='9.010830064185653' />
+  <node id='-137' lat='49.64634787643868' lon='9.01859251804108' />
+  <node id='-138' lat='49.64442618533402' lon='9.018507236795525' />
+  <node id='-139' lat='49.64446300005248' lon='9.017591884759854' />
+  <node id='-140' lat='49.646377327053195' lon='9.017591884759854' />
+  <node id='-141' action='modify' lat='49.645108516691536' lon='9.00501200093294' />
+  <node id='-142' action='modify' lat='49.644279396738646' lon='8.99701844322583' />
+  <node id='-143' action='modify' lat='49.643570600890634' lon='9.005424239693767' />
+  <node id='-144' action='modify' lat='49.64579713321828' lon='9.001725329495672' />
+  <node id='-145' action='modify' lat='49.645870282835396' lon='9.000573288940334' />
+  <node id='-146' action='modify' lat='49.644533441390884' lon='9.001738472218983' />
+  <node id='-147' action='modify' lat='49.64620102922634' lon='8.995285052653605' />
+  <node id='-148' action='modify' lat='49.645549394972946' lon='8.997512960693886' />
+  <node id='-149' action='modify' lat='49.64390917555046' lon='9.00236900726505' />
+  <node id='-150' action='modify' lat='49.643583272941804' lon='8.996426078862356' />
+  <node id='-151' action='modify' lat='49.645064815871' lon='8.999528632035787' />
+  <node id='-152' action='modify' lat='49.64660348405886' lon='9.007804150387257' />
+  <node id='-153' action='modify' lat='49.644215448513066' lon='8.987601210318644' />
+  <node id='-154' action='modify' lat='49.64509437228782' lon='8.985632145182455' />
+  <node id='-155' action='modify' lat='49.64512070290696' lon='8.990191229749867' />
+  <node id='-156' action='modify' lat='49.64400653203681' lon='8.985527741087623' />
+  <node id='-157' action='modify' lat='49.64362715512701' lon='8.983795590458412' />
+  <node id='-158' action='modify' lat='49.6438359138228' lon='8.988873075900425' />
+  <node id='-159' action='modify' lat='49.64624752120218' lon='8.990209078516749' />
+  <node id='-160' action='modify' lat='49.64561767902291' lon='8.987111582364214' />
+  <node id='-161' lat='49.664846643627826' lon='9.02334771024883' />
+  <node id='-162' lat='49.66903704959538' lon='9.022880487521558' />
+  <node id='-163' lat='49.671931242953896' lon='9.026017554404675' />
+  <node id='-164' lat='49.66722269132057' lon='9.026351284924155' />
+  <node id='-165' lat='49.6687778596993' lon='9.028553906352727' />
+  <node id='-166' lat='49.66476023970688' lon='9.029688590118962' />
+  <node id='-167' action='modify' lat='49.64560080131112' lon='8.988407502515859' />
+  <node id='-168' action='modify' lat='49.64619224514668' lon='8.984061673983168' />
+  <node id='-169' action='modify' lat='49.64654691824636' lon='8.982418392157395' />
+  <node id='-170' action='modify' lat='49.64294039575888' lon='8.98264180269116' />
+  <node id='-171' lat='49.66726589111355' lon='9.018808975183893' />
+  <node id='-172' lat='49.67180165588925' lon='9.019276197911166'>
+    <tag k='name' v='out of bounds' />
+  </node>
+  <node id='-173' action='modify' lat='49.64305580574678' lon='8.992869020494213' />
+  <node id='-174' action='modify' lat='49.6465677851024' lon='8.992596999330347' />
+  <way id='-175'>
+    <nd ref='-145' />
+    <nd ref='-146' />
+    <nd ref='-142' />
+    <nd ref='-148' />
+    <nd ref='-145' />
+  </way>
+  <way id='-176'>
+    <nd ref='-66' />
+    <nd ref='-73' />
+    <nd ref='-77' />
+    <nd ref='-80' />
+    <nd ref='-66' />
+    <tag k='parking' v='surface' />
+    <tag k='amenity' v='parking' />
+  </way>
+  <way id='-177'>
+    <nd ref='-168' />
+    <nd ref='-159' />
+    <nd ref='-155' />
+    <nd ref='-154' />
+    <nd ref='-156' />
+    <nd ref='-153' />
+    <nd ref='-160' />
+    <nd ref='-167' />
+    <nd ref='-158' />
+    <nd ref='-157' />
+    <nd ref='-168' />
+    <tag k='name' v='Thing in the Middle' />
+  </way>
+  <way id='-178'>
+    <nd ref='-67' />
+    <nd ref='-68' />
+    <nd ref='-73' />
+    <nd ref='-79' />
+    <nd ref='-78' />
+    <nd ref='-67' />
+    <tag k='parking' v='surface' />
+    <tag k='amenity' v='parking' />
+  </way>
+  <way id='-179'>
+    <nd ref='-116' />
+    <nd ref='-112' />
+    <nd ref='-111' />
+    <nd ref='-117' />
+    <nd ref='-116' />
+  </way>
+  <way id='-180'>
+    <nd ref='-26' />
+    <nd ref='-15' />
+    <nd ref='-28' />
+    <tag k='highway' v='footway' />
+  </way>
+  <way id='-181'>
+    <nd ref='-76' />
+    <nd ref='-81' />
+    <tag k='highway' v='motorway' />
+    <tag k='lanes' v='5' />
+  </way>
+  <way id='-182'>
+    <nd ref='-113' />
+    <nd ref='-115' />
+    <nd ref='-121' />
+    <nd ref='-119' />
+    <nd ref='-113' />
+    <tag k='parking' v='surface' />
+    <tag k='amenity' v='parking' />
+  </way>
+  <way id='-183'>
+    <nd ref='-137' />
+    <nd ref='-138' />
+    <nd ref='-139' />
+    <nd ref='-140' />
+    <nd ref='-137' />
+  </way>
+  <way id='-184'>
+    <nd ref='-96' />
+    <nd ref='-97' />
+    <nd ref='-98' />
+  </way>
+  <way id='-185'>
+    <nd ref='-134' />
+    <nd ref='-136' />
+    <nd ref='-131' />
+    <nd ref='-128' />
+    <nd ref='-134' />
+  </way>
+  <way id='-186'>
+    <nd ref='-101' />
+    <nd ref='-100' />
+    <nd ref='-99' />
+    <nd ref='-105' />
+    <nd ref='-101' />
+    <tag k='parking' v='surface' />
+    <tag k='amenity' v='parking' />
+  </way>
+  <way id='-187'>
+    <nd ref='-124' />
+    <nd ref='-123' />
+    <tag k='highway' v='motorway' />
+    <tag k='lanes' v='5' />
+  </way>
+  <way id='-188'>
+    <nd ref='-25' />
+    <nd ref='-16' />
+    <nd ref='-20' />
+    <nd ref='-23' />
+    <nd ref='-24' />
+    <nd ref='-25' />
+    <tag k='landuse' v='retail' />
+  </way>
+  <way id='-189'>
+    <nd ref='-130' />
+    <nd ref='-135' />
+    <nd ref='-125' />
+    <nd ref='-132' />
+    <nd ref='-130' />
+  </way>
+  <way id='-190'>
+    <nd ref='-65' />
+    <nd ref='-69' />
+    <nd ref='-70' />
+    <nd ref='-71' />
+    <nd ref='-72' />
+    <nd ref='-65' />
+    <tag k='parking' v='surface' />
+    <tag k='amenity' v='parking' />
+  </way>
+  <way id='-191'>
+    <nd ref='-133' />
+    <nd ref='-126' />
+    <nd ref='-129' />
+    <nd ref='-127' />
+    <nd ref='-133' />
+    <tag k='parking' v='surface' />
+    <tag k='amenity' v='parking' />
+  </way>
+  <way id='-192'>
+    <nd ref='-74' />
+    <nd ref='-75' />
+    <tag k='highway' v='motorway' />
+    <tag k='lanes' v='5' />
+  </way>
+  <way id='-193'>
+    <nd ref='-31' />
+    <nd ref='-18' />
+    <nd ref='-29' />
+    <tag k='highway' v='footway' />
+  </way>
+  <way id='-194'>
+    <nd ref='-172' />
+    <nd ref='-171' />
+    <nd ref='-164' />
+    <nd ref='-163' />
+    <nd ref='-172' />
+    <tag k='parking' v='surface' />
+    <tag k='amenity' v='parking' />
+  </way>
+  <way id='-195'>
+    <nd ref='-17' />
+    <nd ref='-51' />
+    <nd ref='-52' />
+    <nd ref='-56' />
+    <nd ref='-55' />
+    <nd ref='-53' />
+    <nd ref='-54' />
+    <nd ref='-50' />
+    <nd ref='-14' />
+    <nd ref='-15' />
+    <nd ref='-17' />
+    <tag k='parking' v='surface' />
+    <tag k='amenity' v='parking' />
+  </way>
+  <way id='-196'>
+    <nd ref='-107' />
+    <nd ref='-108' />
+    <nd ref='-109' />
+    <nd ref='-110' />
+    <nd ref='-107' />
+  </way>
+  <way id='-197'>
+    <nd ref='-150' />
+    <nd ref='-147' />
+    <nd ref='-152' />
+    <nd ref='-143' />
+    <nd ref='-150' />
+    <tag k='parking' v='surface' />
+    <tag k='amenity' v='parking' />
+  </way>
+  <way id='-198'>
+    <nd ref='-86' />
+    <nd ref='-87' />
+    <nd ref='-88' />
+    <nd ref='-89' />
+    <nd ref='-90' />
+    <nd ref='-91' />
+    <nd ref='-92' />
+    <nd ref='-93' />
+    <nd ref='-94' />
+    <nd ref='-95' />
+    <nd ref='-86' />
+    <tag k='parking' v='surface' />
+    <tag k='amenity' v='parking' />
+  </way>
+  <way id='-199'>
+    <nd ref='-10' />
+    <nd ref='-11' />
+    <nd ref='-12' />
+    <nd ref='-13' />
+    <nd ref='-10' />
+    <tag k='parking' v='surface' />
+    <tag k='amenity' v='parking' />
+  </way>
+  <way id='-200'>
+    <nd ref='-151' />
+    <nd ref='-144' />
+    <nd ref='-141' />
+    <nd ref='-149' />
+    <nd ref='-151' />
+  </way>
+  <way id='-201'>
+    <nd ref='-82' />
+    <nd ref='-83' />
+    <tag k='highway' v='motorway' />
+    <tag k='lanes' v='5' />
+  </way>
+  <way id='-202'>
+    <nd ref='-1' />
+    <nd ref='-2' />
+    <nd ref='-3' />
+    <nd ref='-4' />
+    <nd ref='-1' />
+    <tag k='parking' v='surface' />
+    <tag k='amenity' v='parking' />
+  </way>
+  <way id='-203'>
+    <nd ref='-84' />
+    <nd ref='-85' />
+    <tag k='highway' v='motorway' />
+    <tag k='lanes' v='5' />
+  </way>
+  <way id='-204'>
+    <nd ref='-64' />
+    <nd ref='-63' />
+    <nd ref='-69' />
+    <nd ref='-72' />
+    <nd ref='-62' />
+    <nd ref='-61' />
+    <nd ref='-64' />
+    <tag k='parking' v='surface' />
+    <tag k='amenity' v='parking' />
+  </way>
+  <way id='-205'>
+    <nd ref='-42' />
+    <nd ref='-48' />
+    <nd ref='-43' />
+    <nd ref='-49' />
+    <nd ref='-40' />
+    <nd ref='-44' />
+    <nd ref='-46' />
+    <nd ref='-45' />
+    <nd ref='-47' />
+    <nd ref='-41' />
+    <nd ref='-42' />
+    <tag k='parking' v='surface' />
+    <tag k='amenity' v='parking' />
+  </way>
+  <way id='-206'>
+    <nd ref='-118' />
+    <nd ref='-120' />
+    <nd ref='-122' />
+    <nd ref='-114' />
+    <nd ref='-118' />
+  </way>
+  <way id='-207'>
+    <nd ref='-5' />
+    <nd ref='-6' />
+    <nd ref='-7' />
+    <nd ref='-8' />
+    <nd ref='-9' />
+    <nd ref='-5' />
+    <tag k='parking' v='surface' />
+    <tag k='amenity' v='parking' />
+  </way>
+  <way id='-208'>
+    <nd ref='-169' />
+    <nd ref='-170' />
+    <nd ref='-173' />
+    <nd ref='-174' />
+    <nd ref='-169' />
+    <tag k='parking' v='surface' />
+    <tag k='name' v='Outer blob' />
+    <tag k='amenity' v='parking' />
+  </way>
+  <way id='-209'>
+    <nd ref='-21' />
+    <nd ref='-22' />
+    <nd ref='-18' />
+    <nd ref='-19' />
+    <nd ref='-21' />
+    <tag k='landuse' v='basin' />
+  </way>
+  <way id='-210'>
+    <nd ref='-106' />
+    <nd ref='-103' />
+    <nd ref='-102' />
+    <nd ref='-104' />
+    <nd ref='-106' />
+  </way>
+  <way id='-211'>
+    <nd ref='-57' />
+    <nd ref='-58' />
+    <nd ref='-59' />
+    <nd ref='-60' />
+    <nd ref='-57' />
+    <tag k='parking' v='surface' />
+    <tag k='amenity' v='parking' />
+  </way>
+  <way id='-212'>
+    <nd ref='-162' />
+    <nd ref='-161' />
+    <nd ref='-166' />
+    <nd ref='-165' />
+    <nd ref='-162' />
+    <tag k='parking' v='surface' />
+    <tag k='amenity' v='parking' />
+  </way>
+  <way id='-213'>
+    <nd ref='-27' />
+    <nd ref='-37' />
+    <nd ref='-36' />
+    <nd ref='-39' />
+    <nd ref='-38' />
+    <nd ref='-33' />
+    <nd ref='-32' />
+    <nd ref='-35' />
+    <nd ref='-34' />
+    <nd ref='-30' />
+    <nd ref='-27' />
+    <tag k='parking' v='surface' />
+    <tag k='amenity' v='parking' />
+  </way>
+  <relation id='-214'>
+    <member type='way' ref='-206' role='inner' />
+    <member type='way' ref='-182' role='outer' />
+    <tag k='name' v='3.' />
+    <tag k='type' v='multipolygon' />
+  </relation>
+  <relation id='-215'>
+  </relation>
+  <relation id='-216'>
+    <member type='way' ref='-175' role='inner' />
+    <member type='way' ref='-200' role='inner' />
+    <member type='way' ref='-197' role='outer' />
+    <tag k='name' v='5.' />
+    <tag k='type' v='multipolygon' />
+  </relation>
+  <relation id='-217'>
+    <member type='way' ref='-177' role='inner' />
+    <member type='way' ref='-208' role='outer' />
+    <tag k='name' v='6.' />
+    <tag k='type' v='multipolygon' />
+  </relation>
+  <relation id='-218'>
+    <member type='way' ref='-183' role='' />
+    <member type='way' ref='-189' role='' />
+    <tag k='name ' v='4. b' />
+  </relation>
+  <relation id='-219'>
+    <member type='way' ref='-198' role='area' />
+    <member type='node' ref='-90' role='node in area' />
+    <member type='node' ref='-92' role='node in area' />
+    <member type='way' ref='-184' role='some other way' />
+    <member type='node' ref='-93' role='to be deleted node in area' />
+    <tag k='name' v='1.' />
+  </relation>
+  <relation id='-220'>
+    <member type='way' ref='-185' role='inner' />
+    <member type='way' ref='-191' role='outer' />
+    <tag k='name' v='4. a' />
+    <tag k='type' v='multipolygon' />
+  </relation>
+  <relation id='-221'>
+    <member type='way' ref='-210' role='inner' />
+    <member type='way' ref='-186' role='outer' />
+    <tag k='name' v='2.' />
+    <tag k='type' v='multipolygon' />
+  </relation>
+</osm>
Index: images/joinareas.png
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream

Property changes on: images\joinareas.png
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Index: src/UtilsPlugin/JoinAreasAction.java
===================================================================
--- src/UtilsPlugin/JoinAreasAction.java	(revision 0)
+++ src/UtilsPlugin/JoinAreasAction.java	(revision 0)
@@ -0,0 +1,813 @@
+package UtilsPlugin;
+
+import static org.openstreetmap.josm.tools.I18n.tr;
+import static org.openstreetmap.josm.tools.I18n.trn;
+
+import java.awt.event.ActionEvent;
+import java.awt.event.KeyEvent;
+import java.awt.geom.Line2D;
+import java.awt.geom.Point2D;
+import java.awt.GridBagLayout;
+import java.awt.Point;
+import java.awt.Polygon;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.TreeMap;
+import java.util.TreeSet;
+
+import javax.swing.Box;
+import javax.swing.JComboBox;
+import javax.swing.JLabel;
+import javax.swing.JOptionPane;
+import javax.swing.JPanel;
+
+import org.openstreetmap.josm.actions.CombineWayAction;
+import org.openstreetmap.josm.actions.JosmAction;
+import org.openstreetmap.josm.actions.ReverseWayAction;
+import org.openstreetmap.josm.actions.SplitWayAction;
+
+import org.openstreetmap.josm.command.AddCommand;
+import org.openstreetmap.josm.command.ChangeCommand;
+import org.openstreetmap.josm.command.Command;
+import org.openstreetmap.josm.command.DeleteCommand;
+import org.openstreetmap.josm.command.SequenceCommand;
+
+import org.openstreetmap.josm.data.Bounds;
+import org.openstreetmap.josm.data.coor.EastNorth;
+import org.openstreetmap.josm.data.coor.LatLon;
+import org.openstreetmap.josm.data.osm.DataSet;
+import org.openstreetmap.josm.data.osm.DataSource;
+import org.openstreetmap.josm.data.osm.Node;
+import org.openstreetmap.josm.data.osm.OsmPrimitive;
+import org.openstreetmap.josm.data.osm.Relation;
+import org.openstreetmap.josm.data.osm.RelationMember;
+import org.openstreetmap.josm.data.osm.TigerUtils;
+import org.openstreetmap.josm.data.osm.visitor.CollectBackReferencesVisitor;
+import org.openstreetmap.josm.data.osm.Way;
+import org.openstreetmap.josm.data.osm.WaySegment;
+import org.openstreetmap.josm.data.projection.Epsg4326;
+import org.openstreetmap.josm.data.projection.Lambert;
+import org.openstreetmap.josm.data.projection.Mercator;
+import org.openstreetmap.josm.data.UndoRedoHandler;
+
+import org.openstreetmap.josm.gui.ExtendedDialog; 
+import org.openstreetmap.josm.gui.layer.OsmDataLayer;
+import org.openstreetmap.josm.Main;
+import org.openstreetmap.josm.tools.GBC;
+import org.openstreetmap.josm.tools.Pair;
+import org.openstreetmap.josm.tools.Shortcut;
+
+public class JoinAreasAction extends JosmAction {
+    // This will be used to commit commands and unite them into one large command sequence at the end
+    private LinkedList<Command> cmds = new LinkedList<Command>();
+    private int cmdsCount = 0;
+    
+    // HelperClass
+    // Saves a node and two positions where to insert the node into the ways
+    private class NodeToSegs implements Comparable<NodeToSegs> {
+        public int pos;
+        public Node n;
+        public double dis;
+        public NodeToSegs(int pos, Node n, LatLon dis) {
+            this.pos = pos;
+            this.n = n;
+            this.dis = n.coor.greatCircleDistance(dis);
+        }
+        
+        public int compareTo(NodeToSegs o) {
+            if(this.pos == o.pos)
+                return (this.dis - o.dis) > 0 ? 1 : -1;
+            return this.pos - o.pos;
+        }
+    };
+    
+    // HelperClass
+    // Saves a relation and a role an OsmPrimitve was part of until it was stripped from all relations
+    private class RelationRole {
+        public Relation rel;
+        public String role;
+        public RelationRole(Relation rel, String role) {
+            this.rel = rel;
+            this.role = role;
+        }
+        
+        public boolean equals(Object other) {
+            if (!(other instanceof RelationRole)) return false;
+            RelationRole otherMember = (RelationRole) other;
+            return otherMember.role.equals(role) && otherMember.rel.equals(rel);
+        }
+    }
+
+    // Adds the menu entry, Shortcuts, etc.
+    public JoinAreasAction() {
+        super(tr("Join overlapping Areas"), "joinareas", tr("Joins areas that overlap each other"), Shortcut.registerShortcut("tools:joinareas", tr("Tool: {0}", tr("Join overlapping Areas")),
+        KeyEvent.VK_J, Shortcut.GROUP_EDIT, Shortcut.SHIFT_DEFAULT), true);
+    }
+    
+    /**
+     * Gets called whenever the shortcut is pressed or the menu entry is selected
+     * Checks whether the selected objects are suitable to join and joins them if so
+     */
+    public void actionPerformed(ActionEvent e) {
+        int result = new ExtendedDialog(Main.parent, 
+            tr("Enter values for all conflicts."), 
+            new JLabel(tr("THIS IS EXPERIMENTAL. Save your work and verify before uploading.")), 
+            new String[] {tr("Continue anyway"), tr("Cancel")}, 
+            new String[] {"joinareas.png", "cancel.png"}).getValue(); 
+        if(result != 1) return;
+        
+        Collection<OsmPrimitive> selection = Main.ds.getSelectedWays();
+
+        int ways = 0;   
+        Way[] selWays = new Way[2];
+        
+        LinkedList<Bounds> bounds = new LinkedList<Bounds>();
+        OsmDataLayer dataLayer = Main.main.editLayer();
+        for (DataSource ds : dataLayer.data.dataSources) {
+            if (ds.bounds != null)
+                bounds.add(ds.bounds);
+        }
+        
+        boolean askedAlready = false;
+        for (OsmPrimitive prim : selection) {
+            Way way = (Way) prim;       
+            
+            // Too many ways
+            if(ways == 2) {
+                JOptionPane.showMessageDialog(Main.parent, tr("Only up to two areas can be joined at the moment."));
+                return;
+            }
+            
+            if(!way.isClosed()) {
+                JOptionPane.showMessageDialog(Main.parent, tr("\"{0}\" is not closed and therefore can't be joined.", way.getName()));
+                return;
+            }
+            
+            // This is copied from SimplifyAction and should be probably ported to tools
+            for (Node node : way.nodes) {
+                if(askedAlready) break;
+                boolean isInsideOneBoundingBox = false;
+                for (Bounds b : bounds) {
+                    if (b.contains(node.coor)) {
+                        isInsideOneBoundingBox = true;
+                        break;
+                    }
+                }
+                
+                if (!isInsideOneBoundingBox) {
+                    int option = JOptionPane.showConfirmDialog(Main.parent,
+                            tr("The selected way(s) have nodes outside of the downloaded data region.\n"
+                                    + "This can lead to nodes being deleted accidentally.\n"
+                                    + "Are you really sure to continue?"),
+                            tr("Please abort if you are not sure"), JOptionPane.YES_NO_OPTION,
+                            JOptionPane.WARNING_MESSAGE);
+
+                    if (option != JOptionPane.YES_OPTION) return;
+                    askedAlready = true;
+                    break;
+                }
+            }
+              
+            selWays[ways] = way;
+            ways++;
+        }
+
+        if (ways < 1) {
+            JOptionPane.showMessageDialog(Main.parent, tr("Please select at least one closed way the should be joined."));
+            return;
+        }
+		
+        if(joinAreas(selWays[0], selWays[ways == 2 ? 1 : 0])) {
+            Main.map.mapView.repaint();
+            DataSet.fireSelectionChanged(Main.ds.getSelected());
+        } else
+            JOptionPane.showMessageDialog(Main.parent, tr("No intersections found. Nothing was changed."));
+    } 
+    
+
+    /**
+     * Will join two overlapping areas
+     * @param Way First way/area
+     * @param Way Second way/area
+     * @return boolean Whether to display the "no operation" message
+     */
+    private boolean joinAreas(Way a, Way b) {
+        // Fix self-overlapping first or other errors
+        boolean same = a.equals(b);
+        boolean hadChanges = false;
+        if(!same) {
+            if(checkForTagConflicts(a, b)) return true; // User aborted, so don't warn again
+            hadChanges = joinAreas(a, a);
+            hadChanges = joinAreas(b, b) || hadChanges;
+        }
+        
+        ArrayList<OsmPrimitive> nodes = addIntersections(a, b);
+        if(nodes.size() == 0) return hadChanges;
+        commitCommands("Added node on all intersections");  
+        
+        // Remove ways from all relations so ways can be combined/split quietly
+        ArrayList<RelationRole> relations = removeFromRelations(a);
+        if(!same) relations.addAll(removeFromRelations(b));
+        if(relations.size() > 0)
+           JOptionPane.showMessageDialog(Main.parent, tr("Some of the ways were part of relations that have been modified. Please verify no errors have been introduced."));
+          
+        Collection<Way> allWays = splitWaysOnNodes(a, b, nodes);
+
+        // Find all nodes and inner ways save them to a list
+        Collection<Node> allNodes = getNodesFromWays(allWays);        
+        Collection<Way> innerWays = findInnerWays(allWays, allNodes);
+        
+        // Join outer ways
+        Way outerWay = joinOuterWays(allWays, innerWays);
+        
+        // Fix Multipolygons if there are any
+        Collection<Way> newInnerWays = fixMultigons(innerWays, outerWay);
+        
+        // Delete the remaining inner ways
+        if(innerWays.size() > 0)
+            cmds.add(DeleteCommand.delete(innerWays, true));
+        commitCommands("Restore inner ways that belong to multigons and delete remaining ways");
+        
+       
+        // We can attach our new multipolygon relation and pretend it has always been there
+        addOwnMultigonRelation(newInnerWays, outerWay, relations);
+        fixRelations(relations, outerWay);
+        commitCommands("Fix relations");
+        
+        stripTags(newInnerWays);
+        makeCommitsOneAction(
+            a.equals(b)
+                ? "Joined self-overlapping area " + a.getName()
+                : "Joined overlapping areas " + a.getName() + " and " + b.getName()
+        );
+
+        return true;
+    }
+    
+    /**
+     * Checks if tags of two given ways differ, and presents the user a dialog to solve conflicts
+     * @param Way First way to check
+     * @param Way Second Way to check
+     * @return boolean True if not all conflicts could be resolved, False if everything's fine
+     */
+    private boolean checkForTagConflicts(Way a, Way b) {
+        ArrayList<Way> ways = new ArrayList<Way>();
+        ways.add(a);
+        ways.add(b);
+    
+        // This is mostly copied and pasted from CombineWayAction.java and one day should be moved into tools
+        Map<String, Set<String>> props = new TreeMap<String, Set<String>>();
+        for (Way w : ways) {
+            for (Entry<String,String> e : w.entrySet()) {
+                if (!props.containsKey(e.getKey()))
+                    props.put(e.getKey(), new TreeSet<String>());
+                props.get(e.getKey()).add(e.getValue());
+            }
+        }
+        
+        Way ax = new Way(a);
+        Way bx = new Way(b);
+        
+        Map<String, JComboBox> components = new HashMap<String, JComboBox>();
+        JPanel p = new JPanel(new GridBagLayout());
+        for (Entry<String, Set<String>> e : props.entrySet()) {
+            if (TigerUtils.isTigerTag(e.getKey())) {
+                String combined = TigerUtils.combineTags(e.getKey(), e.getValue());
+                ax.put(e.getKey(), combined);
+                bx.put(e.getKey(), combined);
+            } else if (e.getValue().size() > 1) {
+                if("created_by".equals(e.getKey()))
+                {
+                    ax.put("created_by", "JOSM");
+                    bx.put("created_by", "JOSM");
+                } else {
+                    JComboBox c = new JComboBox(e.getValue().toArray());
+                    c.setEditable(true);
+                    p.add(new JLabel(e.getKey()), GBC.std());
+                    p.add(Box.createHorizontalStrut(10), GBC.std());
+                    p.add(c, GBC.eol());
+                    components.put(e.getKey(), c);
+                }
+            } else {
+                String val = e.getValue().iterator().next();
+                ax.put(e.getKey(), val);
+                bx.put(e.getKey(), val);
+            }
+        }
+        
+        if (components.isEmpty())
+            return false; // No conflicts found
+        
+        int result = new ExtendedDialog(Main.parent, 
+            tr("Enter values for all conflicts."), 
+            p, 
+            new String[] {tr("Solve Conflicts"), tr("Cancel")}, 
+            new String[] {"dialogs/conflict.png", "cancel.png"}).getValue(); 
+            
+        if (result != 1) return true; // user cancel, unresolvable conflicts
+        
+        for (Entry<String, JComboBox> e : components.entrySet()) {
+            String val = e.getValue().getEditor().getItem().toString();
+            ax.put(e.getKey(), val);
+            bx.put(e.getKey(), val);
+        }
+    
+        cmds.add(new ChangeCommand(a, ax));
+        cmds.add(new ChangeCommand(b, bx));
+        commitCommands("Fix tag conflicts");
+        return false;
+    }
+    
+    /**
+     * Will find all intersection and add nodes there for two given ways
+     * @param Way First way
+     * @param Way Second way
+     * @return ArrayList<OsmPrimitive> List of new nodes
+     */
+    private ArrayList<OsmPrimitive> addIntersections(Way a, Way b) {
+        boolean same = a.equals(b);
+        int nodesSizeA = a.nodes.size();
+        int nodesSizeB = b.nodes.size();
+
+        // We use OsmPrimitive here instead of Node because we later need to split a way at these nodes.
+        // With OsmPrimitve we can simply add the way and don't have to loop over the nodes
+        ArrayList<OsmPrimitive> nodes = new ArrayList<OsmPrimitive>();
+        ArrayList<NodeToSegs> nodesA = new ArrayList<NodeToSegs>();
+        ArrayList<NodeToSegs> nodesB = new ArrayList<NodeToSegs>();
+
+        for (int i = (same ? 1 : 0); i < nodesSizeA - 1; i++) {
+            for (int j = (same ? i + 2 : 0); j < nodesSizeB - 1; j++) {
+                // Avoid re-adding nodes that already exist on (some) intersections
+                if(a.nodes.get(i).equals(b.nodes.get(j)) || a.nodes.get(i+1).equals(b.nodes.get(j)))   {
+                    nodes.add(b.nodes.get(j));
+                    continue;
+                } else 
+                if(a.nodes.get(i).equals(b.nodes.get(j+1)) || a.nodes.get(i+1).equals(b.nodes.get(j+1))) {
+                    nodes.add(b.nodes.get(j+1));
+                    continue;
+                }
+                LatLon intersection = getLineLineIntersection(
+                        a.nodes.get(i)  .eastNorth.east(), a.nodes.get(i)  .eastNorth.north(),
+                        a.nodes.get(i+1).eastNorth.east(), a.nodes.get(i+1).eastNorth.north(),
+                        b.nodes.get(j)  .eastNorth.east(), b.nodes.get(j)  .eastNorth.north(),
+                        b.nodes.get(j+1).eastNorth.east(), b.nodes.get(j+1).eastNorth.north());
+                if(intersection == null) continue;
+                
+                // Create the node. Adding them to the ways must be delayed because we still loop over them
+                Node n = new Node(intersection);
+                cmds.add(new AddCommand(n));
+                nodes.add(n);
+                // The distance is needed to sort and add the nodes in direction of the way
+                nodesA.add(new NodeToSegs(i,  n, a.nodes.get(i).coor));
+                if(same)
+                    nodesA.add(new NodeToSegs(j,  n, a.nodes.get(j).coor));
+                else    
+                    nodesB.add(new NodeToSegs(j,  n, b.nodes.get(j).coor));
+            }
+        }
+        
+        addNodesToWay(a, nodesA);
+        if(!same) addNodesToWay(b, nodesB);
+        
+        return nodes;
+    }
+    
+    /**
+     * Finds the intersection of two lines
+     * @return LatLon null if no intersection was found, the LatLon coordinates of the intersection otherwise
+     */
+    static private LatLon getLineLineIntersection(
+                double x1, double y1, double x2, double y2,
+                double x3, double y3, double x4, double y4) {
+                
+        if (!Line2D.linesIntersect(x1, y1, x2, y2, x3, y3, x4, y4)) return null;
+
+        // Convert line from (point, point) form to ax+by=c
+        double a1 = y2 - y1;
+        double b1 = x1 - x2;
+        double c1 = x2*y1 - x1*y2;
+
+        double a2 = y4 - y3;
+        double b2 = x3 - x4;
+        double c2 = x4*y3 - x3*y4;
+
+        // Solve the equations
+        double det = a1*b2 - a2*b1;        
+        if(det == 0) return null; // Lines are parallel
+        
+        return Main.proj.eastNorth2latlon(new EastNorth(
+            (b1*c2 - b2*c1)/det, 
+            (a2*c1 -a1*c2)/det
+        ));
+    }
+
+    /**
+     * Inserts given nodes with positions into the given ways
+     * @param Way The way to insert the nodes into
+     * @param Collection<NodeToSegs> The list of nodes with positions to insert
+     */   
+    private void addNodesToWay(Way a, ArrayList<NodeToSegs> nodes) {
+        Way ax=new Way(a);
+        List<NodeToSegs> newnodes = new ArrayList<NodeToSegs>();       
+        Collections.sort(nodes);
+        
+        int numOfAdds = 1;
+        for(NodeToSegs n : nodes) {
+            ax.addNode(n.pos + numOfAdds, n.n);
+            numOfAdds++;
+        }
+        
+        cmds.add(new ChangeCommand(a, ax));
+    }
+    
+    /**
+     * Commits the command list with a description
+     * @param String The description of what the commands do
+     */
+    private void commitCommands(String description) {
+        switch(cmds.size()) {
+            case 0:
+                return;
+            case 1:
+                Main.main.undoRedo.add(cmds.getFirst());
+                break;
+            default:
+                Command c = new SequenceCommand(tr(description), cmds);
+                Main.main.undoRedo.add(c);
+                break;
+        }
+        
+        cmds.clear();
+        cmdsCount++;
+    }
+    
+    /**
+     * Removes a given OsmPrimitive from all relations
+     * @param OsmPrimitive Element to remove from all relations
+     * @return ArrayList<RelationRole> List of relations with roles the primitives was part of
+     */
+    private ArrayList<RelationRole> removeFromRelations(OsmPrimitive osm) {
+        ArrayList<RelationRole> result = new ArrayList<RelationRole>();
+        for (Relation r : Main.ds.relations) {
+            if (r.deleted || r.incomplete) continue;
+            for (RelationMember rm : r.members) {
+                if (rm.member != osm) continue;
+                
+                Relation newRel = new Relation(r);
+                newRel.members.remove(rm);
+                
+                cmds.add(new ChangeCommand(r, newRel));
+                RelationRole saverel =  new RelationRole(r, rm.role);
+                if(!result.contains(saverel)) result.add(saverel);
+                break;
+            }
+        }
+        
+        commitCommands("Removed Element from Relations");
+        return result;
+    }
+
+    /**
+     * This is a hacky implementation to make use of the splitWayAction code and
+     * should be improved. SplitWayAction needs to expose its splitWay function though.
+     */
+    private Collection<Way> splitWaysOnNodes(Way a, Way b, Collection<OsmPrimitive> nodes) {
+        ArrayList<Way> ways = new ArrayList<Way>();
+        ways.add(a);
+        if(!a.equals(b)) ways.add(b);
+        
+        List<OsmPrimitive> affected = new ArrayList<OsmPrimitive>();
+        for (Way way : ways) {
+            nodes.add(way);
+            Main.ds.setSelected(nodes);
+            nodes.remove(way);
+            new SplitWayAction().actionPerformed(null);
+            cmdsCount++;
+            affected.addAll(Main.ds.getSelectedWays());
+        }
+        return osmprim2way(affected);
+    }
+     
+    /**
+     * Converts a list of OsmPrimitives to a list of Ways
+     * @param Collection<OsmPrimitive> The OsmPrimitives list that's needed as a list of Ways
+     * @return Collection<Way> The list as list of Ways
+     */
+    static private Collection<Way> osmprim2way(Collection<OsmPrimitive> ways) {
+        Collection<Way> result = new ArrayList<Way>();
+        for(OsmPrimitive w: ways) {
+            if(w instanceof Way) result.add((Way) w);
+        }
+        return result;
+    }
+    
+    /**
+     * Returns all nodes for given ways
+     * @param Collection<Way> The list of ways which nodes are to be returned
+     * @return Collection<Node> The list of nodes the ways contain
+     */
+    private Collection<Node> getNodesFromWays(Collection<Way> ways) {
+        Collection<Node> allNodes = new ArrayList<Node>();
+        for(Way w: ways) allNodes.addAll(w.nodes);
+        return allNodes;
+    }
+    
+    /**
+     * Finds all inner ways for a given list of Ways and Nodes from a multigon by constructing a polygon
+     * for each way, looking for inner nodes that are not part of this way. If a node is found, all ways
+     * containing this node are added to the list
+     * @param Collection<Way> A list of (splitted) ways that form a multigon
+     * @param Collection<Node> A list of nodes that belong to the multigon
+     * @return Collection<Way> A list of ways that are positioned inside the outer borders of the multigon
+     */
+    private Collection<Way> findInnerWays(Collection<Way> multigonWays, Collection<Node> multigonNodes) {
+        Collection<Way> innerWays = new ArrayList<Way>();
+        for(Way w: multigonWays) {           
+            Polygon poly = new Polygon();
+            for(Node n: ((Way)w).nodes) poly.addPoint(latlonToXY(n.coor.lat()), latlonToXY(n.coor.lon()));
+            
+            for(Node n: multigonNodes) {
+                if(!((Way)w).nodes.contains(n) && poly.contains(latlonToXY(n.coor.lat()), latlonToXY(n.coor.lon()))) {
+                    innerWays.addAll(getWaysByNode(multigonWays, n));
+                }
+            }
+        }
+        
+        return innerWays;
+    }
+    
+    // Polygon only supports int coordinates, so convert them
+    private int latlonToXY(double val) {
+        return (int)Math.round(val*1000000);
+    }
+    
+    /**
+     * Finds all ways that contain the given node.
+     * @param Collection<OsmPrimitive> A collection of OsmPrimitives, but only ways will be honored
+     * @param Node The node the ways should be checked against
+     * @return Collection<Way> A list of ways that contain the given node
+     */
+    private Collection<Way> getWaysByNode(Collection<Way> w, Node n) {
+        Collection<Way> deletedWays = new ArrayList<Way>();
+        for(Way way : w) {
+            if(!((Way)way).nodes.contains(n)) continue;
+            if(!deletedWays.contains(way)) deletedWays.add(way); // Will need this later for multigons
+        }
+        return deletedWays;
+    }
+    
+    /**
+     * Joins the two outer ways and deletes all short ways that can't be part of a multipolygon anyway
+     * @param Collection<OsmPrimitive> The list of all ways that belong to that multigon
+     * @param Collection<Way> The list of inner ways that belong to that multigon
+     * @return Way The newly created outer way
+     */
+    private Way joinOuterWays(Collection<Way> multigonWays, Collection<Way> innerWays) {
+        ArrayList<Way> join = new ArrayList<Way>();
+        for(Way w: multigonWays) {
+            // Skip inner ways
+            if(innerWays.contains(w)) continue;
+            
+            if(w.nodes.size() <= 2)
+                cmds.add(new DeleteCommand(w));
+            else
+                join.add(w);
+        }
+
+        commitCommands("Join Areas: Remove Short Ways");        
+        return joinWays(join);
+    }
+    
+    /**
+     * Joins a list of ways (using CombineWayAction and ReverseWayAction if necessary to quiet the former)
+     * @param ArrayList<Way> The list of ways to join
+     * @return Way The newly created way
+     */
+    private Way joinWays(ArrayList<Way> ways) {
+        if(ways.size() < 2) return ways.get(0);
+        //Main.ds.setSelected(ways);       
+        
+        // This will turn ways so all of them point in the same direction and CombineAction won't bug
+        // the user about this. 
+        Way a = null;
+        for(Way b : ways) {
+            if(a == null) {
+                a = b;
+                continue;
+            }
+            if(a.nodes.get(0).equals(b.nodes.get(0)) ||
+               a.nodes.get(a.nodes.size()-1).equals(b.nodes.get(b.nodes.size()-1))) {
+                Main.ds.setSelected(b);
+                new ReverseWayAction().actionPerformed(null);
+                cmdsCount++;
+            }          
+            a = b;
+        }
+        Main.ds.setSelected(ways);
+        new CombineWayAction().actionPerformed(null);
+        cmdsCount++;
+        return (Way)(Main.ds.getSelectedWays().toArray())[0];
+    }
+    
+    /**
+     * Finds all ways that may be part of a multipolygon relation and removes them from the given list.
+     * It will automatically combine "good" ways
+     * @param Collection<Way> The list of inner ways to check
+     * @param Way The newly created outer way
+     * @return ArrayList<Way> The List of newly created inner ways
+     */
+    private ArrayList<Way> fixMultigons(Collection<Way> uninterestingWays, Way outerWay) {
+        Collection<Node> innerNodes = getNodesFromWays(uninterestingWays);
+        Collection<Node> outerNodes = outerWay.nodes;
+        
+        // The newly created inner ways. uninterestingWays is passed by reference and therefore modified in-place
+        ArrayList<Way> newInnerWays = new ArrayList<Way>();
+        
+        // Now we need to find all inner ways that contain a remaining node, but no outer nodes
+        // Remaining nodes are those that contain to more than one way. All nodes that belong to an
+        // inner multigon part will have at least two ways, so we can use this to find which ways do
+        // belong to the multigon.
+        Collection<Way> possibleWays = new ArrayList<Way>();
+        wayIterator: for(Way w : uninterestingWays) {
+            boolean hasInnerNodes = false;
+            for(Node n : w.nodes) {
+                if(outerNodes.contains(n)) continue wayIterator;
+                if(!hasInnerNodes && innerNodes.contains(n)) hasInnerNodes = true;
+            }
+            if(!hasInnerNodes && w.nodes.size() >= 2) continue;
+            possibleWays.add(w);
+        }
+        
+        // Join all ways that have one start/ending node in common
+        Way joined = null;
+        outerIterator: do {
+            joined = null;
+            for(Way w1 : possibleWays) {
+                if(w1.isClosed()) {
+                    // The way is already closed
+                    uninterestingWays.remove(w1);
+                    if(!newInnerWays.contains(w1))
+                        newInnerWays.add(w1);                    
+                    continue;
+                }
+                for(Way w2 : possibleWays) {
+                    if(w2.isClosed() || !checkIfWaysCanBeCombined(w1, w2)) continue;
+                    
+                    ArrayList<Way> joinThem = new ArrayList<Way>();
+                    joinThem.add(w1);
+                    joinThem.add(w2);
+                    uninterestingWays.removeAll(joinThem);
+                    possibleWays.removeAll(joinThem);
+                    
+                    // Although we joined the ways, we cannot simply assume that they are closed
+                    Way joinedWay = joinWays(joinThem);
+                    uninterestingWays.add(joinedWay);
+                    possibleWays.add(joinedWay);
+                    continue outerIterator;
+                }
+            }
+        } while(joined != null);
+
+        return newInnerWays;
+    }    
+    
+    /**
+     * Checks if two ways share one starting/ending node
+     * @param Way first way
+     * @param Way second way
+     * @return boolean Wheter the ways share a starting/ending node or not
+     */
+    private boolean checkIfWaysCanBeCombined(Way w1, Way w2) {
+        if(w1.equals(w2)) return false;
+        
+        if(w1.nodes.get(0).equals(w2.nodes.get(0))) return true;
+        if(w1.nodes.get(0).equals(w2.nodes.get(w2.nodes.size()-1))) return true;
+        
+        if(w1.nodes.get(w1.nodes.size()-1).equals(w2.nodes.get(0))) return true;
+        if(w1.nodes.get(w1.nodes.size()-1).equals(w2.nodes.get(w2.nodes.size()-1))) return true;
+        
+        return false;
+    }
+    
+    /**
+     * Will add own multipolygon relation to the "previously existing" relations. Fixup is done by fixRelations
+     * @param Collection<Way> List of already closed inner ways
+     * @param Way The outer way
+     * @param ArrayList<RelationRole> The list of relation with roles to add own relation to
+     */
+    private void addOwnMultigonRelation(Collection<Way> inner, Way outer, ArrayList<RelationRole> rels) {
+        if(inner.size() == 0) return;
+        // Create new multipolygon relation and add all inner ways to it
+        Relation newRel = new Relation();
+        newRel.put("type", "multipolygon");
+        for(Way w : inner)
+            newRel.members.add(new RelationMember("inner", w));
+        cmds.add(new AddCommand(newRel));    
+        
+        // We don't add outer to the relation because it will be handed to fixRelations()
+        // which will then do the remaining work. Collections are passed by reference, so no
+        // need to return it
+        rels.add(new RelationRole(newRel, "outer"));
+        //return rels;
+    }
+    
+    /**
+     * Adds the previously removed relations again to the outer way. If there are multiple multipolygon
+     * relations where the joined areas were in "outer" role a new relation is created instead with all
+     * members of both. This function depends on multigon relations to be valid already, it won't fix them.
+     * @param ArrayList<RelationRole> List of relations with roles the (original) ways were part of
+     * @param Way The newly created outer area/way
+     */
+    private void fixRelations(ArrayList<RelationRole> rels, Way outer) {
+        ArrayList<RelationRole> multiouters = new ArrayList<RelationRole>();
+        for(RelationRole r : rels) {
+            if( r.rel.get("type") != null &&
+                r.rel.get("type").equalsIgnoreCase("multipolygon") &&
+                r.role.equalsIgnoreCase("outer")
+              ) {
+                multiouters.add(r);
+                continue;
+            }
+            // Add it back!
+            Relation newRel = new Relation(r.rel);
+            newRel.members.add(new RelationMember(r.role, outer));                
+            cmds.add(new ChangeCommand(r.rel, newRel));
+        }
+        
+        Relation newRel = null;
+        switch(multiouters.size()) {
+            case 0:
+                return;
+            case 1:
+                // Found only one to be part of a multipolygon relation, so just add it back as well
+                newRel = new Relation(multiouters.get(0).rel);
+                newRel.members.add(new RelationMember(multiouters.get(0).role, outer));                
+                cmds.add(new ChangeCommand(multiouters.get(0).rel, newRel));
+                return;
+            default:
+                // Create a new relation with all previous members and (Way)outer as outer. 
+                newRel = new Relation();
+                for(RelationRole r : multiouters) {
+                    // Add members
+                    for(RelationMember rm : r.rel.members)
+                        if(!newRel.members.contains(rm)) newRel.members.add(rm);
+                    // Add tags
+                    for (String key : r.rel.keys.keySet()) {
+                        newRel.put(key, r.rel.keys.get(key));
+                    }
+                    // Delete old relation
+                    cmds.add(new DeleteCommand(r.rel));
+                }
+                newRel.members.add(new RelationMember("outer", outer));
+                cmds.add(new AddCommand(newRel));
+        }
+    }
+    
+    /**
+     * @param Collection<Way> The List of Ways to remove all tags from
+     */
+    private void stripTags(Collection<Way> ways) {
+        for(Way w: ways) stripTags(w);
+        commitCommands("Remove tags from inner ways");
+    }
+    
+    /**
+     * @param Way The Way to remove all tags from
+     */
+    private void stripTags(Way x) {
+        if(x.keys == null) return;
+        Way y = new Way(x);
+        for (String key : x.keys.keySet())
+            y.remove(key);
+        cmds.add(new ChangeCommand(x, y));
+    }
+    
+    /**
+     * Takes the last cmdsCount actions back and combines them into a single action
+     * (for when the user wants to undo the join action)
+     * @param String The commit message to display
+     */
+    private void makeCommitsOneAction(String message) {
+        UndoRedoHandler ur = Main.main.undoRedo;
+        cmds.clear();
+        for(int i = ur.commands.size() - cmdsCount; i < ur.commands.size(); i++)  
+            cmds.add(ur.commands.get(i));
+        
+        for(int i = 0; i < cmdsCount; i++)
+            ur.undo();
+            
+        commitCommands(message == null ? "Join Areas Function" : message);
+        cmdsCount = 0;
+    }
+}
Index: src/UtilsPlugin/UtilsPlugin.java
===================================================================
--- src/UtilsPlugin/UtilsPlugin.java	(revision 13614)
+++ src/UtilsPlugin/UtilsPlugin.java	(working copy)
@@ -22,10 +22,12 @@
 
 public class UtilsPlugin extends Plugin {
     JMenuItem SimplifyWay;
+    JMenuItem JoinAreas;
     JumpToAction JumpToAct = new JumpToAction();
 
     public UtilsPlugin() {
         SimplifyWay = MainMenu.add(Main.main.menu.toolsMenu, new SimplifyWayAction());
+        JoinAreas = MainMenu.add(Main.main.menu.toolsMenu, new JoinAreasAction());
         SimplifyWay.setEnabled(false);
     }
 
@@ -33,6 +35,7 @@
     public void mapFrameInitialized(MapFrame oldFrame, MapFrame newFrame) {
         if (oldFrame == null && newFrame != null) {
             SimplifyWay.setEnabled(true);
+            JoinAreas.setEnabled(true);
             newFrame.statusLine.addMouseListener(JumpToAct);
         }
     }
