File size: 5,935 Bytes
a227ca6
 
 
 
 
 
 
 
 
 
 
 
 
0f2d952
a227ca6
 
 
5d64b25
a227ca6
5b0d5ef
820b4b3
4433c66
820b4b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a146294
a227ca6
 
 
 
 
50fd089
 
 
 
 
 
b3bfa70
50fd089
 
 
 
b3bfa70
5688cac
b878eeb
50fd089
 
b3bfa70
a227ca6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f1d27d
 
 
 
 
a146294
2f1d27d
 
 
 
 
e64e207
 
 
65e60ed
e64e207
95d01d0
2f1d27d
 
a227ca6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Aug  1 13:11:36 2022

@author: syed
"""
import requests
import urllib3
import json
from geocoder import geo_level1
from geocoder import geo_level2
from geocoder import geo_level3
import time
from utils import geoutil
import re
import regex_spatial
import streamlit as st
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
import os
BACKEND_URL = "https://SpatialWebAgent-dockerb22.hf.space/api/predict/"
API_TOKEN = os.getenv('API_TOKEN3')
def call_backend(input_text):
    try:
        headers = {
            "Authorization": f"Bearer {API_TOKEN}"
        }
        response = requests.post(
            BACKEND_URL,
            headers=headers,
            json={"data": [input_text]},
            timeout=10
        )
        if response.status_code == 200:
            return response.json()  # ✅ 保留原始 JSON 对象 (dict)
        return {"error": f"❌ Backend Error (HTTP {response.status_code})"}
    except Exception as e:
        return {"error": f"⚠️ Connection Error: {str(e)}"}



def dismabiguate_entities(doc, ent, ase, level_1, level_2, level_3, midmid):
    return get_coordinates(ent, ase, level_1, level_2, level_3, midmid)
def get_coordinates(ent, ase, level_1, level_2, level_3, midmid):
    # request_url = 'https://nominatim.openstreetmap.org/search.php?q='+ase+'&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"
    # }
    # time.sleep(2)
    # page = requests.get(request_url, headers=headers, verify=False)

    # json_content = json.loads(page.content)
    # print(json_content, 'jjjjj')
    # all_coordinates = json_content[0]['geojson']['coordinates'][0]
    # centroid = (float(json_content[0]['lon']), float(json_content[0]['lat']))

    res = call_backend(ase)['data'][0]
    st.markdown(res)
    all_coordinates = res[0]
    centroid = res[1]
    
    for p in all_coordinates:
        p2 = (p[0], p[1])
        angle = geoutil.calculate_bearing(centroid, p2)
        p.append(angle)
    mid1 = None
    mid2 = None
    coordinates = all_coordinates

    if level_3 is not None:
        all_coordinates, centroid = geo_level3.get_level3_coordinates(coordinates, centroid, level_3, level_1)
    
    geojson = get_geojson(ent, all_coordinates, centroid)
    
    return geojson


def dismabiguate_entities_between(doc, ent, ase, level_1, level_2, level_3, midmid):
    return get_coordinates_between(doc, ent, ase, level_1, level_2, level_3, midmid)

def get_coordinates_between(doc, ent, ase, level_1, level_2, level_3, midmid):

    # first ase
    request_url = 'https://nominatim.openstreetmap.org/search.php?q=' + doc.ents[0].text + '&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"
    }
    page1 = requests.get(request_url, headers=headers, verify=False)

    # second ase
    request_url = 'https://nominatim.openstreetmap.org/search.php?q=' + doc.ents[1].text + '&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)


    # request_url = 'https://nominatim.openstreetmap.org/search.php?q=' + ase + '&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)
  
    # st.markdown('The IP of this Space is blocked by OpenStreetMap, we are fixing this issue. You can still interact with the second table at the first page.')   
    # 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']))


    
    res = call_backend(ase)
    # st.markdown(res)
    res = res['data'][0]
    # st.markdown(res)
    all_coordinates = res[0]
    centroid = res[1]
    for p in all_coordinates:
        p2 = (p[0], p[1])
        angle = geoutil.calculate_bearing(centroid, p2)
        p.append(angle)
    mid1 = None
    mid2 = None
    coordinates = all_coordinates
    if level_1 is not None:

        all_coordinates, centroid, mid1, mid2 = geo_level1.get_level1_coordinates(all_coordinates, centroid, level_1, midmid)

    if level_2 is not None:
        if level_1 is not None and level_1.lower() not in geo_level1.center:
            all_coordinates, centroid = geo_level2.get_level2_coordinates(coordinates, centroid, level_2, level_1)
        else:
            print ("Else executed")
            all_coordinates, centroid = geo_level2.get_level2_coordinates(all_coordinates, centroid, level_2, level_1)

    if level_3 is not None:
        all_coordinates, centroid = geo_level3.get_level3_coordinates(coordinates, centroid, level_3, level_1)

    geojson = get_geojson(ent, all_coordinates, centroid)

    return geojson

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 export(ent, poly_json):
    with open(ent+'.geojson', 'w') as outfile:
        json.dump(poly_json, outfile)