File size: 24,214 Bytes
17e77ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
import requests
import urllib3
import json
from utils import geoutil
import regex_spatial
from shapely.geometry import Polygon, MultiPoint, LineString, Point, mapping
import re
import geopandas as gpd
from geocoder import geo_level1
from openai import OpenAI
from utils.config import api_key

client = OpenAI(
    api_key=api_key
)


model = "gpt-4o"

north = ["north", "N'", "North", "NORTH"]
south = ["south", "S'", "South", "SOUTH"]
east = ["east", "E'", "East", "EAST"]
west = ["west", "W'", "West", "WEST"]
northeast = ["north-east", "NE'", "north east", "NORTH-EAST", "North East", "NORTH EAST"]
southeast = ["south-east", "SE'", "south east", "SOUTH-EAST", "South East", "SOUTH EAST"]
northwest = ["north-west", "NW'", "north west", "NORTH-WEST", "North West", "NORTH WEST"]
southwest = ["south-west", "SW'", "south west", "SOUTH-WEST", "South West", "SOUTH WEST"]
center = ["center","central", "downtown","midtown"]
#
#
# def get_directional_coordinates(coordinates, direction, centroid, minimum, maximum, is_midmid):
#     direction_coordinates = get_directional_coordinates_by_angle(coordinates, direction, minimum, maximum)
#     midmid1, midmid2 = geoutil.get_midmid_point(centroid, direction_coordinates[0], direction_coordinates[-1],
#                                                 is_midmid)
#     if direction in west:
#         maxi = max(p[2] for p in direction_coordinates)
#         mini = min(p[2] for p in direction_coordinates)
#         index_mini = 0
#         index_maxi = 0
#         for idx, p in enumerate(direction_coordinates):
#             if p[2] == mini:
#                 index_mini = idx
#             if p[2] == maxi:
#                 index_maxi = idx
#
#         direction_coordinates.insert(index_maxi + 1, midmid2)
#         direction_coordinates.insert(index_mini + 1, midmid1)
#     else:
#         direction_coordinates.append(midmid2)
#         direction_coordinates.append(midmid1)
#
#     return direction_coordinates, midmid1, midmid2
#
#
# def get_directional_coordinates_by_angle(coordinates, direction, minimum, maximum):
#     direction_coordinates = []
#     for p in coordinates:
#         if direction in east:
#             if p[2] >= minimum or p[2] <= maximum:
#                 direction_coordinates.append(p)
#
#         else:
#             if p[2] >= minimum and p[2] <= maximum:
#                 direction_coordinates.append(p)
#     return direction_coordinates
#
#
# def get_directional_coordinates_by_angle(coordinates, direction, minimum, maximum):
#     direction_coordinates = []
#     for p in coordinates:
#         if direction in east:
#             if p[2] >= minimum or p[2] <= maximum:
#                 direction_coordinates.append(p)
#
#         else:
#             if p[2] >= minimum and p[2] <= maximum:
#                 direction_coordinates.append(p)
#     return direction_coordinates
#
#
# def get_central(coordinates, centroid, direction, is_midmid):
#     n_min_max = get_min_max("north")
#     n_coordinates = get_directional_coordinates_by_angle(coordinates, "north", n_min_max[0], n_min_max[1])
#     n_mid1, n_mid2 = geoutil.get_midmid_point(centroid, n_coordinates[0], n_coordinates[-1], is_midmid)
#
#     ne_min_max = get_min_max("north east")
#     ne_coordinates = get_directional_coordinates_by_angle(coordinates, "north east", ne_min_max[0], ne_min_max[1])
#     ne_mid1, ne_mid2 = geoutil.get_midmid_point(centroid, ne_coordinates[0], ne_coordinates[-1], is_midmid)
#
#     e_min_max = get_min_max("east")
#     e_coordinates = get_directional_coordinates_by_angle(coordinates, "east", e_min_max[0], e_min_max[1])
#     e_mid1, e_mid2 = geoutil.get_midmid_point(centroid, e_coordinates[0], e_coordinates[-1], is_midmid)
#
#     se_min_max = get_min_max("south east")
#     se_coordinates = get_directional_coordinates_by_angle(coordinates, "south east", se_min_max[0], se_min_max[1])
#     se_mid1, se_mid2 = geoutil.get_midmid_point(centroid, se_coordinates[0], se_coordinates[-1], is_midmid)
#
#     s_min_max = get_min_max("south")
#     s_coordinates = get_directional_coordinates_by_angle(coordinates, "south", s_min_max[0], s_min_max[1])
#     s_mid1, s_mid2 = geoutil.get_midmid_point(centroid, s_coordinates[0], s_coordinates[-1], is_midmid)
#
#     sw_min_max = get_min_max("south west")
#     sw_coordinates = get_directional_coordinates_by_angle(coordinates, "south west", sw_min_max[0], sw_min_max[1])
#     sw_mid1, sw_mid2 = geoutil.get_midmid_point(centroid, sw_coordinates[0], sw_coordinates[-1], is_midmid)
#
#     w_min_max = get_min_max("west")
#     w_coordinates = get_directional_coordinates_by_angle(coordinates, "west", w_min_max[0], w_min_max[1])
#     w_mid1, w_mid2 = geoutil.get_midmid_point(centroid, w_coordinates[0], w_coordinates[-1], is_midmid)
#
#     nw_min_max = get_min_max("north west")
#     nw_coordinates = get_directional_coordinates_by_angle(coordinates, "north west", nw_min_max[0], nw_min_max[1])
#     nw_mid1, nw_mid2 = geoutil.get_midmid_point(centroid, nw_coordinates[0], nw_coordinates[-1], is_midmid)
#
#     central_coordindates = [e_mid1, e_mid2, ne_mid1, ne_mid2, n_mid1, n_mid2,
#                             nw_mid1, nw_mid2, w_mid1, w_mid2, sw_mid1, sw_mid2,
#                             s_mid1, s_mid2, se_mid1, se_mid2]
#     return central_coordindates
#
#
# def get_min_max(direction):
#     regex = regex_spatial.get_directional_regex()
#     direction_list = regex.split("|")
#     if direction in direction_list:
#         if direction in east:
#             return (337, 22)
#         if direction in northeast:
#             return (22, 67)
#         if direction in north:
#             return (67, 112)
#         if direction in northwest:
#             return (112, 157)
#         if direction in west:
#             return (157, 202)
#         if direction in southwest:
#             return (202, 247)
#         if direction in south:
#             return (247, 292)
#         if direction in southeast:
#             return (292, 337)
#
#     return None
# def get_level1_coordinates(coordinates, centroid, direction, is_midmid):
#     min_max = get_min_max(direction)
#     if min_max is not None:
#         coordinates, mid1, mid2 = get_directional_coordinates(coordinates, direction, centroid, min_max[0], min_max[1], is_midmid)
#         return coordinates, centroid, mid1, mid2
#     elif direction.lower() in center:
#         return get_central(coordinates, centroid, direction, is_midmid), centroid, None, None
#     else:
#         return coordinates, centroid, None, None
def to_standard_2d_list(data):
    arr = np.array(data)

    # 强制变成一维后 reshape,前提是元素总数是2的倍数
    flat = arr.flatten()
    if flat.size % 2 != 0:
        raise ValueError("元素个数不是2的倍数,不能 reshape 成 [N, 2] 格式")

    return flat.reshape(-1, 2).tolist()


