def new(self, flag): if flag == 0: compression = zipfile.ZIP_DEFLATED elif flag == 1: compression = zipfile.ZIP_STORED CFile.check_and_create_directory(self.__file_name__) self.__zip_obj__ = zipfile.ZipFile(self.__file_name__, 'a', compression)
def save_file(self, filename, encoding=Encoding_UTF8): """ 通过给定的xml文件名, 对xml对象进行初始化 :param filename: :param encoding: :return: """ CFile.check_and_create_directory(filename) self.__xml_tree = etree.ElementTree(self.__xml_root_node) self.__xml_tree.write(filename, encoding=encoding, xml_declaration=True)
def str_2_file(cls, json_content: str, filename: str): if not CFile.check_and_create_directory(filename): raise PathNotCreateException(filename) json = CJson() json.load_json_text(json_content) json.to_file(filename)
def str_2_file(cls, xml_content: str, filename: str) -> str: if not CFile.check_and_create_directory(filename): raise PathNotCreateException(filename) xml_obj = CXml() xml_obj.load_xml(xml_content) xml_obj.to_file(filename, cls.Encoding_UTF8)
def blob2file(self, row: int, name: str, file_name: str): if self._data is None: return value = self._value_by_name(row, name) if value is None: return if not CFile.check_and_create_directory(file_name): raise PathNotCreateException(CFile.file_path(file_name)) f = open(file_name, 'wb') try: f.write(value) finally: f.close()
def extract_all(self, target_path): CFile.check_and_create_directory(target_path) self.__zip_obj__.extractall(target_path)
def extract_file(self, file_name, target_path): CFile.check_and_create_directory(target_path) self.__zip_obj__.extract(file_name, path=target_path)
def create_view(self, image_path: str, browse_path: str, thumb_path: str, geotiff_path: str): """ 由影像文件生成拇指图(jpg)、快视图(png)。 对多波段影像、单波段影像,波段数据类型为Byte(8位无符号整型)、UInt16(16位无符号整型)、Int16(16位有符号整型)的影像,均可进行转换。 注意:转换过程中重采样会生成影像临时文件(tiff),保留原影像基本信息,仅压缩影像尺寸。 快视图:500*xxx(width固定为500,height根据对应比例计算) 拇指图:50*50(按固定尺寸生成) :param image_path: 单个影像文件 :param browse_path: 指定的快视图地址 :param thumb_path: 指定的拇指图地址 :param geotiff_path: 临时文件存放地址 :return: """ # 注册所有驱动,支持中文 gdal.AllRegister() gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES") # <editor-fold desc="重采样"> # 获取并检查影像文件 source_ds = gdal.Open(image_path, gdalconst.GA_ReadOnly) try: if source_ds is None: CLogger().warning('文件[{0}]打开失败!请检查!'.format(image_path)) return CResult.merge_result(self.Failure, '文件[{0}]打开失败!请检查!'.format(image_path)) band_count = source_ds.RasterCount if band_count == 0: CLogger().warning('文件[{0}]不存在波段信息!请检查!'.format(image_path)) return CResult.merge_result(self.Failure, '文件[{0}]不存在波段信息!请检查!'.format(image_path)) # 获取影像的大小、投影、仿射变换参数、第一个波段信息、数据类型 cols = source_ds.RasterXSize rows = source_ds.RasterYSize projection = source_ds.GetProjection() geo_transform = list(source_ds.GetGeoTransform()) in_band1 = source_ds.GetRasterBand(1) data_type = in_band1.DataType # 获取MEM的驱动(GDAL内存文件)、GTiff驱动 driver_mem = gdal.GetDriverByName('MEM') driver_GeoTiff = gdal.GetDriverByName('GTiff') # 制作拇指图 # 设置拇指图尺寸 thumb_cols = 100 # thumb_rows = 50 scale = thumb_cols / cols thumb_rows = rows * scale thumb_rows = CUtils.to_integer(thumb_rows) scale1 = thumb_cols / cols scale2 = thumb_rows / rows geo_transform[1] = geo_transform[1] / scale1 geo_transform[5] = geo_transform[5] / scale2 # 配置拇指图内存临时文件thumb_ds thumb_ds = driver_mem.Create("", xsize=thumb_cols, ysize=thumb_rows, bands=band_count, eType=data_type) try: thumb_ds.SetProjection(projection) thumb_ds.SetGeoTransform(geo_transform) for index in range(band_count): in_band = source_ds.GetRasterBand(index + 1) data = in_band.ReadAsArray(buf_xsize=thumb_cols, buf_ysize=thumb_rows) out_band = thumb_ds.GetRasterBand(index + 1) out_band.WriteArray(data) out_band.FlushCache() out_band.ComputeBandStats(False) # 制作快视图 # 设置快视图尺寸 browse_cols = 1000 scale = browse_cols / cols browse_rows = rows * scale browse_rows = CUtils.to_integer(browse_rows) geo_transform[1] = geo_transform[1] / scale geo_transform[5] = geo_transform[5] / scale # 如果已存在同名影像,则删除之 if os.path.exists(geotiff_path) and os.path.isfile(geotiff_path): # 如果已存在同名影像 os.remove(geotiff_path) # 配置快视图内存临时文件browse_ds CFile.check_and_create_directory(geotiff_path) # 创建目录 browse_ds = driver_GeoTiff.Create(geotiff_path, xsize=browse_cols, ysize=browse_rows, bands=band_count, eType=data_type) if browse_ds is None: CLogger().warning('文件[{0}]处理失败!GeoTiff驱动无法创建, 生成快视图、拇指图失败!'.format(image_path)) return CResult.merge_result(self.Failure, '文件[{0}]处理失败!GeoTiff驱动无法创建, 生成快视图、拇指图失败!'.format(image_path)) try: browse_ds.SetProjection(projection) browse_ds.SetGeoTransform(geo_transform) for index in range(band_count): in_band = source_ds.GetRasterBand(index + 1) data = in_band.ReadAsArray(buf_xsize=browse_cols, buf_ysize=browse_rows) out_band = browse_ds.GetRasterBand(index + 1) out_band.WriteArray(data) out_band.FlushCache() out_band.ComputeBandStats(False) # </editor-fold> # 由重采样后的影像制作快视图、拇指图 browse_path = self.image2view(browse_ds, browse_path) thumb_path = self.image2view(thumb_ds, thumb_path) if os.path.exists(thumb_path) and os.path.exists(browse_path) and os.path.exists(geotiff_path): CLogger().info('文件[{0}]的快视图、拇指图制作成功!'.format(image_path)) return CResult.merge_result(self.Success, '文件[{0}]快视图、拇指图制作成功!'.format(image_path)) else: CLogger().warning('文件[{0}]处理失败!未生成相应的快视图、拇指图!'.format(image_path)) return CResult.merge_result(self.Failure, '文件[{0}]处理失败!未生成相应的快视图、拇指图!'.format(image_path)) finally: del browse_ds finally: del thumb_ds except Exception as error: CLogger().warning('影像数据的快视图、拇指图信息处理出现异常,错误信息为:{0}'.format(error.__str__())) return CResult.merge_result(self.Failure, '影像数据的快视图、拇指图信息处理出现异常,错误信息为:{0}!'.format(error.__str__())) finally: del source_ds
def image2view(self, target_ds, view_path: str) -> str: """ 影像文件转jpg或png 对波段数据类型为Byte(8位无符号整型)的影像,可采用gdal.Translate()方法直接进行文件格式转换。 对波段数据类型为UInt16(16位无符号整型)、Int16(16位有符号整型)的影像: 由于JPEG不支持16位影像的转换,且PNG转换效果非常不理想,图像轮廓模糊。 因此对16位影像采用百分比截断的方法压缩至0~255的范围,改变了像素深度,降低到8位。 该方法生成的快视图的转换效果与gdal.Translate()方法生成的相比,图像轮廓清晰,可辨识度高。 注:gdal == 3.1.3 :param target_ds: 影像临时文件的数据集 :param view_path: 快视图或拇指图文件地址 :return: """ cols = target_ds.RasterXSize rows = target_ds.RasterYSize band_count = target_ds.RasterCount band = target_ds.GetRasterBand(1) data_type_name = gdal.GetDataTypeName(band.DataType) # 检查影像位深度(波段数据类型) if CUtils.equal_ignore_case(data_type_name, 'Byte'): # 对波段数据类型为Byte(8位无符号整型)的影像进行转换 if CFile.check_and_create_directory(view_path): if CFile.file_ext(view_path).lower() == 'jpg': out = gdal.Translate(view_path, target_ds, format='JPEG') elif CFile.file_ext(view_path).lower() == 'png': out = gdal.Translate(view_path, target_ds, format='PNG') else: # 对波段数据类型为UInt16(16位无符号整型)、Int16(16位有符号整型)的影像进行转换 # 检查影像波段数并读取 if band_count >= 3: bandsOrder = [3, 2, 1] data = np.empty([rows, cols, 3], dtype=float) for i in range(3): band = target_ds.GetRasterBand(bandsOrder[i]) data1 = band.ReadAsArray(0, 0, cols, rows) data[:, :, i] = data1 if CFile.check_and_create_directory(view_path): # 百分比截断压缩影像,将像元取值限定在0~255 lower_percent = 0.6 higher_percent = 99.4 n = data.shape[2] out = np.zeros_like(data, dtype=np.uint8) for i in range(n): a = 0 b = 255 c = np.percentile(data[:, :, i], lower_percent) d = np.percentile(data[:, :, i], higher_percent) t = a + (data[:, :, i] - c) * (b - a) / (d - c) t[t < a] = a t[t > b] = b out[:, :, i] = t outImg = Image.fromarray(np.uint8(out)) outImg.save(view_path) else: data = np.empty([rows, cols], dtype=float) band = target_ds.GetRasterBand(1) data1 = band.ReadAsArray(0, 0, cols, rows) data[:, :] = data1 if CFile.check_and_create_directory(view_path): # 百分比截断压缩影像,将像元取值限定在0~255 lower_percent = 0.6 higher_percent = 99.4 out = np.zeros_like(data, dtype=np.uint8) a = 0 b = 255 c = np.percentile(data[:, :], lower_percent) d = np.percentile(data[:, :], higher_percent) t = a + (data[:, :] - c) * (b - a) / (d - c) t[t < a] = a t[t > b] = b out[:, :] = t outImg = Image.fromarray(np.uint8(out)) outImg.save(view_path) del target_ds del out return view_path
def get_metadata_2_file(self, file_name_with_path: str): # print('你的任务: 将文件{0}的元数据信息, 提取出来, 存储到文件{1}中'.format(self.__file_name_with_path__, file_name_with_path)) raster_ds = None json_raster = None # result_success = abs(self.Success) # 成功的标记,元数据json中的为1,而系统常量为-1,暂采用绝对值 result_success = self.Success # 成功的标记-1 gdal.AllRegister() gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES") # 定义栅格数据的json对像 json_raster = CJson() try: # 打开影像数据集 raster_ds = gdal.Open(self.__file_name_with_path__, gdal.GA_ReadOnly) if raster_ds is None: message = '文件[{0}]打开失败!'.format(self.__file_name_with_path__) json_raster.set_value_of_name('result', self.Failure) json_raster.set_value_of_name('message', message) # 判断路径是否存在,不存在则创建 if CFile.check_and_create_directory(file_name_with_path): json_raster.to_file(file_name_with_path) return CResult.merge_result(CResult.Failure, '文件[{0}]打开失败!'.format(self.__file_name_with_path__)) # 基本信息 driver = raster_ds.GetDriver() if driver is None: message = '文件[{0}]读取驱动失败!'.format(self.__file_name_with_path__) json_raster.set_value_of_name('result', self.Failure) json_raster.set_value_of_name('message', message) # 判断路径是否存在,不存在则创建 if CFile.check_and_create_directory(file_name_with_path): json_raster.to_file(file_name_with_path) return CResult.merge_result(CResult.Failure, '文件[{0}]读取驱动失败!'.format(self.__file_name_with_path__)) # 定义driver子节点,并添加到矢量json对象中 json_driver = CJson() json_driver.set_value_of_name('longname', driver.LongName) json_driver.set_value_of_name('shortname', driver.ShortName) json_raster.set_value_of_name('driver', json_driver.json_obj) file_list = raster_ds.GetFileList() if file_list is None: message = '文件[{0}]读取文件列表失败!'.format(self.__file_name_with_path__) json_raster.set_value_of_name('result', self.Failure) json_raster.set_value_of_name('message', message) # 判断路径是否存在,不存在则创建 if CFile.check_and_create_directory(file_name_with_path): json_raster.to_file(file_name_with_path) return CResult.merge_result(CResult.Failure, '文件[{0}]读取文件列表失败!'.format(self.__file_name_with_path__)) # 定义files子对象 json_files = [file_list[0]] for i in range(len(file_list)): if i > 0 and file_list[i] is not None: json_files.append(file_list[i]) json_raster.set_value_of_name('files', json_files) image_size_x = raster_ds.RasterXSize image_size_y = raster_ds.RasterYSize if image_size_x is None and image_size_y is None: message = '文件[{0}]读取文件大小失败!'.format(self.__file_name_with_path__) json_raster.set_value_of_name('result', self.Failure) json_raster.set_value_of_name('message', message) # 判断路径是否存在,不存在则创建 if CFile.check_and_create_directory(file_name_with_path): json_raster.to_file(file_name_with_path) return CResult.merge_result(CResult.Failure, '文件[{0}]读取文件大小失败!'.format(self.__file_name_with_path__)) # 定义size子节点 json_size = CJson() json_size.set_value_of_name('width', image_size_x) json_size.set_value_of_name('height', image_size_y) json_raster.set_value_of_name('size', json_size.json_obj) # 投影信息 projection = raster_ds.GetProjection() # 定义coordinate子对象 json_coordinate = CJson() if not CUtils.equal_ignore_case(projection, ''): spatial = osr.SpatialReference() if spatial.ImportFromWkt(projection) == gdal.CE_None: proj_wkt = spatial.ExportToWkt() json_coordinate.set_value_of_name('wkt', proj_wkt) proj4 = spatial.ExportToProj4() json_coordinate.set_value_of_name('proj4', proj4) spatial.MorphToESRI() esri = spatial.ExportToWkt() json_coordinate.set_value_of_name('esri', esri) else: json_coordinate.set_value_of_name('wkt', projection) json_coordinate.set_value_of_name('proj4', None) json_coordinate.set_value_of_name('esri', None) spatial = None else: json_coordinate.set_value_of_name('wkt', None) json_coordinate.set_value_of_name('proj4', None) json_coordinate.set_value_of_name('esri', None) json_raster.set_value_of_name('coordinate', json_coordinate.json_obj) # 仿射变换信息 geo_transform = raster_ds.GetGeoTransform() if geo_transform is not None: # 定义origin、pixelsize、boundingbox子节点 if geo_transform[2] == 0 and geo_transform[4] == 0: (json_origin, json_pixel, json_bounding) = self.get_geotramsform_by_raster(geo_transform, image_size_x, image_size_y) json_raster.set_value_of_name('origin', json_origin.json_obj) json_raster.set_value_of_name('pixelsize', json_pixel.json_obj) json_raster.set_value_of_name('boundingbox', json_bounding.json_obj) else: (json_geo, json_bounding) = self.get_geotramsform_by_raster(geo_transform, image_size_x, image_size_y) json_raster.set_value_of_name('geotransform', json_geo.json_obj) json_raster.set_value_of_name('boundingbox', json_bounding.json_obj) else: json_bounding = CJson() json_bounding.set_value_of_name('result', 0) json_raster.set_value_of_name('boundingbox', json_bounding.json_obj) # wgs84坐标系转换 json_wgs84 = self.transform_to_WGS84(geo_transform, image_size_x, image_size_y, projection) json_raster.set_value_of_name('wgs84', json_wgs84.json_obj) # GCPs信息 # 注意:这个节点没测试过,可能有bug gcp_count = raster_ds.GetGCPCount() if gcp_count > 0: gcp_projection = raster_ds.GetGCPProjection() if gcp_projection is None: message = '文件[{0}]读取GCP信息失败!'.format(self.__file_name_with_path__) json_raster.set_value_of_name('result', self.Failure) json_raster.set_value_of_name('message', message) # 判断路径是否存在,不存在则创建 if CFile.check_and_create_directory(file_name_with_path): json_raster.to_file(file_name_with_path) return CResult.merge_result(CResult.Failure, '文件[{0}]读取GCP信息失败!'.format(self.__file_name_with_path__)) # 定义gcp_projection、gcp子对象 json_gcp_projection, gcp_list = self.get_gcp_by_raster(gcp_count, gcp_projection, raster_ds) json_raster.set_value_of_name('gcp_projection', json_gcp_projection.json_obj) json_raster.set_value_of_name('gcp', gcp_list) # metadata信息,定义metadata子节点 metadata = raster_ds.GetMetadata() metadata_list = [] if len(metadata) > 0: for i in metadata: if metadata[i] is not None: metadata_item = metadata[i] metadata_list.append(metadata_item) json_raster.set_value_of_name('metadata', metadata_list) # 定义image_structure_metadata子节点 image_metadata = raster_ds.GetMetadata('IMAGE_STRUCTURE') image_metadata_list = [] if len(image_metadata) > 0: for i in image_metadata: if image_metadata[i] is not None: image_metadata_item = image_metadata[i] image_metadata_list.append(image_metadata_item) json_raster.set_value_of_name('image_structure_metadata', image_metadata_list) # 定义subdatasets子节点 sub_metadata = raster_ds.GetMetadata('SUBDATASETS') if len(sub_metadata) > 0: sub_data = self.get_other_metadata_by_raster(sub_metadata) json_raster.set_value_of_name('subdatasets', sub_data.json_obj) # 定义geolocation子节点 geo_metadata = raster_ds.GetMetadata('GEOLOCATION') if len(geo_metadata) > 0: geo_data = self.get_other_metadata_by_raster(geo_metadata) json_raster.set_value_of_name('geolocation', geo_data.json_obj) # 定义rpc子节点 rpc_metadata = raster_ds.GetMetadata('RPC') if len(rpc_metadata) > 0: rpc_data = self.get_other_metadata_by_raster(rpc_metadata) json_raster.set_value_of_name('rpc', rpc_data.json_obj) # 角坐标信息,定义corner_coordinates子节点 json_corner = self.get_corner_by_raster(image_size_x, image_size_y) json_raster.set_value_of_name('corner_coordinates', json_corner.json_obj) # 波段信息 band_count = raster_ds.RasterCount if band_count == 0: message = '文件[{0}]文件不存在波段信息!'.format(self.__file_name_with_path__) json_raster.set_value_of_name('result', self.Failure) json_raster.set_value_of_name('message', message) # 判断路径是否存在,不存在则创建 if CFile.check_and_create_directory(file_name_with_path): json_raster.to_file(file_name_with_path) return CResult.merge_result(CResult.Failure, '文件[{0}]不存在波段信息!'.format(self.__file_name_with_path__)) # 定义bands子节点 band_list = self.get_bands_by_raster(band_count, raster_ds) json_raster.set_value_of_name('bands', band_list) # 定义pyramid子节点 band = raster_ds.GetRasterBand(1) overviews = band.GetOverviewCount() if overviews > 0: pyramid = -1 else: pyramid = 0 band = None json_raster.set_value_of_name('pyramid', pyramid) # 原始gdal_info # json_info = gdal.Info(raster_ds, format='json') # json_raster.set_value_of_name('info', json_info) # 定义result子节点 json_raster.set_value_of_name('result', result_success) # 判断路径是否存在,不存在则创建 if CFile.check_and_create_directory(file_name_with_path): json_raster.to_file(file_name_with_path) CLogger().info('文件[{0}]元数据信息读取成功!'.format(self.__file_name_with_path__)) return CResult.merge_result(CResult.Success, '文件[{0}]元数据信息读取成功!'.format(self.__file_name_with_path__)) except Exception as error: CLogger().info('get_metadata_2_file解析错误:{0}'.format(error)) message = 'get_metadata_2_file解析错误:文件:{0},错误信息为{1}'.format(self.__file_name_with_path__, error) json_raster.set_value_of_name('result', self.Failure) json_raster.set_value_of_name('message', message) # 判断路径是否存在,不存在则创建 if CFile.check_and_create_directory(file_name_with_path): json_raster.to_file(file_name_with_path) return CResult.merge_result(CResult.Failure, '文件[{0}]读取异常!{1}'.format(self.__file_name_with_path__, error.__str__())) finally: del raster_ds
def to_file(self, filename): if not CFile.check_and_create_directory(filename): raise PathNotCreateException(CFile.file_path(filename)) demjson.encode_to_file(filename, self.__json_obj, overwrite=True)
def get_metadata_2_file(self, file_name_with_path: str): # print('你的任务: 将文件{0}的元数据信息, 提取出来, 存储到文件{1}中'.format(self.__file_name_with_path__, file_name_with_path)) vector_ds = None json_vector = None # os.environ['PROJ_LIB'] = r'C:\APP\Python\Python38\Lib\site-packages\osgeo\data\proj' 环境变量中设置 # result_success = abs(self.Success) # 成功的标记,元数据json中的为1,而系统常量为-1,暂采用绝对值 result_success = self.Success # 成功的标记-1 gdal.SetConfigOption("GDAL_FILENAME_IS_UTF8", "YES") gdal.SetConfigOption("SHAPE_ENCODING", "GBK") # gdal.SetConfigOption("SHAPE_ENCODING", "UTF-8") # 定义矢量的json对象 json_vector = CJson() mdb_flag = False vector_ds = ogr.Open(self.__file_name_with_path__) if vector_ds is None: vector_ds = ogr.Open(self.__file_name_with_path__.encode('gbk'), 0) mdb_flag = True if vector_ds is None: message = '文件[{0}]打开失败!'.format(self.__file_name_with_path__) json_vector.set_value_of_name('result', self.Failure) json_vector.set_value_of_name('message', message) # 判断路径是否存在,不存在则创建 if CFile.check_and_create_directory(file_name_with_path): json_vector.to_file(file_name_with_path) return CResult.merge_result( CResult.Failure, '文件[{0}]打开失败!'.format(self.__file_name_with_path__)) try: layer_count = vector_ds.GetLayerCount() if layer_count == 0: message = '文件[{0}]没有图层!'.format(self.__file_name_with_path__) json_vector.set_value_of_name('result', self.Failure) json_vector.set_value_of_name('message', message) # 判断路径是否存在,不存在则创建 if CFile.check_and_create_directory(file_name_with_path): json_vector.to_file(file_name_with_path) return CResult.merge_result( CResult.Failure, '文件[{0}]没有图层!'.format(self.__file_name_with_path__)) shp_lyr = vector_ds.GetLayer(0) if shp_lyr is None: message = '文件[{0}]读取图层失败!'.format(self.__file_name_with_path__) json_vector.set_value_of_name('result', self.Failure) json_vector.set_value_of_name('message', message) # 判断路径是否存在,不存在则创建 if CFile.check_and_create_directory(file_name_with_path): json_vector.to_file(file_name_with_path) return CResult.merge_result( CResult.Failure, '文件[{0}]读取图层失败!'.format(self.__file_name_with_path__)) driver = vector_ds.GetDriver() if driver is None: message = '文件[{0}]读取驱动失败!'.format(self.__file_name_with_path__) json_vector.set_value_of_name('result', self.Failure) json_vector.set_value_of_name('message', message) # 判断路径是否存在,不存在则创建 if CFile.check_and_create_directory(file_name_with_path): json_vector.to_file(file_name_with_path) return CResult.merge_result( CResult.Failure, '文件[{0}]读取驱动失败!'.format(self.__file_name_with_path__)) # 定义datasource子节点,并添加到矢量json对象中 json_datasource = CJson() json_datasource.set_value_of_name('name', self.__file_name_with_path__) json_datasource.set_value_of_name('description', driver.name) json_vector.set_value_of_name('datasource', json_datasource.json_obj) # print(driver.name) layer_count_real, layer_list = self.get_vector_layercount_and_layers( vector_ds) # print('共{0}个有效图层'.format(layer_count_real)) # print(layer_list) json_vector.set_value_of_name('layer_count', layer_count_real) # shp图层只有1个,gdb有多个 json_vector.set_value_of_name('result', result_success) # 定义layers子节点,并添加到矢量json对象中 json_layers = CJson() if layer_count_real == 0: json_vector.set_value_of_name('layers', []) else: list_json_layers = [] for layer_temp in layer_list: print('图层对象: {0}'.format(layer_temp)) if mdb_flag: layer_name = CUtils.conversion_chinese_code( layer_temp.GetName()) else: layer_name = layer_temp.GetName() json_layer = CJson() list_json_layers.append(json_layer.json_obj) # name节点 json_layer.set_value_of_name("name", layer_name) json_layer.set_value_of_name("description", layer_name) # print(layer_name) # projwkt 节点 json_proj_wkt = self.get_projwkt_by_layer(layer_temp) json_layer.set_value_of_name("coordinate", json_proj_wkt.json_obj) # features节点 json_features = CJson() feature_count = layer_temp.GetFeatureCount() json_features.set_value_of_name("count", feature_count) json_layer.set_value_of_name("features", json_features.json_obj) # geometry节点 json_geometry = self.get_geometry_by_vectorlayer( layer_temp) json_layer.set_value_of_name("geometry", json_geometry.json_obj) # extent节点 json_extent = self.get_extent_by_vectorlayer( layer_temp, feature_count) json_layer.set_value_of_name("extent", json_extent.json_obj) # attributes节点 json_attributes = self.get_attributes_by_vectorlayer( layer_temp, mdb_flag) json_layer.set_value_of_name("attributes", json_attributes.json_obj) # wgs84节点 json_wgs84 = self.transform_to_wgs84( layer_temp, feature_count) json_layer.set_value_of_name('wgs84', json_wgs84.json_obj) json_vector.set_value_of_name('layers', list_json_layers) # json_shp_str = json_vector.to_json() # print(json_shp_str) # 判断路径是否存在,不存在则创建 if CFile.check_and_create_directory(file_name_with_path): json_vector.to_file(file_name_with_path) CLogger().info('文件[{0}]元数据信息读取成功!'.format( self.__file_name_with_path__)) return CResult.merge_result( CResult.Success, '文件[{0}]元数据信息读取成功!'.format(self.__file_name_with_path__)) except Exception as error: CLogger().info('get_metadata_2_file解析错误:{0}'.format(error)) message = 'get_metadata_2_file解析错误:文件:{0},错误信息为{1}'.format( self.__file_name_with_path__, error) json_vector.set_value_of_name('result', self.Failure) json_vector.set_value_of_name('message', message) # 判断路径是否存在,不存在则创建 if CFile.check_and_create_directory(file_name_with_path): json_vector.to_file(file_name_with_path) return CResult.merge_result( CResult.Failure, '文件[{0}]读取异常!{1}'.format(self.__file_name_with_path__, error.__str__())) finally: vector_ds.Destroy() vector_ds = None