• 逆地理编码-离线版-part3


    part1: 主要为使用示例,以及主逻辑判断

    逆地理编码-离线版-part1_mtj66的博客-CSDN博客


     

    part2:  主要为工具类

    逆地理编码-离线版-part2_mtj66的博客-CSDN博客


     

    本文为逆地理编码离线版的第三部分,前面提供工具类,以及使用逻辑。

    本文主要提供,数据加载,以及geo对象类,方便格式化数据。

    geo_obj.py

    1. # coding=utf-8
    2. from enum import Enum, unique
    3. # from geo import AdminUtils
    4. import AdminUtils
    5. CHINA_NAME = "中国"
    6. CHINA_ID = "CN"
    7. OVERSEA_NAME_VAL = "海外"
    8. UNKNOWN_NAME_VAL = "未知"
    9. UNKNOWN_ID_VAL = -1
    10. UNKNOWN_LOCATION_VAL = None
    11. @unique
    12. class CoordinateSystem(Enum):
    13. #
    14. WGS84 = 0 # GPS
    15. # 坐标系
    16. GCJ02 = 1 # 国测局坐标系(火星坐标系)
    17. BD09 = 2 # 百度坐标系
    18. BJ54 = 3 # 北京54坐标系
    19. XIAN80 = 4 # 西安80坐标系
    20. CGCS2000 = 5 # 2000
    21. # 国家大地坐标系
    22. XYZ = 6 # 笛卡尔坐标系
    23. MERCATOR = 7 # 墨卡托坐标系
    24. @unique
    25. class DistrictLevel(Enum):
    26. # / ** 国家 ** /
    27. Country = "country"
    28. # / ** 省, 自治区 ** /
    29. Province = "province"
    30. # / ** 地级市 ** /
    31. City = "city"
    32. # / ** 区, 县, 县级市 ** /
    33. District = "district"
    34. # / ** 街道 ** /
    35. Street = "street"
    36. @unique
    37. class AdminLevel(Enum):
    38. # / ** 海外 ** /
    39. Oversea = "oversea"
    40. # / ** 国家 ** /
    41. Country = "country"
    42. # / ** 省, 自治区 ** /
    43. Province = "province"
    44. # / ** 地级市 ** /
    45. City = "city"
    46. # / ** 省辖市(属县级市)
    47. # see: https: // baike.baidu.com / item / 省直辖县级行政单位 ** /
    48. ProvincialCity = "provincialcity"
    49. # / ** 区, 县, 县级市 ** /
    50. District = "district"
    51. # / ** 街道 ** /
    52. Street = "street"
    53. class S2LatLng(object):
    54. def __init__(self, ):
    55. super(S2LatLng, self).__init__()
    56. class Location(object):
    57. def __init__(self, lng: float, lat: float):
    58. super().__init__()
    59. self.lng = lng
    60. self.lat = lat
    61. def __str__(self):
    62. return f"Location({self.lng},{self.lat})"
    63. class Admin(object):
    64. def __init__(self, country: str,
    65. province: str,
    66. city: str,
    67. district: str,
    68. town: str,
    69. level: str,
    70. countryCode: str,
    71. provinceCode: int,
    72. cityCode: int,
    73. districtCode: int,
    74. townCode: int,
    75. center: Location = None):
    76. super(Admin, self).__init__()
    77. self.country = country
    78. self.province = province
    79. self.city = city
    80. self.district = district
    81. self.town = town
    82. self.level = level
    83. self.countryCode = countryCode
    84. self.provinceCode = provinceCode
    85. self.cityCode = cityCode
    86. self.districtCode = districtCode
    87. self.townCode = townCode
    88. self.center = center
    89. def __str__(self):
    90. return f"Admin({self.country},{self.province},{self.city},{self.district},{self.town},{self.level}" \
    91. + f",{self.countryCode},{self.provinceCode},{self.cityCode},{self.districtCode},{self.townCode},{self.center})"
    92. def hasCenter(self):
    93. return self.center != Admin.UNKNOWN_LOCATION_VAL
    94. def hasProvince(self):
    95. return self.province != Admin.UNKNOWN_NAME_VAL
    96. def hasCity(self):
    97. return self.city != Admin.UNKNOWN_NAME_VAL
    98. def hasDistrict(self):
    99. return self.district != Admin.UNKNOWN_NAME_VAL
    100. def hasCityId(self):
    101. return self.cityCode != Admin.UNKNOWN_ID_VAL
    102. def hasDistrictId(self):
    103. return self.districtCode != Admin.UNKNOWN_ID_VAL
    104. def hasTown(self):
    105. return self.town != Admin.UNKNOWN_NAME_VAL
    106. def shortProvince(self):
    107. return AdminUtils.shortProvince(self.province)
    108. def shortCity(self):
    109. return AdminUtils.shortCity(self.city)
    110. def toShort(self):
    111. return self.Admin(self.country,
    112. AdminUtils.shortProvince(self.province),
    113. AdminUtils.shortCity(self.city),
    114. AdminUtils.shortDistrict(self.district),
    115. AdminUtils.shortStreet(self.town),
    116. self.level, self.countryCode, self.provinceCode,
    117. self.cityCode, self.districtCode, self.townCode, self.center)
    118. def toNamestr(self):
    119. return f"$country${self.province if (self.hasProvince()) else ''} " \
    120. + f"{self.city if (self.hasCity()) else ''}${self.district if (self.hasDistrict()) else ''}" \
    121. + f"$ {self.town if (self.hasTown()) else ''}"
    122. @staticmethod
    123. def createOversea():
    124. return Admin(OVERSEA_NAME_VAL,
    125. province=OVERSEA_NAME_VAL,
    126. city=OVERSEA_NAME_VAL,
    127. district=OVERSEA_NAME_VAL,
    128. town=OVERSEA_NAME_VAL,
    129. level=AdminLevel.Oversea,
    130. countryCode="",
    131. provinceCode=UNKNOWN_ID_VAL,
    132. cityCode=UNKNOWN_ID_VAL,
    133. districtCode=UNKNOWN_ID_VAL,
    134. townCode=UNKNOWN_ID_VAL,
    135. center=UNKNOWN_LOCATION_VAL
    136. )
    137. @staticmethod
    138. def createCountry(country: str, countryID: str, center: Location):
    139. return Admin(
    140. country,
    141. province=UNKNOWN_NAME_VAL,
    142. city=UNKNOWN_NAME_VAL,
    143. district=UNKNOWN_NAME_VAL,
    144. town=UNKNOWN_NAME_VAL,
    145. level=AdminLevel.Country,
    146. countryCode=countryID,
    147. provinceCode=UNKNOWN_ID_VAL,
    148. cityCode=UNKNOWN_ID_VAL,
    149. districtCode=UNKNOWN_ID_VAL,
    150. townCode=UNKNOWN_ID_VAL,
    151. center=center
    152. )
    153. @staticmethod
    154. def createProvince(province: str, provinceId: int, center: Location):
    155. return Admin(
    156. country=CHINA_NAME,
    157. province=province,
    158. city=UNKNOWN_NAME_VAL,
    159. district=UNKNOWN_NAME_VAL,
    160. town=UNKNOWN_NAME_VAL,
    161. level=AdminLevel.Province,
    162. countryCode=CHINA_ID,
    163. provinceCode=provinceId,
    164. cityCode=UNKNOWN_ID_VAL,
    165. districtCode=UNKNOWN_ID_VAL,
    166. townCode=UNKNOWN_ID_VAL,
    167. center=center
    168. )
    169. @staticmethod
    170. def createCity(province: str, city: str, provinceId: int, cityId: int, center: Location):
    171. return Admin(
    172. country=CHINA_NAME,
    173. province=province,
    174. city=city,
    175. district=UNKNOWN_NAME_VAL,
    176. town=UNKNOWN_NAME_VAL,
    177. level=AdminLevel.City,
    178. countryCode=CHINA_ID,
    179. provinceCode=provinceId,
    180. cityCode=cityId,
    181. districtCode=UNKNOWN_ID_VAL,
    182. townCode=UNKNOWN_ID_VAL,
    183. center=center
    184. )
    185. @staticmethod
    186. def createProvincialCity(province: str, city: str, provinceId: int, cityId: int, center: Location):
    187. return Admin(
    188. country=CHINA_NAME,
    189. province=province,
    190. city=city,
    191. district=city,
    192. town=UNKNOWN_NAME_VAL,
    193. level=AdminLevel.ProvincialCity,
    194. countryCode=CHINA_ID,
    195. provinceCode=provinceId,
    196. cityCode=cityId,
    197. districtCode=cityId,
    198. townCode=UNKNOWN_ID_VAL,
    199. center=center
    200. )
    201. @staticmethod
    202. def createDistrict(province: str, city: str, district: str,
    203. provinceId: int, cityId: int, districtId: int, center: Location):
    204. return Admin(
    205. country=CHINA_NAME,
    206. province=province,
    207. city=city,
    208. district=district,
    209. town=UNKNOWN_NAME_VAL,
    210. level=AdminLevel.District,
    211. countryCode=CHINA_ID,
    212. provinceCode=provinceId,
    213. cityCode=cityId,
    214. districtCode=districtId,
    215. townCode=UNKNOWN_ID_VAL,
    216. center=center
    217. )
    218. @staticmethod
    219. def createStreet(province: str, city: str, district: str, town: str,
    220. provinceId: int, cityId: int, districtId: int, streetId: int, center: Location):
    221. return Admin(
    222. country=CHINA_NAME,
    223. province=province,
    224. city=city,
    225. district=district,
    226. town=town,
    227. level=AdminLevel.Street,
    228. countryCode=CHINA_ID,
    229. provinceCode=provinceId,
    230. cityCode=cityId,
    231. districtCode=districtId,
    232. townCode=streetId,
    233. center=center
    234. )
    235. class AdminNode(object):
    236. def __init__(self, id: int,
    237. name: str,
    238. shortName: str,
    239. center: Location,
    240. level: DistrictLevel,
    241. parentId: int,
    242. children: list):
    243. super().__init__()
    244. self.id = id
    245. self.name = name
    246. self.shortName = shortName
    247. self.center = center
    248. self.parentId = parentId
    249. self.level = level
    250. self.children = children
    251. def __str__(self):
    252. """
    253. :return: AdminNode(430000,湖南省,湖南,Location(112.9836,28.112743),province,100000,List(430700, 431000, 430400, 431300, 430500, 433100, 430300, 431100, 430900, 430800, 430200, 430600, 430100, 431200))
    254. """
    255. return f"AdminNode({self.id},{self.name},{self.shortName},{self.center},{self.level},{self.children})"
    256. class BusinessAreaData(object):
    257. def __init__(self, name: str, center: Location, areaCode: int):
    258. super().__init__()
    259. self.name = name
    260. self.center = center
    261. self.areaCode = areaCode
    262. class BusinessAreaGroup(object):
    263. # list(BusinessAreaData)
    264. def __init__(self, cityAdCode: int, areas):
    265. super().__init__()
    266. self.cityAdCode = cityAdCode
    267. self.areas = areas
    268. class BusinessArea(object):
    269. def __init__(self,
    270. name: str, areaCode: int, distance: int):
    271. super().__init__()
    272. self.name = name
    273. self.areaCode = areaCode
    274. self.distance = distance
    275. class BusinessAreaInfo(object):
    276. def __init__(self, admin: Admin, areas): # : list(BusinessArea)
    277. super().__init__()
    278. self.admin = admin
    279. self.areas = areas
    280. # case class BusinessAreaData(name: str, center: Location, areaCode: int) extends Serializable
    281. #
    282. # @SerialVersionUID(-5899680396800964972L)
    283. # case class BusinessAreaGroup(cityAdCode: int, areas: Array[BusinessAreaData]) extends Serializable
    284. #
    285. # case class BusinessArea(name: str, areaCode: int, distance:int)
    286. #
    287. # case class BusinessAreaInfo(admin: Admin, areas: Seq[BusinessArea])
    288. if __name__ == '__main__':
    289. print(CoordinateSystem.GCJ02 ==CoordinateSystem.GCJ02)

    data_loader.py

    1. # -*- coding: utf-8 -*-
    2. """
    3. @Time : 2022/6/30 13:49
    4. @Author: Breeze
    5. @File : test.py
    6. """
    7. from geo_obj import *
    8. import pickle
    9. base_dir = "base_dir"
    10. # 请联系WX SpringBreeze1104 付费获取geo数据
    11. import os
    12. boundaryAdminCell = {}
    13. with open(os.path.join(base_dir, "boundaryAdminCell.txt")) as f:
    14. for line in f.readlines():
    15. idx, key, col1 = line.strip().split(",")
    16. if idx != 'idx':
    17. boundaryAdminCell[int(key)] = int(col1)
    18. # '3689199061258207232' -1
    19. boundaryIndex = {} # Map[Long, List[Long]]
    20. if os.path.exists(os.path.join(base_dir, "boundaryIndex.pkl")):
    21. with open(os.path.join(base_dir, "boundaryIndex.pkl"), 'rb') as f:
    22. # boundaryIndex = pickle.loads(f.read())
    23. boundaryIndex = pickle.load(f)
    24. else:
    25. with open(os.path.join(base_dir, "boundaryIndex.txt")) as f:
    26. for line in f.readlines():
    27. idx, key, col1 = line.strip().split(",")
    28. if idx != 'idx':
    29. key = int(key)
    30. if boundaryIndex.get(key) is None:
    31. boundaryIndex[key] = [int(e) for e in col1.split("|")]
    32. else:
    33. data = boundaryIndex[key]
    34. data.extend([int(e) for e in col1.split("|")])
    35. boundaryIndex[key] = data
    36. with open(os.path.join(base_dir, "boundaryIndex.pkl"), 'wb') as f:
    37. pickle.dump(boundaryIndex, f)
    38. # Map[Long, List[(Long, Int, Int)]]
    39. boundaryData = {}
    40. if os.path.exists(os.path.join(base_dir, "boundaryData.pkl")):
    41. with open(os.path.join(base_dir, "boundaryData.pkl"), 'rb') as f:
    42. # boundaryAdminCell = pickle.loads(f.read())
    43. boundaryData = pickle.load(f)
    44. else:
    45. with open(os.path.join(base_dir, "boundaryData.txt")) as f:
    46. for line in f.readlines():
    47. idx, key, col1, col2, col3 = line.strip().split(",")
    48. if idx != 'idx':
    49. data = boundaryData.get(int(key))
    50. if data is not None:
    51. data.append((int(col1), int(col2), int(col3)))
    52. boundaryData[int(key)] = data
    53. else:
    54. boundaryData[int(key)] = [(int(col1), int(col2), int(col3))]
    55. with open(os.path.join(base_dir, "boundaryData.pkl"), 'wb') as f:
    56. pickle.dump(boundaryData, f)
    57. # Map[Int, AdminNode]
    58. adminData = {}
    59. with open(os.path.join(base_dir, "adminData.txt"), encoding='utf-8') as f:
    60. for line in f.readlines():
    61. idx, key, name, shortName, center_lng, center_lat, level, parentId, children = line.strip().split(",")
    62. if idx != 'idx':
    63. children = [int(c) for c in children.split("|") if len(c)>0]
    64. center = Location(float(center_lng), float(center_lat))
    65. adminData[int(key)] = AdminNode(int(key), name, shortName, center, level, int(parentId), children)
    66. # Map[Int, AdminNode]
    67. streetData = {}
    68. with open(os.path.join(base_dir, "streetData.txt"), encoding='utf-8') as f:
    69. for line in f.readlines():
    70. idx, id, name, shortName, center_lng, center_lat, level, parentId, children = line.strip().split(",")
    71. if idx != 'idx':
    72. children = [int(c) for c in children.split("|") if len(c)>0]
    73. center = Location(float(center_lng), float(center_lat))
    74. streetData[int(id)] = AdminNode(int(id), name, shortName, center, level, int(parentId), children)
    75. cityBusinessArea = {} # Map[Int, Array[BusinessAreaData]]
    76. with open(os.path.join(base_dir, "cityBusinessArea.txt"), encoding='utf-8') as f:
    77. for line in f.readlines():
    78. idx,city_code, bussiness, center_lng,center_lat,area_code = line.strip().split(",")
    79. if idx != 'idx':
    80. center = Location(float(center_lng), float(center_lat))
    81. # name: str, center: Location, areaCode: int
    82. businessAreaDatas = cityBusinessArea.get(int(city_code))
    83. if businessAreaDatas is not None:
    84. businessAreaDatas.append(BusinessAreaData(bussiness,center,area_code))
    85. cityBusinessArea[int(city_code)] = businessAreaDatas
    86. else:
    87. cityBusinessArea[int(city_code)] = [BusinessAreaData(bussiness,center,area_code)]
    88. cityLevelData = {}
    89. with open(os.path.join(base_dir, "citylevels.txt"), encoding='utf-8') as f:
    90. for line in f.readlines():
    91. cityname,city_code,level = line.strip().split(",")
    92. cityLevelData[cityname] = level
    93. cityLevelData[city_code] = level
    94. if __name__ == '__main__':
    95. print('init end')

  • 相关阅读:
    儿童帐篷出口美国CPC认证测试项目流程介绍
    Word | 简单可操作的快捷公式编号、右对齐和引用方法
    各种信息论坛
    消息积压了如何处理?
    mysql优化之show profile的使用及分析
    防火墙命令补充和dmz_远程管理
    基于改进粒子群优化算法的柔性车间调度问题(Python代码实现)
    【关系抽取】基于Bert的信息抽取模型CasRel
    聚观早报 | 理想L6正式发布;Meta发布Llama 3
    【JavaEE基础与高级 第56章】Java中的打印流、属性集、IO流异常的处理详细使用介绍
  • 原文地址:https://blog.csdn.net/mtj66/article/details/125556478