def get_geojson(ent, arr, centroid):
    poly_json = {}
    poly_json['type'] = 'FeatureCollection'
    poly_json['features'] = []
    coordinates= []
    coordinates.append(arr)
    poly_json['features'].append({
    'type':'Feature',
    'id': ent,
    'properties': {
        'centroid': centroid
        },
    'geometry': {
        'type':'Polygon',
        'coordinates': coordinates
        }
    })
    return poly_json


def get_coordinates(ent):
    request_url = 'https://nominatim.openstreetmap.org/search.php?q= ' +ent +'&polygon_geojson=1&accept-language=en&format=jsonv2'
    headers = {
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.3 Safari/605.1.15"
    }
    page = requests.get(request_url, headers=headers, verify=False)
    json_content = json.loads(page.content)
    all_coordinates = json_content[0]['geojson']['coordinates'][0]
    centroid = (float(json_content[0]['lon']), float(json_content[0]['lat']))
    for p in all_coordinates:
        p2 = (p[0], p[1])
        angle = geoutil.calculate_bearing(centroid, p2)
        p.append(angle)

    geojson = get_geojson(ent, all_coordinates, centroid)

    return geojson['features'][0]['geometry']['coordinates'][0], geojson['features'][0]['properties']['centroid']

def get_coordinates(location):
    request_url = f'https://nominatim.openstreetmap.org/search.php?q={location}&polygon_geojson=1&accept-language=en&format=jsonv2'

    print(request_url)
    headers = {"User-Agent": "Mozilla/5.0"}
    response = requests.get(request_url, headers=headers, verify=False)
    json_content = json.loads(response.content)
    # print(json_content)
    if json_content[0]['geojson']['type'] == 'Polygon':
        coordinates = json_content[0]['geojson']['coordinates'][0]
    elif json_content[0]['geojson']['type'] == 'Point':
        coordinates = json_content[0]['geojson']['coordinates']
    else:
        print(json_content[0]['geojson']['type'])
    centroid = (float(json_content[0]['lon']), float(json_content[0]['lat']))
    return (coordinates, centroid)


