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>
复制代码 创建连接
连接参数
ParameterrequiredDescriptionurltrue.shp文件的urlnamespacefalseFeatureType的URIcreate spatial indexfalse是否创建空间索引,默认truecharsetfalse解码DBF文件的编码,默认ISO_8859_1timezonefalse解析DBF文件时间的时区memory mapped bufferfalse内存映射,默认falsecache and reuse memory mapsfalse使用内存映射时,缓存并重用,默认trueenable spatial indexfalse是否使用空间索引,默认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()));
- }
- }
复制代码 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |