table_vacuate.py 16.3 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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
# author:        4N
# createtime:    2021/1/27
# email:         nheweijun@sina.com


import datetime
import traceback
import multiprocessing
import uuid
import configure

from ..models import Table, Database, Task,db,TableVacuate,Process,DES

from app.util.component.ApiTemplate import ApiTemplate
from app.util.component.StructurePrint import StructurePrint
from app.util.component.PGUtil import PGUtil
from app.util.component.VacuateConf import VacuateConf
from app.util.component.GeometryAdapter import GeometryAdapter

from app.util.component.TaskController import TaskController
from app.util.component.TaskWriter import TaskWriter
from osgeo.ogr import DataSource,Layer,Geometry
from osgeo import ogr
from app.util.component.UserCheck import UserCheck
import copy


class Api(ApiTemplate):

    api_name = "数据抽稀"

    def process(self):

        res = {}
        res["data"] = {}
        db_session = 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("数据不存在!")

            #验证权限
            UserCheck.verify(table.creator)

            pg_ds :DataSource= PGUtil.open_pg_data_source(0,DES.decode(table.relate_database.sqlalchemy_uri))
            layer = pg_ds.GetLayerByName(table.name)
            # 判断图层是否存在
            if not layer:
                raise Exception("图层不存在!")

            # 判断用户权限
            db_tuple = PGUtil.get_info_from_sqlachemy_uri(DES.decode(table.relate_database.sqlalchemy_uri))
            if not PGUtil.check_table_privilege(table.name,"SELECT",db_tuple[0],pg_ds):
                raise  Exception("用户{}对表{}没有select权限!".format(db_tuple[0],table.name))

            if pg_ds:
                pg_ds.Destroy()

            if Task.query.filter_by(table_guid=table_guid,state=0).one_or_none() or table.is_vacuate==2:
                res["result"] = False
                res["msg"] = "矢量金字塔构建中!"
                return res

            if table.table_type==0:
                res["result"] = False
                res["msg"] = "非空间表!"
                return res


            # 初始化task
            task_guid = uuid.uuid1().__str__()

            task = Task(guid=task_guid,
                        name="矢量金字塔 | {}".format(table.name),
                        table_guid=table_guid,
                        create_time=datetime.datetime.now(),
                        state=0,
                        task_type=2,
                        creator=self.para.get("creator"),
                        file_name=None,
                        database_guid=table.database_guid,
                        process="构建中",
                        # task_pid= vacuate_process.pid
                        # parameter=",".join([str(x) for x in ref_grids])
                        )

            db.session.add(task)
            db.session.commit()

            vacuate_process = multiprocessing.Process(target=self.task,args=(table,task_guid))
            vacuate_process.start()

            res["msg"] = "矢量金字塔构建已提交!"
            res["data"] = task_guid
            res["result"] = True

        except Exception as e:
            raise e
        finally:
            if db_session:
                db_session.close()
        return res

    def task(self,table,task_guid):

        task_writer = None
        pg_session = None
        pg_ds = None
        vacuate_process = None
        try:

            task_writer = TaskWriter(task_guid)
            pid = multiprocessing.current_process().pid
            task_writer.update_task({"task_pid":pid})

            #任务控制,等待执行
            TaskController.wait(task_guid)

            task_writer.update_table(table.guid,{"is_vacuate": 2, "update_time": datetime.datetime.now()})
            task_writer.update_task({"state":2,"process":"构建中"})
            task_writer.update_process("开始构建...")

            database = task_writer.session.query(Database).filter_by(guid=table.database_guid).one_or_none()
            database_sqlalchemy_uri = str(database.sqlalchemy_uri)

            pg_session = PGUtil.get_db_session(DES.decode(database.sqlalchemy_uri))
            pg_ds :DataSource= PGUtil.open_pg_data_source(0,DES.decode(database.sqlalchemy_uri))

            #删除原有数据
            tvs = task_writer.session.query(TableVacuate).filter_by(table_guid=table.guid).all()
            for tv in tvs :
                task_writer.session.delete(tv)
            task_writer.session.commit()


            # 创建抽稀过程
            options = ["OVERWRITE=yes", "GEOMETRY_NAME={}".format(PGUtil.get_geo_column(table.name,pg_session)),
                       "PRECISION=NO"]

            layer = pg_ds.GetLayerByName(table.name)

            vacuate_process:VacuateProcess = VacuateProcess(layer, table.guid, options,database_sqlalchemy_uri)

            count = 0
            for feature in layer:
                geo = feature.GetGeometryRef()
                #插入抽稀图层
                if geo is not None:
                    vacuate_process.vacuate(geo,feature)
                count += 1
                if count%10000==0:
                    StructurePrint().print("{}图层已抽稀{}个对象".format(table.name, count))

            #vacuate_process.set_vacuate_count()

            #新增
            if configure.VACUATE_DB_URI:
                user, passwd, host, port, datab = PGUtil.get_info_from_sqlachemy_uri(configure.VACUATE_DB_URI)
            else:
                user, passwd, host, port, datab = PGUtil.get_info_from_sqlachemy_uri(DES.decode(database_sqlalchemy_uri))
            connectstr = "hostaddr={} port={} dbname='{}' user='{}' password='{}'".format(host, port, datab, user,
                                                                                                  passwd)
            for l in range(vacuate_process.max_level):
                lev = vacuate_process.t_grid_size.index(vacuate_process.this_gridsize[l])

                table_vacuate = TableVacuate(guid=uuid.uuid1().__str__(),
                                             table_guid=table.guid,
                                             level=lev,
                                             name=vacuate_process.vacuate_layers[l].GetName(),
                                             pixel_distance=vacuate_process.this_gridsize[l],
                                             connectstr=DES.encode(connectstr))
                task_writer.session.add(table_vacuate)

            task_writer.update_task({"state":1,"update_time":datetime.datetime.now(),"process": "构建完成"})
            task_writer.update_table(table.guid, {"is_vacuate": 1, "update_time": datetime.datetime.now()})
            task_writer.update_process("构建完成!")

        except Exception as e:
            try:
                task_writer.update_task({"state": -1,"update_time":datetime.datetime.now(),"process": "构建失败"})
                task_writer.update_table(table.guid, {"is_vacuate": 0, "update_time": datetime.datetime.now()})
                task_writer.update_process( e.__str__())
                task_writer.update_process("任务中止!")
                task_writer.session.commit()

                if vacuate_process:
                    vacuate_process.rollback()

                StructurePrint().print(traceback.format_exc())
            except Exception as ee:
                StructurePrint().print(traceback.format_exc())
        finally:
            try:
                task_writer.close()
                if vacuate_process:
                    vacuate_process.end()
                if pg_session:
                    pg_session.close()
                if pg_ds:
                    pg_ds.Destroy()

            except Exception as e:
                StructurePrint.print(traceback.format_exc())
    api_doc = {
        "tags": ["管理接口"],
        "parameters": [
            {"name": "guid",
             "in": "formData",
             "type": "string",
             "description": "表guid", "required": "true"},
            {"name": "creator",
             "in": "formData",
             "type": "string",
             "description": "创建者"}
        ],
        "responses": {
            200: {
                "schema": {
                    "properties": {
                    }
                }
            }
        }
    }


