feature_delete.py 2.0 KB
# coding=utf-8
# author:        4N
# createtime:    2021/8/4
# email:         nheweijun@sina.com
from app.modules.data.models import Table,Database
from app.util.component.ApiTemplate import ApiTemplate
from app.util.component.GeometryAdapter import GeometryAdapter
from app.util.component.LayerUtil import LayerUtil
from app.util.component.PGUtil import PGUtil
from osgeo.ogr import Geometry,Layer,DataSource,Feature
from app.models import DES
import json
class Api(ApiTemplate):
    api_name = "要素删除"
    def para_check(self):
        pass

    def process(self):

        res = {}
        pg_ds:DataSource = None
        try:
            table:Table = Table.query.filter_by(guid=self.para.get("guid")).one_or_none()
            if not table:
                raise Exception("数据不存在!")
            if table.table_type == 0:
                raise Exception("非空间数据!")

            database:Database = table.relate_database
            pg_ds:DataSource = PGUtil.open_pg_data_source(1,DES.decode(database.sqlalchemy_uri))
            layer:Layer = pg_ds.GetLayerByName(table.name)

            fid = int(self.para.get("fid"))
            is_success = layer.DeleteFeature(fid)
            if is_success == 0:
                res["msg"] = "删除成功!"
                res["result"] = True
            else :
                res["msg"] = "要素不存在,删除失败!"
                res["result"] = False

        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"},
        {"name": "fid",
         "in": "formData",
         "type": "string",
         "description": "fid"}
    ],
    "responses":{
        200:{
            "schema":{
                "properties":{
                }
            }
            }
        }
    }