|
@@ -24,9 +24,12 @@ def jwd_to_area(location):
|
|
response = requests.post(url=url, headers=headers, json=location, verify=False)
|
|
response = requests.post(url=url, headers=headers, json=location, verify=False)
|
|
if response.status_code == 200:
|
|
if response.status_code == 200:
|
|
try:
|
|
try:
|
|
- return response.json()['data'][0]['district']
|
|
|
|
|
|
+ data = response.json()['data'][0]
|
|
|
|
+ district = data['district']
|
|
|
|
+ township = data['township']
|
|
|
|
+ return district,township
|
|
except:
|
|
except:
|
|
- return None
|
|
|
|
|
|
+ return None,None
|
|
def put_area(table : str,longitude='longitude',latitude='latitude'):
|
|
def put_area(table : str,longitude='longitude',latitude='latitude'):
|
|
conn=None
|
|
conn=None
|
|
cur = None
|
|
cur = None
|
|
@@ -44,7 +47,7 @@ def put_area(table : str,longitude='longitude',latitude='latitude'):
|
|
for info in resu:
|
|
for info in resu:
|
|
id_1 = info[0]
|
|
id_1 = info[0]
|
|
location = [{"x":info[1],"y":info[2]}]
|
|
location = [{"x":info[1],"y":info[2]}]
|
|
- area = jwd_to_area(location)
|
|
|
|
|
|
+ area,township = jwd_to_area(location)
|
|
update_sql = f"UPDATE {table} SET area = %s WHERE id = %s"
|
|
update_sql = f"UPDATE {table} SET area = %s WHERE id = %s"
|
|
cur.execute(update_sql, (area, id_1))
|
|
cur.execute(update_sql, (area, id_1))
|
|
conn.commit() # 提交事务
|
|
conn.commit() # 提交事务
|
|
@@ -56,6 +59,34 @@ def put_area(table : str,longitude='longitude',latitude='latitude'):
|
|
if conn:
|
|
if conn:
|
|
conn.close()
|
|
conn.close()
|
|
|
|
|
|
|
|
+def put_township(table : str,longitude='longitude',latitude='latitude'):
|
|
|
|
+ conn=None
|
|
|
|
+ cur = None
|
|
|
|
+ try:
|
|
|
|
+ conn = pymysql.connect(host=settings.MYSQL_SERVER,
|
|
|
|
+ user=settings.MYSQL_USER,
|
|
|
|
+ password=settings.MYSQL_PASSWORD,
|
|
|
|
+ database=settings.MYSQL_DB_NAME,
|
|
|
|
+ port=settings.MYSQL_PORT,
|
|
|
|
+ charset='utf8mb4')
|
|
|
|
+ cur = conn.cursor()
|
|
|
|
+ sql = f'select id,{longitude},{latitude} from {table}'
|
|
|
|
+ cur.execute(sql)
|
|
|
|
+ resu = cur.fetchall()
|
|
|
|
+ for info in resu:
|
|
|
|
+ id_1 = info[0]
|
|
|
|
+ location = [{"x":info[1],"y":info[2]}]
|
|
|
|
+ area,township = jwd_to_area(location)
|
|
|
|
+ update_sql = f"UPDATE {table} SET township = %s WHERE id = %s"
|
|
|
|
+ cur.execute(update_sql, (township, id_1))
|
|
|
|
+ conn.commit() # 提交事务
|
|
|
|
+ except pymysql.MySQLError as e:
|
|
|
|
+ print(f"Error: {e}")
|
|
|
|
+ finally:
|
|
|
|
+ if cur:
|
|
|
|
+ cur.close()
|
|
|
|
+ if conn:
|
|
|
|
+ conn.close()
|
|
def put_uuid(table,column):
|
|
def put_uuid(table,column):
|
|
conn = None
|
|
conn = None
|
|
cur = None
|
|
cur = None
|
|
@@ -92,5 +123,6 @@ def put_uuid(table,column):
|
|
# put_area('mid_school')
|
|
# put_area('mid_school')
|
|
# put_area('mid_hospital')
|
|
# put_area('mid_hospital')
|
|
# put_area('tower_list')
|
|
# put_area('tower_list')
|
|
-put_area('govdata_real_time_address')
|
|
|
|
|
|
+# put_area('govdata_real_time_address')
|
|
|
|
+put_township('govdata_real_time_address')
|
|
|
|
|