class VacuateProcess:

    max_level=0
    fill_dict={}
    vacuate_layers={}
    vacuate_layers_gridsize={}
    pg_ds_dict = {}
    # 图层要素大于5W才抽稀
    least_vacuate_count = VacuateConf.least_vacuate_count

    extent=[]
    is_spatial=False

    lonlat_gridsize = VacuateConf.lonlat_gridsize
    project_gridsize = VacuateConf.project_gridsize

    # 该抽稀过程使用的grid_size
    t_grid_size = []

    # 该抽稀过程的抽稀网格
    this_gridsize=[]


    def __init__(self,layer:Layer,table_guid, options,sqlalchemy_uri):

        #是空间图层才初始化
        if layer.GetExtent()[0] > 0 or layer.GetExtent()[0] < 0:

            self.is_spatial=True

            # 判断需要抽稀多少级

            lc = layer.GetFeatureCount()
            extent = layer.GetExtent()
            self.extent=extent

            #判断疏密程度
            p_x = (extent[1]-extent[0])/10.0
            p_y = (extent[3] - extent[2]) / 10.0
            fill_precent=0
            StructurePrint().print("判断疏密")
            for ix in range(10):
                for iy in range(10):
                    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)

                    layer.SetSpatialFilter(None)
                    layer.SetSpatialFilter(poly)
                    layer.ResetReading()
                    if layer.GetNextFeature():
                        fill_precent += 1

            print(fill_precent)
            StructurePrint().print("判断疏密结束")

            layer.SetSpatialFilter(None)
            layer.ResetReading()


            if extent[0]>180:
                self.t_grid_size=self.project_gridsize
            else:
                self.t_grid_size = self.lonlat_gridsize

            for grid_size in self.t_grid_size:
                # 最少抽稀个数
                if lc > self.least_vacuate_count:
                    # 网格数至少大于
                    if ((extent[1] - extent[0]) * (extent[3] - extent[2])) / (grid_size**2)>self.least_vacuate_count:
                        # 要素数量大于网格数量
                        # 要考虑图层的疏密程度,original_density*(100.0/fill_precent) 为疏密指数
                        if lc * VacuateConf.original_density * (100.0/fill_precent)>((extent[1] - extent[0])*(extent[3] - extent[2]))/(grid_size**2) :
                            print(grid_size)
                            self.this_gridsize.append(grid_size)
                            self.max_level += 1



            # 创建抽稀ds
            for l in range(self.max_level):
                if configure.VACUATE_DB_URI:
                    pg_ds_l: DataSource = PGUtil.open_pg_data_source(1, configure.VACUATE_DB_URI)
                else:
                    pg_ds_l: DataSource = PGUtil.open_pg_data_source(1, DES.decode(sqlalchemy_uri))
                pg_ds_l.StartTransaction()
                self.pg_ds_dict[l] = pg_ds_l

            # 生成抽稀图层
            options = options[1:]
            options.append("OVERWRITE=yes")
            options.append("LAUNDER=no")

            schema = layer.schema
            # 增加统计字段
            # schema.append(ogr.FieldDefn("_dcigrid_count_", ogr.OFTInteger))
            # schema.append(ogr.FieldDefn("_dcigrid_name_", ogr.OFTString))

            for l in range(self.max_level):
                this_grid_len = self.this_gridsize[l]

                self.vacuate_layers_gridsize[l] = this_grid_len

                pg = self.pg_ds_dict[l]

                grid_name = str(this_grid_len)
                if this_grid_len<1:
                    grid_name = str(this_grid_len).split(".")[-1]
                    if this_grid_len.__eq__(0.00008):
                        grid_name = "00008"

                # 抽稀图层是点面混合的
                # 抽稀表有固定的命名规则
                # 抽稀表一定要覆盖


                print("{}:{}".format(self.t_grid_size.index(this_grid_len),this_grid_len))


                v_ln = "z{}_vacuate_{}_{}".format(table_guid, self.t_grid_size.index(this_grid_len), grid_name)
                vl = pg.CreateLayer(v_ln, layer.GetSpatialRef(),ogr.wkbUnknown, options)
                # 抽稀表需要属性
                vl.CreateFields(schema)
                self.vacuate_layers[l] = vl

        else:
            pass


    def vacuate(self,g,feature):

        if self.is_spatial:
            feat = copy.copy(feature)
            # 插入到所有抽稀图层中
            for level in range(self.max_level):

                center: Geometry = g.Centroid()

                extent = g.GetEnvelope()
                long_extent= extent[1]-extent[0]
                lat_extent = extent[3]-extent[2]

                this_grid_len =self.vacuate_layers_gridsize[level]

                row = int((center.GetY() - self.extent[2]) / this_grid_len)
                col = int((center.GetX() - self.extent[0]) / this_grid_len)
                key = "{}.{}.{}".format(level, row, col)

                if not self.fill_dict.get(key):
                    self.fill_dict[key] = 0
                if self.fill_dict[key] == 0:

                    vacuate_layer: Layer = self.vacuate_layers.get(level)


                    #feat = ogr.Feature(vacuate_layer.GetLayerDefn())
                    # 如果图形比网格小,直接存储其中心点
                    if this_grid_len>long_extent and this_grid_len>lat_extent:
                        feat.SetGeometry(center)
                    else:
                        feat.SetGeometry(g)

                    # 复制旧feature属性
                    # field_dict = feature.items()
                    # for field_name in field_dict:
                    #     feat.SetField(field_name, field_dict[field_name])
                    #
                    # feat.SetField("_dcigrid_name_",".".join(key.split(".")[1:]))

                    vacuate_layer.CreateFeature(feat)

                    self.fill_dict[key] += 1
                #超大的还有机会
                elif long_extent > 10 * this_grid_len or lat_extent > 10 * this_grid_len:
                    vacuate_layer: Layer = self.vacuate_layers.get(level)

                    # feat = ogr.Feature(vacuate_layer.GetLayerDefn())

                    feat.SetGeometry(g)

                    # 复制旧feature属性
                    # field_dict = feature.items()
                    # for field_name in field_dict:
                    #     feat.SetField(field_name, field_dict[field_name])
                    # feat.SetField("_dcigrid_name_",".".join(key.split(".")[1:]))

                    vacuate_layer.CreateFeature(feat)
                    self.fill_dict[key] += 1
                else:
                    self.fill_dict[key] += 1

    def set_vacuate_count(self):
        if self.is_spatial:
            # 插入到所有抽稀图层中
            for level in range(self.max_level):
                vacuate_layer: Layer = self.vacuate_layers.get(level)
                for feat in vacuate_layer:
                    key = "{}.{}".format(level,feat.GetField("_dcigrid_name_"))
                    feat.SetField("_dcigrid_count_",self.fill_dict.get(key))
                    vacuate_layer.SetFeature(feat)

    def end(self):
        for pg in self.pg_ds_dict.values():
            pg.Destroy()

    def rollback(self):
        for pg in self.pg_ds_dict.values():
            pg.RollbackTransaction()