table_vacuate_delete.py 2.8 KB
# author:        4N
# createtime:    2021/1/27
# email:         nheweijun@sina.com


from ..models import Table,DES,db,TableVacuate
from app.util.component.ApiTemplate import ApiTemplate
from app.util.component.PGUtil import PGUtil
import configure
from osgeo.ogr import DataSource
from flask import current_app

class Api(ApiTemplate):
    api_name = "删除抽稀表"
    def process(self):

        res = {}
        res["data"] = {}
        pg_ds = None
        va_ds = None
        try:
            table_guid = self.para.get("guid")
            table: Table = Table.query.filter_by(guid=table_guid).one_or_none()
            grid = None
            if not self.para.get("grid"):
                raise Exception("请输入grids参数!")
            else:
                grid = float(self.para.get("grid"))

            if not table:
                raise Exception("数据不存在!")

            # 判断图层是否存在
            pg_ds :DataSource= PGUtil.open_pg_data_source(0,DES.decode(table.relate_database.sqlalchemy_uri))

            if configure.VACUATE_DB_URI:
                va_ds: DataSource = PGUtil.open_pg_data_source(1, configure.VACUATE_DB_URI)
            else:
                va_ds: DataSource = PGUtil.open_pg_data_source(1,DES.decode(table.relate_database.sqlalchemy_uri))


            layer = pg_ds.GetLayerByName(table.name)

            if not layer:
                raise Exception("图层不存在!")

            #删除数据
            tvs = db.session.query(TableVacuate).filter_by(pixel_distance=grid, table_guid=table.guid).all()
            if not tvs:
                raise Exception("数据不存在!")
            for tv in tvs :
                db.session.delete(tv)
                try:
                    va_ds.DeleteLayer(tv.name)
                except Exception as e :
                    current_app.logger.warning("抽稀图层不存在!")
            db.session.commit()
            res["result"] = True

        except Exception as e:
            raise e
        finally:
            if pg_ds:
                pg_ds.Destroy()
            if va_ds:
                va_ds.Destroy()
        return res


    api_doc = {
        "tags": ["管理接口"],
        "parameters": [
            {"name": "guid",
             "in": "formData",
             "type": "string",
             "description": "表guid", "required": "true"},
            {"name": "grid",
             "in": "formData",
             "type": "string",
             "description": "网格大小", "required": "true"}
        ],
        "responses": {
            200: {
                "schema": {
                    "properties": {
                    }
                }
            }
        }
    }