# How to order points of a polygon?

I want to create a polygon, which represents the administrative boundary of a city.

It is specified by a relation, which consists of ways

``````<member type="way" ref="25657490" role="outer"/>
<member type="way" ref="209124836" role="outer"/>
<member type="way" ref="119880388" role="outer"/>
<member type="way" ref="119880389" role="outer"/>
<member type="way" ref="119983752" role="outer"/>
<member type="way" ref="136380499" role="outer"/>
<member type="way" ref="120024176" role="outer"/>
<member type="way" ref="209124834" role="outer"/>
<member type="way" ref="77909008" role="outer"/>
``````

In order to get all buildings inside that boundary using the method described here, I need to convert these ways to an ordered list of points such that every point is connected to its successor.

Example 1

I have way 119880388, whose last point is `132.100403,43.3959161`, hence, it must be followed by a way, which starts there (way 209124836 in this case).

``````119880388;132.1469949,43.504065|132.1439962,43.4906939|132.1400282,43.4747441|132.1402881,43.4739524|132.1397559,43.4733796|132.139471,43.4710427|132.1324332,43.4650797|132.1321001,43.4625553|132.1273123,43.4602785|132.1268996,43.4611024|132.1262186,43.4615517|132.1250473,43.4606504|132.1253362,43.4590926|132.1199706,43.4567557|132.1181958,43.4534|132.1165449,43.4536997|132.1151003,43.4558569|132.1122111,43.4550479|132.1120048,43.4568456|132.1106349,43.456653|132.1074084,43.4565518|132.1045214,43.4568074|132.1018878,43.457799|132.1045918,43.45418|132.1045214,43.4533315|132.1049016,43.4526109|132.0974329,43.4515698|132.0970943,43.4510349|132.0962379,43.4508758|132.0956005,43.4499071|132.0965565,43.4495456|132.100208,43.4491561|132.096659,43.4434507|132.098349,43.4423872|132.0988278,43.440833|132.0981518,43.4397695|132.099194,43.4388083|132.0939234,43.4347791|132.0934023,43.4339098|132.0915574,43.4311281|132.0898252,43.4291849|132.0888394,43.4281416|132.0866256,43.4271254|132.0844248,43.4271615|132.0837273,43.4275697|132.0837273,43.4262298|132.0852483,43.4242149|132.0850934,43.4232227|132.0822767,43.4233557|132.0820373,43.4226294|132.0811501,43.4219032|132.083854,43.4190391|132.0911773,43.4176683|132.0920223,43.4173205|132.0863045,43.4153769|132.0873467,43.4135355|132.0895155,43.4113666|132.0937405,43.4059441|132.0943038,43.4036522|132.0952051,43.4011965|132.0967132,43.3988428|132.0991074,43.3975125|132.100403,43.3959161|

209124836;132.100403,43.3959161|132.0973611,43.3915157|132.0945444,43.3876676|132.0947698,43.3862962|132.0962344,43.3844129|132.0891928,43.3825706|132.0861226,43.3815675|132.0843482,43.3835532|132.0826018,43.3824273|132.0823202,43.381158|132.0812181,43.3793895|132.0799157,43.3777247|132.0783487,43.3754872|132.0775037,43.3725185|132.076884,43.3713105|132.0734759,43.3689559|132.0715324,43.3696725|132.0686313,43.3693654|132.0671666,43.3700206|132.0670821,43.3692425|132.0649696,43.3681778|132.0646316,43.3658435|132.0619981,43.3665295|132.0613643,43.3651985|132.0623502,43.3648913|132.0608851,43.3614618|132.0596304,43.360689|132.0584397,43.359838|132.0568436,43.3605808|132.0524573,43.3620336|132.0516256,43.3627178|132.0467659,43.3583303|132.0505899,43.3557236|132.0502911,43.3542031|132.0457676,43.3518866|132.0457395,43.350668|132.0445706,43.3502788|132.0457676,43.3470836|132.0464436,43.3469607|132.0466971,43.3455064|132.046345,43.3446358|132.0472089,43.342042|132.0438833,43.3408081|132.043113,43.3408202|132.0429138,43.3413996|132.0405483,43.3413749|132.0393135,43.3412952|132.0381384,43.3409041|132.0377749,43.3393052|132.0369876,43.3385224|132.0374383,43.3363304|132.0392269,43.3357363|132.0392019,43.3313722|
``````

