get_meta.py
4.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
# coding=utf-8
# author: 4N
# createtime: 2020/9/4
# email: nheweijun@sina.com
import traceback
from osgeo.ogr import *
from osgeo import ogr
from flask import request
import os
import uuid
import json
from app.util.component.ApiTemplate import ApiTemplate
from app.util.component.ZipUtil import ZipUtil
from app.util.component.FileProcess import FileProcess
class Api(ApiTemplate):
api_name = "获取meta"
def process(self):
res = {}
try:
spatial_files=[]
if self.para.get("data_path"):
filename = os.path.basename(self.para.get("data_path"))
#处理
if self.para.get("data_path").endswith("zip"):
store_path = ZipUtil.unzip(self.para.get("data_path"),True)
spatial_files = FileProcess.get_spatial_file(store_path)
elif self.para.get("data_path").endswith("shp"):
data_path=self.para.get("data_path")
encoding_cpg_path = data_path.split(".shp")[0]+".cpg"
with open(encoding_cpg_path) as fd:
encoding_cpg = fd.readline().strip()
if not os.path.exists(encoding_cpg_path):
encoding_cpg=None
spatial_files.append((data_path, encoding_cpg))
elif self.para.get("data_path").endswith("gdb"):
data_path=self.para.get("data_path")
encoding_cpg=None
spatial_files.append((data_path, encoding_cpg))
else:
raise Exception("文件不符合规格!")
else:
# 保存文件
parent = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
dir_path, store_file = FileProcess.save(parent)
store_path = ZipUtil.unzip(store_file)
spatial_files = FileProcess.get_spatial_file(store_path)
file = request.files['file']
filename = file.filename.split('"')[0]
res["data"] =[]
for data_path,code in spatial_files:
one_data = self.get_meta(data_path)
one_data["encoding"]=code
one_data["filename"] = filename
res["data"].append(one_data)
res["result"] = True
except Exception as e:
raise e
return json.dumps(res,ensure_ascii=False)
def get_meta(self,data_path):
ds: DataSource = None
info = {}
layer_name = {}
info["data_path"] = os.path.normpath(data_path)
info["layer"] =layer_name
try:
# 分为shp和gdb 2种
if data_path.endswith("shp"):
info["type"]="shp"
driver: Driver = ogr.GetDriverByName("ESRI Shapefile")
ds: DataSource = driver.Open(data_path, 1)
if not ds:
raise Exception("打开数据失败!")
layer: Layer = ds.GetLayer(0)
layer_name[layer.GetName().lower()] = layer.GetName().lower()
if data_path.endswith("gdb"):
info["type"] = "gdb"
driver: Driver = ogr.GetDriverByName("OpenFileGDB")
ds: DataSource = driver.Open(data_path, 0)
if not ds:
raise Exception("打开数据失败!")
for i in range(ds.GetLayerCount()):
layer: Layer = ds.GetLayer(i)
layer_name[layer.GetName().lower()] = layer.GetName().lower()
except Exception as e :
print(traceback.format_exc())
info={}
finally:
if ds:
ds.Destroy()
return info
api_doc={
"tags":["IO接口"],
"parameters":[
{"name": "file",
"in": "formData",
"type":"file",
"description":"数据文件zip压缩包"},
{"name": "data_path",
"in": "formData",
"type": "string",
"description": "数据文件路径"}
],
"responses":{
200:{
"schema":{
"properties":{
}
}
}
}
}