import csv from datetime import datetime from pathlib import Path import geopandas as gpd import json def get_metadata(camTy: str, fileId: str): # CSV 파일 경로 설정 # base_dir = "mx15hdi" if pollId == "1" else "starsafire" if camTy == "mx15hdi": csv_path = f"{camTy}/Metadata/CSV/{fileId}/mx15hdi_interpolation.csv" elif camTy == "starsafire": csv_path = f"{camTy}/Metadata/CSV/{fileId}/Metadata_Extracted.csv" try: # CSV 파일 읽기 with open(csv_path, 'r', newline='', encoding='utf-8-sig') as csvfile: reader = csv.reader(csvfile) next(reader, None) row = next(reader, None) return ','.join(row) except FileNotFoundError: print(f"CSV file not found: {csv_path}") raise except ValueError as e: print(f"Value error: {str(e)}") raise except Exception as e: print(f"Error processing CSV: {e}") raise def get_oil_type(camTy: str, fileId: str): # Shapefile 경로 설정 path = f"{camTy}/Polygon/Shp/{fileId}" shp_file = list(Path(path).glob("*.shp")) if not shp_file: return [] shp_path = f"{camTy}/Polygon/Shp/{fileId}/{shp_file[0].name}" print(shp_path) # if camTy == "mx15hdi": # fileSub = f"{Path(fileName).stem}_gsd" # elif camTy == "starsafire": # fileSub = f"{Path(fileName).stem}" # shp_path = f"{camTy}/Polygon/Shp/{fileId}/{fileSub}.shp" # 두께 정보 class_thickness_mm = { 1: 1.0, # Black oil (Emulsion) 2: 0.1, # Brown oil (Crude) 3: 0.0003, # Rainbow oil (Slick) 4: 0.0001 # Silver oil (Slick) } # 알고리즘 정보 algorithm = { 1: "검정", 2: "갈색", 3: "무지개", 4: "은색" } try: # Shapefile 읽기 gdf = gpd.read_file(shp_path) if gdf.crs != "epsg:4326": gdf = gdf.to_crs("epsg:4326") # 데이터 준비 data = [] for _, row in gdf.iterrows(): class_id = row.get('class_id', None) area_m2 = row.get('area_m2', None) volume_m3 = row.get('volume_m3', None) note = row.get('note', None) thickness_m = class_thickness_mm.get(class_id, 0) / 1000.0 geom_wkt = row.geometry.wkt if row.geometry else None result = { "classId": algorithm.get(class_id, 0), "area": area_m2, "volume": volume_m3, "note": note, "thickness": thickness_m, "wkt": geom_wkt } data.append(result) return data except FileNotFoundError: print(f"Shapefile not found: {shp_path}") raise except Exception as e: print(f"Error processing shapefile or database: {str(e)}") raise