提交 a7ad48548b6ba59dc8a0dd7bd10b8d9401d0c8bb

作者 qianyingz
2 个父辈 de97ab29 1699ef5c
@@ -94,17 +94,21 @@ class Api(ApiTemplate): @@ -94,17 +94,21 @@ class Api(ApiTemplate):
94 this_time = datetime.datetime.now() 94 this_time = datetime.datetime.now()
95 95
96 pg_ds: DataSource = PGUtil.open_pg_data_source(1, DES.decode(database.sqlalchemy_uri)) 96 pg_ds: DataSource = PGUtil.open_pg_data_source(1, DES.decode(database.sqlalchemy_uri))
  97 + db_session: Session = PGUtil.get_db_session(DES.decode(database.sqlalchemy_uri))
  98 +
97 db_tuple = PGUtil.get_info_from_sqlachemy_uri(DES.decode(database.sqlalchemy_uri)) 99 db_tuple = PGUtil.get_info_from_sqlachemy_uri(DES.decode(database.sqlalchemy_uri))
98 100
99 # 注册空间表 101 # 注册空间表
100 - spatial_table_name,tables = self.register_spatial_table(pg_ds, database, this_time,db_tuple) 102 + spatial_table_name,tables = self.register_spatial_table(pg_ds,db_session, database, this_time,db_tuple)
101 103
102 #注册普通表 104 #注册普通表
103 - self.register_common_table(this_time,database,spatial_table_name,db_tuple) 105 + self.register_common_table(db_session,this_time,database,spatial_table_name,db_tuple)
  106 +
  107 + db_session.close()
104 pg_ds.Destroy() 108 pg_ds.Destroy()
105 109
106 110
107 - def register_spatial_table(self,pg_ds,database,this_time,db_tuple): 111 + def register_spatial_table(self,pg_ds,db_session,database,this_time,db_tuple):
108 spatial_table_name =[] 112 spatial_table_name =[]
109 tables=[] 113 tables=[]
110 for i in range(pg_ds.GetLayerCount()): 114 for i in range(pg_ds.GetLayerCount()):
@@ -122,24 +126,46 @@ class Api(ApiTemplate): @@ -122,24 +126,46 @@ class Api(ApiTemplate):
122 continue 126 continue
123 127
124 # 没有权限的表跳过 128 # 没有权限的表跳过
125 - if not PGUtil.check_table_privilege(l_name,"SELECT",db_tuple[0],pg_ds):  
126 - StructurePrint().print("用户{}对表{}没有select权限!".format(db_tuple[0],l_name),"warn") 129 + # if not PGUtil.check_table_privilege(l_name,"SELECT",db_tuple[0],pg_ds):
  130 + # StructurePrint().print("用户{}对表{}没有select权限!".format(db_tuple[0],l_name),"warn")
  131 + # continue
  132 +
  133 + if not SQLUtil.check_table_privilege(l_name, "SELECT", db_tuple[0], db_session):
  134 + StructurePrint().print("用户{}对表{}没有select权限!".format(db_tuple[0], l_name), "warn")
127 continue 135 continue
128 136
129 # 范围统计和数量统计以100w为界限 137 # 范围统计和数量统计以100w为界限
130 - query_count_layer: Layer = pg_ds.ExecuteSQL(  
131 - '''SELECT reltuples::bigint AS ec FROM pg_class WHERE oid = 'public."{}"'::regclass'''.format(l_name))  
132 138
133 - feature_count = query_count_layer.GetFeature(0).GetField("ec") 139 + # query_count_layer: Layer = pg_ds.ExecuteSQL(
  140 + # '''SELECT reltuples::bigint AS ec FROM pg_class WHERE oid = 'public."{}"'::regclass'''.format(l_name))
  141 + #
  142 + # feature_count = query_count_layer.GetFeature(0).GetField("ec")
  143 +
  144 + query_count_layer = db_session.execute(
  145 + '''SELECT reltuples::bigint AS ec FROM pg_class WHERE oid = 'public."{}"'::regclass'''.format(
  146 + l_name)).fetchone()
  147 + feature_count = query_count_layer[0]
  148 +
  149 +
134 # 要素少于100w可以精确统计 150 # 要素少于100w可以精确统计
135 - if feature_count < 1000000: 151 + if feature_count < 100000:
136 feature_count = layer.GetFeatureCount() 152 feature_count = layer.GetFeatureCount()
137 ext = layer.GetExtent() 153 ext = layer.GetExtent()
138 else: 154 else:
139 - query_ext_layer: Layer = pg_ds.ExecuteSQL(  
140 - "select geometry(ST_EstimatedExtent('public', '{}','{}'))".format(l_name,  
141 - layer.GetGeometryColumn()))  
142 - ext = query_ext_layer.GetExtent() 155 + # query_ext_layer: Layer = pg_ds.ExecuteSQL(
  156 + # "select geometry(ST_EstimatedExtent('public', '{}','{}'))".format(l_name,
  157 + # layer.GetGeometryColumn()))
  158 + # ext = query_ext_layer.GetExtent()
  159 +
  160 + try:
  161 + ext = db_session.execute(
  162 + "select st_xmin(box3d),st_xmax(box3d),st_ymin(box3d),st_ymax(box3d) from (select box3d(geometry(ST_EstimatedExtent('public', '{}','{}')))) as b".format(
  163 + l_name,
  164 + layer.GetGeometryColumn())).fetchone()
  165 + except:
  166 + db_session.rollback()
  167 + ext = layer.GetExtent()
  168 +
143 if ext[0] < 360: 169 if ext[0] < 360:
144 ext = [round(e, 6) for e in ext] 170 ext = [round(e, 6) for e in ext]
145 else: 171 else:
@@ -157,6 +183,7 @@ class Api(ApiTemplate): @@ -157,6 +183,7 @@ class Api(ApiTemplate):
157 name=layer.GetName(), create_time=this_time, update_time=this_time, 183 name=layer.GetName(), create_time=this_time, update_time=this_time,
158 table_type=GeometryAdapter.get_table_type(geom_type), 184 table_type=GeometryAdapter.get_table_type(geom_type),
159 extent=extent, 185 extent=extent,
  186 + creator=self.para.get("creator"),
160 feature_count=feature_count 187 feature_count=feature_count
161 ) 188 )
162 189
@@ -176,14 +203,14 @@ class Api(ApiTemplate): @@ -176,14 +203,14 @@ class Api(ApiTemplate):
176 203
177 spatial_table_name.append(layer.GetName()) 204 spatial_table_name.append(layer.GetName())
178 except: 205 except:
179 - StructurePrint().print("表{}注册失败!".format(l_name), "warn") 206 + StructurePrint().print("空间表{}注册失败!".format(l_name), "warn")
  207 + db_session.rollback()
