-
Notifications
You must be signed in to change notification settings - Fork 14
/
amapMaxMin.py
68 lines (58 loc) · 2.08 KB
/
amapMaxMin.py
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
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
"""
@File: amapMaxMin.py
@Project: amap
@Desc: 第三步:计算出province, city, district表中的最大最小边界经纬度,并填充到数据库中
@Time: 2020/04/30 15:13:03
@Author: wuwenrufeng ([email protected])
@Last Modified: 2020/04/30 15:13:06
@Modified By: wuwenrufeng ([email protected])
@Version: 1.0
@License: Copyright(C) 2019 - 2020 Borland
"""
from pony.orm import commit, db_session, select, delete
from util.models import Province, City, District, db
class MaxMin:
def trimPolyline(self, model):
"""
将由多个区域组成的行政区分离出来
model: 行政区Entity
"""
for row in model.select():
polylineRaw = row.polyline
if '|' in polylineRaw:
polylines = polylineRaw.split('|')
for polyline in polylines:
model(name=row.name, adcode=row.adcode, center=row.center, polyline=polyline)
delete(m for m in model if m.id == row.id)
def calculate(self, polyline):
"""
计算当前边界坐标中的最大坐标点和最小坐标点
polyline: 边界坐标
return: 最大坐标点,最小坐标点
"""
# ['116.58289,39.623118';'116.58289,39.623118';...]
polylines = polyline.split(';')
lats=lons = []
for polyline in polylines:
lat,lon = polyline.split(',')
lats.append(float(lat))
lons.append(float(lon))
maxPoint = str(max(lats)) + ',' + str(max(lons))
minPoint = str(min(lats)) + ',' + str(min(lons))
return maxPoint, minPoint
@db_session
def main(self, model):
"""
model: 行政区Entity
"""
self.trimPolyline(model)
for row in model.select():
max_point, min_point = self.calculate(row.polyline)
model[row.id].set(max_point=max_point, min_point=min_point)
commit()
if __name__ == '__main__':
maxMin = MaxMin()
for model in (Province, City, District):
maxMin.main(model)