Shunfeng Zheng commited on
Commit
a227ca6
·
verified ·
1 Parent(s): d2fa504

Upload 18 files

Browse files
disambiguation/__pycache__/disambiguate.cpython-310.pyc ADDED
Binary file (3.25 kB). View file
 
disambiguation/__pycache__/disambiguate.cpython-39.pyc ADDED
Binary file (2.2 kB). View file
 
disambiguation/disambiguate.py ADDED
@@ -0,0 +1,155 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ Created on Mon Aug 1 13:11:36 2022
5
+
6
+ @author: syed
7
+ """
8
+ import requests
9
+ import urllib3
10
+ import json
11
+ from geocoder import geo_level1
12
+ from geocoder import geo_level2
13
+ from geocoder import geo_level3
14
+
15
+ from utils import geoutil
16
+ import re
17
+ import regex_spatial
18
+ urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
19
+
20
+
21
+
22
+
23
+ def dismabiguate_entities(doc, ent, ase, level_1, level_2, level_3, midmid):
24
+ return get_coordinates(ent, ase, level_1, level_2, level_3, midmid)
25
+ def get_coordinates(ent, ase, level_1, level_2, level_3, midmid):
26
+ request_url = 'https://nominatim.openstreetmap.org/search.php?q='+ase+'&polygon_geojson=1&accept-language=en&format=jsonv2'
27
+ headers = {
28
+ "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"
29
+ }
30
+ page = requests.get(request_url, headers=headers, verify=False)
31
+ # page = requests.get(request_url, verify=False)
32
+ # print(request_url, '++++++')
33
+ # print(ent, '++++++')
34
+ # print(ase, '++++++')
35
+ # print(level_1, '++++++')
36
+ # print(level_2, '++++++')
37
+ # print(level_3, '++++++')
38
+ # print(midmid, '++++++')
39
+
40
+
41
+ json_content = json.loads(page.content)
42
+ # json_content = json.loads(aa)
43
+ print(json_content, 'jjjjj')
44
+ all_coordinates = json_content[0]['geojson']['coordinates'][0]
45
+ centroid = (float(json_content[0]['lon']), float(json_content[0]['lat']))
46
+ for p in all_coordinates:
47
+ p2 = (p[0], p[1])
48
+ angle = geoutil.calculate_bearing(centroid, p2)
49
+ p.append(angle)
50
+ mid1 = None
51
+ mid2 = None
52
+ coordinates = all_coordinates
53
+ # if level_1 is not None:
54
+ #
55
+ # all_coordinates, centroid, mid1, mid2 = geo_level1.get_level1_coordinates(all_coordinates, centroid, level_1, midmid)
56
+
57
+ # if level_2 is not None:
58
+ # if level_1 is not None and level_1.lower() not in geo_level1.center:
59
+ # all_coordinates, centroid = geo_level2.get_level2_coordinates(coordinates, centroid, level_2, level_1)
60
+ # else:
61
+ # print ("Else executed")
62
+ # all_coordinates, centroid = geo_level2.get_level2_coordinates(all_coordinates, centroid, level_2, level_1)
63
+
64
+ if level_3 is not None:
65
+ all_coordinates, centroid = geo_level3.get_level3_coordinates(coordinates, centroid, level_3, level_1)
66
+
67
+ geojson = get_geojson(ent, all_coordinates, centroid)
68
+
69
+ return geojson
70
+
71
+
72
+ def dismabiguate_entities_between(doc, ent, ase, level_1, level_2, level_3, midmid):
73
+ return get_coordinates_between(doc, ent, ase, level_1, level_2, level_3, midmid)
74
+
75
+
76
+ def get_coordinates_between(doc, ent, ase, level_1, level_2, level_3, midmid):
77
+
78
+ # first ase
79
+ request_url = 'https://nominatim.openstreetmap.org/search.php?q=' + doc.ents[0].text + '&polygon_geojson=1&accept-language=en&format=jsonv2'
80
+ headers = {
81
+ "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"
82
+ }
83
+ page1 = requests.get(request_url, headers=headers, verify=False)
84
+
85
+ # second ase
86
+ request_url = 'https://nominatim.openstreetmap.org/search.php?q=' + doc.ents[1].text + '&polygon_geojson=1&accept-language=en&format=jsonv2'
87
+ headers = {
88
+ "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"
89
+ }
90
+ page = requests.get(request_url, headers=headers, verify=False)
91
+
92
+
93
+ request_url = 'https://nominatim.openstreetmap.org/search.php?q=' + ase + '&polygon_geojson=1&accept-language=en&format=jsonv2'
94
+ headers = {
95
+ "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"
96
+ }
97
+ page = requests.get(request_url, headers=headers, verify=False)
98
+ print('bebe')
99
+ print(ase)
100
+ print(doc.ents)
101
+
102
+ json_content = json.loads(page.content)
103
+ # json_content = json.loads(aa)
104
+ # print(json_content, 'jjj')
105
+ all_coordinates = json_content[0]['geojson']['coordinates'][0]
106
+ centroid = (float(json_content[0]['lon']), float(json_content[0]['lat']))
107
+ for p in all_coordinates:
108
+ p2 = (p[0], p[1])
109
+ angle = geoutil.calculate_bearing(centroid, p2)
110
+ p.append(angle)
111
+ mid1 = None
112
+ mid2 = None
113
+ coordinates = all_coordinates
114
+ if level_1 is not None:
115
+
116
+ all_coordinates, centroid, mid1, mid2 = geo_level1.get_level1_coordinates(all_coordinates, centroid, level_1, midmid)
117
+
118
+ if level_2 is not None:
119
+ if level_1 is not None and level_1.lower() not in geo_level1.center:
120
+ all_coordinates, centroid = geo_level2.get_level2_coordinates(coordinates, centroid, level_2, level_1)
121
+ else:
122
+ print ("Else executed")
123
+ all_coordinates, centroid = geo_level2.get_level2_coordinates(all_coordinates, centroid, level_2, level_1)
124
+
125
+ if level_3 is not None:
126
+ all_coordinates, centroid = geo_level3.get_level3_coordinates(coordinates, centroid, level_3, level_1)
127
+
128
+ geojson = get_geojson(ent, all_coordinates, centroid)
129
+
130
+ return geojson
131
+
132
+ def get_geojson(ent, arr, centroid):
133
+ poly_json = {}
134
+ poly_json['type'] = 'FeatureCollection'
135
+ poly_json['features'] = []
136
+ coordinates= []
137
+ coordinates.append(arr)
138
+ poly_json['features'].append({
139
+ 'type':'Feature',
140
+ 'id': ent,
141
+ 'properties': {
142
+ 'centroid': centroid
143
+ },
144
+ 'geometry': {
145
+ 'type':'Polygon',
146
+ 'coordinates': coordinates
147
+ }
148
+ })
149
+ return poly_json
150
+
151
+ def export(ent, poly_json):
152
+ with open(ent+'.geojson', 'w') as outfile:
153
+ json.dump(poly_json, outfile)
154
+
155
+
geocoder/.DS_Store ADDED
Binary file (6.15 kB). View file
 
geocoder/__pycache__/geo_level1.cpython-310.pyc ADDED
Binary file (4.18 kB). View file
 
geocoder/__pycache__/geo_level1.cpython-39.pyc ADDED
Binary file (3.95 kB). View file
 
geocoder/__pycache__/geo_level11.cpython-310.pyc ADDED
Binary file (3.86 kB). View file
 
geocoder/__pycache__/geo_level2.cpython-310.pyc ADDED
Binary file (2.84 kB). View file
 
geocoder/__pycache__/geo_level2.cpython-39.pyc ADDED
Binary file (2.87 kB). View file
 
geocoder/__pycache__/geo_level3.cpython-310.pyc ADDED
Binary file (3.61 kB). View file
 
geocoder/__pycache__/geo_level3.cpython-39.pyc ADDED
Binary file (2.78 kB). View file
 
geocoder/geo_level1.py ADDED
@@ -0,0 +1,185 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ Created on Tue Aug 2 12:38:31 2022
5
+
6
+ @author: syed
7
+ """
8
+ import regex_spatial
9
+ from utils import geoutil
10
+
11
+
12
+ north = ["north", "N'", "North", "NORTH"]
13
+ south = ["south", "S'", "South", "SOUTH"]
14
+ east = ["east", "E'", "East", "EAST"]
15
+ west = ["west", "W'", "West", "WEST"]
16
+ northeast = ["north-east", "NE'", "north east", "NORTH-EAST", "North East", "NORTH EAST", 'northeast']
17
+ southeast = ["south-east", "SE'", "south east", "SOUTH-EAST", "South East", "SOUTH EAST", 'southeast']
18
+ northwest = ["north-west", "NW'", "north west", "NORTH-WEST", "North West", "NORTH WEST", 'northwest']
19
+ southwest = ["south-west", "SW'", "south west", "SOUTH-WEST", "South West", "SOUTH WEST", 'southwest']
20
+ center = ["center","central", "downtown","midtown"]
21
+ def get_min_max(direction):
22
+ regex = regex_spatial.get_directional_regex()
23
+ direction_list = regex.split("|")
24
+ if direction in direction_list:
25
+ if direction in east:
26
+ return (337, 22)
27
+ if direction in northeast:
28
+ return (22, 67)
29
+ if direction in north:
30
+ return (67, 112)
31
+ if direction in northwest:
32
+ return (112, 157)
33
+ if direction in west:
34
+ return (157, 202)
35
+ if direction in southwest:
36
+ return (202, 247)
37
+ if direction in south:
38
+ return (247, 292)
39
+ if direction in southeast:
40
+ return (292, 337)
41
+
42
+ return None
43
+
44
+
45
+ def get_min_max(direction):
46
+ regex = regex_spatial.get_directional_regex()
47
+ direction_list = regex.split("|")
48
+ if direction in direction_list:
49
+ if direction in east:
50
+ return (337, 22)
51
+ if direction in northeast:
52
+ return (292, 337)
53
+ if direction in north:
54
+ return (247, 292)
55
+ if direction in northwest:
56
+ return (202, 247)
57
+ if direction in west:
58
+ return (157, 202)
59
+ if direction in southwest:
60
+ return (112, 157)
61
+ if direction in south:
62
+ return (67, 112)
63
+ if direction in southeast:
64
+ return (22, 67)
65
+
66
+ return None
67
+
68
+ # def get_min_max(direction):
69
+ # regex = regex_spatial.get_directional_regex()
70
+ # direction_list = regex.split("|")
71
+ # if direction in direction_list:
72
+ # if direction in north:
73
+ # return (337, 22)
74
+ # if direction in northeast:
75
+ # return (22, 67)
76
+ # if direction in east:
77
+ # return (67, 112)
78
+ # if direction in southeast:
79
+ # return (112, 157)
80
+ # if direction in south:
81
+ # return (157, 202)
82
+ # if direction in southwest:
83
+ # return (202, 247)
84
+ # if direction in west:
85
+ # return (247, 292)
86
+ # if direction in northwest:
87
+ # return (292, 337)
88
+ #
89
+ # return None
90
+
91
+ def get_directional_coordinates_by_angle(coordinates, direction, minimum, maximum):
92
+ direction_coordinates = []
93
+ for p in coordinates:
94
+ if direction in east:
95
+ if p[2] >= minimum or p[2] <= maximum:
96
+ direction_coordinates.append(p)
97
+
98
+ else:
99
+ if p[2] >= minimum and p[2] <= maximum:
100
+ direction_coordinates.append(p)
101
+ return direction_coordinates
102
+
103
+ def get_directional_coordinates(coordinates, direction, centroid , minimum, maximum, is_midmid):
104
+ direction_coordinates = get_directional_coordinates_by_angle(coordinates, direction, minimum, maximum)
105
+ print(direction_coordinates, 'ddddd')
106
+ midmid1, midmid2= geoutil.get_midmid_point(centroid, direction_coordinates[0],direction_coordinates[-1], is_midmid)
107
+ if direction in west:
108
+ maxi = max(p[2] for p in direction_coordinates)
109
+ mini = min(p[2] for p in direction_coordinates)
110
+ index_mini = 0
111
+ index_maxi = 0
112
+ for idx,p in enumerate(direction_coordinates):
113
+ if p[2] == mini:
114
+ index_mini = idx
115
+ if p[2] == maxi:
116
+ index_maxi = idx
117
+
118
+ direction_coordinates.insert(index_maxi+1, midmid2)
119
+ direction_coordinates.insert(index_mini+1, midmid1)
120
+ print(index_maxi+1, midmid2, 'imim')
121
+ print(index_mini+1, midmid1, 'imim')
122
+ else:
123
+ direction_coordinates.append(midmid2)
124
+ direction_coordinates.append(midmid1)
125
+
126
+ return direction_coordinates, midmid1, midmid2
127
+
128
+ def get_level1_coordinates(coordinates, centroid, direction, is_midmid):
129
+ min_max = get_min_max(direction)
130
+ if min_max is not None:
131
+ coordinates, mid1, mid2 = get_directional_coordinates(coordinates, direction, centroid,
132
+ min_max[0], min_max[1],is_midmid)
133
+ # print(mid1, 'min_max')
134
+ # print(mid2, 'min_max')
135
+ print(coordinates, 'min_max')
136
+ print("Level 1 Coordinates///")
137
+ for idx, p in enumerate(coordinates):
138
+ print(idx, p)
139
+ return coordinates, centroid, mid1, mid2
140
+ elif direction.lower() in center:
141
+ return get_central(coordinates, centroid, direction, is_midmid), centroid, None, None
142
+ else :
143
+ return coordinates, centroid, None, None
144
+
145
+
146
+ def get_central(coordinates, centroid, direction, is_midmid):
147
+
148
+ n_min_max = get_min_max("north")
149
+ n_coordinates=get_directional_coordinates_by_angle(coordinates, "north", n_min_max[0], n_min_max[1])
150
+ n_mid1, n_mid2 = geoutil.get_midmid_point(centroid,n_coordinates[0],n_coordinates[-1], is_midmid)
151
+
152
+ ne_min_max = get_min_max("north east")
153
+ ne_coordinates=get_directional_coordinates_by_angle(coordinates, "north east", ne_min_max[0], ne_min_max[1])
154
+ ne_mid1, ne_mid2 = geoutil.get_midmid_point(centroid,ne_coordinates[0],ne_coordinates[-1], is_midmid)
155
+
156
+ e_min_max = get_min_max("east")
157
+ e_coordinates=get_directional_coordinates_by_angle(coordinates, "east", e_min_max[0], e_min_max[1])
158
+ e_mid1, e_mid2 = geoutil.get_midmid_point(centroid,e_coordinates[0],e_coordinates[-1], is_midmid)
159
+
160
+ se_min_max = get_min_max("south east")
161
+ se_coordinates=get_directional_coordinates_by_angle(coordinates, "south east", se_min_max[0], se_min_max[1])
162
+ se_mid1, se_mid2 = geoutil.get_midmid_point(centroid,se_coordinates[0],se_coordinates[-1], is_midmid)
163
+
164
+ s_min_max = get_min_max("south")
165
+ s_coordinates=get_directional_coordinates_by_angle(coordinates, "south", s_min_max[0], s_min_max[1])
166
+ s_mid1, s_mid2 = geoutil.get_midmid_point(centroid,s_coordinates[0],s_coordinates[-1], is_midmid)
167
+
168
+ sw_min_max = get_min_max("south west")
169
+ sw_coordinates=get_directional_coordinates_by_angle(coordinates, "south west", sw_min_max[0], sw_min_max[1])
170
+ sw_mid1, sw_mid2 = geoutil.get_midmid_point(centroid,sw_coordinates[0],sw_coordinates[-1], is_midmid)
171
+
172
+ w_min_max = get_min_max("west")
173
+ w_coordinates=get_directional_coordinates_by_angle(coordinates, "west", w_min_max[0], w_min_max[1])
174
+ w_mid1, w_mid2 = geoutil.get_midmid_point(centroid,w_coordinates[0],w_coordinates[-1], is_midmid)
175
+
176
+ nw_min_max = get_min_max("north west")
177
+ nw_coordinates=get_directional_coordinates_by_angle(coordinates, "north west", nw_min_max[0], nw_min_max[1])
178
+ nw_mid1, nw_mid2 = geoutil.get_midmid_point(centroid,nw_coordinates[0],nw_coordinates[-1], is_midmid)
179
+
180
+ central_coordindates =[e_mid1, e_mid2, ne_mid1, ne_mid2, n_mid1, n_mid2,
181
+ nw_mid1, nw_mid2, w_mid1, w_mid2, sw_mid1, sw_mid2,
182
+ s_mid1, s_mid2, se_mid1, se_mid2]
183
+ return central_coordindates
184
+
185
+
geocoder/geo_level11.py ADDED
@@ -0,0 +1,130 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ Created on Tue Aug 2 12:38:31 2022
5
+
6
+ @author: syed
7
+ """
8
+ import regex_spatial
9
+ from utils import geoutil
10
+
11
+
12
+ north = ["north", "N'", "North", "NORTH"]
13
+ south = ["south", "S'", "South", "SOUTH"]
14
+ east = ["east", "E'", "East", "EAST"]
15
+ west = ["west", "W'", "West", "WEST"]
16
+ northeast = ["north-east", "NE'", "north east", "NORTH-EAST", "North East", "NORTH EAST"]
17
+ southeast = ["south-east", "SE'", "south east", "SOUTH-EAST", "South East", "SOUTH EAST"]
18
+ northwest = ["north-west", "NW'", "north west", "NORTH-WEST", "North West", "NORTH WEST"]
19
+ southwest = ["south-west", "SW'", "south west", "SOUTH-WEST", "South West", "SOUTH WEST"]
20
+ center = ["center","central", "downtown","midtown"]
21
+ def get_min_max(direction):
22
+ regex = regex_spatial.get_directional_regex()
23
+ direction_list = regex.split("|")
24
+ if direction in direction_list:
25
+ if direction in north:
26
+ return (337, 22)
27
+ if direction in northeast:
28
+ return (22, 67)
29
+ if direction in east:
30
+ return (67, 112)
31
+ if direction in southeast:
32
+ return (112, 157)
33
+ if direction in south:
34
+ return (157, 202)
35
+ if direction in southwest:
36
+ return (202, 247)
37
+ if direction in west:
38
+ return (247, 292)
39
+ if direction in northwest:
40
+ return (292, 337)
41
+
42
+ return None
43
+
44
+
45
+ def get_directional_coordinates_by_angle(coordinates, direction, minimum, maximum):
46
+ direction_coordinates = []
47
+ for p in coordinates:
48
+ if direction in north:
49
+ if p[2] >= minimum or p[2] <= maximum:
50
+ direction_coordinates.append(p)
51
+
52
+ else:
53
+ if p[2] >= minimum and p[2] <= maximum:
54
+ direction_coordinates.append(p)
55
+ return direction_coordinates
56
+
57
+ def get_directional_coordinates(coordinates, direction, centroid , minimum, maximum, is_midmid):
58
+ direction_coordinates = get_directional_coordinates_by_angle(coordinates, direction, minimum, maximum)
59
+
60
+ midmid1, midmid2= geoutil.get_midmid_point(centroid, direction_coordinates[0],direction_coordinates[-1], is_midmid)
61
+ if direction in south:
62
+ maxi = max(p[2] for p in direction_coordinates)
63
+ mini = min(p[2] for p in direction_coordinates)
64
+ index_mini = 0
65
+ index_maxi = 0
66
+ for idx,p in enumerate(direction_coordinates):
67
+ if p[2] == mini:
68
+ index_mini = idx
69
+ if p[2] == maxi:
70
+ index_maxi = idx
71
+
72
+ direction_coordinates.insert(index_maxi+1, midmid2)
73
+ direction_coordinates.insert(index_mini+1, midmid1)
74
+ else:
75
+ direction_coordinates.append(midmid2)
76
+ direction_coordinates.append(midmid1)
77
+
78
+ return direction_coordinates, midmid1, midmid2
79
+
80
+ def get_level1_coordinates(coordinates, centroid, direction, is_midmid):
81
+ min_max = get_min_max(direction)
82
+ if min_max is not None:
83
+ coordinates, mid1, mid2 = get_directional_coordinates(coordinates, direction, centroid,
84
+ min_max[0], min_max[1],is_midmid)
85
+ #centroid = geoutil.get_centroid(coordinates, centroid,min_max[0], min_max[1])
86
+ return coordinates, centroid, mid1, mid2
87
+ elif direction.lower() in center:
88
+ return get_central(coordinates, centroid, direction, is_midmid), centroid, None, None
89
+ else :
90
+ return coordinates, centroid, None, None
91
+ def get_central(coordinates, centroid, direction, is_midmid):
92
+
93
+ n_min_max = get_min_max("north")
94
+ n_coordinates=get_directional_coordinates_by_angle(coordinates, "north", n_min_max[0], n_min_max[1])
95
+ n_mid1, n_mid2 = geoutil.get_midmid_point(centroid,n_coordinates[0],n_coordinates[-1], is_midmid)
96
+
97
+ ne_min_max = get_min_max("north east")
98
+ ne_coordinates=get_directional_coordinates_by_angle(coordinates, "north east", ne_min_max[0], ne_min_max[1])
99
+ ne_mid1, ne_mid2 = geoutil.get_midmid_point(centroid,ne_coordinates[0],ne_coordinates[-1], is_midmid)
100
+
101
+ e_min_max = get_min_max("east")
102
+ e_coordinates=get_directional_coordinates_by_angle(coordinates, "east", e_min_max[0], e_min_max[1])
103
+ e_mid1, e_mid2 = geoutil.get_midmid_point(centroid,e_coordinates[0],e_coordinates[-1], is_midmid)
104
+
105
+ se_min_max = get_min_max("south east")
106
+ se_coordinates=get_directional_coordinates_by_angle(coordinates, "south east", se_min_max[0], se_min_max[1])
107
+ se_mid1, se_mid2 = geoutil.get_midmid_point(centroid,se_coordinates[0],se_coordinates[-1], is_midmid)
108
+
109
+ s_min_max = get_min_max("south")
110
+ s_coordinates=get_directional_coordinates_by_angle(coordinates, "south", s_min_max[0], s_min_max[1])
111
+ s_mid1, s_mid2 = geoutil.get_midmid_point(centroid,s_coordinates[0],s_coordinates[-1], is_midmid)
112
+
113
+ sw_min_max = get_min_max("south west")
114
+ sw_coordinates=get_directional_coordinates_by_angle(coordinates, "south west", sw_min_max[0], sw_min_max[1])
115
+ sw_mid1, sw_mid2 = geoutil.get_midmid_point(centroid,sw_coordinates[0],sw_coordinates[-1], is_midmid)
116
+
117
+ w_min_max = get_min_max("west")
118
+ w_coordinates=get_directional_coordinates_by_angle(coordinates, "west", w_min_max[0], w_min_max[1])
119
+ w_mid1, w_mid2 = geoutil.get_midmid_point(centroid,w_coordinates[0],w_coordinates[-1], is_midmid)
120
+
121
+ nw_min_max = get_min_max("north west")
122
+ nw_coordinates=get_directional_coordinates_by_angle(coordinates, "north west", nw_min_max[0], nw_min_max[1])
123
+ nw_mid1, nw_mid2 = geoutil.get_midmid_point(centroid,nw_coordinates[0],nw_coordinates[-1], is_midmid)
124
+
125
+ central_coordindates =[n_mid1, n_mid2, ne_mid1, ne_mid2, e_mid1, e_mid2,
126
+ se_mid1, se_mid2, s_mid1, s_mid2, sw_mid1, sw_mid2,
127
+ w_mid1, w_mid2, nw_mid1, nw_mid2]
128
+ return central_coordindates
129
+
130
+
geocoder/geo_level2.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ Created on Tue Aug 2 12:38:31 2022
5
+
6
+ @author: syed
7
+ """
8
+ import regex_spatial
9
+ from utils import geoutil
10
+ import geopandas as gpd
11
+ import pandas as pd
12
+ import re
13
+ from shapely.geometry import Polygon,mapping
14
+ import numpy as np
15
+ from shapely.geometry import Polygon, MultiPoint
16
+ from shapely.geometry.base import geom_factory
17
+ from shapely.geos import lgeos
18
+ from geocoder import geo_level1
19
+
20
+ #def get_common_coordinates(coordinates, level2):
21
+ #gdp.
22
+
23
+ def get_near(level2):
24
+ near = re.search(regex_spatial.get_near_regex(), level2)
25
+ if near is not None:
26
+ return near.group()
27
+ return None
28
+
29
+ def get_surrounding(level2):
30
+ surrounding = re.search(regex_spatial.get_surrounding_regex(), level2)
31
+ if surrounding is not None:
32
+ return surrounding.group()
33
+ return None
34
+
35
+
36
+ def sort_west(coords1, poly2, centroid):
37
+ #coords1 = mapping(poly1)["features"][0]["geometry"]["coordinates"]
38
+ coords2 = mapping(poly2)["features"][0]["geometry"]["coordinates"]
39
+ coord1 = []
40
+ coord2 = []
41
+ coord = []
42
+ for c in coords1:
43
+ pol = list(c[::-1])
44
+ coord1.extend(pol)
45
+ for c in coords2:
46
+ pol = list(c[::-1])
47
+ coord2.extend(pol)
48
+ coo1 = []
49
+ coo2 = []
50
+ for p in coord1:
51
+ angle = geoutil.calculate_bearing(centroid, p)
52
+ if angle >= 157 and angle <= 202:
53
+ coo1.append((p[0], p[1], angle))
54
+ for p in coord2:
55
+ angle = geoutil.calculate_bearing(centroid, p)
56
+ if angle >= 157 and angle <= 202:
57
+ coo2.append((p[0], p[1], angle))
58
+ coo1.extend(coo2)
59
+ return coo1
60
+
61
+ def get_directional_coordinates_by_angle(coordinates, centroid, direction, minimum, maximum):
62
+ direction_coordinates = []
63
+ for p in coordinates:
64
+ angle = geoutil.calculate_bearing(centroid, p)
65
+ if direction in geo_level1.east:
66
+ if angle >= minimum or angle <= maximum:
67
+ direction_coordinates.append(p)
68
+
69
+ else:
70
+ if angle >= minimum and angle <= maximum:
71
+ direction_coordinates.append(p)
72
+ #if(direction in geo_level1.west):
73
+ # direction_coordinates.sort(key=lambda k: k[2], reverse=True)
74
+
75
+ return direction_coordinates
76
+
77
+ def get_direction_coordinates(coordinates, centroid, level1):
78
+ min_max = geo_level1.get_min_max(level1)
79
+ if min_max is not None:
80
+ coord = get_directional_coordinates_by_angle(coordinates, centroid, level1, min_max[0], min_max[1])
81
+ return coord
82
+ return coordinates
83
+
84
+
85
+ def get_level2_coordinates(coordinates, centroid, level_2, level_1):
86
+ near = get_near(level_2)
87
+ surrounding = get_surrounding(level_2)
88
+
89
+
90
+ poly1 = Polygon(coordinates)
91
+ polygon1 = gpd.GeoSeries(poly1)
92
+ if near is not None:
93
+ poly2 = polygon1.buffer(0.0095, join_style=2)
94
+ if surrounding is not None:
95
+ poly2 = polygon1.buffer(0.012, join_style=2)
96
+
97
+ poly = poly2.difference(polygon1)
98
+ coords = mapping(poly)["features"][0]["geometry"]["coordinates"]
99
+
100
+ coord = []
101
+ for c in coords:
102
+ pol = list(c[::-1])
103
+ coord.extend(pol)
104
+
105
+ if level_1 is not None and level_1.lower() not in geo_level1.center:
106
+ coord = get_direction_coordinates(coord, centroid, level_1)
107
+ if level_1 in geo_level1.west:
108
+ coord = sort_west(coordinates,poly2, centroid)
109
+
110
+ print("Level 2 Coordinates")
111
+ for idx, p in enumerate(coord):
112
+ print(idx, p)
113
+
114
+ return coord, centroid
115
+
116
+
117
+
geocoder/geo_level3.py ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ Created on Tue Aug 2 12:38:31 2022
5
+
6
+ @author: syed
7
+ """
8
+ import regex_spatial
9
+ from utils import geoutil
10
+ import geopandas as gpd
11
+ import pandas as pd
12
+ import re
13
+ from shapely.geometry import Polygon,mapping
14
+ import numpy as np
15
+ from shapely.geometry import Polygon, MultiPoint, LineString, Point
16
+ from shapely.geometry.base import geom_factory
17
+ from shapely.geos import lgeos
18
+ from geocoder import geo_level1
19
+
20
+
21
+
22
+ def get_level3(level3):
23
+ digits = re.findall('[0-9]+', level3)[0]
24
+ unit = re.findall('[A-Za-z]+', level3)[0]
25
+ return digits, unit
26
+
27
+
28
+ def get_directional_coordinates_by_angle(coordinates, centroid, direction, minimum, maximum):
29
+ direction_coordinates = []
30
+ for p in coordinates:
31
+ angle = geoutil.calculate_bearing(centroid, p)
32
+ p2= (p[0],p[1],angle)
33
+ if direction in geo_level1.east:
34
+ if angle >= minimum or angle <= maximum:
35
+ direction_coordinates.append(p2)
36
+
37
+ else:
38
+ if angle >= minimum and angle <= maximum:
39
+ direction_coordinates.append(p2)
40
+ #print(type(direction_coordinates[0]))
41
+ #if(direction in geo_level1.west):
42
+ # direction_coordinates.sort(key=lambda k: k[2], reverse=True)
43
+
44
+ return direction_coordinates
45
+
46
+ def sort_west(poly1, poly2, centroid):
47
+ coords1 = mapping(poly1)["features"][0]["geometry"]["coordinates"]
48
+ coords2 = mapping(poly2)["features"][0]["geometry"]["coordinates"]
49
+ coord1 = []
50
+ coord2 = []
51
+ coord = []
52
+ for c in coords1:
53
+ pol = list(c[::-1])
54
+ coord1.extend(pol)
55
+ for c in coords2:
56
+ pol = list(c[::-1])
57
+ coord2.extend(pol)
58
+ coo1 = []
59
+ coo2 = []
60
+ for p in coord1:
61
+ angle = geoutil.calculate_bearing(centroid, p)
62
+ if angle >= 157 and angle <= 202:
63
+ coo1.append((p[0], p[1], angle))
64
+ for p in coord2:
65
+ angle = geoutil.calculate_bearing(centroid, p)
66
+ if angle >= 157 and angle <= 202:
67
+ coo2.append((p[0], p[1], angle))
68
+ coo1.extend(coo2)
69
+ return coo1
70
+
71
+ def get_direction_coordinates(coordinates, centroid, level1):
72
+ min_max = geo_level1.get_min_max(level1)
73
+ if min_max is not None:
74
+ coord = get_directional_coordinates_by_angle(coordinates, centroid, level1, min_max[0], min_max[1])
75
+ return coord
76
+ return coordinates
77
+
78
+ def get_level3_coordinates(coordinates, centroid, level_3, level1):
79
+ distance, unit = get_level3(level_3)
80
+
81
+ kms = geoutil.get_kilometers(distance, unit)
82
+ print(distance, unit, kms, 'duk')
83
+
84
+ coord = []
85
+
86
+ poly1 = Polygon(coordinates)
87
+ polygon1 = gpd.GeoSeries(poly1)
88
+ poly2 = polygon1.buffer(0.0095*kms, join_style=2) # 扩大小面积
89
+ poly3 = polygon1.buffer(0.013*kms, join_style=2) # 扩大大面积
90
+ poly = poly3.difference(poly2) # 合二为一成为环状
91
+ coords = mapping(poly)["features"][0]["geometry"]["coordinates"] # 改为坐标格式
92
+ # print(mapping(poly2)["features"][0]["geometry"]["coordinates"], 'cmcm')
93
+ # print(mapping(poly3)["features"][0]["geometry"]["coordinates"], 'cmcm')
94
+ # print(coords, 'cmcm')
95
+
96
+
97
+ for c in coords:
98
+ pol = list(c[::-1])
99
+ coord.extend(pol)
100
+ if level1 is not None:
101
+ coord = get_direction_coordinates(coord, centroid, level1)
102
+ if level1 in geo_level1.west:
103
+ coord = sort_west(poly3, poly2, centroid)
104
+ print("Level 3 Coordinates")
105
+ for idx, p in enumerate(coord):
106
+ print(idx, p)
107
+ return coord, centroid
108
+
109
+
110
+ def get_between_coordinates(coordinates1, coordinates2, centroid1, centroid2):
111
+ # 创建多边形对象
112
+ poly1 = Polygon(coordinates1)
113
+ poly2 = Polygon(coordinates2)
114
+
115
+ # 计算中心点连线
116
+ center_line = LineString([centroid1, centroid2])
117
+
118
+ def max_perpendicular_distance(poly, line):
119
+ max_dist = 0
120
+ farthest_points = None
121
+
122
+ for point in poly.exterior.coords:
123
+ p = Point(point)
124
+ # 计算点到中心线的垂直距离
125
+ dist = p.distance(line)
126
+ if dist > max_dist:
127
+ max_dist = dist
128
+ farthest_points = p
129
+
130
+ return max_dist * 2 # 计算直径(双倍最大垂直距离)
131
+
132
+ # 计算两个区域的最大垂线距离
133
+ diameter1 = max_perpendicular_distance(poly1, center_line)
134
+ diameter2 = max_perpendicular_distance(poly2, center_line)
135
+
136
+ # 计算平均直径 R
137
+ R = (diameter1 + diameter2) / 2
138
+
139
+ # 计算圆心(两个中心点的中点)
140
+ midpoint = ((centroid1[0] + centroid2[0]) / 2, (centroid1[1] + centroid2[1]) / 2)
141
+
142
+ # 生成圆形区域
143
+ circle = Point(midpoint).buffer(R / 2, resolution=100)
144
+
145
+ # 获取圆的坐标
146
+ circle_coords = list(circle.exterior.coords)
147
+
148
+ return [circle_coords], midpoint
149
+
150
+
geocoder/llm_coding.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import urllib3
3
+ import json
4
+ from utils import geoutil
5
+
6
+
7
+
8
+
9
+ def get_coordinates(ent, ase, level_1, level_2, level_3, midmid):
10
+ request_url = 'https://nominatim.openstreetmap.org/search.php?q= ' +ase +'&polygon_geojson=1&accept-language=en&format=jsonv2'
11
+ headers = {
12
+ "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"
13
+ }
14
+ page = requests.get(request_url, headers=headers, verify=False)
15
+
16
+
17
+
18
+ json_content = json.loads(page.content)
19
+ # json_content = json.loads(aa)
20
+ print(json_content, 'jjjjj')
21
+ all_coordinates = json_content[0]['geojson']['coordinates'][0]
22
+ centroid = (float(json_content[0]['lon']), float(json_content[0]['lat']))
23
+ for p in all_coordinates:
24
+ p2 = (p[0], p[1])
25
+ angle = geoutil.calculate_bearing(centroid, p2)
26
+ p.append(angle)
27
+ mid1 = None
28
+ mid2 = None
29
+ coordinates = all_coordinates
30
+ # if level_1 is not None:
31
+ #
32
+ # all_coordinates, centroid, mid1, mid2 = geo_level1.get_level1_coordinates(all_coordinates, centroid, level_1, midmid)
33
+
34
+ # if level_2 is not None:
35
+ # if level_1 is not None and level_1.lower() not in geo_level1.center:
36
+ # all_coordinates, centroid = geo_level2.get_level2_coordinates(coordinates, centroid, level_2, level_1)
37
+ # else:
38
+ # print ("Else executed")
39
+ # all_coordinates, centroid = geo_level2.get_level2_coordinates(all_coordinates, centroid, level_2, level_1)
40
+
41
+ if level_3 is not None:
42
+ all_coordinates, centroid = geo_level3.get_level3_coordinates(coordinates, centroid, level_3, level_1)
43
+
44
+ geojson = get_geojson(ent, all_coordinates, centroid)
45
+
46
+ return geojson
geocoder/locate_function.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ Created on Tue Aug 2 12:38:31 2022
5
+
6
+ @author: syed
7
+ """
8
+ import regex_spatial
9
+ from utils import geoutil
10
+ import geopandas as gpd
11
+ import pandas as pd
12
+ import re
13
+ from shapely.geometry import Polygon,mapping
14
+ import numpy as np
15
+ from shapely.geometry import Polygon, MultiPoint, LineString, Point
16
+ from shapely.geometry.base import geom_factory
17
+ from shapely.geos import lgeos
18
+ from geocoder import geo_level1
19
+
20
+
21
+
22
+
23
+
24
+ def get_between_coordinates(coordinates1, coordinates2, centroid1, centroid2):
25
+ # 创建多边形对象
26
+ poly1 = Polygon(coordinates1)
27
+ poly2 = Polygon(coordinates2)
28
+
29
+ # 计算中心点连线
30
+ center_line = LineString([centroid1, centroid2])
31
+
32
+ def max_perpendicular_distance(poly, line):
33
+ max_dist = 0
34
+ farthest_points = None
35
+
36
+ for point in poly.exterior.coords:
37
+ p = Point(point)
38
+ # 计算点到中心线的垂直距离
39
+ dist = p.distance(line)
40
+ if dist > max_dist:
41
+ max_dist = dist
42
+ farthest_points = p
43
+
44
+ return max_dist * 2 # 计算直径(双倍最大垂直距离)
45
+
46
+ # 计算两个区域的最大垂线距离
47
+ diameter1 = max_perpendicular_distance(poly1, center_line)
48
+ diameter2 = max_perpendicular_distance(poly2, center_line)
49
+
50
+ # 计算平均直径 R
51
+ R = (diameter1 + diameter2) / 2
52
+
53
+ # 计算圆心(两个中心点的中点)
54
+ midpoint = ((centroid1[0] + centroid2[0]) / 2, (centroid1[1] + centroid2[1]) / 2)
55
+
56
+ # 生成圆形区域
57
+ circle = Point(midpoint).buffer(R / 2, resolution=100)
58
+
59
+ # 获取圆的坐标
60
+ circle_coords = list(circle.exterior.coords)
61
+
62
+ return [circle_coords], midpoint
63
+
64
+
65
+
66
+
67
+ def get_angular_coordinates(coordinates, angle, distance):
68
+ """
69
+ 计算基于输入地点坐标、方向角度和距离的目标点,并返回以该点为圆心的等面积圆形坐标集。
70
+ :param coordinates: 地点的边界坐标列表
71
+ :param angle: 方向角度(以正北为 0 度,顺时针方向)
72
+ :param distance: 目标点距离(单位:公里)
73
+ :return: 以目标点为圆心,等面积半径的圆形坐标集
74
+ """
75
+ # 计算输入地点的几何中心
76
+ poly = Polygon(coordinates)
77
+ poly_center = poly.centroid
78
+ lat, lon = poly_center.y, poly_center.x # 纬度在前,经度在后
79
+
80
+ # 计算地球上的 1 度经纬度距离(近似值)
81
+ lat_km = 111.32 # 1 度纬度 ≈ 111.32 km
82
+ lon_km = 111.32 * np.cos(np.radians(lat)) # 1 度经度 ≈ 111.32 × cos(纬度)
83
+
84
+ # 计算输入区域的面积(近似 km²,需进一步优化投影转换)
85
+ area_degrees = poly.area # 原始经纬度面积
86
+ area_km2 = area_degrees * (lat_km * lon_km) # 近似转换为 km²
87
+
88
+ # 计算等面积圆的半径 r(单位 km)
89
+ r_km = np.sqrt(area_km2 / np.pi) # 使得 πr² ≈ area_km2
90
+
91
+ # 计算方向向量(基于输入角度)
92
+ angle_rad = np.radians(angle) # 转换为弧度
93
+ d_lat = (np.sin(angle_rad) * distance) / lat_km
94
+ d_lon = (np.cos(angle_rad) * distance) / lon_km
95
+
96
+ # 计算目标点
97
+ target_point = (lon + d_lon, lat + d_lat)
98
+
99
+ # 计算以目标点为圆心,半径 r_km 的圆形坐标集
100
+ circle_points = []
101
+ for theta in np.linspace(0, 360, num=100): # 生成 100 个点构成圆形
102
+ theta_rad = np.radians(theta)
103
+ d_lat = (np.sin(theta_rad) * r_km) / lat_km
104
+ d_lon = (np.cos(theta_rad) * r_km) / lon_km
105
+ circle_points.append((target_point[0] + d_lon, target_point[1] + d_lat))
106
+
107
+ return circle_points
108
+
109
+
110
+
111
+
112
+
geocoder/untitled0.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ # -*- coding: utf-8 -*-
3
+ """
4
+ Created on Wed Sep 21 21:44:47 2022
5
+
6
+ @author: syed
7
+ """
8
+ import sys
9
+
10
+
11
+
12
+
13
+ loc_dict = {"east":"This is east", "west": "This is west", "north":"This is north",
14
+ "south":"This is south","up":"This is up","down":"This is down",
15
+ "look":"This is the description", "quit": "To end the game"}
16
+
17
+ user_input = input("Enter your desired location")
18
+
19
+ def findLocation(input, loc_dict):
20
+ if input == "quit":
21
+ sys.exit(f"End of the game with code{input}!")
22
+ else:
23
+ print(loc_dict[input])
24
+
25
+
26
+