1

I have a routing application based on pgRouting and OSM data. When I generate a route from coordinates A to B, it works well, but when I export the geojson to GPX format, I notice, that some linestrings are in the wrong order.

See this file for example, where the track goes back sometimes: https://filebin.net/n146hmmse81vupqs

This is my SQL to generate the route:

"SELECT route.edge, ST_AsGeoJSON(ST_Transform(routing.ways.the_geom,4326)) AS geojson, length_m AS length, routing.ways.gid
                FROM pgr_dijkstra('SELECT gid AS id,source::integer,target::integer, cost::numeric AS cost FROM routing.ways
                WHERE ways.the_geom && ST_Envelope (ST_MakeLine(ST_MakePoint(".($bbox_x[0]-0.3).",".($bbox_y[0]-0.3)."), ST_MakePoint(".($bbox_x[1]+0.3).",".($bbox_y[1]+0.3).")))',
                ".$startEdge['source'].", ".$endEdge['target'].", false ) 
                AS route LEFT JOIN routing.ways ON route.edge = routing.ways.gid
                WHERE routing.ways.gid IS NOT NULL;"

My workflow is as follows: I send two coordinates from a web application using Leaflet.js to the postgis server, a route is generated and then the php script sends a GeoJSON encoded route back (feature collection of linestring).