180 continue 208 continue
181 return spatial_table_name,tables 209 return spatial_table_name,tables
182 210
183 211
184 - def register_common_table(self,this_time,database,spatial_table_name,db_tuple): 212 + def register_common_table(self,db_session,this_time,database,spatial_table_name,db_tuple):
185 # 注册普通表 213 # 注册普通表
186 - db_session: Session = PGUtil.get_db_session(DES.decode(database.sqlalchemy_uri))  
187 214
188 # 只注册public中的表 215 # 只注册public中的表
189 result = db_session.execute( 216 result = db_session.execute(
@@ -191,50 +218,56 @@ class Api(ApiTemplate): @@ -191,50 +218,56 @@ class Api(ApiTemplate):
191 218
192 for re in result: 219 for re in result:
193 table_name = re[0] 220 table_name = re[0]
194 - if table_name not in spatial_table_name:  
195 -  
196 - # 没有权限的表跳过  
197 - if not SQLUtil.check_table_privilege(table_name, "SELECT", db_tuple[0], db_session):  
198 - StructurePrint().print("用户{}对表{}没有select权限!".format(db_tuple[0],table_name), "warn")  
199 - continue  
200 -  
201 - table_guid = uuid.uuid1().__str__()  
202 -  
203 - count = SQLUtil.get_table_count(table_name,db_session)  
204 -  
205 - table = Table(guid=table_guid,  
206 - database_guid=database.guid,  
207 - # alias=layer.GetName(),  
208 - name=table_name, create_time=this_time, update_time=this_time,  
209 - table_type=0,  
210 - feature_count=count  
211 - )  
212 -  
213 - db.session.add(table) 221 + try:
  222 + if table_name not in spatial_table_name:
  223 +
  224 + # 没有权限的表跳过
  225 + if not SQLUtil.check_table_privilege(table_name, "SELECT", db_tuple[0], db_session):
  226 + StructurePrint().print("用户{}对表{}没有select权限!".format(db_tuple[0],table_name), "warn")
  227 + continue
  228 +
  229 + table_guid = uuid.uuid1().__str__()
  230 +
  231 + count = SQLUtil.get_table_count(table_name,db_session)
  232 +
  233 + table = Table(guid=table_guid,
  234 + database_guid=database.guid,
  235 + # alias=layer.GetName(),
  236 + name=table_name, create_time=this_time, update_time=this_time,
  237 + table_type=0,
  238 + creator=self.para.get("creator"),
  239 + feature_count=count
  240 + )
  241 +
  242 + db.session.add(table)
  243 +
  244 + sql = '''
  245 + SELECT
  246 + a.attnum,
  247 + a.attname AS field
  248 + FROM
  249 + pg_class c,
  250 + pg_attribute a,
  251 + pg_type t
  252 + WHERE
  253 + c.relname = '{}'
  254 + and a.attnum > 0
  255 + and a.attrelid = c.oid
  256 + and a.atttypid = t.oid
  257 + ORDER BY a.attnum
  258 + '''.format(table_name)
  259 +
  260 + cols = db_session.execute(sql).fetchall()
  261 + for col in cols:
  262 + column = Columns(guid=uuid.uuid1().__str__(), table_guid=table_guid,
  263 + name=col[1], create_time=this_time, update_time=this_time)
  264 + db.session.add(column)
  265 + except:
  266 + StructurePrint().print("非空间表{}注册失败!".format(table_name), "warn")
  267 + db_session.rollback()
  268 + continue
214 269
215 - sql = '''  
216 - SELECT  
217 - a.attnum,  
218 - a.attname AS field  
219 - FROM  
220 - pg_class c,  
221 - pg_attribute a,  
222 - pg_type t  
223 - WHERE  
224 - c.relname = '{}'  
225 - and a.attnum > 0  
226 - and a.attrelid = c.oid  
227 - and a.atttypid = t.oid  
228 - ORDER BY a.attnum  
229 - '''.format(table_name)  
230 -  
231 - cols = db_session.execute(sql).fetchall()  
232 - for col in cols:  
233 - column = Columns(guid=uuid.uuid1().__str__(), table_guid=table_guid,  
234 - name=col[1], create_time=this_time, update_time=this_time)  
235 - db.session.add(column)  
236 db_session.commit() 270 db_session.commit()
237 - db_session.close()  
238 271
239 272
240 def regiser_vacuate_table(self,pg_ds,tables,db_tuple): 273 def regiser_vacuate_table(self,pg_ds,tables,db_tuple):
@@ -244,8 +277,6 @@ class Api(ApiTemplate): @@ -244,8 +277,6 @@ class Api(ApiTemplate):
244 layer:Layer = pg_ds.GetLayer(i) 277 layer:Layer = pg_ds.GetLayer(i)
245 l_name = layer.GetName() 278 l_name = layer.GetName()
246 279
247 -  
248 -  
249 if l_name.__contains__("_vacuate_"): 280 if l_name.__contains__("_vacuate_"):
250 281
251 base_layer_name=l_name.split("_vacuate_")[1] 282 base_layer_name=l_name.split("_vacuate_")[1]
@@ -5,7 +5,7 @@ @@ -5,7 +5,7 @@
5 from flasgger import swag_from 5 from flasgger import swag_from
6 from flask import Blueprint 6 from flask import Blueprint
7 from app.util import BlueprintApi 7 from app.util import BlueprintApi
8 - 8 +from app.modules.data.models import Task
9 from flask import send_from_directory 9 from flask import send_from_directory
10 import os 10 import os
11 from . import data_download_task 11 from . import data_download_task
@@ -13,9 +13,15 @@ from . import get_meta @@ -13,9 +13,15 @@ from . import get_meta
13 from . import data_entry_by_meta 13 from . import data_entry_by_meta
14 from . import get_data_list 14 from . import get_data_list
15 from . import data_entry_simple 15 from . import data_entry_simple
  16 +from . import data_download
16 import configure 17 import configure
  18 +from flask import abort
17 from app.decorators.auth_decorator import auth_decorator 19 from app.decorators.auth_decorator import auth_decorator
18 import os 20 import os
  21 +from app.util.component.ParameterUtil import ParameterUtil
  22 +from app.modules.auth.models import OAuth2Token,User,db
  23 +import configure
  24 +
19 class DataManager(BlueprintApi): 25 class DataManager(BlueprintApi):
20 26
21 bp:Blueprint = Blueprint("DataTool", __name__, url_prefix="/API/IO") 27 bp:Blueprint = Blueprint("DataTool", __name__, url_prefix="/API/IO")
@@ -23,15 +29,11 @@ class DataManager(BlueprintApi): @@ -23,15 +29,11 @@ class DataManager(BlueprintApi):
23 29
24 @staticmethod 30 @staticmethod
25 @bp.route('/Download/<dir>/<file>', methods=['GET']) 31 @bp.route('/Download/<dir>/<file>', methods=['GET'])
26 - @auth_decorator(configure.DataPermission)  
27 - def table_download_file(dir,file):  
28 -  
29 - parent = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))  
30 - dirpath = os.path.join(parent,"file_tmp",dir)  
31 - return send_from_directory(dirpath, filename=file, as_attachment=True) 32 + @swag_from(data_download.Api.api_doc)
  33 + def data_download(dir,file):
  34 + api = data_download.Api(dir,file)
  35 + return api.result
32 36
33 - # dirpath = os.path.join(parent, "file_tmp")  
34 - # return send_from_directory(dirpath, filename=file, as_attachment=True)  
35 37
36 38
37 @staticmethod 39 @staticmethod
  1 +# coding=utf-8
  2 +#author: 4N
  3 +#createtime: 2020/11/27
  4 +#email: nheweijun@sina.com
  5 +
  6 +
  7 +from flask import abort,send_from_directory
  8 +from app.modules.auth.models import OAuth2Token,User,db
  9 +from app.modules.data.models import Task
  10 +import os
  11 +from app.util.component.ApiTemplate import ApiTemplate
  12 +import configure
  13 +
  14 +class Api(ApiTemplate):
  15 +
  16 + api_name = "下载"
  17 +
  18 + def __init__(self,dirname,file):
  19 + super().__init__()
  20 + self.dirname = dirname
  21 + self.file = file
  22 +
  23 +
  24 + def process(self):
  25 +
  26 + parent = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
  27 + dirpath = os.path.join(parent, "file_tmp", self.dirname)
  28 +
  29 + if configure.PermissionActive:
  30 + token = self.para.get("token")
  31 + if not token:
  32 + abort(401)
  33 + task_guid = self.para.get("task_guid")
  34 + task = Task.query.filter_by(guid=task_guid).one_or_none()
  35 + if not task:
  36 + raise Exception("任务不存在!")
  37 +
  38 + user = User.query.join(OAuth2Token).filter(OAuth2Token.access_token == token).one_or_none()
  39 + if not user:
  40 + abort(403)
  41 + if user.username != task.creator:
  42 + if user.username != "admin":
  43 + abort(403)
  44 + if not os.path.exists(os.path.join(dirpath,self.file)):
  45 + raise Exception("数据不不存在了!")
  46 +
  47 + return send_from_directory(dirpath, filename=self.file, as_attachment=True)
  48 +
  49 +
  50 + api_doc={
  51 + "tags":["IO接口"],
  52 + "description":"下载数据",
  53 + "parameters":[
  54 + {"name": "token",
  55 + "in": "formData",
  56 + "type": "string","required":"true"
  57 + },
  58 + {"name": "task_guid",
  59 + "in": "formData",
  60 + "type": "string","required":"true"
  61 + }
  62 + ],
  63 + "responses":{
  64 + 200:{
  65 + "schema":{
  66 + "properties":{
  67 + "content":{
  68 + "type": "string",
  69 + "description": "The name of the user"
  70 + }
  71 + }
  72 + }
  73 + }
  74 + }
  75 +
  76 +}
  77 +
  78 +
