def matchingSens(osmLine): index = 0 # Looping over the stations because sometimes some of them # are not the same depending of the direction while index+2 < len(osmLine["sensA"])-1: firstStationName = ots.stationName(osmLine["sensA"][index+0]) secondStationName = ots.stationName(osmLine["sensA"][index+1]) if secondStationName == firstStationName: secondStationName = ots.stationName(osmLine["sensA"][index+2]) lineId = mbt.idForLine(osmLine["name"]) #ordered Stations list from mobitrans lineStations = mbt.stationsForLine(lineId, 1) stationList = [x["name"] for x in lineStations] result1 = difflib.get_close_matches(firstStationName, stationList) result2 = difflib.get_close_matches(secondStationName, stationList) #second chance, looking for substrings: if not result1: result1 = [s for s in stationList if s in firstStationName] if not result2: result2 = [s for s in stationList if s in secondStationName] # third change, doing the same but with no accent nor diacritics if not result1: asciiName = ''.join(c for c in unicodedata.normalize('NFD', firstStationName) if unicodedata.category(c) != 'Mn') result1 = [s for s in stationList if s in asciiName] if not result2: asciiName = ''.join(c for c in unicodedata.normalize('NFD', secondStationName) if unicodedata.category(c) != 'Mn') result2 = [s for s in stationList if s in asciiName] if result1 and result2: break else: index += 1 if not result1 or not result2: #print(firstStationName, secondStationName, stationList) print("\n*No match found while calculating directions for line", osmLine["name"], firstStationName, secondStationName, "") print(stationList) return index1 = stationList.index(result1[0]) index2 = stationList.index(result2[0]) if index1 < index2: sens = 1 else: sens = 2 return sens
def mergingData() : if not os.path.isfile('osmDirections.json'): print("Recreating OSM relations file") OSMjson = osmAPI.parseOsmTAGRelation() text_file = open("osmDirections.json", "w") text_file.write(OSMjson) text_file.close() print("Loading OSM relations file") file = open("osmDirections.json", "r") s = file.read() linesDict = json.loads(s) (termWidth, height) = console.getTerminalSize() total = len(linesDict) index=0 print ("Merging the data...") for osmLine in linesDict: if not verbose: # Progressbar stuff index = index+1 percentage = index/total sys.stdout.write("\r") for i in range(int(termWidth*percentage)): sys.stdout.write("-") sys.stdout.flush() # Assign directions retrived from from OSM to the direction in Mobitrans # Testing if the line is available on Mobitrans MbtLineId = mbt.idForLine(osmLine["name"]) if MbtLineId: sens = matchingSens(osmLine) osmLine["MbtId"] = MbtLineId if sens == 1: osmLine["sens1"] = osmLine["sensA"] osmLine["sens2"] = osmLine["sensB"] osmLine["terminus1"] = osmLine["terminusA"] osmLine["terminus2"] = osmLine["terminusB"] osmLine.pop("sensA", None) osmLine.pop("sensB", None) osmLine.pop("terminusA", None) osmLine.pop("terminusB", None) elif sens == 2: osmLine["sens2"] = osmLine["sensA"] osmLine["sens1"] = osmLine["sensB"] osmLine["terminus2"] = osmLine["terminusA"] osmLine["terminus1"] = osmLine["terminusB"] osmLine.pop("sensA", None) osmLine.pop("sensB", None) osmLine.pop("terminusA", None) osmLine.pop("terminusB", None) else: osmLine.pop("MbtId", None) osmLine.pop("sensA", None) osmLine.pop("sensB", None) #In case the line ain't present on Mobitrans else: print(osmLine["name"]) osmLine.pop("sensA", None) osmLine.pop("sensB", None) if verbose: pprint.pprint(linesDict) jsonData = json.dumps(linesDict, indent=4, sort_keys=True) text_file = open("MergedData.json", "w") text_file.write(jsonData) text_file.close() return linesDict