Geotools处理shape文件
shape文件结构
- filename.shp: shapes
- filename.shx: 索引文件
- filename.dbf: 结构化数据文件
- filename.qix: 空间索引文件
- filename.fix: fid索引文件
- filename.sld: 样式文件
依赖
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-main</artifactId>
<version>27.2</version>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-shapefile</artifactId>
<version>27.2</version>
</dependency>
创建连接
连接参数
Parameter | required | Description |
---|---|---|
url | true | .shp文件的url |
namespace | false | FeatureType的URI |
create spatial index | false | 是否创建空间索引,默认true |
charset | false | 解码DBF文件的编码,默认ISO_8859_1 |
timezone | false | 解析DBF文件时间的时区 |
memory mapped buffer | false | 内存映射,默认false |
cache and reuse memory maps | false | 使用内存映射时,缓存并重用,默认true |
enable spatial index | false | 是否使用空间索引,默认true |
代码示例
/**
*
* 创建shape文件
*
* */
FileDataStoreFactorySpi factory = new ShapefileDataStoreFactory();
//新建文件
File file = new File("my.shp");
//datastore
Map<String, Object> params = new HashMap<>();
params.put("url", file.toURI().toURL());
params.put("create spatial index", Boolean.TRUE);
DataStore dataStore = factory.createNewDataStore(params);
//Feature数据定义
SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
//图层名
typeBuilder.setName("myLayer");
//空间坐标
typeBuilder.setCRS(CRS.decode("EPSG:4490"));
typeBuilder.add("the_geom", Point.class);
//普通属性字段
typeBuilder.add("id", String.class);
typeBuilder.length(10).add("name", String.class);//字段长度
typeBuilder.add("number",Integer.class);
typeBuilder.add("double",Double.class);
typeBuilder.add("time",Date.class);
SimpleFeatureType featureType = typeBuilder.buildFeatureType();
//创建feature定义到shape
dataStore.createSchema(featureType);
/**
* 写数据到shape文件
*
* */
//shape中feature定义
Map<String, Object> params = new HashMap<>();
params.put("url", file.toURI().toURL());
DataStore dataStore2 = DataStoreFinder.getDataStore(params);
String typeName = dataStore2.getTypeNames()[0];
SimpleFeatureSource featureSource = dataStore2.getFeatureSource(typeName);
SimpleFeatureType schema = featureSource.getSchema();
//事务处理
Transaction transaction = new DefaultTransaction("create");
if (featureSource instanceof SimpleFeatureStore) {
SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
//创建一条数据 feature
List<SimpleFeature> features = new ArrayList<>();
GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(schema);
Point point = geometryFactory.createPoint(new Coordinate(106.69563874,29.563694210810283));
featureBuilder.set("the_geom",point);
featureBuilder.set("id","id1");
featureBuilder.set("name","name1");
featureBuilder.set("number1",100);
featureBuilder.set("number2",66.0);
featureBuilder.set("time",new Date());
SimpleFeature feature = featureBuilder.buildFeature(null);
features.add(feature);
SimpleFeatureCollection collection = new ListFeatureCollection(schema, features);
featureStore.setTransaction(transaction);
try {
//写入shape中
featureStore.addFeatures(collection);
transaction.commit();
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
} finally {
transaction.close();
}
} else {
System.out.println("写入失败");
}
/**
* 读取shape文件数据
*
* */
DataStore dataStore3 = new ShapefileDataStore(file.toURI().toURL());
String typeName = dataStore3.getTypeNames()[0];
FeatureSource<SimpleFeatureType, SimpleFeature> source =dataStore3.getFeatureSource(typeName);
Filter filter = Filter.INCLUDE;
FeatureCollection<SimpleFeatureType, SimpleFeature> collection = source.getFeatures(filter);
try (FeatureIterator<SimpleFeature> features = collection.features()) {
//features必须关闭,否则会造成内存泄漏
while (features.hasNext()) {
SimpleFeature feature = features.next();
feature.getProperties().stream().peek(e-> System.out.println(e.getName().getLocalPart() + " = " + e.getValue()));
}
}
热门相关:帝少的专属:小甜心,太缠人 刺客之王 横行霸道 战神 夫人,你马甲又掉了!