SimpleFeature の構築
HBase Ganos は、SimpleFeature クラスを使用して空間フィーチャを指定します。各 SimpleFeature は、ID、Geometry オブジェクト、およびその他の属性で構成されます。 GeoTools API は、SimpleFeature オブジェクトの作成に役立つ SimpleFeatureBuilder クラスを提供します。
SimpleFeatureType sft = ....; SimpleFeatureBuilder sfBuilder = new SimpleFeatureBuilder(sft); builder.set("attribute name", attribute value); ... builder.set("geom", geometry); SimpleFeature feature = builder.buildFeature(object_id + "_" + date.getTime());
// SimpleFeature を構築する場合、Ganos はデフォルトで 128 ビットの UUID をフィーチャ ID として生成します。ストレージ容量を節約するために、ユーザーは独自の ID を指定できます。具体的なメソッドには、次のステートメントを使用できます。
説明 SimpleFeature を構築する場合、Ganos はデフォルトで 128 ビットの UUID をフィーチャ ID として生成します。ストレージ容量を節約するために、ユーザーは独自の ID を指定できます。具体的なメソッドには、次のステートメントを使用できます。
SimpleFeature feature =... feature.getUserData().put(Hints.USE_PROVIDED_FID, java.lang.Boolean.TRUE);
// SimpleFeature feature = ... feature.getUserData().put(Hints.USE_PROVIDED_FID, java.lang.Boolean.TRUE);
Geometry オブジェクトの作成
各 SimpleFeature には、フィーチャの空間オブジェクトを示す Geometry オブジェクトが含まれています。Geometry の各空間エンティティオブジェクトの定義については、公式ドキュメント を参照してください。
GeoTools API は、Geometry オブジェクトの作成に役立つ GeometryFactory クラスを提供します。Geometry オブジェクトは、次の方法で作成できます。
- ポイント
- Coordinate オブジェクトを使用します。
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(); Coordinate coord = new Coordinate(1, 1); Point point = geometryFactory.createPoint(coord); // GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(); Coordinate coord = new Coordinate(1, 1); Point point = geometryFactory.createPoint(coord);
- WKT による記述: WKT(Well-known text)は、ベクトル空間オブジェクト、空間参照系、および空間参照系間の変換を表すために使用されるテキストマークアップ言語です。
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(); WKTReader reader = new WKTReader(geometryFactory); Point point = (Point) reader.read("POINT (1 1)"); // GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(); WKTReader reader = new WKTReader(geometryFactory); Point point = (Point) reader.read("POINT (1 1)");
- Coordinate オブジェクトを使用します。
- ライン
- Coordinate オブジェクトを使用します。
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(); Coordinate[] coords = new Coordinate[] {new Coordinate(0, 2), new Coordinate(2, 0), new Coordinate(8, 6) }; LineString line = geometryFactory.createLineString(coordinates); // GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(); Coordinate[] coords = new Coordinate[] {new Coordinate(0, 2), new Coordinate(2, 0), new Coordinate(8, 6) }; LineString line = geometryFactory.createLineString(coordinates);
- WKT による記述
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(); WKTReader reader = new WKTReader( geometryFactory ); LineString line = (LineString) reader.read("LINESTRING(0 2, 2 0, 8 6)"); // GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(); WKTReader reader = new WKTReader( geometryFactory ); LineString line = (LineString) reader.read("LINESTRING(0 2, 2 0, 8 6)");
- Coordinate オブジェクトを使用します。
- ポリゴン
- Coordinate オブジェクトを使用します。
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(); Coordinate[] coords = new Coordinate[] {new Coordinate(4, 0), new Coordinate(2, 2), new Coordinate(4, 4), new Coordinate(6, 2), new Coordinate(4, 0) }; LinearRing ring = geometryFactory.createLinearRing( coords ); LinearRing holes[] = null; // use LinearRing[] to represent holes Polygon polygon = geometryFactory.createPolygon(ring, holes ); // GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(); Coordinate[] coords = new Coordinate[] {new Coordinate(4, 0), new Coordinate(2, 2), new Coordinate(4, 4), new Coordinate(6, 2), new Coordinate(4, 0) }; LinearRing ring = geometryFactory.createLinearRing( coords ); LinearRing holes[] = null; // 穴を表すには LinearRing[] を使用します。 Polygon polygon = geometryFactory.createPolygon(ring, holes );
- WKT による記述
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null ); WKTReader reader = new WKTReader( geometryFactory ); Polygon polygon = (Polygon) reader.read("POLYGON((20 10, 30 0, 40 10, 30 20, 20 10))"); // GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null ); WKTReader reader = new WKTReader( geometryFactory ); Polygon polygon = (Polygon) reader.read("POLYGON((20 10, 30 0, 40 10, 30 20, 20 10))");
- Coordinate オブジェクトを使用します。
ApsaraDB for RDS へのデータ挿入
HBase Ganos は、GeoTools API の SimpleFeatureWriter クラスを使用してデータを書き込みます。 SimpleFeatureWriter はトランザクションをサポートし、DataStore の getFeatureWriterAppend メソッドを呼び出すことで構築できます。
- 単一の SimpleFeature の挿入
SimpleFeatureType sft = ....; SimpleFeatureWriter writer=(SimpleFeatureWriter)ds.getFeatureWriterAppend(sft.getTypeName(), Transaction.AUTO_COMMIT); SimpleFeature toWrite=writer.next(); toWrite.setAttributes(feature.getAttributes()); toWrite.getUserData().putAll(feature.getUserData()); writer.write(); writer.close(); // SimpleFeatureType sft = ....; SimpleFeatureWriter writer=(SimpleFeatureWriter)ds.getFeatureWriterAppend(sft.getTypeName(), Transaction.AUTO_COMMIT); SimpleFeature toWrite=writer.next(); toWrite.setAttributes(feature.getAttributes()); toWrite.getUserData().putAll(feature.getUserData()); writer.write(); writer.close();
- SimpleFeature のバッチ挿入
HBase Ganos は、GeoTools API の SimpleFeatureStore クラスを介して SimpleFeature のバッチ挿入をサポートしています。
List<SimpleFeature> features=... SimpleFeatureStore featureStore = (SimpleFeatureStore) ds.getFeatureSource(sft.getTypeName()); List<FeatureId> featureIds = featureStore.addFeatures(new ListFeatureCollection(sft,features)); // List features=... SimpleFeatureStore featureStore = (SimpleFeatureStore) ds.getFeatureSource(sft.getTypeName()); List featureIds = featureStore.addFeatures(new ListFeatureCollection(sft,features));