File size: 2,850 Bytes
9be4956
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from apis import GoogleDistanceMatrix
import os
import json
from tqdm import tqdm
import csv

os.environ["http_proxy"] = "http://127.0.0.1:7890"
os.environ["https_proxy"] = "http://127.0.0.1:7890"
gdm = GoogleDistanceMatrix()

# print(gdm.run_for_build_database("New York","Seattle"))


def load_line_json_data(filename):
    data = []
    with open(filename, 'r', encoding='utf-8') as f:
        for line in f.read().strip().split('\n'):
            unit = json.loads(line)
            data.append(unit)
    return data


def get_city_list(days, deparure_city, destination):
    city_list = []
    city_list.append(deparure_city)
    if days == 3:
        city_list.append(destination)
    else:
        city_set = open('/home/xj/toolAugEnv/code/toolConstraint/database/background/citySet_with_states.txt').read().split('\n')
        state_city_map = {}
        for unit in city_set:
            city, state = unit.split('\t')
            if state not in state_city_map:
                state_city_map[state] = []
            state_city_map[state].append(city)
        for city in state_city_map[destination]:
            if city != deparure_city:
                city_list.append(city + f"({destination})")
    return city_list

if __name__ == '__main__':

    org_dest_list = []
    query_list =load_line_json_data('/home/xj/toolAugEnv/code/toolConstraint/data/query/all.jsonl')
    info_list = []

    for query in tqdm(query_list):

        destination_city_list = get_city_list(query['days'],query['org'],query['dest'])

        # for city in destination_city_list:
        #         if [query['org'],city] not in org_dest_list:
        #             org_dest_list.append([query['org'],city])
        #             info_list.append(gdm.run_for_build_database(query['org'],city))
        #         if [city,query['org']] not in org_dest_list:
        #             org_dest_list.append([city,query['org']])
        #             info_list.append(gdm.run_for_build_database(city,query['org']))
                

        for city_1 in tqdm(destination_city_list):
            for city_2 in destination_city_list:
                if city_1 != city_2 and [city_1,city_2] not in org_dest_list:
                    org_dest_list.append([city_1,city_2])
                    info_list.append(gdm.run_for_build_database(city_1,city_2))

    # # write to csv file
    # # example:{'origin': 'New York', 'destination': 'Seattle', 'cost': None, 'duration': '1 day 18 hours', 'distance': '4,589 km'}
    with open('/home/xj/toolAugEnv/code/toolConstraint/database/background/distance.csv', 'w', newline='') as csvfile:
        fieldnames = ['origin', 'destination', 'cost', 'duration', 'distance']
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        writer.writeheader()
        for unit in info_list:
            writer.writerow(unit)