[GIS] JTS Geometry Class Diagram

JTS는 자바로 잘짜여진 지오메트리 공간연산 기능을 제공하는 오픈소스입니다. 세상에 나온지 수년이 지났고 워낙 잘짜여진터라 C언어로도 포팅되졌습니다. 위의 UML을 그려본 이유는 최근에 자바언어로 ESRI의 SHP 파일과 DBF 파일을 읽고 쓸 수 있는 라이브러리를 오픈소스 차원에서 개발할때 JTS에서 제공하는 Geometry 타입을 사용하기 위함입니다. 자바언어로 SHP와 DBF 파일을 읽는 오픈소스를 찾아보았으나 너무 다른 라이브러리에 깊이 관계를 맺고 있어 최적화시켜 사용하기가 제겐 부담이 되어 이번 기회에 새롭게 만들어 오픈소스 형태로 공유해볼 생각입니다.

[GIS] GeoTools를 이용해 SHP, DBF 파일 읽기

GeoTools(http://www.geotools.org)는 자바 기반의 GIS 시스템을 구축할 수 있는 다양한 기능을 갖춘 오픈소스입니다. 제가 처음 GeoTools에 관심을 가졌던 이유는 SHP 파일을 읽기 위한 자바 라이브러리가 필요해서 였는데요. 이 기능을 파악하기 위해 테스트로 작성했던 코드를 공유해 봅니다.

먼저 SHP 파일에서 좌표를 읽어 들이는 코드입니다.

import java.io.IOException;
import java.net.MalformedURLException;

import org.geotools.data.shapefile.shp.ShapefileException;
import org.geotools.data.shapefile.shp.ShapefileReader;
import org.geotools.data.shapefile.shp.ShapefileReader.Record;
import org.geotools.data.shapefile.ShpFiles;

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

public class ShapefileReaderTestMainEntry {
    public static void main(String[] args) {
        ShapefileReader r = null;
        try {
            ShpFiles shpFile = new ShpFiles("G:\\__Data__\\dong.shp");

            GeometryFactory geometryFactory = new GeometryFactory();
            r = new ShapefileReader(shpFile, true, false, geometryFactory);

            while (r.hasNext()) {
                Record record = r.nextRecord();
                Geometry shape = (Geometry)record.shape();
                Point centroid = shape.getCentroid();
                System.out.println(
                    "(" 
                    + centroid.getX() 
                    + ", " 
                    + centroid.getY() 
                    + ")"
                );
            }
            r.close();
        } catch (MalformedURLException e1) {
            e1.printStackTrace();   
        } catch (ShapefileException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        };
    }
}

다음으로는 DBF 파일에서 값을 읽어 들이는 코드입니다.

import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.charset.Charset;

import org.geotools.data.shapefile.ShpFiles;
import org.geotools.data.shapefile.dbf.DbaseFileHeader;
import org.geotools.data.shapefile.dbf.DbaseFileReader;

public class DbaseFileReaderTestMainEntry {
    public static void main(String[] args) {
        DbaseFileReader r = null;
        try {
            ShpFiles shpFile = new ShpFiles("G:\\__Data__\\dong.shp");
            r = new DbaseFileReader(shpFile, false, Charset.defaultCharset());
            DbaseFileHeader header = r.getHeader();

            int numFields = header.getNumFields();
            for(int iField=0; iField < numFields; ++iField) {
                String fieldName = header.getFieldName(iField);
                System.out.println(fieldName);
            }

            while (r.hasNext()) {
                Object[] values = r.readEntry();
                for(int iField=0; iField < numFields; ++iField) {
                    System.out.println(values[iField].toString());
                }
                System.out.println("---------------");
            }

            r.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }  
    }
}