@@ -35,7 +35,7 @@ class Api(ApiTemplate): @@ -35,7 +35,7 @@ class Api(ApiTemplate):
35 # 初始化task 35 # 初始化task
36 task_guid = uuid.uuid1().__str__() 36 task_guid = uuid.uuid1().__str__()
37 37
38 - refresh_process = multiprocessing.Process(target=self.table_refresh,args=(database,task_guid)) 38 + refresh_process = multiprocessing.Process(target=self.table_refresh,args=(database,task_guid,self.para.get("creator")))
39 refresh_process.start() 39 refresh_process.start()
40 40
41 task = Task(guid=task_guid, 41 task = Task(guid=task_guid,
@@ -61,7 +61,7 @@ class Api(ApiTemplate): @@ -61,7 +61,7 @@ class Api(ApiTemplate):
61 return res 61 return res
62 62
63 63
64 - def table_refresh(self,database,task_guid): 64 + def table_refresh(self,database,task_guid,creator):
65 65
66 pg_ds =None 66 pg_ds =None
67 sys_ds =None 67 sys_ds =None
@@ -82,6 +82,8 @@ class Api(ApiTemplate): @@ -82,6 +82,8 @@ class Api(ApiTemplate):
82 82
83 sys_session = PGUtil.get_db_session(configure.SQLALCHEMY_DATABASE_URI) 83 sys_session = PGUtil.get_db_session(configure.SQLALCHEMY_DATABASE_URI)
84 sys_ds = PGUtil.open_pg_data_source(0,configure.SQLALCHEMY_DATABASE_URI) 84 sys_ds = PGUtil.open_pg_data_source(0,configure.SQLALCHEMY_DATABASE_URI)
  85 + # 实体库连接
  86 + data_session: Session = PGUtil.get_db_session(DES.decode(database.sqlalchemy_uri))
85 87
86 this_time = datetime.datetime.now() 88 this_time = datetime.datetime.now()
87 database_guid = database.guid 89 database_guid = database.guid
@@ -98,21 +100,17 @@ class Api(ApiTemplate): @@ -98,21 +100,17 @@ class Api(ApiTemplate):
98 100
99 # 更新空间表 101 # 更新空间表
100 # 增加表 102 # 增加表
101 - db_tables_names = self.add_spatail_table(database, pg_ds, sys_session,spatial_tables_names, this_time,db_tuple)# 实体库中空间表名 103 + db_tables_names = self.add_spatail_table(database, pg_ds, data_session,sys_session,spatial_tables_names, this_time,db_tuple,creator)# 实体库中空间表名
102 104
103 # 删除/修改表 105 # 删除/修改表
104 - self.edit_spatial_table(pg_ds, sys_session,spatial_tables, db_tables_names, this_time,db_tuple) 106 + self.edit_spatial_table(pg_ds, data_session, sys_session,spatial_tables, db_tables_names, this_time,db_tuple)
105 107
106 # 空间表处理完毕 108 # 空间表处理完毕
107 sys_session.commit() 109 sys_session.commit()
108 110
109 111
110 - # 空间表处理完毕  
111 - sys_session.commit()  
112 -  
113 # 注册普通表 112 # 注册普通表
114 - # 实体库连接  
115 - data_session: Session = PGUtil.get_db_session(DES.decode(database.sqlalchemy_uri)) 113 +
116 114
117 # 处理后空间表 115 # 处理后空间表
118 spatial_tables = sys_session.query(Table).order_by(Table.create_time.desc()).filter_by(database_guid=database_guid).filter( 116 spatial_tables = sys_session.query(Table).order_by(Table.create_time.desc()).filter_by(database_guid=database_guid).filter(
@@ -123,7 +121,7 @@ class Api(ApiTemplate): @@ -123,7 +121,7 @@ class Api(ApiTemplate):
123 # 原有普通表 121 # 原有普通表
124 common_tables = sys_session.query(Table).order_by(Table.create_time.desc()).filter_by(database_guid=database_guid).filter( 122 common_tables = sys_session.query(Table).order_by(Table.create_time.desc()).filter_by(database_guid=database_guid).filter(
125 Table.table_type == 0).all() 123 Table.table_type == 0).all()
126 - # 原有普通表 124 + # 原有普通表名
127 origin_common_tables_name = [table.name for table in common_tables] 125 origin_common_tables_name = [table.name for table in common_tables]
128 126
129 # 现有普通表 127 # 现有普通表
@@ -140,7 +138,8 @@ class Api(ApiTemplate): @@ -140,7 +138,8 @@ class Api(ApiTemplate):
140 # 增加新普通表 138 # 增加新普通表
141 139
142 self.add_common_table(data_session, sys_session, database_guid, real_common_tables_name, origin_common_tables_name, 140 self.add_common_table(data_session, sys_session, database_guid, real_common_tables_name, origin_common_tables_name,
143 - this_time,db_tuple) 141 + this_time,db_tuple,creator)
  142 + sys_session.commit()
144 143
145 # 删除、修改普通表 144 # 删除、修改普通表
146 self.edit_common_table(data_session,sys_session, database_guid, real_common_tables_name, origin_common_tables_name, 145 self.edit_common_table(data_session,sys_session, database_guid, real_common_tables_name, origin_common_tables_name,
@@ -173,7 +172,7 @@ class Api(ApiTemplate): @@ -173,7 +172,7 @@ class Api(ApiTemplate):
173 sys_ds.Destroy() 172 sys_ds.Destroy()
174 return result 173 return result
175 174
176 - def add_spatail_table(self,database,pg_ds,sys_session,spatial_tables_names,this_time,db_tuple): 175 + def add_spatail_table(self,database,pg_ds,data_session,sys_session,spatial_tables_names,this_time,db_tuple,creator):
177 ''' 176 '''
178 注册新增空间表 177 注册新增空间表
179 :param database: 178 :param database:
@@ -202,40 +201,68 @@ class Api(ApiTemplate): @@ -202,40 +201,68 @@ class Api(ApiTemplate):
202 if layer.GetName().__contains__("_vacuate_"): 201 if layer.GetName().__contains__("_vacuate_"):
203 continue 202 continue
204 203
  204 + # # 没有权限的表跳过
  205 + # if not PGUtil.check_table_privilege(l_name, "SELECT", db_tuple[0], pg_ds):
  206 + # StructurePrint().print("用户{}对表{}没有select权限!".format(db_tuple[0], l_name), "warn")
  207 + # continue
  208 +
205 # 没有权限的表跳过 209 # 没有权限的表跳过
206 - if not PGUtil.check_table_privilege(l_name, "SELECT", db_tuple[0], pg_ds): 210 + if not SQLUtil.check_table_privilege(l_name, "SELECT", db_tuple[0], data_session):
207 StructurePrint().print("用户{}对表{}没有select权限!".format(db_tuple[0], l_name), "warn") 211 StructurePrint().print("用户{}对表{}没有select权限!".format(db_tuple[0], l_name), "warn")
208 continue 212 continue
209 213
210 -  
211 # 范围统计和数量统计以100w为界限 214 # 范围统计和数量统计以100w为界限
212 - query_count_layer: Layer = pg_ds.ExecuteSQL(  
213 - '''SELECT reltuples::bigint AS ec FROM pg_class WHERE oid = 'public."{}"'::regclass'''.format(  
214 - l_name)) 215 + query_count_layer = data_session.execute('''SELECT reltuples::bigint AS ec FROM pg_class WHERE oid = 'public."{}"'::regclass'''.format(l_name)).fetchone()
  216 + feature_count = query_count_layer[0]
  217 +
  218 + # query_count_layer: Layer = pg_ds.ExecuteSQL(
  219 + # '''SELECT reltuples::bigint AS ec FROM pg_class WHERE oid = 'public."{}"'::regclass'''.format(
  220 + # l_name))
  221 +
  222 + # feature_count = query_count_layer.GetFeature(0).GetField("ec")
  223 +
  224 +
  225 +
215 226
216 - feature_count = query_count_layer.GetFeature(0).GetField("ec")  
217 # 要素少于100w可以精确统计 227 # 要素少于100w可以精确统计
218 if feature_count < 1000000: 228 if feature_count < 1000000:
219 feature_count = layer.GetFeatureCount() 229 feature_count = layer.GetFeatureCount()
220 ext = layer.GetExtent() 230 ext = layer.GetExtent()
221 else: 231 else:
222 - query_ext_layer: Layer = pg_ds.ExecuteSQL(  
223 - "select geometry(ST_EstimatedExtent('public', '{}','{}'))".format(l_name,  
224 - layer.GetGeometryColumn()))  
225 - ext = query_ext_layer.GetExtent() 232 + # query_ext_layer: Layer = pg_ds.ExecuteSQL(
  233 + # "select geometry(ST_EstimatedExtent('public', '{}','{}'))".format(l_name,
  234 + # layer.GetGeometryColumn()))
  235 + #
  236 + # ext = query_ext_layer.GetExtent()
  237 + try:
  238 +
  239 + ext = data_session.execute("select st_xmin(box3d),st_xmax(box3d),st_ymin(box3d),st_ymax(box3d) from (select box3d(geometry(ST_EstimatedExtent('public', '{}','{}')))) as b".format(l_name,
  240 + layer.GetGeometryColumn())).fetchone()
  241 + except:
  242 + data_session.rollback()
  243 + ext = layer.GetExtent()
  244 +
  245 +
226 if ext[0] < 360: 246 if ext[0] < 360:
227 ext = [round(e, 6) for e in ext] 247 ext = [round(e, 6) for e in ext]
228 else: 248 else:
229 ext = [round(e, 2) for e in ext] 249 ext = [round(e, 2) for e in ext]
230 extent = "{},{},{},{}".format(ext[0], ext[1], ext[2], ext[3]) 250 extent = "{},{},{},{}".format(ext[0], ext[1], ext[2], ext[3])
231 251
232 - StructurePrint().print("空间表增加!") 252 + StructurePrint().print("空间表{}增加!".format(l_name))
233 253
234 geom_type = GeometryAdapter.get_geometry_type(layer) 254 geom_type = GeometryAdapter.get_geometry_type(layer)
235 - except:  
236 - StructurePrint().print("表{}注册失败!".format(l_name), "warn") 255 + except Exception as e:
  256 + StructurePrint().print("{}".format(e.__str__()+":"+ traceback.format_exc()), "warn")
  257 + StructurePrint().print("空间表{}新增失败!".format(l_name), "warn")
  258 + data_session.rollback()
237 continue 259 continue
238 260
  261 + olds = sys_session.query(Table).filter_by(name=l_name,database_guid=database.guid).all()
  262 + for old in olds:
  263 + sys_session.delete(old)
  264 +
  265 +
239 table_guid = uuid.uuid1().__str__() 266 table_guid = uuid.uuid1().__str__()
240 table = Table(guid=table_guid, 267 table = Table(guid=table_guid,
241 database_guid=database.guid, 268 database_guid=database.guid,
@@ -243,6 +270,7 @@ class Api(ApiTemplate): @@ -243,6 +270,7 @@ class Api(ApiTemplate):
243 name=layer.GetName(), create_time=this_time, update_time=this_time, 270 name=layer.GetName(), create_time=this_time, update_time=this_time,
244 table_type=GeometryAdapter.get_table_type(geom_type), 271 table_type=GeometryAdapter.get_table_type(geom_type),
245 extent=extent, 272 extent=extent,
  273 + creator=creator,
246 feature_count=feature_count 274 feature_count=feature_count
247 ) 275 )
248 sys_session.add(table) 276 sys_session.add(table)
@@ -301,158 +329,136 @@ class Api(ApiTemplate): @@ -301,158 +329,136 @@ class Api(ApiTemplate):
301 329
302 330
303 331
304 - def edit_spatial_table(self,pg_ds,sys_session,spatial_tables,db_tables_names,this_time,db_tuple):  
305 -  
306 - 332 + def edit_spatial_table(self,pg_ds, data_session,sys_session,spatial_tables,db_tables_names,this_time,db_tuple):
307 333
308 for table in spatial_tables: 334 for table in spatial_tables:
309 -  
310 - # 删除表  
311 - if table.name not in db_tables_names:  
312 - StructurePrint().print("空间表减少!")  
313 - sys_session.delete(table)  
314 - # 修改表  
315 - else:  
316 - layer: Layer = pg_ds.GetLayerByName(table.name)  
317 - l_name = layer.GetName()  
318 -  
319 - # 只注册public的空间表,其他表空间的表名会有.  
320 - if layer.GetName().__contains__("."):  
321 - continue  
322 -  
323 - if layer.GetName().__contains__("_vacuate_"):  
324 - continue  
325 -  
326 - # 没有权限的表跳过  
327 - if not PGUtil.check_table_privilege(l_name, "SELECT", db_tuple[0], pg_ds):  
328 - StructurePrint().print("用户{}对表{}没有select权限!".format(db_tuple[0], l_name), "warn") 335 + try:
  336 + # 删除表
  337 + if table.name not in db_tables_names:
  338 + StructurePrint().print("空间表减少!")
329 sys_session.delete(table) 339 sys_session.delete(table)
330 - continue 340 + # 修改表
  341 + else:
  342 + layer: Layer = pg_ds.GetLayerByName(table.name)
  343 + l_name = layer.GetName()
331 344
332 - columns = table.relate_columns  
333 - columns_names = [column.name for column in columns]  
334 - feature_defn: FeatureDefn = layer.GetLayerDefn()  
335 - db_columns_names = [] 345 + # 只注册public的空间表,其他表空间的表名会有.
  346 + if layer.GetName().__contains__("."):
  347 + continue
336 348
337 - # 增加列  
338 - for i in range(feature_defn.GetFieldCount()):  
339 - field_defn: FieldDefn = feature_defn.GetFieldDefn(i)  
340 - field_name = field_defn.GetName()  
341 - db_columns_names.append(field_name)  
342 -  
343 - if field_name not in columns_names:  
344 - StructurePrint().print("{}空间表属性增加!".format(table.name))  
345 - field_alias = field_name if field_defn.GetAlternativeName() is None or field_defn.GetAlternativeName().__eq__(  
346 - "") else field_defn.GetAlternativeName()  
347 - column = Columns(guid=uuid.uuid1().__str__(), table_guid=table.guid,  
348 - name=field_name, alias=field_alias, create_time=this_time,  
349 - update_time=this_time)  
350 - sys_session.add(column) 349 + if layer.GetName().__contains__("_vacuate_"):
  350 + continue
351 351
352 - # 删除列  
353 - for column in columns:  
354 - if column.name not in db_columns_names:  
355 - StructurePrint().print("{}空间表属性减少!".format(table.name))  
356 - sys_session.delete(column)  
357 -  
358 - # 范围统计和数量统计以100w为界限  
359 - query_count_layer: Layer = pg_ds.ExecuteSQL(  
360 - '''SELECT reltuples::bigint AS ec FROM pg_class WHERE oid = 'public."{}"'::regclass'''.format(  
361 - l_name))  
362 - feature_count = query_count_layer.GetFeature(0).GetField("ec")  
363 - # 要素少于100w可以精确统计  
364 - if feature_count < 1000000:  
365 - feature_count = layer.GetFeatureCount()  
366 - ext = layer.GetExtent() 352 + # # 没有权限的表跳过
  353 + # if not PGUtil.check_table_privilege(l_name, "SELECT", db_tuple[0], pg_ds):
  354 + # StructurePrint().print("用户{}对表{}没有select权限!".format(db_tuple[0], l_name), "warn")
  355 + # sys_session.delete(table)
  356 + # continue
367 357
368 - else:  
369 - query_ext_layer: Layer = pg_ds.ExecuteSQL(  
370 - "select geometry(ST_EstimatedExtent('public', '{}','{}'))".format(l_name,  
371 - layer.GetGeometryColumn()))  
372 - ext = query_ext_layer.GetExtent()  
373 - if ext[0] < 360:  
374 - ext = [round(e, 6) for e in ext]  
375 - else:  
376 - ext = [round(e, 2) for e in ext]  
377 - extent = "{},{},{},{}".format(ext[0], ext[1], ext[2], ext[3]) 358 + # 没有权限的表跳过
  359 + if not SQLUtil.check_table_privilege(l_name, "SELECT", db_tuple[0], data_session):
  360 + StructurePrint().print("用户{}对表{}没有select权限!".format(db_tuple[0], l_name), "warn")
  361 + sys_session.delete(table)
  362 + continue
378 363
379 - # 修改要素量  
380 - if not table.feature_count.__eq__(feature_count):  
381 - StructurePrint().print("{}空间表要素!".format(table.name))  
382 - sys_session.query(Table).filter_by(guid=table.guid).update({"feature_count": feature_count,  
383 - "extent": extent}) 364 + columns = table.relate_columns
  365 + columns_names = [column.name for column in columns]
  366 + feature_defn: FeatureDefn = layer.GetLayerDefn()
  367 + db_columns_names = []
  368 +
  369 + # 增加列
  370 + for i in range(feature_defn.GetFieldCount()):
  371 + field_defn: FieldDefn = feature_defn.GetFieldDefn(i)
  372 + field_name = field_defn.GetName()
  373 + db_columns_names.append(field_name)
  374 +
  375 + if field_name not in columns_names:
  376 + StructurePrint().print("{}空间表属性增加!".format(table.name))
  377 + field_alias = field_name if field_defn.GetAlternativeName() is None or field_defn.GetAlternativeName().__eq__(
  378 + "") else field_defn.GetAlternativeName()
  379 + column = Columns(guid=uuid.uuid1().__str__(), table_guid=table.guid,
  380 + name=field_name, alias=field_alias, create_time=this_time,
  381 + update_time=this_time)
  382 + sys_session.add(column)
384 383
  384 + # 删除列
  385 + for column in columns:
  386 + if column.name not in db_columns_names:
  387 + StructurePrint().print("{}空间表属性减少!".format(table.name))
  388 + sys_session.delete(column)
385 389
386 - def add_common_table(self,data_session,sys_session,database_guid,real_common_tables_name,origin_common_tables_name,this_time,db_tuple):  
387 - for table_name in real_common_tables_name:  
388 - if table_name not in origin_common_tables_name:  
389 - StructurePrint().print("{}非空间表增加!".format(table_name))  
390 - table_guid = uuid.uuid1().__str__() 390 + # 范围统计和数量统计以100w为界限
  391 + # query_count_layer: Layer = pg_ds.ExecuteSQL(
  392 + # '''SELECT reltuples::bigint AS ec FROM pg_class WHERE oid = 'public."{}"'::regclass'''.format(
  393 + # l_name))
  394 + # feature_count = query_count_layer.GetFeature(0).GetField("ec")
391 395
  396 + query_count_layer = data_session.execute('''SELECT reltuples::bigint AS ec FROM pg_class WHERE oid = 'public."{}"'::regclass'''.format(l_name)).fetchone()
  397 + feature_count = query_count_layer[0]
392 398
393 - # 没有权限的表跳过  
394 - if not SQLUtil.check_table_privilege(table_name, "SELECT", db_tuple[0], data_session):  
395 - StructurePrint().print("用户{}对表{}没有select权限!".format(db_tuple[0], table_name), "warn")  
396 - continue 399 + # 要素少于100w可以精确统计
  400 + if feature_count < 1000000:
  401 + feature_count = layer.GetFeatureCount()
  402 + ext = layer.GetExtent()
397 403
398 - count = data_session.execute('select count(*) from "{}"'.format(table_name)).fetchone()[0] 404 + else:
  405 + # query_ext_layer: Layer = pg_ds.ExecuteSQL(
  406 + # "select geometry(ST_EstimatedExtent('public', '{}','{}'))".format(l_name,
  407 + # layer.GetGeometryColumn()))
  408 + # ext = query_ext_layer.GetExtent()
  409 + try:
  410 + ext = data_session.execute("select st_xmin(box3d),st_xmax(box3d),st_ymin(box3d),st_ymax(box3d) from (select box3d(geometry(ST_EstimatedExtent('public', '{}','{}')))) as b".format(l_name,
  411 + layer.GetGeometryColumn())).fetchone()
  412 + except:
  413 + data_session.rollback()
  414 + ext = layer.GetExtent()
399 415
400 - table = Table(guid=table_guid,  
401 - database_guid=database_guid,  
402 - name=table_name, create_time=this_time, update_time=this_time,  
403 - table_type=0,  
404 - feature_count=count  
405 - ) 416 + if ext[0] < 360:
  417 + ext = [round(e, 6) for e in ext]
  418 + else:
  419 + ext = [round(e, 2) for e in ext]
  420 + extent = "{},{},{},{}".format(ext[0], ext[1], ext[2], ext[3])
406 421
407 - sys_session.add(table) 422 + # 修改要素量
  423 + if not table.feature_count.__eq__(feature_count):
  424 + StructurePrint().print("{}空间表要素!".format(table.name))
  425 + sys_session.query(Table).filter_by(guid=table.guid).update({"feature_count": feature_count,
  426 + "extent": extent})
  427 + except Exception as e:
  428 + StructurePrint().print("{}".format(e.__str__() + ":" + traceback.format_exc()), "warn")
  429 + StructurePrint().print("空间表{}修改失败!".format(table.name), "warn")
  430 + data_session.rollback()
  431 + continue
408 432
409 - sql = '''  
410 - SELECT  
411 - a.attnum,  
412 - a.attname AS field  
413 - FROM  
414 - pg_class c,  
415 - pg_attribute a,  
416 - pg_type t  
417 - WHERE  
418 - c.relname = '{}'  
419 - and a.attnum > 0  
420 - and a.attrelid = c.oid  
421 - and a.atttypid = t.oid  
422 - ORDER BY a.attnum  
423 - '''.format(table_name)  
424 -  
425 - cols = data_session.execute(sql).fetchall()  
426 - for col in cols:  
427 - column = Columns(guid=uuid.uuid1().__str__(), table_guid=table_guid,  
428 - name=col[1], create_time=this_time, update_time=this_time)  
429 - sys_session.add(column) 433 + def add_common_table(self,data_session,sys_session,database_guid,real_common_tables_name,origin_common_tables_name,this_time,db_tuple,creator):
  434 + for table_name in real_common_tables_name:
  435 + if table_name not in origin_common_tables_name:
  436 + try:
430 437
431 - # 删除不存在的表  
432 - for n in origin_common_tables_name:  
433 - if n not in real_common_tables_name:  
434 - tables = Table.query.filter_by(name=n).filter_by(database_guid=database_guid).all()  
435 - for table in tables:  
436 - sys_session.delete(table) 438 + StructurePrint().print("{}非空间表增加!".format(table_name))
  439 + table_guid = uuid.uuid1().__str__()
437 440
438 - def edit_common_table(self,data_session,sys_session,database_guid,real_common_tables_name,origin_common_tables_name,this_time,db_tuple):  
439 - for table_name in origin_common_tables_name:  
440 - tables = sys_session.query(Table).filter_by(name=table_name).filter_by(database_guid=database_guid).all()  
441 - for table in tables:  
442 - if table_name not in real_common_tables_name:  
443 - StructurePrint().print("{}非空间表减少!".format(table_name))  
444 - sys_session.delete(table)  
445 - # 修改表  
446 - else:  
447 441
448 - # 没有权限的表删除 442 + # 没有权限的表跳过
449 if not SQLUtil.check_table_privilege(table_name, "SELECT", db_tuple[0], data_session): 443 if not SQLUtil.check_table_privilege(table_name, "SELECT", db_tuple[0], data_session):
450 StructurePrint().print("用户{}对表{}没有select权限!".format(db_tuple[0], table_name), "warn") 444 StructurePrint().print("用户{}对表{}没有select权限!".format(db_tuple[0], table_name), "warn")
451 - sys_session.delete(table)  
452 continue 445 continue
453 446
454 - columns = table.relate_columns  
455 - columns_names = [column.name for column in columns] 447 + count = data_session.execute('select count(*) from "{}"'.format(table_name)).fetchone()[0]
  448 +
  449 + olds = sys_session.query(Table).filter_by(name=table_name, database_guid=database_guid).all()
  450 + for old in olds:
  451 + sys_session.delete(old)
  452 +
  453 + table = Table(guid=table_guid,
  454 + database_guid=database_guid,
  455 + name=table_name, create_time=this_time, update_time=this_time,
  456 + table_type=0,
  457 + feature_count=count,
  458 + creator = creator
  459 + )
  460 +
  461 + sys_session.add(table)
456 462
457 sql = ''' 463 sql = '''
458 SELECT 464 SELECT
@@ -471,28 +477,86 @@ class Api(ApiTemplate): @@ -471,28 +477,86 @@ class Api(ApiTemplate):
471 '''.format(table_name) 477 '''.format(table_name)
472 478
473 cols = data_session.execute(sql).fetchall() 479 cols = data_session.execute(sql).fetchall()
474 - real_cols_name = [col[1] for col in cols]  
475 -  
476 - # 属性增加  
477 - for col in real_cols_name:  
478 - if col not in columns_names:  
479 - StructurePrint().print("{}表要素属性增加!".format(table_name))  
480 - column = Columns(guid=uuid.uuid1().__str__(), table_guid=table.guid,  
481 - name=col, create_time=this_time, update_time=this_time)  
482 - sys_session.add(column)  
483 -  
484 - # 属性减少  
485 - for column in columns:  
486 - if column.name not in real_cols_name:  
487 - StructurePrint().print("{}表要素属性减少!".format(table_name))  
488 - sys_session.delete(column) 480 + for col in cols:
  481 + column = Columns(guid=uuid.uuid1().__str__(), table_guid=table_guid,
  482 + name=col[1], create_time=this_time, update_time=this_time)
  483 + sys_session.add(column)
489 484
490 - count = SQLUtil.get_table_count(table_name,data_session) 485 + # 删除不存在的表
  486 + for n in origin_common_tables_name:
  487 + if n not in real_common_tables_name:
  488 + tables = Table.query.filter_by(name=n).filter_by(database_guid=database_guid).all()
  489 + for table in tables:
  490 + sys_session.delete(table)
  491 + except Exception as e:
  492 + StructurePrint().print("{}".format(e.__str__() + ":" + traceback.format_exc()), "warn")
  493 + StructurePrint().print("非空间表{}增加失败!".format(table_name), "warn")
  494 + data_session.rollback()
  495 + continue
491 496
492 - if not table.feature_count.__eq__(count):  
493 - StructurePrint().print("{}表要素变化!".format(table_name))  
494 - sys_session.query(Table).filter_by(guid=table.guid).update({"feature_count": count}) 497 + def edit_common_table(self,data_session,sys_session,database_guid,real_common_tables_name,origin_common_tables_name,this_time,db_tuple):
  498 + for table_name in origin_common_tables_name:
  499 + try:
  500 + tables = sys_session.query(Table).filter_by(name=table_name).filter_by(database_guid=database_guid).all()
  501 + for table in tables:
  502 + if table_name not in real_common_tables_name:
  503 + StructurePrint().print("{}非空间表减少!".format(table_name))
  504 + sys_session.delete(table)
  505 + # 修改表
  506 + else:
495 507
  508 + # 没有权限的表删除
  509 + if not SQLUtil.check_table_privilege(table_name, "SELECT", db_tuple[0], data_session):
  510 + StructurePrint().print("用户{}对表{}没有select权限!".format(db_tuple[0], table_name), "warn")
  511 + sys_session.delete(table)
  512 + continue
  513 +
  514 + columns = table.relate_columns
  515 + columns_names = [column.name for column in columns]
  516 +
  517 + sql = '''
  518 + SELECT
  519 + a.attnum,
  520 + a.attname AS field
  521 + FROM
  522 + pg_class c,
  523 + pg_attribute a,
  524 + pg_type t
  525 + WHERE
  526 + c.relname = '{}'
  527 + and a.attnum > 0
  528 + and a.attrelid = c.oid
  529 + and a.atttypid = t.oid
  530 + ORDER BY a.attnum
  531 + '''.format(table_name)
  532 +
  533 + cols = data_session.execute(sql).fetchall()
  534 + real_cols_name = [col[1] for col in cols]
  535 +
  536 + # 属性增加
  537 + for col in real_cols_name:
  538 + if col not in columns_names:
  539 + StructurePrint().print("{}表要素属性增加!".format(table_name))
  540 + column = Columns(guid=uuid.uuid1().__str__(), table_guid=table.guid,
  541 + name=col, create_time=this_time, update_time=this_time)
  542 + sys_session.add(column)
  543 +
  544 + # 属性减少
  545 + for column in columns:
  546 + if column.name not in real_cols_name:
  547 + StructurePrint().print("{}表要素属性减少!".format(table_name))
  548 + sys_session.delete(column)
  549 +
  550 + count = SQLUtil.get_table_count(table_name,data_session)
  551 +
  552 + if not table.feature_count.__eq__(count):
  553 + StructurePrint().print("{}表要素变化!".format(table_name))
  554 + sys_session.query(Table).filter_by(guid=table.guid).update({"feature_count": count})
  555 + except Exception as e:
  556 + StructurePrint().print("{}".format(e.__str__() + ":" + traceback.format_exc()), "warn")
  557 + StructurePrint().print("非空间表{}修改失败!".format(table_name), "warn")
  558 + data_session.rollback()
  559 + continue
496 560
497 api_doc={ 561 api_doc={
498 "tags":["管理接口"], 562 "tags":["管理接口"],
@@ -156,6 +156,6 @@ class ProjectFile: @@ -156,6 +156,6 @@ class ProjectFile:
156 ) 156 )
157 157
158 project_xml = project_xml.strip() 158 project_xml = project_xml.strip()
159 - StructurePrint().print(project_xml)  
160 - StructurePrint().print(str(base64.b64encode(project_xml.encode('utf-8')), encoding="utf8")) 159 + # StructurePrint().print(project_xml)
  160 + # StructurePrint().print(str(base64.b64encode(project_xml.encode('utf-8')), encoding="utf8"))
161 return str(base64.b64encode(project_xml.encode('utf-8')), encoding="utf8") 161 return str(base64.b64encode(project_xml.encode('utf-8')), encoding="utf8")
@@ -7,7 +7,7 @@ from flask import current_app,request @@ -7,7 +7,7 @@ from flask import current_app,request
7 import traceback 7 import traceback
8 import json 8 import json
9 from app.modules.auth.models import OAuth2Token,User,db 9 from app.modules.auth.models import OAuth2Token,User,db
10 - 10 +import app
11 class ApiTemplate: 11 class ApiTemplate:
12 #模板方法 12 #模板方法
13 para = dict() 13 para = dict()
@@ -17,13 +17,21 @@ class ApiTemplate: @@ -17,13 +17,21 @@ class ApiTemplate:
17 pass 17 pass
18 18
19 def get_para(self): 19 def get_para(self):
  20 +
20 self.para = ParameterUtil.get_parameter() 21 self.para = ParameterUtil.get_parameter()
21 token = request.headers.get('Authorization') 22 token = request.headers.get('Authorization')
  23 +
  24 + if self.para.get("token"):
  25 + token = self.para.get("token")
22 if token: 26 if token:
23 token = token.split(" ")[-1] 27 token = token.split(" ")[-1]
24 - user = User.query.join(OAuth2Token).filter(OAuth2Token.access_token == token).one_or_none()  
25 - if user:  
26 - self.para["creator"] = user.username 28 + if app.GLOBAL_DIC.get(token):
  29 + self.para["creator"] = app.GLOBAL_DIC.get(token)
  30 + else:
  31 + user = User.query.join(OAuth2Token).filter(OAuth2Token.access_token == token).one_or_none()
  32 + if user:
  33 + self.para["creator"] = user.username
  34 + app.GLOBAL_DIC[token] = user.username
27 35
28 def para_check(self): 36 def para_check(self):
29 pass 37 pass
@@ -116,13 +116,14 @@ class PGUtil: @@ -116,13 +116,14 @@ class PGUtil:
116 pri = pg_ds.ExecuteSQL("select * from information_schema.table_privileges " 116 pri = pg_ds.ExecuteSQL("select * from information_schema.table_privileges "
117 "where grantee='{}' and table_name='{}' and privilege_type='{}' " 117 "where grantee='{}' and table_name='{}' and privilege_type='{}' "
118 .format(user,table_name,pri_type)) 118 .format(user,table_name,pri_type))
119 -  
120 - if pri.GetNextFeature():  
121 - return True 119 + if pri:
  120 + if pri.GetNextFeature():
  121 + return True
  122 + else:
  123 + return False
122 else: 124 else:
123 return False 125 return False
124 126
125 -  
126 @classmethod 127 @classmethod
127 def get_srid(cls,pg_ds,table_name): 128 def get_srid(cls,pg_ds,table_name):
128 layer = pg_ds.GetLayerByName(table_name) 129 layer = pg_ds.GetLayerByName(table_name)
@@ -142,9 +143,7 @@ class PGUtil: @@ -142,9 +143,7 @@ class PGUtil:
142 def check_database_privilege(cls,table_name,pri_type,user,session): 143 def check_database_privilege(cls,table_name,pri_type,user,session):
143 pass 144 pass
144 145
145 - @classmethod  
146 - def check_database_privilege(cls,table_name,pri_type,user,session):  
147 - pass 146 +
148 147
149 if __name__ == '__main__': 148 if __name__ == '__main__':
150 # session:Session = PGUtil.get_db_session("postgresql://postgres:chinadci@172.26.60.100:5432/template1") 149 # session:Session = PGUtil.get_db_session("postgresql://postgres:chinadci@172.26.60.100:5432/template1")
  1 +# coding=utf-8
  2 +#author: 4N
  3 +#createtime: 2022/3/4
  4 +#email: nheweijun@sina.com
  5 +
  6 +from osgeo import gdal
  7 +from osgeo import ogr
  8 +from osgeo.ogr import *
  9 +
  10 +fn = "PG: user=%s password=%s host=%s port=%s dbname=%s " % ("postgres","chinadci","172.26.60.101","5432","ceshi")
  11 +driver = ogr.GetDriverByName("PostgreSQL")
  12 +if driver is None:
  13 + raise Exception("打开PostgreSQL驱动失败,可能是当前GDAL未支持PostgreSQL驱动!")
  14 +ds:DataSource = driver.Open(fn, 1)
  15 +
  16 +layer:Layer = ds.GetLayerByName("BeijingBusStops")
  17 +
  18 +
  19 +dd = gdal.Grid('outcome.tif',"J:\Data\矢量数据\北京\北京公交线路和公交站点shp\stops\BeijingBusStops.shp",algorithm = 'linear:radius=0')
不能预览此文件类型
@@ -108,9 +108,6 @@ def get_rc(parameter,l,lon,lat): @@ -108,9 +108,6 @@ def get_rc(parameter,l,lon,lat):
108 return int(r),int(c) 108 return int(r),int(c)
109 109
110 def wmts_overview(extent,url_format): 110 def wmts_overview(extent,url_format):
111 -  
112 - # scheme = {"0": {"resolution": 1.4062500000059488, "scale": 590995186.12}, "1": {"resolution": 0.7031250000029744, "scale": 295497593.06}, "2": {"resolution": 0.3515625000014872, "scale": 147748796.53}, "3": {"resolution": 0.1757812500007436, "scale": 73874398.265}, "4": {"resolution": 0.0878906250003718, "scale": 36937199.1325}, "5": {"resolution": 0.0439453125001859, "scale": 18468599.56625}, "6": {"resolution": 0.02197265625009295, "scale": 9234299.783125}, "7": {"resolution": 0.010986328125046475, "scale": 4617149.8915625}, "8": {"resolution": 0.0054931640625232375, "scale": 2308574.94578125}, "9": {"resolution": 0.0027465820312616187, "scale": 1154287.472890625}, "10": {"resolution": 0.0013732910156308094, "scale": 577143.7364453125}, "11": {"resolution": 0.0006866455078154047, "scale": 288571.86822265625}, "12": {"resolution": 0.00034332275390770234, "scale": 144285.93411132813}, "13": {"resolution": 0.00017166137695385117, "scale": 72142.96705566406}, "14": {"resolution": 8.583068847692559e-05, "scale": 36071.48352783203}, "15": {"resolution": 4.291534423846279e-05, "scale": 18035.741763916016}, "16": {"resolution": 2.1457672119231396e-05, "scale": 9017.870881958008}, "17": {"resolution": 1.0728836059615698e-05, "scale": 4508.935440979004}, "18": {"resolution": 5.364418029807849e-06, "scale": 2254.467720489502}, "19": {"resolution": 2.6822090149039246e-06, "scale": 1127.233860244751}, "20": {"resolution": 1.3411045074519623e-06, "scale": 563.6169301223755}, "cols": 256, "rows": 256, "dpi": 96, "wkt": "", "x": -400, "y": 400}  
113 -  
114 scheme = {"0": {"resolution": 156543.34701231902, "scale": 591658710.9}, 111 scheme = {"0": {"resolution": 156543.34701231902, "scale": 591658710.9},
115 "1": {"resolution": 78271.67350615951, "scale": 295829355.45}, 112 "1": {"resolution": 78271.67350615951, "scale": 295829355.45},
116 "2": {"resolution": 39135.83675440268 , "scale": 147914677.73} , 113 "2": {"resolution": 39135.83675440268 , "scale": 147914677.73} ,
@@ -134,6 +131,10 @@ def wmts_overview(extent,url_format): @@ -134,6 +131,10 @@ def wmts_overview(extent,url_format):
134 "20": {"resolution": 1.3411045074519623e-06, "scale": 563.6169301223755}, 131 "20": {"resolution": 1.3411045074519623e-06, "scale": 563.6169301223755},
135 "cols": 256, "rows": 256, "dpi": 96, "wkt": "", "x":-4923200, "y":10002100} 132 "cols": 256, "rows": 256, "dpi": 96, "wkt": "", "x":-4923200, "y":10002100}
136 133
  134 + scheme = {"0": {"resolution": 1.4062500000059488, "scale": 590995186.12}, "1": {"resolution": 0.7031250000029744, "scale": 295497593.06}, "2": {"resolution": 0.3515625000014872, "scale": 147748796.53}, "3": {"resolution": 0.1757812500007436, "scale": 73874398.265}, "4": {"resolution": 0.0878906250003718, "scale": 36937199.1325}, "5": {"resolution": 0.0439453125001859, "scale": 18468599.56625}, "6": {"resolution": 0.02197265625009295, "scale": 9234299.783125}, "7": {"resolution": 0.010986328125046475, "scale": 4617149.8915625}, "8": {"resolution": 0.0054931640625232375, "scale": 2308574.94578125}, "9": {"resolution": 0.0027465820312616187, "scale": 1154287.472890625}, "10": {"resolution": 0.0013732910156308094, "scale": 577143.7364453125}, "11": {"resolution": 0.0006866455078154047, "scale": 288571.86822265625}, "12": {"resolution": 0.00034332275390770234, "scale": 144285.93411132813}, "13": {"resolution": 0.00017166137695385117, "scale": 72142.96705566406}, "14": {"resolution": 8.583068847692559e-05, "scale": 36071.48352783203}, "15": {"resolution": 4.291534423846279e-05, "scale": 18035.741763916016}, "16": {"resolution": 2.1457672119231396e-05, "scale": 9017.870881958008}, "17": {"resolution": 1.0728836059615698e-05, "scale": 4508.935440979004}, "18": {"resolution": 5.364418029807849e-06, "scale": 2254.467720489502}, "19": {"resolution": 2.6822090149039246e-06, "scale": 1127.233860244751}, "20": {"resolution": 1.3411045074519623e-06, "scale": 563.6169301223755}, "cols": 256, "rows": 256, "dpi": 96, "wkt": "", "x": -400, "y": 400}
  135 +
  136 +
  137 +
137 lev = 0 138 lev = 0
138 r1 = 0 139 r1 = 0
139 r2 = 0 140 r2 = 0
@@ -173,35 +174,30 @@ def wmts_overview(extent,url_format): @@ -173,35 +174,30 @@ def wmts_overview(extent,url_format):
173 # 拼接瓦片 174 # 拼接瓦片
174 for r in range(r2, r1 + 1): 175 for r in range(r2, r1 + 1):
175 for c in range(c1, c2 + 1): 176 for c in range(c1, c2 + 1):
176 -  
177 url = url_format.format(lev, c, r) 177 url = url_format.format(lev, c, r)
178 - print(url)  
179 dataset: Dataset = gdal.Open(url, 0) 178 dataset: Dataset = gdal.Open(url, 0)
180 -  
181 -  
182 -  
183 if dataset.RasterCount==1: 179 if dataset.RasterCount==1:
184 continue 180 continue
185 -  
186 for b in range(1, 4): 181 for b in range(1, 4):
187 band: Band = dataset.GetRasterBand(b) 182 band: Band = dataset.GetRasterBand(b)
188 183
189 dat[(r - r2) * 256:(r - r2 + 1) * 256, (c - c1) * 256: (c - c1 + 1) * 256, 184 dat[(r - r2) * 256:(r - r2 + 1) * 256, (c - c1) * 256: (c - c1 + 1) * 256,
190 2 - (b - 1)] = band.ReadAsArray(0, 0, 256, 256) 185 2 - (b - 1)] = band.ReadAsArray(0, 0, 256, 256)
191 - cv2.imwrite("overwmtsori2.jpg", dat, [cv2.IMWRITE_JPEG_QUALITY, 30]) 186 +
  187 + cv2.imwrite("overwmtsori1.jpg", dat, [cv2.IMWRITE_JPEG_QUALITY, 30])
192 188
193 dat2 = dat[int(offy):int(offy + ynum), int(offx):int(offx + xnum), :] 189 dat2 = dat[int(offy):int(offy + ynum), int(offx):int(offx + xnum), :]
194 190
195 #裁剪 191 #裁剪
196 - cv2.imwrite("overwmts2.jpg", dat2, [cv2.IMWRITE_JPEG_QUALITY, 30]) 192 + cv2.imwrite("overwmts1.jpg", dat2, [cv2.IMWRITE_JPEG_QUALITY, 30])
197 193
198 if __name__ == '__main__': 194 if __name__ == '__main__':
199 195
200 # tms_overview([107.78981494180618, 120.43553603935675, 18.870480519260582, 25.999868757737033],"http://172.26.60.101:8820/DMap/Services/GDMap/TileServer/TMSService" 196 # tms_overview([107.78981494180618, 120.43553603935675, 18.870480519260582, 25.999868757737033],"http://172.26.60.101:8820/DMap/Services/GDMap/TileServer/TMSService"
201 # ) 197 # )
202 - # wmts_url = "http://172.26.60.101:8820/DMap/Services/GDMap/TileServer/WMTSService?layer=&style=default&tilematrixset=nativeTileMatrixSet&Service=WMTS&Request=GetTile&Version=1.0.0&Format=image%2Fpng&TileMatrix={}&TileCol={}&TileRow={}"  
203 - # wmts_overview([107.78981494180618, 120.43553603935675, 18.870480519260582, 25.999868757737033],wmts_url) 198 + wmts_url = "http://172.26.60.101:8820/DMap/Services/GDMap/TileServer/WMTSService?layer=&style=default&tilematrixset=nativeTileMatrixSet&Service=WMTS&Request=GetTile&Version=1.0.0&Format=image%2Fpng&TileMatrix={}&TileCol={}&TileRow={}"
  199 + wmts_overview([107.78981494180618, 120.43553603935675, 18.870480519260582, 25.999868757737033],wmts_url)
204 200
205 201
206 - wmts_url = "http://172.26.60.101:8820/DMap/Services/SDMap/TileServer/WMTSService?layer=&style=default&tilematrixset=nativeTileMatrixSet&Service=WMTS&Request=GetTile&Version=1.0.0&Format=image%2Fpng&TileMatrix={}&TileCol={}&TileRow={}"  
207 - wmts_overview([699385.63699999999,740828.85219999996,2507679.2525999998,2546752.4369999999],wmts_url)  
  202 + # wmts_url = "http://172.26.60.101:8820/DMap/Services/SDMap/TileServer/WMTSService?layer=&style=default&tilematrixset=nativeTileMatrixSet&Service=WMTS&Request=GetTile&Version=1.0.0&Format=image%2Fpng&TileMatrix={}&TileCol={}&TileRow={}"
  203 + # wmts_overview([699385.63699999999,740828.85219999996,2507679.2525999998,2546752.4369999999],wmts_url)
注册登录 后发表评论