To export to GPX, I combine the route with turf.combine() and convert it to GPX with togpx() (https://github.com/tyrasd/togpx).

Visually, the route looks fine when added to leaflet, but the problem arises when I export the route to GPX. How can I make sure that the track goes along the route segments directly without going back and forth?

This is my GeoJSON string:

{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"collectedProperties":[{"id":"1061947","length":"59.345420431621484"},{"id":"2681419","length":"58.25187960222122"},{"id":"2685131","length":"55.096989309788334"},{"id":"2685130","length":"126.22098103408175"},{"id":"2689920","length":"93.34233213706281"},{"id":"2690058","length":"6.038812472561724"},{"id":"2689919","length":"18.220329208840155"},{"id":"2689929","length":"14.445228126464436"},{"id":"2685459","length":"18.899525432986678"},{"id":"2690095","length":"10.839237185451168"},{"id":"2690077","length":"16.722942760414174"},{"id":"2685169","length":"288.8914432709863"},{"id":"2681657","length":"124.996763866783"},{"id":"1264862","length":"14.852126462886977"},{"id":"1259857","length":"54.28765032679762"},{"id":"1657705","length":"148.18332935321575"},{"id":"1207373","length":"14.920461143567733"},{"id":"149154","length":"150.67699053272383"},{"id":"980882","length":"9.977692063768659"},{"id":"2680890","length":"387.3370675361763"},{"id":"2680102","length":"106.121215835013"},{"id":"1658034","length":"390.06547771313797"},{"id":"1659669","length":"172.61674951824213"},{"id":"137367","length":"315.05501349696317"},{"id":"132379","length":"52.58025580108381"},{"id":"107918","length":"17.56121851773412"},{"id":"1657769","length":"47.76655021594913"},{"id":"1660381","length":"13.907168896070683"},{"id":"1659684","length":"14.768769661910248"},{"id":"1659658","length":"63.20410828009008"},{"id":"1659659","length":"15.565412447063919"},{"id":"1659660","length":"10.50101655270901"},{"id":"1658032","length":"8.783783428811455"},{"id":"1659661","length":"4.702089910973081"},{"id":"1659662","length":"8.833246289536564"},{"id":"1659663","length":"20.779850411206553"},{"id":"1659683","length":"3.5352461293113175"},{"id":"1659664","length":"14.908248109260159"},{"id":"1658033","length":"7.722961646513577"},{"id":"1659665","length":"8.782459042882676"},{"id":"1659666","length":"5.0258946457135485"},{"id":"1659667","length":"4.416643857130424"},{"id":"1666079","length":"34.04575698094555"},{"id":"1659668","length":"14.868364067955927"},{"id":"2297919","length":"29.697739548361163"},{"id":"2156138","length":"82.44888960714746"},{"id":"2156260","length":"83.4842702356298"},{"id":"2300582","length":"107.76997640968517"},{"id":"2304321","length":"17.50096554947559"},{"id":"2299514","length":"17.523840946649283"},{"id":"189008","length":"23.811309104692914"},{"id":"177974","length":"46.360525174045534"},{"id":"189009","length":"29.465955026676266"},{"id":"155565","length":"23.952861755359336"},{"id":"155566","length":"20.376196277355685"},{"id":"198839","length":"22.021462423011055"},{"id":"155571","length":"3.156203894733359"},{"id":"155567","length":"25.31022479366865"},{"id":"166794","length":"39.213104307512495"},{"id":"166795","length":"18.58802082424087"},{"id":"174047","length":"23.762747092586835"},{"id":"171454","length":"94.01516709862466"},{"id":"2294982","length":"33.82494546358187"},{"id":"2304379","length":"12.535650249155433"},{"id":"2304378","length":"2.96247072979423"},{"id":"2299541","length":"3.3956624865163163"},{"id":"2294981","length":"37.853731729885254"},{"id":"2294980","length":"70.98898164203783"},{"id":"2297520","length":"14.014909244384906"},{"id":"2294979","length":"18.012205671652058"},{"id":"2294978","length":"27.383389984265065"},{"id":"2294977","length":"4.682054550403654"},{"id":"2156271","length":"14.479720661209948"},{"id":"2156317","length":"21.479969932322497"},{"id":"2156272","length":"22.317040420239284"},{"id":"2159506","length":"64.97419806908468"},{"id":"2156139","length":"84.92013079550195"},{"id":"2156273","length":"8.48868795184659"},{"id":"2156274","length":"38.15044500793799"},{"id":"2156538","length":"24.352438298434397"},{"id":"2156275","length":"55.509633747065294"},{"id":"2156584","length":"11.785356688696554"},{"id":"2155228","length":"51.38678816637977"},{"id":"2154635","length":"35.09293119445656"},{"id":"2156276","length":"29.196572973234442"},{"id":"2155090","length":"36.812570805711516"},{"id":"2156277","length":"79.81851588906149"},{"id":"2156278","length":"29.771678370775057"},{"id":"2156279","length":"33.39886088744816"},{"id":"2156280","length":"77.78826788462088"},{"id":"2155091","length":"34.570293659471346"},{"id":"166793","length":"21.685241005187912"},{"id":"162670","length":"58.37225599900196"},{"id":"166791","length":"39.71901957910329"},{"id":"166790","length":"39.860494210695535"},{"id":"155563","length":"15.848624283861632"},{"id":"174046","length":"44.67883349238596"},{"id":"155560","length":"39.11738738901496"},{"id":"164243","length":"33.175870862419615"},{"id":"178149","length":"32.17198205183179"},{"id":"174039","length":"19.021068737117673"},{"id":"155562","length":"17.38628638906571"},{"id":"166789","length":"34.38369009176267"},{"id":"174063","length":"10.994221721078853"},{"id":"166788","length":"8.829850160943934"},{"id":"176300","length":"2.6238934512320764"},{"id":"198123","length":"3.3209900874491503"},{"id":"187322","length":"2.707433871282557"},{"id":"198399","length":"2.5768961833240667"},{"id":"155770","length":"64.77877751639554"},{"id":"189194","length":"143.413429562793"},{"id":"189195","length":"71.94703297386468"},{"id":"189197","length":"102.78213042799912"},{"id":"2153949","length":"130.43879107764"},{"id":"178150","length":"71.79247362406609"},{"id":"176521","length":"83.46311840273727"},{"id":"178151","length":"15.324003728152745"},{"id":"178152","length":"13.83026526425776"},{"id":"189196","length":"12.203144604424615"},{"id":"166966","length":"17.720168828105063"},{"id":"153647","length":"156.10891213745833"},{"id":"108714","length":"200.41945612139028"},{"id":"166968","length":"60.527556741876886"},{"id":"162845","length":"174.50235554117634"},{"id":"178153","length":"9.340105800087198"},{"id":"165345","length":"18.512046308891815"},{"id":"161145","length":"14.929024267586676"},{"id":"166969","length":"10.652461628904348"},{"id":"1299149","length":"87.47904427317653"},{"id":"1300280","length":"24.13820648853429"},{"id":"1305091","length":"115.25141639585783"},{"id":"1298724","length":"22.39983488064001"},{"id":"660181","length":"33.6057651668821"},{"id":"665791","length":"50.67057571502072"},{"id":"1305595","length":"23.54189450518001"},{"id":"1302901","length":"1.1864892376262377"},{"id":"1305090","length":"55.86513345435519"},{"id":"1300094","length":"170.72448761816716"},{"id":"1300095","length":"53.191868223752635"},{"id":"1305089","length":"8.810568937146131"},{"id":"1300096","length":"133.57857662809477"},{"id":"1299104","length":"144.12210878951942"},{"id":"133118","length":"56.28479515563125"},{"id":"133986","length":"16.663373430441588"},{"id":"125394","length":"45.72416411415534"},{"id":"133987","length":"294.48754408844957"},{"id":"154620","length":"228.04048031289133"},{"id":"195844","length":"59.825367589185234"},{"id":"81433","length":"79.08419267255842"},{"id":"2323988","length":"8.965888200772742"},{"id":"2326261","length":"4.2886062843298305"},{"id":"2327115","length":"24.12087908766955"},{"id":"2326304","length":"45.81514205012746"},{"id":"2326305","length":"35.33468865104678"},{"id":"2323999","length":"31.84636057830047"},{"id":"2326306","length":"57.30040491356562"},{"id":"2326307","length":"274.1557848054419"},{"id":"2323971","length":"21.13778944493651"},{"id":"2326911","length":"9.249912312473802"},{"id":"2324286","length":"8.433150047407944"},{"id":"118948","length":"107.17864990809278"},{"id":"143256","length":"75.72020097911172"},{"id":"131229","length":"304.35116875721775"}]},"geometry":{"type":"MultiLineString","coordinates":[[[7.5483511,46.9541755],[7.5477563,46.9541977],[7.5475732,46.9542097]],[[7.5474863,46.9536891],[7.5475732,46.9542097]],[[7.547415,46.9531959],[7.5474863,46.9536891]],[[7.547415,46.9531959],[7.5465189,46.9531545],[7.5461932,46.9531145],[7.5457793,46.953037]],[[7.5457793,46.953037],[7.5448953,46.952833],[7.54476,46.9527969],[7.544648,46.9527305]],[[7.544648,46.9527305],[7.5446117,46.9526822]],[[7.5446117,46.9526822],[7.5445988,46.9526423],[7.5446036,46.9525768],[7.5446258,46.9525214]],[[7.5446258,46.9525214],[7.5446771,46.9523963]],[[7.5446771,46.9523963],[7.544751,46.952234]],[[7.544751,46.952234],[7.5447502,46.9521365]],[[7.5447502,46.9521365],[7.5447222,46.9519873]],[[7.5447222,46.9519873],[7.5447116,46.9518968],[7.5446267,46.9514372],[7.5446035,46.9513344],[7.5445361,46.9512155],[7.5442321,46.9508596],[7.5440684,46.9507376],[7.5437464,46.9506247],[7.5428264,46.9503122],[7.5424548,46.9501859]],[[7.5424548,46.9501859],[7.5422046,46.9501055],[7.5412346,46.9498563],[7.5410979,46.9498206],[7.5409965,46.9498078],[7.5409164,46.9498096]],[[7.5409164,46.9498096],[7.5407345,46.9498579]],[[7.5407345,46.9498579],[7.5405567,46.9499201],[7.540107,46.9500898]],[[7.540107,46.9500898],[7.5400045,46.950056],[7.5393897,46.9498139],[7.5384034,46.9494454]],[[7.5382438,46.9493675],[7.5384034,46.9494454]],[[7.5371611,46.9483199],[7.5373148,46.9486713],[7.5374032,46.9488561],[7.5374808,46.9489747],[7.5375808,46.9490745],[7.5377096,46.949157],[7.5378729,46.9492294],[7.5382438,46.9493675]],[[7.5371611,46.9483199],[7.5372739,46.9482742]],[[7.5372739,46.9482742],[7.5381607,46.9479277],[7.5393318,46.9474904],[7.5397837,46.9473148],[7.5399826,46.9472686],[7.5402194,46.9472298],[7.5403952,46.9472164],[7.5405799,46.9472163],[7.5409025,46.9472291],[7.5412773,46.9472655],[7.5417952,46.9473395],[7.5419607,46.9473572]],[[7.5419607,46.9473572],[7.5421304,46.9473672],[7.5423924,46.9473814],[7.5426514,46.9473777],[7.5428121,46.9473537],[7.5429472,46.9473278],[7.5430502,46.9472959],[7.5431436,46.9472545],[7.5432772,46.9471796]],[[7.5387301,46.9456447],[7.5398864,46.9460013],[7.540803,46.9462826],[7.5412275,46.9464328],[7.5416629,46.946618],[7.5423684,46.9469886],[7.5425359,46.9470611],[7.542707,46.9471226],[7.5428566,46.947155],[7.5429989,46.947173],[7.5432772,46.9471796]],[[7.537046,46.9446854],[7.5371311,46.9448311],[7.5373597,46.9450545],[7.5375267,46.945197],[7.5376967,46.9452866],[7.5379293,46.9453762],[7.5387301,46.9456447]],[[7.5369106,46.9427135],[7.5369583,46.9426914],[7.5369966,46.942665],[7.5370272,46.9426292],[7.5370611,46.9425632],[7.5370755,46.9424679],[7.5370877,46.9424295],[7.5371038,46.9424059],[7.5371267,46.9423878],[7.5371686,46.9423781],[7.5372121,46.9423817],[7.5372507,46.9424055],[7.5372826,46.9424362],[7.5374373,46.9427506],[7.5374534,46.94283],[7.5374077,46.9429594],[7.5373426,46.9432061],[7.5370068,46.9439187],[7.5369413,46.9440944],[7.5369307,46.9442952],[7.5369598,46.9445018],[7.537046,46.9446854]],[[7.5363553,46.9424963],[7.5365448,46.9426373],[7.5366248,46.942687],[7.5367232,46.9427216],[7.5368015,46.94273],[7.5368571,46.9427273],[7.5369106,46.9427135]],[[7.5361758,46.9423971],[7.5363553,46.9424963]],[[7.5361758,46.9423971],[7.5363016,46.9422859],[7.5364919,46.9421594],[7.53662,46.9420975]],[[7.53662,46.9420975],[7.5367813,46.9420388]],[[7.5367813,46.9420388],[7.5369516,46.9419752]],[[7.5369516,46.9419752],[7.5371806,46.9418156],[7.5375085,46.941554]],[[7.5375085,46.941554],[7.5376407,46.9414472]],[[7.5376407,46.9414472],[7.5377315,46.9413761]],[[7.5377315,46.9413761],[7.5378238,46.9413287]],[[7.5378238,46.9413287],[7.5378848,46.9413353]],[[7.5378848,46.9413353],[7.537988,46.9413716]],[[7.537988,46.9413716],[7.538222,46.9414678]],[[7.538222,46.9414678],[7.5382669,46.9414759]],[[7.5382669,46.9414759],[7.5384623,46.9414845]],[[7.5384623,46.9414845],[7.5385635,46.9414798]],[[7.5385635,46.9414798],[7.5385639,46.9414008]],[[7.5385639,46.9414008],[7.5385531,46.9413562]],[[7.5385531,46.9413562],[7.5385292,46.94132]],[[7.5385292,46.94132],[7.5383136,46.9410517]],[[7.5383136,46.9410517],[7.5382376,46.9409285]],[[7.5382376,46.9409285],[7.538184,46.9408309],[7.5381246,46.9406732]],[[7.5381246,46.9406732],[7.5380949,46.9405617],[7.5380794,46.9403833],[7.5381154,46.9401172],[7.5381719,46.939939]],[[7.5381719,46.939939],[7.538246,46.9397014],[7.5382399,46.9395414],[7.5382348,46.9393692],[7.5382379,46.9391935]],[[7.5382379,46.9391935],[7.5383757,46.9385603],[7.5384378,46.9383396],[7.5384819,46.9382396]],[[7.5384819,46.9382396],[7.538563,46.9380923]],[[7.538563,46.9380923],[7.5387204,46.9379773]],[[7.5387204,46.9379773],[7.538976,46.9378539]],[[7.538976,46.9378539],[7.5395591,46.9377339]],[[7.5395591,46.9377339],[7.5399314,46.9376616]],[[7.5399314,46.9376616],[7.5402114,46.9375634]],[[7.5402114,46.9375634],[7.5403764,46.9374191]],[[7.5403764,46.9374191],[7.5405592,46.9372656]],[[7.5405592,46.9372656],[7.5405854,46.9372436]],[[7.5405854,46.9372436],[7.5406922,46.937028]],[[7.5406922,46.937028],[7.5408951,46.9367038]],[[7.5408951,46.9367038],[7.5410413,46.9365699]],[[7.5410413,46.9365699],[7.5412613,46.9364183]],[[7.5412613,46.9364183],[7.5416397,46.9361432],[7.5417079,46.9360731],[7.5417733,46.9359771],[7.5419212,46.9357187]],[[7.5419212,46.9357187],[7.5420399,46.9354255]],[[7.5420399,46.9354255],[7.542078,46.9353158]],[[7.542078,46.9353158],[7.5420955,46.935292]],[[7.5420955,46.935292],[7.5421207,46.9352668]],[[7.5421207,46.9352668],[7.5421652,46.9352471],[7.5422283,46.9352307],[7.5423163,46.9352148],[7.5425985,46.9351846]],[[7.5425985,46.9351846],[7.5430353,46.9351643],[7.5431763,46.9351476],[7.5434983,46.9350513]],[[7.5434983,46.9350513],[7.543667,46.9350009]],[[7.543667,46.9350009],[7.5438838,46.9349361]],[[7.5438838,46.9349361],[7.5440847,46.934876],[7.5442047,46.9348261]],[[7.5442047,46.9348261],[7.5442389,46.9347911]],[[7.5442389,46.9347911],[7.5442756,46.9346633]],[[7.5442756,46.9346633],[7.5443382,46.9344749]],[[7.5443382,46.9344749],[7.5444302,46.9342843]],[[7.5444302,46.9342843],[7.5448099,46.9337609]],[[7.5448099,46.9337609],[7.5451711,46.9330382]],[[7.5451711,46.9330382],[7.5452319,46.9329742]],[[7.5452319,46.9329742],[7.545509,46.9326883]],[[7.545509,46.9326883],[7.5456589,46.9324948]],[[7.5456589,46.9324948],[7.5460005,46.9320537]],[[7.5460005,46.9320537],[7.5460527,46.9319539]],[[7.5460527,46.9319539],[7.5462801,46.9315187]],[[7.5462801,46.9315187],[7.5465714,46.9312741]],[[7.5465714,46.9312741],[7.5468028,46.9310647]],[[7.5468028,46.9310647],[7.5470424,46.9307771]],[[7.5470424,46.9307771],[7.5471713,46.9305344],[7.5472696,46.930311],[7.5473232,46.9300876]],[[7.5473232,46.9300876],[7.5474215,46.9298284]],[[7.5474215,46.9298284],[7.5475913,46.9295514]],[[7.5475913,46.9295514],[7.5480739,46.9289347]],[[7.5480739,46.9289347],[7.5482861,46.9286598]],[[7.5480014,46.9286632],[7.5482861,46.9286598]],[[7.5472739,46.9288285],[7.5480014,46.9286632]],[[7.5467593,46.9288866],[7.5472739,46.9288285]],[[7.5462442,46.928823],[7.5467593,46.9288866]],[[7.5460497,46.9287723],[7.5462442,46.928823]],[[7.5455262,46.9285944],[7.5456894,46.9286676],[7.5458849,46.9287293],[7.5460497,46.9287723]],[[7.5451357,46.9283702],[7.5453193,46.928451],[7.5454153,46.9285053],[7.5455262,46.9285944]],[[7.5447691,46.928209],[7.5451357,46.9283702]],[[7.5444331,46.9280337],[7.5445943,46.928114],[7.5447691,46.928209]],[[7.544235,46.9279295],[7.5444331,46.9280337]],[[7.5440297,46.9278611],[7.544235,46.9279295]],[[7.5436511,46.9277048],[7.5437276,46.9277687],[7.5440297,46.9278611]],[[7.543602,46.9276118],[7.5436511,46.9277048]],[[7.5435626,46.9275371],[7.543602,46.9276118]],[[7.5435509,46.9275149],[7.5435626,46.9275371]],[[7.5435361,46.9274868],[7.5435509,46.9275149]],[[7.543524,46.9274639],[7.5435361,46.9274868]],[[7.5435125,46.9274421],[7.543524,46.9274639]],[[7.5435125,46.9274421],[7.54398,46.9272998],[7.5442575,46.9271664]],[[7.5442575,46.9271664],[7.5452925,46.9266916],[7.5458506,46.9264806]],[[7.5458506,46.9264806],[7.5464423,46.9261909],[7.5466055,46.9260925]],[[7.5466055,46.9260925],[7.5467384,46.9259606],[7.5470692,46.9256032],[7.5473093,46.9253044]],[[7.5473093,46.9253044],[7.5474,46.925203],[7.5475334,46.9249309],[7.5477361,46.9243814],[7.5478428,46.9241946]],[[7.5478428,46.9241946],[7.5480082,46.9240399],[7.5485204,46.9237518]],[[7.5485204,46.9237518],[7.5487979,46.9236407],[7.5493207,46.9235193],[7.549542,46.9235041]],[[7.549542,46.9235041],[7.5497422,46.9234904]],[[7.5497422,46.9234904],[7.5499023,46.9234317]],[[7.5499023,46.9234317],[7.5500197,46.923357]],[[7.5500197,46.923357],[7.5501851,46.9232449]],[[7.5501851,46.9232449],[7.5504892,46.9229515],[7.5510601,46.9224481],[7.5512066,46.9223141],[7.551515,46.9222014]],[[7.551515,46.9222014],[7.552043,46.9220367],[7.5526715,46.9218249],[7.5530612,46.9216858],[7.553428,46.921586],[7.5538758,46.9214094]],[[7.5538758,46.9214094],[7.5541834,46.9213817],[7.5543762,46.9213926],[7.5545434,46.9214322],[7.5546343,46.9214794]],[[7.5546343,46.9214794],[7.5550747,46.9218784],[7.5552472,46.9221495],[7.5554754,46.9224695],[7.5557642,46.9228372]],[[7.5557642,46.9228372],[7.5558149,46.9229137]],[[7.5558149,46.9229137],[7.5559161,46.9230651]],[[7.5559161,46.9230651],[7.5560204,46.9231788]],[[7.5560204,46.9231788],[7.5561417,46.9232265]],[[7.5570881,46.9228018],[7.556935,46.9229191],[7.556654,46.9230297],[7.556511,46.9231103],[7.5562574,46.9231717],[7.5561417,46.9232265]],[[7.5572459,46.9226135],[7.5570881,46.9228018]],[[7.5582643,46.921855],[7.5580217,46.9220861],[7.5574754,46.9224107],[7.5572459,46.9226135]],[[7.5583908,46.9216731],[7.5582643,46.921855]],[[7.5585298,46.9213862],[7.5583908,46.9216731]],[[7.5589348,46.9210255],[7.5587974,46.9211297],[7.5586133,46.9213005],[7.5585298,46.9213862]],[[7.5589348,46.9210255],[7.5592234,46.9209497]],[[7.5592234,46.9209497],[7.5592379,46.9209458]],[[7.5592379,46.9209458],[7.5599341,46.9207877]],[[7.5599341,46.9207877],[7.5600639,46.9207588],[7.5601963,46.9207061],[7.5603323,46.9206454],[7.5610922,46.9201369],[7.5613391,46.9200151],[7.561707,46.919872]],[[7.561707,46.919872],[7.5621895,46.9195261]],[[7.5621895,46.9195261],[7.5622755,46.9194731]],[[7.5622755,46.9194731],[7.5624251,46.9193997],[7.5630807,46.9192145],[7.5633752,46.9190498],[7.5634766,46.9188792],[7.563549,46.918743]],[[7.563549,46.918743],[7.563632,46.9186586],[7.5637271,46.9185999],[7.5638202,46.9185825],[7.5639133,46.9185835],[7.5642063,46.91869],[7.5646889,46.9189434],[7.5649836,46.9190193],[7.5651538,46.9190053]],[[7.5648079,46.9185579],[7.5651538,46.9190053]],[[7.564673,46.9184399],[7.5648079,46.9185579]],[[7.564265,46.9181399],[7.5645057,46.9182936],[7.564673,46.9184399]],[[7.5609793,46.916911],[7.5609801,46.9169764],[7.5610107,46.9170573],[7.561127,46.9171385],[7.5612713,46.9171976],[7.561953,46.9174048],[7.562511,46.9175449],[7.5634355,46.9177657],[7.5637378,46.9178791],[7.5640408,46.9180183],[7.564265,46.9181399]],[[7.5638898,46.9164883],[7.5615186,46.9169036],[7.5612293,46.916937],[7.5609793,46.916911]],[[7.5645642,46.9162311],[7.5641515,46.9164425],[7.5638898,46.9164883]],[[7.564248,46.9155617],[7.5643056,46.9156069],[7.5643534,46.915666],[7.5644431,46.9159027],[7.564481,46.916044],[7.5645642,46.9162311]],[[7.5643387,46.9155103],[7.564248,46.9155617]],[[7.5643863,46.9154897],[7.5643387,46.9155103]],[[7.5646777,46.9154048],[7.5643863,46.9154897]],[[7.5652599,46.9153014],[7.5646777,46.9154048]],[[7.5657119,46.91523],[7.5652599,46.9153014]],[[7.5661153,46.9151548],[7.5657119,46.91523]],[[7.5668303,46.9149947],[7.5661153,46.9151548]],[[7.5702738,46.9142827],[7.5696082,46.9144338],[7.5686071,46.9146204],[7.5679807,46.9147709],[7.5672082,46.9149082],[7.5668303,46.9149947]],[[7.5705372,46.9142229],[7.5702738,46.9142827]],[[7.5705372,46.9142229],[7.5704792,46.9141498]],[[7.5704792,46.9141498],[7.5704262,46.9140832]],[[7.5704262,46.9140832],[7.5701417,46.9136906],[7.5699475,46.9133313],[7.5698844,46.9131954]],[[7.5698844,46.9131954],[7.5697048,46.9130594],[7.5694426,46.9128944],[7.5693164,46.9128555],[7.5690848,46.9128641]],[[7.5690848,46.9128641],[7.5687265,46.9129336],[7.5684712,46.9130106],[7.5682099,46.9130912],[7.5676371,46.9133051],[7.5673184,46.9134429],[7.5669802,46.913558],[7.5665832,46.9138449],[7.5661802,46.9141226],[7.5657336,46.9142798]]]}}]}
1

pgRouting functions return an ordered list of nodes that describe the route. When you join the result with your network table to retrieve the geometry of the route, the linestring geometries are not necessarily in the correct direction.

To bring them in the right direction, you need to compare the route's node ID's with the source and target value of the edge in the network table, and if they are different reverse the linestring geometry.

[...]
CASE
    WHEN dijkstra.node = ways.source THEN the_geom
    ELSE ST_Reverse(the_geom)
END AS route_geom
[...]

The pgRouting workshop gives a full example how to handle the direction correctly: https://workshop.pgrouting.org/2.6/en/chapters/SQLfunction.html#exercise-8-geometry-directionality

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.