GeoTools提供了DataStore接口,用于表示一个包含空间数据(spatial data)的文件、数据库、服务,即:空间数据源。API结构如下所示
FeatureSource被用于读取数据源中的Feature要素数据,其子类FeatureStore
拥有对数据源的读写权限。对shp文件要素的操作主要是通过FeatureStore实现的
一、新增要素(addFeatures)
@Test
public void addFeaturesToShp() throws IOException {
//Geometry工厂类
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
//创建要素类
final SimpleFeatureType TYPE = createFeatureType(Point.class);
String name = "dsandjkadmskladakndsaldmalkdmaldkas";
Random random = new Random();
//创建10个新的Feature要素-[带有属性信息的简单Feature-SimpleFeature]
List<SimpleFeature> features = new ArrayList<>();
for (int i = 0; i < 10; i++) {
//通过featureType创建SimpleFeatureBuilder,用于后续构建要素
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(TYPE);
Point point = geometryFactory.createPoint(new Coordinate(120.0 + Math.random(), 32.0 + Math.random()));
//注意字段添加顺序
featureBuilder.add(point);
featureBuilder.add(name.substring(0, random.nextInt(name.length())));
featureBuilder.add(10 + random.nextInt(20));
SimpleFeature simpleFeature = featureBuilder.buildFeature(null);//创建Feature实例
features.add(simpleFeature);
}
//向shp文件中添加新的Feature要素
SimpleFeatureCollection collection = new ListFeatureCollection(TYPE, features);
//获取Shapefile数据源
String filePath = "F:/data/location.shp";
File file = new File(filePath);
DataStore dataStore = new ShapefileDataStore(file.toURI().toURL());//创建ShapefileDataStore实例
SimpleFeatureSource featureSource = dataStore.getFeatureSource(dataStore.getTypeNames()[0]);//获取FeatureSource
if (featureSource instanceof SimpleFeatureStore) {
SimpleFeatureStore store = (SimpleFeatureStore) featureSource; // write access!
store.addFeatures(collection);
}
}
二、修改要素(modifyFeatures)
//根据条件修改记录
@Test
public void modifyFeatureFromShpByFilter() throws IOException, CQLException {
//获取数据源
String filePath = "F:/data/location.shp";
File file = new File(filePath);
DataStore dataStore = new ShapefileDataStore(file.toURI().toURL());//创建ShapefileDataStore实例
SimpleFeatureSource featureSource = dataStore.getFeatureSource(dataStore.getTypeNames()[0]);//获取FeatureSource
if (featureSource instanceof SimpleFeatureStore) {
SimpleFeatureStore store = (SimpleFeatureStore) featureSource; // write access!
Filter filter = CQL.toFilter("name = '' OR name IS NULL");
store.modifyFeatures("name", "xxx", filter);
}
}
三、删除要素(removeFeatures)
//根据条件删除记录
@Test
public void deleteFeatureFromShp() throws IOException, CQLException {
//获取数据源
String filePath = "F:/data/location.shp";
File file = new File(filePath);
DataStore dataStore = new ShapefileDataStore(file.toURI().toURL());//创建ShapefileDataStore实例
SimpleFeatureSource featureSource = dataStore.getFeatureSource(dataStore.getTypeNames()[0]);//获取FeatureSource
if (featureSource instanceof SimpleFeatureStore) {
SimpleFeatureStore store = (SimpleFeatureStore) featureSource; // write access!
Filter filter = CQL.toFilter("name = 'xxx'");
store.removeFeatures(filter);
}
}
四、读取要素(getFeatures)
@Test
public void readShapefile() throws IOException {
String filePath = "F:/data/location.shp";
File file = new File(filePath);
DataStore dataStore = new ShapefileDataStore(file.toURI().toURL());//创建ShapefileDataStore实例
String[] typeNames = dataStore.getTypeNames();//获取数据源中所有可获取的图层名称
System.out.println(Arrays.toString(typeNames));
//逐个解析图层数据
for (int i = 0; i < typeNames.length; i++) {
//图层名称
String typeName = typeNames[i];
//featureSource用于读取对应图层中的Feature要素数据
SimpleFeatureSource featureSource = dataStore.getFeatureSource(typeName);
//获取FeatureSource中的Feature集合
SimpleFeatureCollection features = featureSource.getFeatures();
//获取集合迭代器
SimpleFeatureIterator iterator = features.features();
while (iterator.hasNext()) {
SimpleFeature next = iterator.next();
//要素类
SimpleFeatureType featureType = next.getFeatureType();
//获取对应的类属性
List<AttributeDescriptor> attributeDescriptors = featureType.getAttributeDescriptors();
for (int j = 0; j < attributeDescriptors.size(); j++) {
Name name = attributeDescriptors.get(j).getName();
System.out.println(name + ":" + next.getAttribute(name));
}
}
}
}