在创建一个新的Feature对象前要定义一个FeatureType
SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();
//设置名字
b.setName( "Flag" );
//添加属性
b.add( "name", String.class );
b.add( "classification", Integer.class );
b.add( "height", Double.class );
//add a geometry property
b.setCRS( DefaultGeographicCRS.WSG84 ); // set crs first
b.add( "location", Point.class ); // then add geometry
//build the type
final SimpleFeatureType TYPE = b.buildFeatureType();
或者
final SimpleFeatureType TYPE =
DataUtilities.createType(
"Location",
"the_geom:Point:srid=4326,"
+ // <- the geometry attribute: Point type
"name:String,"
+ // <- a String attribute
"number:Integer" // a number attribute
);
System.out.println("TYPE:" + TYPE);
修改FeatureType
抱歉,您没法改,因为其是用final
修饰的
但是,您可以创建一个新的FeatureType
SimpleFeatureType lineType = DataUtilities.createType("LINE", "geom:LineString,name:\"\",id:0");
SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();
//使用预先存在的要素类型的状态初始化生成器
b.init( lineType );
b.setName("POINT");
b.add(0, "geom", Point.class );
SimpleFeatureType pointType = b.buildFeatureType();
FeatureCollection
FeatureCollection的读取
FeatureCollection无法通过foreach来读取,FeatureCollection是对实时数据流的包装;因此,我们需要确保在使用完后关闭迭代器。
try (SimpleFeatureIterator iterator = features.features()) {
while (iterator.hasNext()) {
SimpleFeature feature = iterator.next();
// process feature
}
}
这里try
的写法和一般有些不同,它使用了try-with-resource
结构,会在try完毕后自动关闭stream。
DataUtilities是一个数据工具类,类似java里的util,全是静态方法。
创建FeatureType对于制作测试用例时快速创建 FeatureType 非常有用,有以下两个方法
public static SimpleFeatureType createType(String typeName, String typeSpec)
public static SimpleFeatureType createType(String namespace, String name, String typeSpec)
快速创建一个FeatureType
SimpleFeatureType lineType = DataUtilities.createType("LINE", "centerline:LineString,name:\"\",id:0");
也可以选择使用类名
SimpleFeatureType schema = DataUtilities.createType("EDGE", "edge:Polygon,name:String,timestamp:java.util.Date");
顺便把参考系也加上
SimpleFeatureType lineType = DataUtilities.createType("LINE", "centerline:LineString:srid=32615,name:\"\",id:0");
使用命名空间
SimpleFeatureType lineType = DataUtilities.createType("http://somewhere.net/","LINE", "centerline:LineString,name:\"\",id:0");
修改FeatureType
无法修改,但可以创建一个副本
FeatureType schema = DataUtilities.createType("EDGE", "edge:Polygon,name:String");
System.out.println("1:"+schema.hashCode());
CoordinateReferenceSystem crs = CRS.decode( "EPSG:4326" );
schema = DataUtilities.createSubType((SimpleFeatureType) schema, null, crs );
System.out.println("2:"+schema.hashCode());
FeatureType schema = DataUtilities.createType("EDGE", "edge:Polygon,name:String,timestamp:java.util.Date");
System.out.println(schema);
CoordinateReferenceSystem crs = CRS.decode( "EPSG:4326" );
schema = DataUtilities.createSubType((SimpleFeatureType) schema, new String[]{"edge","name"}, crs );
System.out.println(schema);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)