table_vacuate_ref.py
5.1 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
# author: 4N
# createtime: 2021/1/27
# email: nheweijun@sina.com
from ..models import Table, DES
from app.util.component.ApiTemplate import ApiTemplate
from app.util.component.PGUtil import PGUtil
from app.util.component.GeometryAdapter import GeometryAdapter
from app.util.component.StructurePrint import StructurePrint
from osgeo.ogr import DataSource,Layer
from osgeo.osr import SpatialReference
from app.util.component.VacuateConf import VacuateConf
import copy
import json
class Api(ApiTemplate):
api_name = "推荐抽稀"
def process(self):
res = {}
res["data"] = {}
pg_ds = None
try:
table_guid = self.para.get("guid")
table: Table = Table.query.filter_by(guid=table_guid).one_or_none()
if not table:
raise Exception("数据不存在!")
# 判断图层是否存在
pg_ds :DataSource= PGUtil.open_pg_data_source(0,DES.decode(table.relate_database.sqlalchemy_uri))
layer:Layer = pg_ds.GetLayerByName(table.name)
# 判断用户权限
if not layer:
raise Exception("图层不存在!")
lonlat_gridsize = copy.deepcopy(VacuateConf.lonlat_gridsize)
project_gridsize = copy.deepcopy(VacuateConf.project_gridsize)
least_vacuate_count = copy.deepcopy(VacuateConf.least_vacuate_count)
this_gridsize = []
max_level = 0
# 是空间图层才初始化
if layer.GetExtent()[0] > 0 or layer.GetExtent()[0] < 0:
# 判断需要抽稀多少级
# lc = layer.GetFeatureCount()
lc = table.feature_count
# extent = layer.GetExtent()
extent = [float(x) for x in table.extent.split(",")]
# 判断疏密程度
p_x = (extent[1] - extent[0]) / 5.0
p_y = (extent[3] - extent[2]) / 5.0
fill_precent = 0
StructurePrint().print("判断疏密")
for ix in range(5):
for iy in range(5):
grid_extent = [extent[0] + ix * p_x, extent[0] + ix * p_x + p_x, extent[2] + iy * p_y,
extent[2] + iy * p_y + p_y]
poly = GeometryAdapter.envelop_2_polygon(grid_extent)
sql = '''select * from "{}" where st_intersects({},st_setsrid(st_geometryfromtext('{}'),st_srid({}))) limit 1'''.format(layer.GetName(),
layer.GetGeometryColumn(),poly.ExportToWkt(),layer.GetGeometryColumn())
query_layer: Layer = pg_ds.ExecuteSQL(sql)
if query_layer.GetFeatureCount() == 1:
fill_precent += 4
# layer.SetSpatialFilter(None)
# layer.SetSpatialFilter(poly)
# layer.ResetReading()
# if layer.GetNextFeature():
# fill_precent += 4
StructurePrint().print(fill_precent)
StructurePrint().print("判断疏密结束")
layer.SetSpatialFilter(None)
layer.ResetReading()
# 固有疏密程度
original_density = 8
if extent[0] > 180:
t_grid_size = project_gridsize
else:
t_grid_size = lonlat_gridsize
for grid_size in t_grid_size:
# 最少抽稀个数
if lc > least_vacuate_count:
# 网格数至少大于
if ((extent[1] - extent[0]) * (extent[3] - extent[2])) / (
grid_size ** 2) > least_vacuate_count:
# 要素数量大于网格数量
# 要考虑图层的疏密程度,original_density*(100.0/fill_precent) 为疏密指数
if lc * original_density * (100.0 / fill_precent) > (
(extent[1] - extent[0]) * (extent[3] - extent[2])) / (grid_size ** 2):
this_gridsize.append(grid_size)
max_level += 1
res["data"] = this_gridsize
res["result"] = True
except Exception as e:
raise e
finally:
if pg_ds:
pg_ds.Destroy()
return res
api_doc = {
"tags": ["管理接口"],
"parameters": [
{"name": "guid",
"in": "formData",
"type": "string",
"description": "表guid", "required": "true"}
],
"responses": {
200: {
"schema": {
"properties": {
}
}
}
}
}