(资料图片仅供参考)
org.geotools gt-main 27.2 org.geotools gt-shapefile 27.2
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");//datastoreMap 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定义到shapedataStore.createSchema(featureType);/** * 写数据到shape文件 * * *///shape中feature定义Map 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 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 source =dataStore3.getFeatureSource(typeName);Filter filter = Filter.INCLUDE;FeatureCollection collection = source.getFeatures(filter);try (FeatureIterator features = collection.features()) { //features必须关闭,否则会造成内存泄漏 while (features.hasNext()) { SimpleFeature feature = features.next(); feature.getProperties().stream().peek(e-> System.out.println(e.getName().getLocalPart() + " = " + e.getValue())); }}
标签: