本文介紹HBase Ganos時空引擎功能如何通過API匯入資料。
構建SimpleFeature
HBase Ganos通過SimpleFeature類表示空間要素。每個SimpleFeature由ID,Geometry對象與其他屬性構成,GeoTools API提供了SimpleFeatureBuilder類協助使用者建立SimpleFeature對象。
SimpleFeatureType sft = ....;
SimpleFeatureBuilder sfBuilder = new SimpleFeatureBuilder(sft);
builder.set("屬性名稱", 屬性值);
...
builder.set("geom", Geometry);
SimpleFeature feature = builder.buildFeature(object_id + "_" + date.getTime());
說明 構建SimpleFeature時,Ganos會預設產生128位的UUID作為Feature ID。為了節省儲存空間,使用者可以自己指定ID,具體方式可使用以下語句。
SimpleFeature feature =...
feature.getUserData().put(Hints.USE_PROVIDED_FID, java.lang.Boolean.TRUE); 建立Geometry對象
每個SimpleFeature包含一個Geometry對象用來表示要素的空間對象。Geometry的各個空間實體物件定義具體請參見空間對象說明。
GeoTools API提供了GeometryFactory工具類協助使用者建立Geometry對象,具體方法如下:
- 點要素
- 通過Coordinate對象建立Geometry。
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(); Coordinate coord = new Coordinate(1, 1); Point point = geometryFactory.createPoint(coord); - 通過WKT語言建立Geometry。Well-known text(簡稱WKT)是一種文本標記語言,用於表示向量空間對象、空間參照系統及空間參照系統之間的轉換。
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(); WKTReader reader = new WKTReader(geometryFactory); Point point = (Point) reader.read("POINT (1 1)");
- 通過Coordinate對象建立Geometry。
- 線要素
- 通過Coordinate對象建立Geometry。
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語言建立Geometry。
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(); WKTReader reader = new WKTReader( geometryFactory ); LineString line = (LineString) reader.read("LINESTRING(0 2, 2 0, 8 6)");
- 通過Coordinate對象建立Geometry。
- 面要素
- 通過Coordinate對象建立Geometry。
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 ); - 通過WKT語言建立Geometry。
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對象建立Geometry。
資料入庫
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(); - 批量插入SimpleFeature。
HBase Ganos支援批量插入SimpleFeature,通過GeoTools API中的SimpleFeatureStore類實現。
List<SimpleFeature> features=... SimpleFeatureStore featureStore = (SimpleFeatureStore) ds.getFeatureSource(sft.getTypeName()); List<FeatureId> featureIds = featureStore.addFeatures(new ListFeatureCollection(sft,features));