먼저 테스트할 적절한 좌표를 epsg.io 에서 하나 뽑아야합니다.
저는 EPSG:4326
좌표계를 기반으로 한 덕수궁을 좌표를 아래처럼 뽑았습니다.
전체 코드를 보고 싶다면 여기에서 참조하시기 바랍니다.
이제 4326 좌표계에서 우리나라의 투영좌표계 중 하나인 EPSG:5179
로 변환해보겠습니다.
참고로 System.setProperty("org.geotools.referencing.forceXY", "true");
는
반드시 세팅하고 코드를 실행하시길 바랍니다!
이걸 해야되는 이유는 이 링크에서 참고하시기 바랍니다.
import org.geotools.geometry.jts.JTS;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.referencing.CRS;
import org.junit.jupiter.api.Test;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.NoSuchAuthorityCodeException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;
@Test
void testOne() throws FactoryException, TransformException {
// 이걸 안 하면 X,Y 를 Y,X 를 반대로 해석하여 굉장히 짜증나는 문제를 발생시킵니다.
// 꼭 서비스 시작전에 이 프로퍼티를 세팅하시기 바랍니다!
// 아래 참고 링크에서 "GeoTools 가 X,Y 를 Y,X 로 읽는 이유"를 읽어보시길 바랍니다.
System.setProperty("org.geotools.referencing.forceXY", "true");
// 덕수궁의 좌표입니다 😎
String coordX = "126.97476625442985";
String coordY = "37.565611356905336";
CoordinateReferenceSystem sourceCrs = CRS.decode("EPSG:4326");
CoordinateReferenceSystem targetCrs = CRS.decode("EPSG:5179");
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
Coordinate coordinate = new Coordinate(Double.parseDouble(coordX), Double.parseDouble(coordY));
Geometry point = geometryFactory.createPoint(coordinate);
MathTransform transform = CRS.findMathTransform(sourceCrs, targetCrs, true);
Geometry transFormedPoint = JTS.transform(point, transform);
System.out.println("좌표변경 전(EPSG:4326) Point = " + point);
System.out.println("좌표변경 후(EPSG:5179) Point = " + transFormedPoint);
}
콘솔출력:
20:27:53.814 [main] INFO hsqldb.db.HSQLDB6BD103EBD8.ENGINE - dataFileCache open start
transFormedPoint = POINT (953615.0338799839 1951935.085915152)
좌표변경 전(EPSG:4326) Point = POINT (126.97476625442985 37.565611356905336)
좌표변경 후(EPSG:5179) Point = POINT (953615.0338799839 1951935.085915152)
콘솔에 찍힌 transform 된 좌표인 953615.0338799839 1951935.085915152
좌표로 가볼까요?
아래 링크를 클릭해서 직접 눈으로 확인해보시죠!
https://epsg.io/map#srs=5179&x=953615.0338799839&y=1951935.085915152&z=19&layer=streets