# level3
def get_directional_coordinates_by_angle(coordinates, centroid, direction, minimum, maximum):
    # minimum = 157
    # maximum = 202

    direction_coordinates = []
    for p in coordinates:
        angle = geoutil.calculate_bearing(centroid, p)
        p2 = (p[0], p[1], angle)
        if direction in geo_level1.east:
            if angle >= minimum or angle <= maximum:
                direction_coordinates.append(p2)

        else:
            if angle >= minimum and angle <= maximum:
                direction_coordinates.append(p2)
    # print(type(direction_coordinates[0]))
    # if(direction in geo_level1.west):
    #    direction_coordinates.sort(key=lambda k: k[2], reverse=True)

    return direction_coordinates
def get_level3(level3):
    digits = re.findall('[0-9]+', level3)[0]
    unit = re.findall('[A-Za-z]+', level3)[0]
    return digits, unit

def get_direction_coordinates(coordinates, centroid, level1):
    min_max = geo_level1.get_min_max(level1)
    if min_max is not None:
        coord = get_directional_coordinates_by_angle(coordinates, centroid, level1, min_max[0], min_max[1])
        return coord
    return coordinates
def sort_west(poly1, poly2, centroid):
    coords1 = mapping(poly1)["features"][0]["geometry"]["coordinates"]
    coords2 = mapping(poly2)["features"][0]["geometry"]["coordinates"]
    coord1 = []
    coord2 = []
    coord = []
    for c in coords1:
        pol = list(c[::-1])
        coord1.extend(pol)
    for c in coords2:
        pol = list(c[::-1])
        coord2.extend(pol)
    coo1 = []
    coo2 = []
    for p in coord1:
        angle = geoutil.calculate_bearing(centroid, p)
        if angle >= 157 and angle <= 202:
            coo1.append((p[0], p[1], angle))
    for p in coord2:
        angle = geoutil.calculate_bearing(centroid, p)
        if angle >= 157 and angle <= 202:
            coo2.append((p[0], p[1], angle))
    coo1.extend(coo2)
    return coo1


