geotools隨筆:FeatureType的創(chuàng)建與修改
geotools隨筆
FeatureType
創(chuàng)建FeatureType
在創(chuàng)建一個(gè)新的Feature對(duì)象前要定義一個(gè)FeatureType
SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();
//設(shè)置名字
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
抱歉,您沒法改,因?yàn)槠涫怯胒inal修飾的
但是,您可以創(chuàng)建一個(gè)新的FeatureType
SimpleFeatureType lineType = DataUtilities.createType("LINE", "geom:LineString,name:\"\",id:0");
SimpleFeatureTypeBuilder b = new SimpleFeatureTypeBuilder();
//使用預(yù)先存在的要素類型的狀態(tài)初始化生成器
b.init( lineType );
b.setName("POINT");
b.add(0, "geom", Point.class );
SimpleFeatureType pointType = b.buildFeatureType();
FeatureCollection
FeatureCollection的讀取
FeatureCollection無法通過foreach來讀取,F(xiàn)eatureCollection是對(duì)實(shí)時(shí)數(shù)據(jù)流的包裝;因此,我們需要確保在使用完后關(guān)閉迭代器。
try (SimpleFeatureIterator iterator = features.features()) {
while (iterator.hasNext()) {
SimpleFeature feature = iterator.next();
// process feature
}
}
這里try的寫法和一般有些不同,它使用了try-with-resource結(jié)構(gòu),會(huì)在try完畢后自動(dòng)關(guān)閉stream。
DataUtilities
DataUtilities是一個(gè)數(shù)據(jù)工具類,類似java里的util,全是靜態(tài)方法。
創(chuàng)建FeatureType
對(duì)于制作測(cè)試用例時(shí)快速創(chuàng)建 FeatureType 非常有用,有以下兩個(gè)方法
public static SimpleFeatureType createType(String typeName, String typeSpec)
public static SimpleFeatureType createType(String namespace, String name, String typeSpec)
快速創(chuàng)建一個(gè)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
無法修改,但可以創(chuàng)建一個(gè)副本
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);
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。

在創(chuàng)建FeatureType時(shí),如何設(shè)置其屬性和幾何屬性?
答案:可以通過`add`方法添加屬性,使用`setCRS`方法設(shè)置坐標(biāo)參考系統(tǒng),并使用`add`方法添加幾何屬性。