Package: tsp-brouter
tsp_brouterA Python CLI script to run travelling salesman problem (tsp) algorithms usingbrouterrouting times.In addition to the CLI script thetsp_brouterlibrary includes functions to call the algorithms from within Python, of course.brouterprovides routing services with profiles that can be particularly interesting for touring bicyclists.  It is highly configurable.tsp_brouter_cli.pyis a Python3 script that calls the brouter server and returns (in-createdmmode) a "distance matrix" and route segments for all the points that the user provided (via a point file that is read by the gdal/ogr library).In the-routesmodetsp_broutercalls up to four algorithms to get traveling salesman problem solutions.  Three of them I wrote myself (as an exercise, I suppose):aBrute Forcealgorithm to get an exact solution at such a high cost in processing time that it can't be run for more than about 12 points.aNearest Neighboralgorithm that starts at one point and continues along the route to the end point always going to the next closest available point to a defined end point.aShortest Segmentalgorithm that looks at all available route segments and always includes the shortest one until there are no available segments left.a much more clever and fast algorithm than the ones I wrote, using theortoolslibrary from Google.The algorithms are available in bothone way(the user indicates the start and end points) andround tripversions.The user selects which parameter from the brouter results to minimize (distance, travel time, energy, or cost (as defined by the profile)).  By supplying your own custom profile for brouter you can get time estimates and preferred routes that are tailored to your own riding and prefernces.  One custom profile is included in thebrouter_profilesfolder.  It's one that works well for me in mountainous regions of Turkey.  Them11n serverhas many interesting profiles.The output is agpxfile of the route (to put in one's phone and follow on a bicycle tour, perhaps!).installationRequired Python3 libraries that may not be installed by default include:gdal/ogrnumpyurllib3ortoolsIn my testing I was unable to getGDALandnumpyincluded in the tsp_brouter installation package.  Install them separately if you don't already have them.Numpy should easy:pip install numpyGDAL can perhaps be installed like this:pip install GDALLet me know if that doesn't work.  There seem to be lots of ways to installGDAL.Then installingtsp_broutershould be easy:pip install tsp_brouteruser guideLet's find a bicycle touring route from Demre to Kaş while visiting some Likyan sites.If you're not familiar withogrvector datasets, then the easiest way to prepare your data fortsp_broutermay be with simple text files.  Insample_dataI provide some points in a.csvfile (demre_kaş_pt.csv) and an associated.vrtfile that enablesogrto read the.csvThe first few lines of the.csvfile look like this:name,desc,longitude,latitude
Kyaneai antik kenti (aka Cyaneae),,29.815071,36.245389
Demre,start,29.987891,36.242588
turnoff to Trysa,,29.896623,36.263519
Antiphellos (Likya) rating 2,end,29.638490,36.201590You'll see that I explicitly specified a start point and an end point in the desc field.  Coordinates are in wgs84.basic usage$ tsp_brouter_cli.py 
tsp_brouter_cli.py
        -h help
        -list-servers
        -list-profiles server_name
        -verify input_file -l layer -fname name_field -limit number
        -createdm input_file -l layer -fse se_field -fname name_field
            out_distance_matrix
        -routes -rt -ow -bf in_distance_matrix -dp distance_proxy_name out_gpx
        -server name -profile nameFirst we'll create our distance matrix, a file we namedemre_kaş_dm.p:$ tsp_brouter_cli.py -createdm demre_kaş_pt.vrt -fse desc -fname name demre_kaş_dm.p
server not specified, using default value: brouter
profile not specified, using default value: trekking
[============================================================] 100.0% ...
demre_kaş_dm.p written
number of points: 15
script execution time:  0:01:36.033249Then we create a gpx file of our route, a file we namedemre_kaş_ln.gpx:$ tsp_brouter_cli.py -routes -ow demre_kaş_dm.p demre_kaş_ln.gpx
distance matrix profile not specified, using the default value: time
ERROR 1: Empty geometries cannot be constructed
NNF: 52992.0 startPT: 0 PTCount: 15
NNR: 48333.0 startPT: 0 PTCount: 15
number of points: 15
script execution time:  0:00:00.773349Now you can follow the route on your bicycle tour!to doallow user to specify which algorithms to run (currently the only optional one is the brute force algorithm because it is slow)cache data from server to avoid needing to call it again for the same from point-to point-server-profile combinationallow user to override check that their non-local profile already exists (in case the profile exist on the server but not in the tsp_brouter dictionary)return dictionaries instead of lists (GetShortestRoute, ReturnTimeGeometryFromGeoJSON, GetTravelTimes, VerifyPTs) (I think this is better coding practice; I'm sure there's a lot I can do toward better coding practice)how should I structure the code?  It's all just functions (procedural?) now.  I suppose I should have a class that runs the algorithms as requested since they pretty much all take the same inputs?