def get_level3_coordinates(coordinates, level_3, level1):
    distance, unit = get_level3(level_3)
    kms = geoutil.get_kilometers(distance, unit)
    coord = []

    coords0, center = coordinates

    if not isinstance(coords0, list) or len(coords0) < 3:

        # 从原始点出发,根据方向移动距离 kms 得到新圆心
        lat_km = 111.32
        lon_km = 111.32 * np.cos(np.radians(center[1]))

        dx = dy = 0

        if level1 is not None:
            if level1 in geo_level1.east:
                dx = kms / lon_km
            elif level1 in geo_level1.west:
                dx = -kms / lon_km
            elif level1 in geo_level1.north:
                dy = kms / lat_km
            elif level1 in geo_level1.south:
                dy = -kms / lat_km
            # 你也可以支持 northeast、southwest 等复合方向

        new_center = (center[0] + dx, center[1] + dy)

        # 用固定半径画个圆(例如半径2km)
        r_km = 1  # 半径设为1km,你也可以设为其他值

        circle_points = []
        for theta in np.linspace(0, 360, num=100):
            theta_rad = np.radians(theta)
            d_lat = (np.sin(theta_rad) * r_km) / lat_km
            d_lon = (np.cos(theta_rad) * r_km) / lon_km
            circle_points.append((new_center[0] + d_lon, new_center[1] + d_lat))

        # 输出中心(使用新圆心)
        if circle_points:
            center_point = MultiPoint(circle_points).centroid
            center = (center_point.x, center_point.y)
        else:
            center = new_center

        return circle_points, center

    # 正常 polygon 流程
    poly1 = Polygon(coords0)
    polygon1 = gpd.GeoSeries(poly1)

    # 生成环形区域
    poly2 = polygon1.buffer(0.0095 * kms, join_style=2)
    poly3 = polygon1.buffer(0.013 * kms, join_style=2)
    poly = poly3.difference(poly2)

    # 获取坐标
    coords = mapping(poly)["features"][0]["geometry"]["coordinates"]
    for c in coords:
        pol = list(c[::-1])
        coord.extend(pol)

    # 方向裁剪
    if level1 is not None:
        coord = get_direction_coordinates(coord, coordinates[1], level1)
        if level1 in geo_level1.west:
            coord = sort_west(poly3, poly2, coordinates[1])

    # 计算质心
    if coord:
        center_point = MultiPoint(coord).centroid
        center = (center_point.x, center_point.y)
    else:
        center = coordinates[1]

    return coord, center
# level 3 end

# between
def get_between_coordinates(coordinates1, coordinates2):
    """
    计算两个区域之间的中间点,并生成一个等面积的圆形区域。
    如果某个输入仅为点(坐标长度 < 3),则其面积设为 0;
    如果两个输入都是点,则默认半径为 2km。
    :param coordinates1: 第一个区域的边界坐标和中心点
    :param coordinates2: 第二个区域的边界坐标和中心点
    :return: 圆形区域的坐标集和圆心
    """

    def is_valid_polygon(coords):
        return isinstance(coords, list) and len(coords) >= 3

    coords1, center1 = coordinates1
    coords2, center2 = coordinates2

    # 判断输入是否为合法多边形(>=3个点)
    if is_valid_polygon(coords1):
        poly1 = Polygon(coords1)
        area1 = poly1.area
    else:
        area1 = 0

    if is_valid_polygon(coords2):
        poly2 = Polygon(coords2)
        area2 = poly2.area
    else:
        area2 = 0

    # 计算中心点(两个中心的中点)
    midpoint = (
        (center1[0] + center2[0]) / 2,
        (center1[1] + center2[1]) / 2
    )

    # 如果两个区域都是点,则使用默认半径 2km
    if area1 == 0 and area2 == 0:
        r_km = 2
    else:
        avg_area = (area1 + area2) / 2
        r_km = np.sqrt(avg_area / np.pi) * 111.32  # 近似 km 半径

    # 经纬度距离换算因子
    lat_km = 111.32
    lon_km = 111.32 * np.cos(np.radians(midpoint[1]))

    # 生成圆形区域坐标(100个点)
    circle_points = []
    for theta in np.linspace(0, 360, num=100):
        theta_rad = np.radians(theta)
        d_lat = (np.sin(theta_rad) * r_km) / lat_km
        d_lon = (np.cos(theta_rad) * r_km) / lon_km
        circle_points.append((midpoint[0] + d_lon, midpoint[1] + d_lat))

    return circle_points, midpoint
# between end


