api scene test

This commit is contained in:
2025-12-30 18:02:09 +09:00
parent 09b35a5879
commit 73a3e1360c
8 changed files with 315 additions and 224 deletions

View File

@@ -0,0 +1,157 @@
package com.kamco.cd.kamcoback.common.geometry;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import org.locationtech.jts.geom.Geometry;
/** GeoJSON 파일 생성 유틸리티 */
public class GeoJsonFileWriter {
private final ObjectMapper objectMapper;
public GeoJsonFileWriter() {
this.objectMapper = new ObjectMapper();
}
public GeoJsonFileWriter(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
/**
* GeoJSON 문자열 생성
*
* @param features Feature 목록
* @param name GeoJSON name 속성
* @param srid CRS EPSG 코드
* @return GeoJSON 문자열
*/
public String buildGeoJson(List<ImageFeature> features, String name, int srid) {
try {
ObjectNode root = objectMapper.createObjectNode();
root.put("type", "FeatureCollection");
root.put("name", name);
// CRS 정보
ObjectNode crs = objectMapper.createObjectNode();
crs.put("type", "name");
ObjectNode crsProps = objectMapper.createObjectNode();
crsProps.put("name", "urn:ogc:def:crs:EPSG::" + srid);
crs.set("properties", crsProps);
root.set("crs", crs);
// Features 배열
ArrayNode featuresArray = objectMapper.createArrayNode();
for (ImageFeature f : features) {
featuresArray.add(buildFeature(f));
}
root.set("features", featuresArray);
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(root);
} catch (Exception e) {
throw new RuntimeException("GeoJSON 생성 실패", e);
}
}
/** 단일 Feature 객체 생성 */
private ObjectNode buildFeature(ImageFeature f) throws Exception {
ObjectNode feature = objectMapper.createObjectNode();
feature.put("type", "Feature");
// Properties
ObjectNode properties = objectMapper.createObjectNode();
properties.put("scene_id", f.getSceneId());
properties.put("abs_path", f.getAbsPath());
properties.put("basename", f.getFileName());
properties.put("georef_source", "internal");
properties.put("crs_source", "transformed");
feature.set("properties", properties);
// Geometry (CRS 제거)
ObjectNode geometry = (ObjectNode) objectMapper.readTree(f.getGeomJson());
geometry.remove("crs");
feature.set("geometry", geometry);
return feature;
}
/**
* 파일로 저장
*
* @param geojson GeoJSON 문자열
* @param filePath 저장 경로
*/
public void writeToFile(String geojson, String filePath) throws IOException {
try (FileWriter writer = new FileWriter(filePath)) {
writer.write(geojson);
}
}
/** Feature 목록을 바로 파일로 저장 */
public void exportToFile(List<ImageFeature> features, String name, int srid, String filePath)
throws IOException {
String geojson = buildGeoJson(features, name, srid);
writeToFile(geojson, filePath);
}
/** Feature 데이터 클래스 */
public static class ImageFeature {
private String sceneId;
private String filePath;
private String fileName;
private String geomJson;
public ImageFeature() {}
public ImageFeature(String sceneId, String filePath, String fileName, Geometry geomJson) {
this.sceneId = sceneId;
this.filePath = filePath;
this.fileName = fileName;
if (geomJson != null) {
this.geomJson = GeometryUtils.toGeoJson(geomJson);
}
}
public String getSceneId() {
return sceneId;
}
public void setSceneId(String sceneId) {
this.sceneId = sceneId;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getGeomJson() {
return geomJson;
}
public void setGeomJson(String geomJson) {
this.geomJson = geomJson;
}
public String getAbsPath() {
return filePath + "/" + fileName;
}
}
}

View File

@@ -0,0 +1,17 @@
package com.kamco.cd.kamcoback.common.geometry;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.geojson.GeoJsonWriter;
public class GeometryUtils {
private static final GeoJsonWriter GEOJSON_WRITER = new GeoJsonWriter(8);
/** JTS Geometry를 GeoJSON 문자열로 변환 */
public static String toGeoJson(Geometry geometry) {
if (geometry == null) {
return null;
}
return GEOJSON_WRITER.write(geometry);
}
}