JUMP (OpenJUMP) で Java を使って Shapefile を読むだけのコード

JUMP (OpenJUMP) で Java 言語を使って Shapefile を読むだけのコードを作成してみました。
Geo::Reader の Deegree 版と JUMP 版を雑に作ってみました - Relevant, Timely, and Accurate の翻案ですが、RubyJRuby/Rjb)を封印して Java 言語のみで記述したところが異なります。

the code

// ReadShapefile.java
// @see http://jump-pilot.sourceforge.net/javadoc/openjump_javadoc/
import com.vividsolutions.jump.io.ShapefileReader;
import com.vividsolutions.jump.io.DriverProperties;
import com.vividsolutions.jump.feature.FeatureCollection;
import com.vividsolutions.jump.feature.FeatureSchema;
import com.vividsolutions.jump.feature.Feature;
import java.util.Iterator;

class ReadShapefile {
    public static void main(String[] args) throws Throwable {
        String path = "/Users/hfu/src/globalm/bnda_1_1/bnda_1_1.shp";
        DriverProperties dp = new DriverProperties(path);
        ShapefileReader r = new ShapefileReader();
        FeatureCollection fc = r.read(dp);
        // FeatureSchema fs = fc.getFeatureSchema(); // for future use.
        Iterator iter = fc.iterator(); 
	while(iter.hasNext()) {
	    Feature feat = (Feature) iter.next();

	    // to demonstrate that the geometry could be read.
	    System.out.println(feat.getGeometry().toString());
	}
    }
}

実行

$ javac ReadShapefile.java
$ java ReadShapefile

(OpenJUMP) の必要なライブラリファイルを Java コンパイラ及び Java インタプリタに知らせる必要がありますが、それは別の話です。

感想(メモ)

Ruby を介して Java ライブラリを使用することに慣れた身としては、次の2点が苦痛でした。

  1. 使用するすべての変数について、型を明示しなければならない。型は、原則として一つ一つをインポートしなければならない*1
  2. Java にはパッケージ管理ソフトがない。クラスパス指定は、コンパイラ系・実行系が行うものであり、プログラムには記述できない。(Ruby にはパッケージ管理ソフトがある。JRuby も Rjb も、読み込むべき JAR ファイルを指定できる。)

*1:たぶん、Joshua Bloch の教えによると。