全部产品
云市场

导入数据

更新时间:2019-02-27 10:55:46

1. 构建SimpleFeature

构建SimpleFeature一般流程

HBase Ganos通过SimpleFeature类表示空间要素。每个SimpleFeature由ID,Geometry对象与其他属性构成,GeoTools API提供了SimpleFeatureBuilder类帮助用户创建SimpleFeature对象:

  1. SimpleFeatureType sft = ....;
  2. SimpleFeatureBuilder sfBuilder = new SimpleFeatureBuilder(sft);
  3. builder.set("属性名", 属性值);
  4. ...
  5. builder.set("geom", Geometry);
  6. SimpleFeature feature = builder.buildFeature(object_id + "_" + date.getTime());

注意: 构建SimpleFeature时,Ganos会默认生成128位的UUID作为Feature ID。为了节省存储空间,用户可以自己指定ID,具体方式为:

  1. SimpleFeature feature =...
  2. feature.getUserData().put(Hints.USE_PROVIDED_FID, java.lang.Boolean.TRUE);

创建Geometry对象:

每个SimpleFeature包含一个Geometry对象用来表示要素的空间对象。Geometry的各个空间实体对象定义以及关系如下图所示,具体请参考http://docs.geotools.org/stable/userguide/library/jts/geometry.html

GeoTools API提供了GeometryFactory工具类帮助用户创建Geometry对象,具体方法如下:

  • 点要素方法 1: 通过Coordinate对象
    1. GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
    2. Coordinate coord = new Coordinate(1, 1);
    3. Point point = geometryFactory.createPoint(coord);
    方法 2: 通过WKT描述(WKT: https://en.wikipedia.org/wiki/Well-known_text):WKT(Well-known text)是一种文本标记语言,用于表示矢量空间对象、空间参照系统及空间参照系统之间的转换
    1. GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
    2. WKTReader reader = new WKTReader(geometryFactory);
    3. Point point = (Point) reader.read("POINT (1 1)");
  • 线要素方法 1: 通过Coordinate对象
    1. GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
    2. Coordinate[] coords =
    3. new Coordinate[] {new Coordinate(0, 2), new Coordinate(2, 0), new Coordinate(8, 6) };
    4. LineString line = geometryFactory.createLineString(coordinates);
    方法 2: 通过WKT描述:
  1. GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
  2. WKTReader reader = new WKTReader( geometryFactory );
  3. LineString line = (LineString) reader.read("LINESTRING(0 2, 2 0, 8 6)");
  • 面要素方法 1: 通过Coordinate对象
  1. GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
  2. Coordinate[] coords =
  3. new Coordinate[] {new Coordinate(4, 0), new Coordinate(2, 2),
  4. new Coordinate(4, 4), new Coordinate(6, 2), new Coordinate(4, 0) };
  5. LinearRing ring = geometryFactory.createLinearRing( coords );
  6. LinearRing holes[] = null; // use LinearRing[] to represent holes
  7. Polygon polygon = geometryFactory.createPolygon(ring, holes );

方法 2: 通过WKT描述:

  1. GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory( null );
  2. WKTReader reader = new WKTReader( geometryFactory );
  3. Polygon polygon = (Polygon) reader.read("POLYGON((20 10, 30 0, 40 10, 30 20, 20 10))");

2. 数据入库

HBase Ganos通过GeoTools API中的SimpleFeatureWriter写入数据,SimpleFeatureWriter支持事务,可以通过DataStore的getFeatureWriterAppend方法获取。

  • 插入单个SimpleFeature:

    1. SimpleFeatureType sft = ....;
    2. SimpleFeatureWriter writer=(SimpleFeatureWriter)ds.getFeatureWriterAppend(sft.getTypeName(), Transaction.AUTO_COMMIT);
    3. SimpleFeature toWrite=writer.next();
    4. toWrite.setAttributes(feature.getAttributes());
    5. toWrite.getUserData().putAll(feature.getUserData());
    6. writer.write();
    7. writer.close();
  • 批量插入SimpleFeature:

HBase Ganos支持批量插入SimpleFeature,通过GeoTools API中的SimpleFeatureStore类实现:

  1. List<SimpleFeature> features=...
  2. SimpleFeatureStore featureStore = (SimpleFeatureStore) ds.getFeatureSource(sft.getTypeName());
  3. List<FeatureId> featureIds = featureStore.addFeatures(new ListFeatureCollection(sft,features));