def llmapi(text):
    system_prompt = (
        "你是一个资深的地理学家,你的任务是通过给定的一段自然语言,来选择正确的定位函数顺序以及他们的输入。\n"
        "你能选择的定位函数有:\n"
        "1. 相对定位(Relative Positioning):输入为地点坐标,方位,距离。输出为距离‘距离’输入的地点坐标的‘方位’的坐标。\n"
        "2. 中间定位(Between Positioning):输入为两个地点的坐标,输出为两个地点坐标的中点。\n"
        "请先进行思维链(CoT)推理,并最终用 JSON 格式输出你的答案,用 `<<<JSON>>>` 和 `<<<END>>>` 包裹起来。\n"
        "请确保所有输入仅包含:地点名称(字符串)、索引(整数)、方位(字符串,必须是英文)或距离(字符串,带单位),不允许返回诸如 'Chatswood 南4 km的坐标' 这样的内容。\n"
        "每个步骤编号都有 id 记录,然后如果某个输入是之前步骤的输出,那么输入对应步骤的 id。\n"
        "所有方向必须使用英文(如 south, west, northeast, etc.)。\n"
        "示例输出:\n"
        "<<<JSON>>>\n"
        "[{\"id\": 1, \"function\": \"Relative\", \"inputs\": [\"Chatswood\", \"south\", \"4 km\"]},"
        "{\"id\": 2, \"function\": \"Relative\", \"inputs\": [\"North Sydney\", \"west\", \"2 km\"]},"
        "{\"id\": 3, \"function\": \"Between\", \"inputs\": [1, 2]},"
        "{\"id\": 4, \"function\": \"Relative\", \"inputs\": [3, \"southwest\", \"5 km\"]}]\n"
        "<<<END>>>")

    messages = [
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": text},
    ]

    chat_completion = client.chat.completions.create(
        messages=messages,
        model=model,
    )

    result = chat_completion.choices[0].message.content
    json_match = re.search(r'<<<JSON>>>\n(.*?)\n<<<END>>>', result, re.DOTALL)

    if json_match:
        # print(json.loads(json_match.group(1)))
        return json.loads(json_match.group(1))
    else:
        raise ValueError("LLM 输出未包含预期的 JSON 格式数据。")
def llmapi(text):
    system_prompt = (
        "You are an experienced geographer. Your task is to determine the correct sequence of positioning functions and their inputs based on a given piece of natural language.\n"
        "The positioning functions you can choose from are:\n"
        "1. Relative Positioning: Inputs is (location coordinate or location name, direction, and distance). Outputs the coordinates that are in the given 'direction' and 'distance' from the input location.\n"
        "2. Between Positioning: Inputs is (location 1 coordinates or location 1 name, location 2 coordinates or location 2 name). Outputs the midpoint coordinate between the two locations.\n"
        "You can only use the given functions, and the inputs to the functions must obey the above properties. The given functions can be combined to solve complex situations."
        "First, perform chain-of-thought (CoT) reasoning, and finally output your answer in JSON format, wrapped between `<<<JSON>>>` and `<<<END>>>`.\n"
        "Make sure all inputs only include: location names (strings), step indices (integers), directions (strings, must be in English), or distances (strings with units). Do not return expressions like 'the coordinate 4 km south of Chatswood'.\n"
        "Each step must have an 'id'. If the input of a step is the output of a previous step, use that step’s 'id' as the input.\n"
        "All directions must be in English (e.g., south, west, northeast, etc.).\n"
        "Example output:\n"
        "<<<JSON>>>\n"
        "[{\"id\": 1, \"function\": \"Relative\", \"inputs\": [\"Chatswood\", \"south\", \"4 km\"]},"
        "{\"id\": 2, \"function\": \"Relative\", \"inputs\": [\"North Sydney\", \"west\", \"2 km\"]},"
        "{\"id\": 3, \"function\": \"Between\", \"inputs\": [1, 2]},"
        "{\"id\": 4, \"function\": \"Relative\", \"inputs\": [3, \"southwest\", \"5 km\"]}]\n"
        "<<<END>>>")

    messages = [
        {"role": "system", "content": system_prompt},
        {"role": "user", "content": text},
    ]

    chat_completion = client.chat.completions.create(
        messages=messages,
        model=model,
    )

    result = chat_completion.choices[0].message.content
    print(result)
    json_match = re.search(r'<<<JSON>>>\n(.*?)\n<<<END>>>', result, re.DOTALL)

    if json_match:
        return json.loads(json_match.group(1))
    else:
        raise ValueError("LLM 输出未包含预期的 JSON 格式数据。")





