-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapi_fast.py
More file actions
307 lines (240 loc) · 9.42 KB
/
api_fast.py
File metadata and controls
307 lines (240 loc) · 9.42 KB
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
# -*- coding: utf-8 -*-
"""
This module provides a FastAPI-based HTTP server for geocoding and reverse geocoding operations.
__author__ = "gisman@gmail.com"
Usage:
Endpoints:
- GET /: Returns a welcome HTML page with links to API documentation, source code, and an online demo.
- GET /geocode: Geocodes a single address or multiple addresses provided as a newline-separated string.
- GET /reverse_geocode: Reverse geocodes a given x and y coordinate.
- POST /batch_geocode: Geocodes multiple addresses provided in a JSON payload.
Classes:
- Batch_Geocode_Item: Pydantic model for batch geocoding input.
- ApiHandler: Handles geocoding and reverse geocoding operations using RocksDB.
Attributes:
APP_NAME (str): Name of the application.
VERSION (float): Version of the application.
DEFAULT_PORT (int): Default port for the server.
LINES_LIMIT (int): Maximum number of lines for batch geocoding.
X_AXIS (str): Key for x-axis coordinate in the response.
Y_AXIS (str): Key for y-axis coordinate in the response.
Functions:
- read_root: Returns a static HTML page with links to API documentation, source code, and an online demo.
- geocode: Geocodes a single address or multiple addresses.
- reverse_geocode: Reverse geocodes a given x and y coordinate.
- batch_geocode: Geocodes multiple addresses provided in a JSON payload.
Main Execution:
- Configures logging.
- Runs the FastAPI server using Uvicorn.
Geocode API HTTP Server
Usage:
./api_fast.py [<port>]
"""
import time
import logging
from pyproj import Transformer, CRS
from fastapi import APIRouter
from fastapi import FastAPI, HTTPException
from fastapi.responses import JSONResponse, HTMLResponse
from pydantic import BaseModel
from geocoder_kr.geocoder import Geocoder
from geocoder_kr.reverse_geocoder import ReverseGeocoder
import sys
APP_NAME = "Geocode API Server"
VERSION = 0.2
DEFAULT_PORT = 4002
LINES_LIMIT = 3000
X_AXIS = "x_axis"
Y_AXIS = "y_axis"
app = FastAPI()
router = APIRouter()
@app.get("/")
async def read_root():
"""
Asynchronous function to handle the root endpoint of the Geocode API Server.
Returns:
HTMLResponse: A static HTML response containing a welcome message,
a link to the API documentation, a link to the source code on GitHub,
and a link to an online demo.
"""
return HTMLResponse(
content="""
<!DOCTYPE html>
<html>
<head>
<title>Geocode API Server</title>
</head>
<body>
<h1>Welcome to the Geocode API Server</h1>
<p>For API documentation, visit <a href="/docs">/docs</a></p>
<p>Source Code: <a href="https://github.com/gisman/geocoder-kr">Github</a></p>
<p>Online Demo: <a href="https://geocode.gimi9.com/">geocode.gimi9.com</a></p>
</body>
</html>
"""
)
@router.get("/geocode")
async def geocode(q: str):
"""
Geocodes a given query string.
Args:
q (str): A string containing addresses separated by newline characters.
Returns:
JSONResponse: A JSON response containing the geocoded addresses.
"""
addrs = q.split("\n")
summary = ApiHandler().geocode(addrs)
return JSONResponse(content=summary)
@router.get("/reverse_geocode")
async def reverse_geocode(x: float, y: float):
"""
Asynchronously performs reverse geocoding for the given coordinates.
Args:
x (float): The longitude of the location to reverse geocode.
y (float): The latitude of the location to reverse geocode.
Returns:
JSONResponse: A JSON response containing the reverse geocoding result.
"""
val = ApiHandler().reverse_geocode(x, y)
return JSONResponse(content=val)
class Batch_Geocode_Item(BaseModel):
"""
Batch_Geocode_Item is a Pydantic model representing a batch geocode request item.
Attributes:
q (list[str]): A list of query strings to be geocoded.
"""
q: list[str]
@router.post("/batch_geocode")
async def batch_geocode(data: Batch_Geocode_Item):
"""
Geocode multiple addresses asynchronously.
Args:
data (Batch_Geocode_Item): An object containing a list of addresses to be geocoded.
Raises:
HTTPException: If the input data is invalid (i.e., the list of addresses is empty).
Returns:
JSONResponse: A JSON response containing the geocoding summary for the provided addresses.
"""
# multi addresses geocode
addrs = data.q
if not addrs:
raise HTTPException(status_code=400, detail="Invalid input")
summary = ApiHandler().geocode(addrs)
return JSONResponse(content=summary)
# app에 router를 추가
app.include_router(router)
class ApiHandler:
"""
ApiHandler class provides methods for geocoding and reverse geocoding using RocksDB.
Attributes:
geocoder (Geocoder): An instance of Geocoder initialized with RocksDbGeocode.
reverse_geocoder (ReverseGeocoder): An instance of ReverseGeocoder initialized with RocksDB.
tf (Transformer): A Transformer object for converting coordinates between EPSG:5179 and EPSG:4326.
Methods:
read_key(key):
Reads a value from the geocoder database using the provided key.
reverse_geocode(x: float, y: float):
Performs reverse geocoding for the given coordinates (x, y).
geocode(addrs):
Performs geocoding for a list of addresses and returns a summary of the results.
"""
# rocks db
geocoder = Geocoder()
# geocoder = Geocoder(RocksDbGeocode("db/rocks", create_if_missing=True))
reverse_geocoder = ReverseGeocoder()
# reverse_geocoder = ReverseGeocoder(
# rocksdb3.open_default(
# "db/rocks-reverse-geocoder",
# )
# )
tf = Transformer.from_crs(
CRS.from_string("EPSG:5179"), CRS.from_string("EPSG:4326"), always_xy=True
)
def read_key(self, key):
val = ApiHandler.geocoder.db.get(key)
return val
def reverse_geocode(self, x: float, y: float):
"""
Perform reverse geocoding to find the address corresponding to the given coordinates.
Args:
x (float): The longitude of the location to reverse geocode.
y (float): The latitude of the location to reverse geocode.
Returns:
dict: The address information corresponding to the given coordinates.
Logs:
Execution time for the reverse geocoding process.
"""
start_time = time.time() # 시작 시간 측정
val = self.reverse_geocoder.search(x, y)
elapsed_time = time.time() - start_time # 소요 시간 계산
# 소요 시간을 로그에 기록
logging.debug(
f"Execution time for reverse_geocode({x}, {y}): {elapsed_time:.6f} seconds"
)
return val
def geocode(self, addrs):
"""
Geocode a list of addresses.
Args:
addrs (list): A list of addresses to geocode.
Returns:
dict: A summary dictionary containing:
- total_time (float): The total time taken to geocode the addresses.
- total_count (int): The total number of addresses processed.
- success_count (int): The number of successfully geocoded addresses.
- fail_count (int): The number of addresses that failed to geocode.
- results (list): A list of dictionaries containing the geocoding results for each address.
Each dictionary includes:
- inputaddr (str): The input address.
- success (bool): Whether the geocoding was successful.
- x (float, optional): The x-coordinate of the geocoded address.
- y (float, optional): The y-coordinate of the geocoded address.
- X_AXIS (float, optional): The transformed x-coordinate.
- Y_AXIS (float, optional): The transformed y-coordinate.
"""
summary = {}
result = []
count = 0
success_count = 0
fail_count = 0
start_time = time.time()
for addr in addrs[:LINES_LIMIT]:
count += 1
val = ApiHandler.geocoder.search(addr)
if not val:
val = {}
fail_count += 1
elif val["success"]:
if val["x"]:
x1, y1 = ApiHandler.tf.transform(val["x"], val["y"])
val[X_AXIS] = x1
val[Y_AXIS] = y1
success_count += 1
else:
fail_count += 1
val["inputaddr"] = addr
result.append(val)
summary["total_time"] = time.time() - start_time
summary["total_count"] = count
summary["success_count"] = success_count
fail_count = count - success_count
summary["fail_count"] = fail_count
summary["results"] = result
return summary
# if __name__ == "__main__":
# LOG_PATH = f"{os.getcwd()}/log/geocode-api.log"
# print(f"logging to {LOG_PATH}")
# # 로그 설정
# logging.basicConfig(
# filename=LOG_PATH,
# encoding="utf-8",
# format="%(asctime)s - %(levelname)s - %(message)s",
# level=logging.INFO,
# force=True,
# )
# if len(sys.argv) > 1:
# port = int(sys.argv[1])
# else:
# port = DEFAULT_PORT
# uvicorn.run(app, host="0.0.0.0", port=port)
# logging.info(f"Stopping {APP_NAME}...\n")