JUMP (OpenJUMP) で Java を使って Shapefile を読むだけのコード
JUMP (OpenJUMP) で Java 言語を使って Shapefile を読むだけのコードを作成してみました。
Geo::Reader の Deegree 版と JUMP 版を雑に作ってみました - Relevant, Timely, and Accurate の翻案ですが、Ruby (JRuby/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 インタプリタに知らせる必要がありますが、それは別の話です。
感想(メモ)
*1:たぶん、Joshua Bloch の教えによると。