def test_json(self): json_shp = CJson() json_datasource = CJson() json_datasource.set_value_of_name('name', r'镶嵌影像\\石嘴山市-3xq.shp') json_datasource.set_value_of_name('description', 'ESRI Shapefile') json_datasource_str = json_datasource.to_json() print(json_datasource_str) json_shp.set_value_of_name('datasource', json_datasource.json_obj) json_shp.set_value_of_name('layer_count', '1') list_layers = list() list_layers.append(json_datasource.json_obj) json_shp.set_value_of_name('layers', list_layers) json_shp.set_value_of_name('valid', True) json_shp_str = json_shp.to_json() json_shp.to_file(r'c:\app\aa.txt') print(json_shp_str)
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 process(self) -> str: """ 在这里提取文档数据的元数据, 将元数据文件存储在self.file_content.work_root_dir下, 固定名称为self.FileName_MetaData, 注意返回的串中有元数据的格式 注意: 如果出现内存泄漏现象, 则使用新建进程提取元数据, 放置到文件中, 在本进程中解析元数据!!! :return: """ default_result = super().process() out_metadata_file_fullname = CFile.join_file( self.file_content.work_root_dir, self.FileName_MetaData) in_file_fullname = self.file_info.file_name_with_full_path if not settings.application.xpath_one( self.Path_Setting_Dependence_Tika_Enable, True): return default_result tika_dependence_mode = settings.application.xpath_one( self.Path_Setting_Dependence_Tika_Mode, self.Name_Server) if CUtils.equal_ignore_case(tika_dependence_mode, self.Name_Server): tika_server_url = settings.application.xpath_one( self.Path_Setting_Dependence_Tika_Server_Url, None) tika_server_connect_timeout = settings.application.xpath_one( self.Path_Setting_Dependence_Tika_Server_Timeout, 30) if CUtils.equal_ignore_case(tika_server_url, ''): return default_result try: parsed = TikaServer.from_file( in_file_fullname, tika_server_url, requestOptions={'timeout': tika_server_connect_timeout}) meta_data_dict = parsed["metadata"] json_obj = CJson() json_obj.load_obj(meta_data_dict) json_obj.to_file(out_metadata_file_fullname) return CResult.merge_result_info( CResult.merge_result( self.Success, '文档[{0}]的元数据提取成功'.format(in_file_fullname)), self.Name_Format, self.MetaDataFormat_Json) except Exception as error: return CResult.merge_result( self.Failure, '文档[{0}]的元数据提取过程出现错误, 详细信息为: [{1}]'.format( in_file_fullname, error.__str__())) else: tika_application = settings.application.xpath_one( self.Path_Setting_Dependence_Tika_Client_App, None) if CUtils.equal_ignore_case(tika_application, ''): return default_result if not CFile.file_or_path_exist(tika_application): return CResult.merge_result( self.Failure, '文档[{0}]的元数据无法提取, 详细原因为: [依赖中间件{1}文件不存在, 请修正后重试!]'.format( in_file_fullname, tika_application)) try: tika_client = TikaApplication(file_jar=tika_application) meta_data_dict = tika_client.extract_only_metadata( in_file_fullname) json_obj = CJson() json_obj.load_obj(meta_data_dict) json_obj.to_file(out_metadata_file_fullname) return CResult.merge_result_info( CResult.merge_result( self.Success, '文档[{0}]的元数据提取成功'.format(in_file_fullname)), self.Name_Format, self.MetaDataFormat_Json) except Exception as error: return CResult.merge_result( self.Failure, '文档[{0}]的元数据提取过程出现错误, 详细信息为: [{1}]'.format( in_file_fullname, error.__str__())) # result = raster_mdreader.get_metadata_2_file(out_metadata_file_fullname) # result = CProcessUtils.processing_method(raster_mdreader.get_metadata_2_file, out_metadata_file_fullname) # 进程调用模式 # p_one = Process(target=raster_mdreader.get_metadata_2_file, args=(out_metadata_file_fullname,)) # p_one.start() # p_one.join() return CResult.merge_result_info(result, self.Name_Format, self.MetaDataFormat_Json)
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