Example 2

There are ways 25657490 and 77909008, which both end in point `132.0638277,43.3156504`. Hence the correct order is `25657490, 77909008'` where `77909008'` is the inverted list of points of way 77909008 (`132.0638277,43.3156504; 132.0641092,43.3159; ...`).

``````25657490;132.0392019,43.3313722|132.0440197,43.3311274|132.0473289,43.3308512|132.0508932,43.3302733|132.0528244,43.3296177|132.0532452,43.3290625|132.0534252,43.3291183|132.0536528,43.3295151|132.0540176,43.3294683|132.0547127,43.3293056|132.0556996,43.3292972|132.0561718,43.3297738|132.0578026,43.3296177|132.0592617,43.3291807|132.0595621,43.3283378|132.0597767,43.327651|132.0608925,43.3269642|132.061665,43.3273076|132.0623516,43.3275573|132.0628666,43.3273388|132.0628666,43.3264023|132.063442,43.3254975|132.063507,43.3251849|132.0634183,43.3248517|132.0633476,43.3245859|132.0636823,43.3243424|132.0639065,43.3243493|132.0640514,43.3245921|132.0643604,43.3245921|132.0643422,43.3244722|132.0645664,43.3243798|132.065056,43.3230585|132.0654247,43.322644|132.065562,43.3219696|132.0655105,43.3216699|132.0657594,43.3212203|132.0653989,43.3213951|132.0650127,43.3217448|132.0648496,43.3223005|132.0648239,43.3225503|132.064223,43.3218947|132.0638111,43.3208893|132.0635333,43.3200936|132.0635103,43.3191278|132.0638536,43.3185658|132.0647549,43.3177852|132.0635103,43.315943|132.0638277,43.3156504|

77909008;132.1112088,43.3053453|132.1109191,43.305439|132.1103183,43.3058684|132.1100501,43.3059386|132.1093419,43.3059308|132.1090415,43.305962|132.1088377,43.3061182|132.10843,43.3068755|132.1073678,43.3069848|132.1070567,43.3069302|132.1067563,43.3067662|132.106134,43.3064617|132.1052113,43.3061963|132.1050397,43.306165|132.1042136,43.3064695|132.1039132,43.3070238|132.1035055,43.3071722|132.1032372,43.3073439|132.1030763,43.3075704|132.1030012,43.3078358|132.1030012,43.308312|132.1027223,43.3084682|132.1024004,43.3085853|132.1019927,43.3086399|132.1014992,43.308679|132.1011237,43.3088351|132.1002761,43.3091708|132.0999757,43.3092333|132.0997611,43.3094753|132.0996967,43.3096704|132.0989886,43.3104043|132.0984736,43.3104589|132.0975939,43.31074|132.0971647,43.310982|132.0966712,43.3111225|132.0953623,43.311185|132.09474,43.3111225|132.0941177,43.311021|132.0936886,43.311021|132.0932594,43.3111147|132.0930556,43.3112474|132.092959,43.3114192|132.0929054,43.3116143|132.0927337,43.3117549|132.0923475,43.311872|132.0918754,43.3122076|132.0917467,43.3122935|132.091339,43.312395|132.0909313,43.312395|132.0907274,43.3124262|132.0904485,43.3124184|132.0902983,43.3124262|132.0899764,43.3123638|132.089912,43.3124418|132.089397,43.3125902|132.0891181,43.312598|132.088528,43.3124965|132.08841,43.3125121|132.0878521,43.3127229|132.0876053,43.3127385|132.0872942,43.3126916|132.0869831,43.3127229|132.0861677,43.3130273|132.0860496,43.3132147|132.0857278,43.3133474|132.0855561,43.3133318|132.0851055,43.3135113|132.0848158,43.3137377|132.0844403,43.3138626|132.083818,43.3142061|132.0836786,43.3143388|132.0836035,43.3145418|132.0836464,43.3146589|132.0834211,43.3148228|132.0833996,43.3149945|132.0830027,43.3151585|132.0828095,43.315338|132.0824555,43.3153458|132.0820907,43.3152209|132.0817903,43.3152756|132.0810286,43.3153146|132.0808354,43.315377|132.0805994,43.3155566|132.0804921,43.31558|132.0803741,43.3155644|132.0802883,43.3155019|132.0801917,43.3153146|132.0801059,43.3152443|132.0799664,43.3152131|132.0797733,43.3152365|132.0796231,43.3152834|132.0794192,43.3154239|132.0792583,43.3154629|132.0791295,43.3154629|132.0790115,43.3154083|132.078797,43.3149243|132.0786575,43.3148618|132.0785073,43.314893|132.0784,43.3149555|132.0781103,43.3150179|132.0777455,43.3151428|132.0774344,43.3153146|132.0771018,43.3155253|132.076619,43.3156893|132.0762328,43.3159078|132.0757607,43.3160093|132.0751277,43.3160562|132.0745161,43.316142|132.0739261,43.3163996|132.0717588,43.31711|132.0699349,43.3178203|132.0687011,43.3184448|132.0679286,43.3187726|132.0674244,43.3189053|132.0670596,43.3189522|132.0666412,43.3189522|132.0663408,43.3188585|132.0661369,43.3186868|132.0660511,43.3184838|132.0660082,43.3182106|132.0658043,43.3178359|132.0650748,43.3172427|132.0641092,43.3159|132.0638277,43.3156504|
``````

