diff --git a/src/main/java/com/kamco/cd/kamcoback/inference/dto/InferenceSendDto.java b/src/main/java/com/kamco/cd/kamcoback/inference/dto/InferenceSendDto.java index e4783f00..fe7dbfaf 100644 --- a/src/main/java/com/kamco/cd/kamcoback/inference/dto/InferenceSendDto.java +++ b/src/main/java/com/kamco/cd/kamcoback/inference/dto/InferenceSendDto.java @@ -24,11 +24,11 @@ public class InferenceSendDto { private Double priority; public String getCd_model_path() { - return cd_model_path==null?null:cd_model_path.replace("kamcd-nfs", "data"); + return cd_model_path == null ? null : cd_model_path.replace("kamcd-nfs", "data"); } public String getCls_model_path() { - return cls_model_path==null?null:cls_model_path.replace("kamcd-nfs", "data"); + return cls_model_path == null ? null : cls_model_path.replace("kamcd-nfs", "data"); } @Getter @@ -45,11 +45,11 @@ public class InferenceSendDto { public String getInput1_scene_path() { - return input1_scene_path==null?null:input1_scene_path.replace("kamcd-nfs", "data"); + return input1_scene_path == null ? null : input1_scene_path.replace("kamcd-nfs", "data"); } public String getInput2_scene_path() { - return input2_scene_path==null?null:input2_scene_path.replace("kamcd-nfs", "data"); + return input2_scene_path == null ? null : input2_scene_path.replace("kamcd-nfs", "data"); } } } diff --git a/src/main/java/com/kamco/cd/kamcoback/postgres/core/MapSheetMngCoreService.java b/src/main/java/com/kamco/cd/kamcoback/postgres/core/MapSheetMngCoreService.java index a06dda7a..e3ec0ad4 100644 --- a/src/main/java/com/kamco/cd/kamcoback/postgres/core/MapSheetMngCoreService.java +++ b/src/main/java/com/kamco/cd/kamcoback/postgres/core/MapSheetMngCoreService.java @@ -27,7 +27,6 @@ import java.nio.file.Paths; import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; @@ -236,84 +235,22 @@ public class MapSheetMngCoreService { } /** - * 추론 실행에 필요한 geojson 파일 생성 + * geojson 생성 * - * @param yyyy 영상관리 파일별 년도 - * @param scenes 5k 도엽 번호 리스트 - * @param mapSheetScope EXCL : 추론제외, PREV 이전 년도 도엽 사용 + * @param yyyy + * @param scenes + * @param mapSheetScope + * @param detectOption * @return */ public Scene getSceneInference( String yyyy, List scenes, String mapSheetScope, String detectOption) { - - Map result = new HashMap<>(); - boolean isAll = MapSheetScope.ALL.getId().equals(mapSheetScope); - - String optionSuffix = ""; - if (DetectOption.EXCL.getId().equals(detectOption)) { - optionSuffix = "_EXCL"; - } else if (DetectOption.PREV.getId().equals(detectOption)) { - optionSuffix = "_PREV"; - } - - // 1) 경로/파일명 결정 - String targetDir = - "local".equals(activeEnv) ? System.getProperty("user.home") + "/geojson" : inferenceDir; - - String filename = - isAll - ? String.format("%s_%s_ALL%s.geojson", yyyy, activeEnv, optionSuffix) - : String.format("%s_%s%s.geojson", yyyy, activeEnv, optionSuffix); - - Path outputPath = Paths.get(targetDir, filename); - - // 2) ALL일 때만 재사용 - // if (isAll && Files.exists(outputPath)) { - // return outputPath.toString(); - // } - - // 3) 데이터 조회 - List sceneInference = mapSheetMngRepository.getSceneInference(yyyy, scenes); - - if (sceneInference == null || sceneInference.isEmpty()) { - log.warn( - "NOT_FOUND_TARGET_YEAR: yyyy={}, isAll={}, scenesSize={}", - yyyy, - isAll, - scenes == null ? 0 : scenes.size()); - throw new CustomApiException("NOT_FOUND_TARGET_YEAR", HttpStatus.NOT_FOUND); - } - - // 4) 파일 생성 - try { - log.info("create Directories outputPath: {}", outputPath); - log.info( - "activeEnv={}, inferenceDir={}, targetDir={}, filename={}", - activeEnv, - inferenceDir, - targetDir, - filename); - log.info("outputPath={}, parent={}", outputPath.toAbsolutePath(), outputPath.getParent()); - Files.createDirectories(outputPath.getParent()); - - new GeoJsonFileWriter() - .exportToFile(sceneInference, "scene_inference_" + yyyy, 5186, outputPath.toString()); - log.info("GeoJsonFileWriter: {}", "scene_inference_" + yyyy); - Scene scene = new Scene(); - scene.setFeatures(sceneInference); - scene.setFilePath(outputPath.toString()); - - return scene; - - } catch (IOException e) { - log.error( - "FAIL_CREATE_MAP_SHEET_FILE: yyyy={}, isAll={}, path={}", yyyy, isAll, outputPath, e); - throw new CustomApiException("INTERNAL_SERVER_ERROR", HttpStatus.INTERNAL_SERVER_ERROR, e); - } + List features = loadSceneInferenceBySheets(yyyy, scenes); + return writeSceneGeoJson(yyyy, mapSheetScope, detectOption, features); } /** - * 년도별로 추론 실행에 필요한 geojson 파일 생성 + * geojson 생성 * * @param yearDtos * @param yyyy @@ -326,17 +263,24 @@ public class MapSheetMngCoreService { String yyyy, String mapSheetScope, String detectOption) { - Map result = new HashMap<>(); + List features = loadSceneInferenceByFallbackYears(yearDtos); + return writeSceneGeoJson(yyyy, mapSheetScope, detectOption, features); + } + + /** + * 파일 경로/이름 , 파일 생성 , 도엽번호 반환 + * + * @param yyyy + * @param mapSheetScope + * @param detectOption + * @param sceneInference + * @return Scene + */ + private Scene writeSceneGeoJson( + String yyyy, String mapSheetScope, String detectOption, List sceneInference) { boolean isAll = MapSheetScope.ALL.getId().equals(mapSheetScope); + String optionSuffix = buildOptionSuffix(detectOption); - String optionSuffix = ""; - if (DetectOption.EXCL.getId().equals(detectOption)) { - optionSuffix = "_EXCL"; - } else if (DetectOption.PREV.getId().equals(detectOption)) { - optionSuffix = "_PREV"; - } - - // 1) 경로/파일명 결정 String targetDir = "local".equals(activeEnv) ? System.getProperty("user.home") + "/geojson" : inferenceDir; @@ -347,33 +291,11 @@ public class MapSheetMngCoreService { Path outputPath = Paths.get(targetDir, filename); - // 3) 데이터 조회 - Map> groupedByYear = - yearDtos.stream().collect(Collectors.groupingBy(MapSheetFallbackYearDto::getMngYyyy)); - - List sceneInference = new ArrayList<>(); - - // 년도별로 루프 돌리기 - for (Map.Entry> entry : groupedByYear.entrySet()) { - - Integer compareYear = entry.getKey(); - List listByYear = entry.getValue(); - - // 도엽번호만 추출 - List sheetNums = - listByYear.stream().map(MapSheetFallbackYearDto::getMapSheetNum).toList(); - - List tempSceneInference = - mapSheetMngRepository.getSceneInference(compareYear.toString(), sheetNums); - tempSceneInference.addAll(sceneInference); - } - if (sceneInference == null || sceneInference.isEmpty()) { - log.warn("NOT_FOUND_TARGET_YEAR: yyyy={}, isAll={}, scenesSize={}", yyyy, isAll, 0); + log.warn("NOT_FOUND_TARGET_YEAR: yyyy={}, isAll={}, featuresSize={}", yyyy, isAll, 0); throw new CustomApiException("NOT_FOUND_TARGET_YEAR", HttpStatus.NOT_FOUND); } - // 4) 파일 생성 try { log.info("create Directories outputPath: {}", outputPath); log.info( @@ -383,15 +305,15 @@ public class MapSheetMngCoreService { targetDir, filename); log.info("outputPath={}, parent={}", outputPath.toAbsolutePath(), outputPath.getParent()); + Files.createDirectories(outputPath.getParent()); new GeoJsonFileWriter() .exportToFile(sceneInference, "scene_inference_" + yyyy, 5186, outputPath.toString()); - log.info("GeoJsonFileWriter: {}", "scene_inference_" + yyyy); + Scene scene = new Scene(); scene.setFeatures(sceneInference); scene.setFilePath(outputPath.toString()); - return scene; } catch (IOException e) { @@ -401,6 +323,71 @@ public class MapSheetMngCoreService { } } + private String buildOptionSuffix(String detectOption) { + if (DetectOption.EXCL.getId().equals(detectOption)) return "_EXCL"; + if (DetectOption.PREV.getId().equals(detectOption)) return "_PREV"; + return ""; + } + + /** + * 년도, 도엽번호로 조회 + * + * @param yyyy + * @param scenes + * @return ImageFeature + */ + private List loadSceneInferenceBySheets(String yyyy, List scenes) { + List sceneInference = mapSheetMngRepository.getSceneInference(yyyy, scenes); + + if (sceneInference == null || sceneInference.isEmpty()) { + log.warn( + "NOT_FOUND_TARGET_YEAR: yyyy={}, scenesSize={}", + yyyy, + scenes == null ? 0 : scenes.size()); + throw new CustomApiException("NOT_FOUND_TARGET_YEAR", HttpStatus.NOT_FOUND); + } + return sceneInference; + } + + /** + * 년도별로 나눠 조회 + * + * @param yearDtos + * @return ImageFeature + */ + private List loadSceneInferenceByFallbackYears( + List yearDtos) { + if (yearDtos == null || yearDtos.isEmpty()) { + return List.of(); + } + + Map> groupedByYear = + yearDtos.stream() + .filter(d -> d.getMngYyyy() != null && d.getMapSheetNum() != null) + .collect(Collectors.groupingBy(MapSheetFallbackYearDto::getMngYyyy)); + + List sceneInference = new ArrayList<>(); + + for (Map.Entry> entry : groupedByYear.entrySet()) { + Integer year = entry.getKey(); + + List sheetNums = + entry.getValue().stream() + .map(MapSheetFallbackYearDto::getMapSheetNum) + .filter(Objects::nonNull) + .distinct() + .toList(); + + List temp = mapSheetMngRepository.getSceneInference(year.toString(), sheetNums); + + if (temp != null && !temp.isEmpty()) { + sceneInference.addAll(temp); + } + } + + return sceneInference; + } + /** * 변화탐지 실행 가능 기준 년도 조회 *