def execute_steps(steps):
    data = {}

    for step in steps:
        step_id = step['id']
        function = step['function']
        inputs = step['inputs']
        # print('-' * 50)
        # print(function)
        # print(inputs)


        resolved_inputs = []
        for inp in inputs:
            if isinstance(inp, int):
                resolved_inputs.append(data[inp])
            else:
                resolved_inputs.append(inp)
        if function == "Relative":
            location, direction, distance = resolved_inputs
            if isinstance(location, str):
                location = get_coordinates(location)

            location = [to_standard_2d_list(location[0])] + list(location[1:])
            location = [[[151.214901,-33.859175]], (151.214901,-33.859175)]
            result = get_level3_coordinates(location, distance, direction)
            data[step_id] = result

        elif function == "Between":


            location1, location2 = resolved_inputs
            # print(location1)
            # print(111)
            # print(location2)
            if isinstance(location1, str):
                location1 = get_coordinates(location1)

                location1 = [to_standard_2d_list(location1[0])] + list(location1[1:])
            if isinstance(location2, str):

                location2 = get_coordinates(location2)
                location2 = [to_standard_2d_list(location2[0])] + list(location2[1:])
            result = get_between_coordinates(location1, location2)

            data[step_id] = result

    return data



if __name__ == '__main__':
    # a = get_coordinates('Burwood')
    # a2 = get_coordinates('Glebe')
    # b = get_level3_coordinates(a, '5 km', 'east')
    # c = get_between_coordinates(a, a2)

    # 完整通道
    # 默认输入
    # default_input_text = "在Chatswood南边4公里与North Sydney 东边2公里的中间的西南5公里。"
    # default_input_text = "你是一位规划师,正在为华盛顿州的一项新森林监测站选址。两个潜在的参考位置分别是雷尼尔山国家公园(Mount Rainier National Park)和北喀斯喀特国家公园(North Cascades National Park)。首先,你想在这两个国家公园之间找到一个中间点。接着,你希望在这个中间点与北喀斯喀特国家公园之间,再取一个中间位置,以便确定最终的建设候选地。"
    # default_input_text = "在Chatswood和North Sydney的中间靠近North Sydney的四分之一位置"
    # default_input_text = "Plan a trip that involves determining the midpoint between Paris and London, and then finding another midpoint between this location and Paris to identify potential stopovers during travel."
    # default_input_text = "5km southwest of Chatswood, 4km south of Chatswood and 2km north of North Sydney."



    # 解析 LLM 结果
    # parsed_steps = llmapi(default_input_text)
    # parsed_steps = [{'id': 1, 'function': 'Relative', 'inputs': ['Chatswood', 'south', '4 km']}, {'id': 2, 'function': 'Relative', 'inputs': ['North Sydney', 'east', '2 km']}, {'id': 3, 'function': 'Between', 'inputs': [1, 2]}, {'id': 4, 'function': 'Relative', 'inputs': [3, 'south west', '5 km']}]
    # parsed_steps = [{"id": 1, "function": "Between", "inputs": ["Chatswood", "North Sydney"]},{"id": 2, "function": "Between", "inputs": [1, "North Sydney"]}]
    # parsed_steps = [{"id": 1, "function": "Relative", "inputs": ["Katoomba", "southeast", "3 km"]}, {"id": 2, "function": "Between", "inputs": [1, "Echo Point"]}]
    # parsed_steps = [{'id': 1, 'function': 'Relative', 'inputs': ['Scafell Pike', 'east', '9 km']}]
    # parsed_steps = [{'id': 1, 'function': 'Relative', 'inputs': ['Colosseum', 'northeast', '8 km']}, {'id': 2, 'function': 'Relative', 'inputs': [1, 'northeast', '2 km']}]
    parsed_steps = [
      {"id": 1, "function": "Between", "inputs": ["Statue of Liberty", "Eiffel Tower"]},
      {"id": 2, "function": "Relative", "inputs": [1, "west", "8 km"]}
    ]

    # 执行步骤
    result = execute_steps(parsed_steps)
    # 输出最终计算结果
    print(result)
    print('-' * 100)
    print(result[(max(result.keys()))][0])
    # 通道结束

    # location = get_coordinates('Chatswood')
    # result = get_level3_coordinates(location, '4 km', 'north west')
    # print(result)