Are there any ready-made routines (ideally in Java), which do that (append points of ways to each other, inverting them where necessary).

• There is no direct way till now, closest approach is gis.stackexchange.com/questions/73545/… – neogeomat Nov 9 '13 at 11:22
• Note that a relation doesn't necessarily consist of one single connected polygon. It may contain an arbitrary number of polygons and it may even define holes inside these polygons whenever there are members with the role inner instead of outer. See the multipolygon concept. – scai Nov 11 '13 at 9:36
• @scai Yes, I know that a relation can have "holes" inside it. For now, I sorted the partial polygons manually. – Mentiflectax Nov 11 '13 at 10:12
• possible duplicate of How to visually sort the order of Points with QGIS, GDAL, ...? – AndreJ Jan 31 '14 at 18:27
• Possibly a stupid question, but what is the source of the data? Is it geometrically valid? It is possible that for example the city was digitized in a clockwise direction, while the building was digitized in the anti-clockwise direction. In this case you might not have to set the order of points, if it is unedited valid data from OSM. – Devdatta Tengshe Feb 1 '14 at 14:03

The easiest way to get around with OSM polygon relations is to use OSM tools:

1. Save the /full osm data to disk
2. Load it into the OSM editor JOSM
3. Remove the admin centre point
4. Open the relation in the relation editor. If the polygon is closed, you will see the relation elements connected by a closed line. If not, you see one or several interrupted lines
5. If necessary, correct the discontinuities using editor tools
6. Select all by rectangle
7. `Tools -> Join lines`
8. Save to disk under another name
10. Perform step 2 and 3 of the Openstreetmap plugin from QGIS Vector menu

EDIT

I have explained a more GIS-like way here: How to visually sort the order of Points with QGIS, GDAL, ...?

If this is a simple polygon, you can use a custom script that triggers on the and stores a list of way ids in RAM. Then in a second step, you can use this informations to detect the wayids and creates a node list. In a last 3. run, you lookup the node ids and get your lat/lon pairs.

If you want just single boundaries, you might have a look for preprocessed packages or scripts:
http://wiki.openstreetmap.org/wiki/Shapefiles
http://wiki.openstreetmap.org/wiki/Boundaries

Hint 1: Boundaries can consist of multipolygones or nested relations.
Hint 2: If you mix data with OSM you must license it under ODbL.