table_info.py
2.6 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
#author: 4N
#createtime: 2021/1/27
#email: nheweijun@sina.com
from ..models import Table,Columns,DES
from app.util.component.ApiTemplate import ApiTemplate
from app.util.component.ModelVisitor import ModelVisitor
from app.util.component.PGUtil import PGUtil
from osgeo.ogr import DataSource,Layer,Feature
from osgeo.osr import SpatialReference
class Api(ApiTemplate):
api_name = "表信息"
def process(self):
res = {}
pg_ds = None
try:
table_guid = self.para.get("guid")
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("数据异常!")
append_dict ={"srid":None,"sr_wkt":None,"sr_proj4":None,"exist":1,"schema":"public","geomfield":layer.GetGeometryColumn()}
if layer:
sr:SpatialReference = layer.GetSpatialRef()
if sr is not None:
append_dict["sr_wkt"] = sr.ExportToWkt()
append_dict["sr_proj4"] = sr.ExportToProj4()
srid_sql = '''select st_srid({}) from public."{}" limit 1'''.format(layer.GetGeometryColumn(),layer.GetName())
srid_layer:Layer = pg_ds.ExecuteSQL(srid_sql)
if srid_layer:
srid_feature : Feature = srid_layer.GetNextFeature()
if srid_feature:
if srid_feature.GetField(0):
append_dict["srid"] = str(srid_feature.GetField(0))
else:
append_dict["exist"]=0
columns = table.relate_columns.order_by(Columns.name).all()
res["data"]=ModelVisitor.table_to_json(table)
res["data"]["columns"] = ModelVisitor.objects_to_jsonarray(columns)
res["data"].update(append_dict)
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":{
}
}
}
}
}