잡동사니

[Geotools] 좌표변환 본문

IT/Open Source

[Geotools] 좌표변환

yeTi 2016. 5. 20. 10:43

안녕하세요. yeTi입니다.

오늘은 Geotools를 활용하여 좌표변환을 하는 방법에 대해 알아보겠습니다.



환경

 - 전자정부프레임워크

 - Geotools-14.3


1. 메이븐을 활용하여 라이브러리를 추가합니다.

pom.xml에 아래 사항을 추가합니다.


<properties>
    <geotools.version>14.3</geotools.version>
</properties>


<repositories>
    <repository>
        <id>osgeo</id>
        <name>Open Source Geospatial Foundation Repository</name>
        <url>http://download.osgeo.org/webdav/geotools/</url>
    </repository>
</repositories>


<!-- Geotools CRS -->
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-referencing</artifactId>
    <version>${geotools.version}</version>
</dependency>
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-epsg-hsql</artifactId>
    <version>${geotools.version}</version>
</dependency>
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-epsg-extension</artifactId>
    <version>${geotools.version}</version>
</dependency>

<!-- Geotools JTS -->
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-api</artifactId>
    <version>${geotools.version}</version>
</dependency>
<dependency>
    <groupId>org.geotools</groupId>
    <artifactId>gt-main</artifactId>
    <version>${geotools.version}</version>
</dependency>
<dependency>
    <groupId>com.vividsolutions</groupId>
    <artifactId>jts</artifactId>
    <version>1.12</version>
</dependency>


2. 다음과 같이 구현합니다.

import org.geotools.geometry.jts.JTS;
import org.geotools.geometry.jts.JTSFactoryFinder;
import org.geotools.referencing.CRS;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.Point;

public class CRSConverter {

    private CoordinateReferenceSystem sourceCRS;
    private CoordinateReferenceSystem targetCRS;
   
    public CRSConverter(String sSrcCRS, String sDestCRS) throws Exception {
        this.sourceCRS = CRS.decode("EPSG:" + sSrcCRS);
        this.targetCRS = CRS.decode("EPSG:" + sDestCRS);
    }
    public Point transform(String coordX, String coordY) throws Exception {
        // sourceGeometry 생성
        GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
        Coordinate coord = new Coordinate(Double.parseDouble(coordX), Double.parseDouble(coordY));
        Geometry sourceGeometry = geometryFactory.createPoint(coord);
       
        MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS, false);
        Geometry targetGeometry = JTS.transform(sourceGeometry, transform);
        return targetGeometry.getCentroid();
    }

}


3. 참고 문헌

 - Geotools User Guide : http://docs.geotools.org/stable/userguide/index.html

Comments