DataGovernance.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # -*- coding: utf-8 -*-
  2. from config import settings
  3. from common.BigDataCenterAPI import *
  4. from utils import *
  5. import pymysql
  6. def jwd_to_area(location):
  7. url = 'https://19.15.75.180:8581/GatewayMsg/http/api/proxy/invoke'
  8. service_code = 'YZT1685418808667'
  9. passid = 'C90-44004428'
  10. passtoken = 'f539f616b2834160bc47fda5f298512c'
  11. signTime = str(GetTime() // 1000)
  12. nonce = GetNonce(5)
  13. sign = GetSign(signTime, nonce, passtoken)
  14. headers = {
  15. 'Content-Type': 'application/json',
  16. 'x-tif-signature': sign,
  17. 'x-tif-timestamp': signTime,
  18. 'x-tif-nonce': nonce,
  19. 'x-tif-paasid': passid,
  20. 'x-tif-serviceId': service_code
  21. }
  22. response = requests.post(url=url, headers=headers, json=location, verify=False)
  23. if response.status_code == 200:
  24. return response.json()['data'][0]['district']
  25. def put_area(table : str,longitude='longitude',latitude='latitude'):
  26. conn=None
  27. cur = None
  28. try:
  29. conn = pymysql.connect(host=settings.mysql_dwd_config['host'],
  30. user=settings.mysql_dwd_config['username'],
  31. password=settings.mysql_dwd_config['password'],
  32. database=settings.mysql_dwd_config['database'],
  33. port=settings.mysql_dwd_config['port'],
  34. charset='utf8mb4')
  35. cur = conn.cursor()
  36. sql = f'select id,{longitude},{latitude} from {table}'
  37. cur.execute(sql)
  38. resu = cur.fetchall()
  39. for info in resu:
  40. id_1 = info[0]
  41. location = [{"x":info[1],"y":info[2]}]
  42. area = jwd_to_area(location)
  43. update_sql = f"UPDATE {table} SET area = %s WHERE id = %s"
  44. cur.execute(update_sql, (area, id_1))
  45. conn.commit() # 提交事务
  46. except pymysql.MySQLError as e:
  47. print(f"Error: {e}")
  48. finally:
  49. if cur:
  50. cur.close()
  51. if conn:
  52. conn.close()
  53. def put_uuid(table,column):
  54. conn = None
  55. cur = None
  56. try:
  57. conn = pymysql.connect(host=settings.mysql_dwd_config['host'],
  58. user=settings.mysql_dwd_config['username'],
  59. password=settings.mysql_dwd_config['password'],
  60. database=settings.mysql_dwd_config['database'],
  61. port=settings.mysql_dwd_config['port'],
  62. charset='utf8mb4')
  63. cur = conn.cursor()
  64. sql = f'select distinct {column} from {table}'
  65. cur.execute(sql)
  66. resu = cur.fetchall()
  67. for info in resu:
  68. column_data = info[0]
  69. uid = new_guid()
  70. update_sql = f"UPDATE {table} SET uid = %s WHERE {column} = %s"
  71. cur.execute(update_sql, (uid,column_data))
  72. conn.commit() # 提交事务
  73. except pymysql.MySQLError as e:
  74. print(f"Error: {e}")
  75. finally:
  76. if cur:
  77. cur.close()
  78. if conn:
  79. conn.close()
  80. put_uuid('rescue_materia','management_unit')
  81. #put_area('emergency_expert')
  82. # put_area('mid_waterlogged_roads','lng','lat')
  83. # put_area('mid_school')
  84. # put_area('mid_hospital')