def generate_shp(self): """ transformation des données au format shp et sauvegarde sous forme d'une archive """ FionaShapeService.create_shapes_struct( db_cols=self.columns, srid=self.export.get('geometry_srid'), dir_path=self.export_dir, file_name=self.file_name) items = self.data.get('items') for feature in items['features']: geom, props = (feature.get(field) for field in ('geometry', 'properties')) FionaShapeService.create_feature( props, from_shape(asShape(geom), self.export.get('geometry_srid'))) FionaShapeService.save_and_zip_shapefiles() # Suppression des fichiers générés et non compressé for gtype in ['POINT', 'POLYGON', 'POLYLINE']: file_path = Path(self.export_dir, gtype + '_' + self.file_name) if file_path.is_dir(): shutil.rmtree(file_path) return True
def export(info_role): filters = dict(request.args) if 'limit' in filters: result_limit = filters.pop('limit')[0] else: result_limit = current_app.config['SYNTHESE']['NB_MAX_OBS_EXPORT'] export_format = filters.pop('export_format')[0] allowed_datasets = TDatasets.get_user_datasets(info_role) q = DB.session.query(VSyntheseForExport) q = synthese_query.filter_query_all_filters(VSyntheseForExport, q, filters, info_role, allowed_datasets) q = q.order_by(VSyntheseForExport.date_min.desc()) data = q.limit(result_limit) file_name = datetime.datetime.now().strftime('%Y_%m_%d_%Hh%Mm%S') file_name = filemanager.removeDisallowedFilenameChars(file_name) formated_data = [d.as_dict_ordered() for d in data] export_columns = formated_data[0].keys() if export_format == 'csv': return to_csv_resp( file_name, formated_data, separator=';', columns=export_columns, ) elif export_format == 'geojson': results = FeatureCollection(formated_data) return to_json_resp(results, as_file=True, filename=file_name, indent=4) else: filemanager.delete_recursively(str(ROOT_DIR / 'backend/static/shapefiles'), excluded_files=['.gitkeep']) dir_path = str(ROOT_DIR / 'backend/static/shapefiles') FionaShapeService.create_shapes_struct( db_cols=VSyntheseForExport.db_cols, srid=current_app.config['LOCAL_SRID'], dir_path=dir_path, file_name=file_name, col_mapping=current_app.config['SYNTHESE']['EXPORT_COLUMNS']) for row in data: geom = row.the_geom_local row_as_dict = row.as_dict_ordered() FionaShapeService.create_feature(row_as_dict, geom) FionaShapeService.save_and_zip_shapefiles() return send_from_directory(dir_path, file_name + '.zip', as_attachment=True)
def export_visit(info_role): ''' Télécharge les données d'une visite (ou des visites ) ''' parameters = request.args export_format = parameters[ 'export_format'] if 'export_format' in request.args else 'shapefile' file_name = datetime.datetime.now().strftime('%Y_%m_%d_%Hh%Mm%S') q = (DB.session.query(ExportVisits)) if 'id_base_visit' in parameters: q = (DB.session.query(ExportVisits).filter( ExportVisits.idbvisit == parameters['id_base_visit'])) elif 'id_releve_plot' in parameters: q = (DB.session.query(ExportVisits).filter( ExportVisits.idreleve == parameters['id_releve_plot'])) elif 'id_base_site' in parameters: q = (DB.session.query(ExportVisits).filter( ExportVisits.idbsite == parameters['id_base_site'])) elif 'organisme' in parameters: q = (DB.session.query(ExportVisits).filter( ExportVisits.organisme == parameters['organisme'])) elif 'year' in parameters: q = (DB.session.query(ExportVisits).filter( func.date_part('year', ExportVisits.visitdate) == parameters['year'])) elif 'cd_hab' in parameters: q = (DB.session.query(ExportVisits).filter( ExportVisits.cd_hab == parameters['cd_hab'])) data = q.all() features = [] # formate data cor_hab_taxon = [] flag_cdhab = 0 strates = [] tab_header = [] column_name = get_base_column_name() column_name_pro = get_pro_column_name() mapping_columns = get_mapping_columns() strates_list = get_stratelist_plot() tab_visit = [] for d in data: visit = d.as_dict() # Get list hab/taxon cd_hab = visit['cd_hab'] if flag_cdhab != cd_hab: cor_hab_taxon = get_taxonlist_by_cdhab(cd_hab) flag_cdhab = cd_hab # remove geom Type geom_wkt = to_shape(d.geom) geom_array = array(geom_wkt) visit['geom_wkt'] = geom_wkt if export_format == 'csv' or export_format == 'shapefile': visit['geom'] = d.geom if geom_wkt.type.lower() == 'linestring': visit['geom'] = str(geom_array[0]) + " / " + str(geom_array[1]) # remove html tag visit['lbhab'] = striphtml(visit['lbhab']) # Translate label column visit = dict((mapping_columns[key], value) for (key, value) in visit.items() if key in mapping_columns) # pivot strate if visit['covstrate']: for strate, cover in visit['covstrate'].items(): visit[strate] = " % " + str(cover) if 'covstrate' in visit: visit.pop('covstrate') # pivot taxons if visit['covtaxons']: for taxon, cover in visit['covtaxons'].items(): visit[taxon] = " % " + str(cover) if 'covtaxons' in visit: visit.pop('covtaxons') tab_visit.append(visit) if export_format == 'geojson': for d in tab_visit: feature = mapping(d['geom_wkt']) d.pop('geom_wkt', None) properties = d features.append(feature) features.append(properties) result = FeatureCollection(features) return to_json_resp(result, as_file=True, filename=file_name, indent=4) elif export_format == 'csv': tab_header = column_name + [clean_string(x) for x in strates_list] + [ clean_string(x) for x in cor_hab_taxon ] + column_name_pro return to_csv_resp(file_name, tab_visit, tab_header, ';') else: dir_path = str(ROOT_DIR / 'backend/static/shapefiles') FionaShapeService.create_shapes_struct( db_cols=ExportVisits.__mapper__.c, srid=4326, dir_path=dir_path, file_name=file_name, ) for row in data: FionaShapeService.create_feature(row.as_dict(), row.geom) FionaShapeService.save_and_zip_shapefiles() return send_from_directory(dir_path, file_name + '.zip', as_attachment=True)
def export_visit(): ''' Télécharge les données d'une visite (ou des visites ) ''' parameters = request.args # q = q.filter(TInfoSite.id_base_site == parameters['id_base_site']) export_format = parameters[ 'export_format'] if 'export_format' in request.args else 'shapefile' file_name = datetime.datetime.now().strftime('%Y_%m_%d_%Hh%Mm%S') q = (DB.session.query(ExportVisits)) if 'id_base_visit' in parameters: q = (DB.session.query(ExportVisits).filter( ExportVisits.id_base_visit == parameters['id_base_visit'])) elif 'id_base_site' in parameters: q = (DB.session.query(ExportVisits).filter( ExportVisits.id_base_site == parameters['id_base_site'])) elif 'organisme' in parameters: q = (DB.session.query(ExportVisits).filter( ExportVisits.organisme == parameters['organisme'])) elif 'commune' in parameters: q = (DB.session.query(ExportVisits).filter( ExportVisits.area_name == parameters['commune'])) elif 'year' in parameters: q = (DB.session.query(ExportVisits).filter( func.date_part('year', ExportVisits.visit_date) == parameters['year'])) elif 'cd_nom' in parameters: q = (DB.session.query(ExportVisits).filter( ExportVisits.cd_nom == parameters['cd_nom'])) data = q.all() features = [] if export_format == 'geojson': for d in data: feature = d.as_geofeature('geom', 'id_area', False) features.append(feature) result = FeatureCollection(features) return to_json_resp(result, as_file=True, filename=file_name, indent=4) elif export_format == 'csv': tab_visit = [] for d in data: visit = d.as_dict() geom_wkt = to_shape(d.geom) visit['geom'] = geom_wkt tab_visit.append(visit) return to_csv_resp(file_name, tab_visit, tab_visit[0].keys(), ';') else: dir_path = str(ROOT_DIR / 'backend/static/shapefiles') FionaShapeService.create_shapes_struct( db_cols=ExportVisits.__mapper__.c, srid=2154, dir_path=dir_path, file_name=file_name, ) for row in data: FionaShapeService.create_feature(row.as_dict(), row.geom) FionaShapeService.save_and_zip_shapefiles() return send_from_directory(dir_path, file_name + '.zip', as_attachment=True)
def export_ap(): """ Télécharge les données d'une aire de présence """ parameters = request.args export_format = (parameters["export_format"] if "export_format" in request.args else "shapefile") file_name = datetime.datetime.now().strftime("%Y_%m_%d_%Hh%Mm%S") q = DB.session.query(ExportAp) if "indexap" in parameters: q = DB.session.query(ExportAp).filter( ExportAp.indexap == parameters["indexap"]) elif "indexzp" in parameters: q = DB.session.query(ExportAp).filter( ExportAp.id_base_site == parameters["indexzp"]) elif "organisme" in parameters: q = DB.session.query(ExportAp).filter( ExportAp.organisme == parameters["organisme"]) elif "commune" in parameters: q = DB.session.query(ExportAp).filter( ExportAp.area_name == parameters["commune"]) elif "year" in parameters: q = DB.session.query(ExportAp).filter( func.date_part("year", ExportAp.visit_date) == parameters["year"]) elif "cd_nom" in parameters: q = DB.session.query(ExportAp).filter( ExportAp.cd_nom == parameters["cd_nom"]) data = q.all() features = [] if export_format == "geojson": for d in data: feature = d.as_geofeature("geom_local", "indexap", False) features.append(feature) result = FeatureCollection(features) return to_json_resp(result, as_file=True, filename=file_name, indent=4) elif export_format == "csv": tab_ap = [] for d in data: ap = d.as_dict() geom_wkt = to_shape(d.geom_local) ap["geom_local"] = geom_wkt tab_ap.append(ap) return to_csv_resp(file_name, tab_ap, tab_ap[0].keys(), ";") else: dir_path = str(ROOT_DIR / "backend/static/shapefiles") FionaShapeService.create_shapes_struct( db_cols=ExportAp.__mapper__.c, srid=2154, dir_path=dir_path, file_name=file_name, ) for row in data: FionaShapeService.create_feature(row.as_dict(), row.geom_local) FionaShapeService.save_and_zip_shapefiles() return send_from_directory(dir_path, file_name + ".zip", as_attachment=True)