Geotools处理shape文件

打印 上一主题 下一主题

主题 951|帖子 951|积分 2853

shape文件结构


  • filename.shp: shapes
  • filename.shx: 索引文件
  • filename.dbf: 结构化数据文件
  • filename.qix: 空间索引文件
  • filename.fix: fid索引文件
  • filename.sld: 样式文件
依赖
  1. <dependency>
  2.     <groupId>org.geotools</groupId>
  3.     <artifactId>gt-main</artifactId>
  4.     <version>27.2</version>
  5. </dependency>
  6. <dependency>
  7.     <groupId>org.geotools</groupId>
  8.     <artifactId>gt-shapefile</artifactId>
  9.     <version>27.2</version>
  10. </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代码示例
  1. /**
  2. *
  3. * 创建shape文件
  4. *
  5. * */
  6. FileDataStoreFactorySpi factory = new ShapefileDataStoreFactory();
  7. //新建文件
  8. File file = new File("my.shp");
  9. //datastore
  10. Map<String, Object> params = new HashMap<>();
  11. params.put("url", file.toURI().toURL());
  12. params.put("create spatial index", Boolean.TRUE);
  13. DataStore dataStore = factory.createNewDataStore(params);
  14. //Feature数据定义
  15. SimpleFeatureTypeBuilder typeBuilder = new SimpleFeatureTypeBuilder();
  16. //图层名
  17. typeBuilder.setName("myLayer");
  18. //空间坐标
  19. typeBuilder.setCRS(CRS.decode("EPSG:4490"));
  20. typeBuilder.add("the_geom", Point.class);
  21. //普通属性字段
  22. typeBuilder.add("id", String.class);
  23. typeBuilder.length(10).add("name", String.class);//字段长度
  24. typeBuilder.add("number",Integer.class);
  25. typeBuilder.add("double",Double.class);
  26. typeBuilder.add("time",Date.class);
  27. SimpleFeatureType featureType = typeBuilder.buildFeatureType();
  28. //创建feature定义到shape
  29. dataStore.createSchema(featureType);
  30. /**
  31. * 写数据到shape文件
  32. *
  33. * */
  34. //shape中feature定义
  35. Map<String, Object> params = new HashMap<>();
  36. params.put("url", file.toURI().toURL());
  37. DataStore dataStore2 = DataStoreFinder.getDataStore(params);
  38. String typeName = dataStore2.getTypeNames()[0];
  39. SimpleFeatureSource featureSource = dataStore2.getFeatureSource(typeName);
  40. SimpleFeatureType schema = featureSource.getSchema();
  41. //事务处理
  42. Transaction transaction = new DefaultTransaction("create");
  43. if (featureSource instanceof SimpleFeatureStore) {
  44.     SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;
  45.     //创建一条数据 feature
  46.     List<SimpleFeature> features = new ArrayList<>();
  47.     GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory();
  48.     SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(schema);
  49.     Point point = geometryFactory.createPoint(new Coordinate(106.69563874,29.563694210810283));
  50.     featureBuilder.set("the_geom",point);
  51.     featureBuilder.set("id","id1");
  52.     featureBuilder.set("name","name1");
  53.     featureBuilder.set("number1",100);
  54.     featureBuilder.set("number2",66.0);
  55.     featureBuilder.set("time",new Date());
  56.     SimpleFeature feature = featureBuilder.buildFeature(null);
  57.     features.add(feature);
  58.     SimpleFeatureCollection collection = new ListFeatureCollection(schema, features);
  59.     featureStore.setTransaction(transaction);
  60.     try {
  61.         //写入shape中
  62.         featureStore.addFeatures(collection);
  63.         transaction.commit();
  64.     } catch (Exception e) {
  65.         e.printStackTrace();
  66.         transaction.rollback();
  67.     } finally {
  68.         transaction.close();
  69.     }
  70. } else {
  71.     System.out.println("写入失败");
  72. }
  73. /**
  74. * 读取shape文件数据
  75. *
  76. * */
  77. DataStore dataStore3 = new ShapefileDataStore(file.toURI().toURL());
  78. String typeName = dataStore3.getTypeNames()[0];
  79. FeatureSource<SimpleFeatureType, SimpleFeature> source =dataStore3.getFeatureSource(typeName);
  80. Filter filter = Filter.INCLUDE;
  81. FeatureCollection<SimpleFeatureType, SimpleFeature> collection = source.getFeatures(filter);
  82. try (FeatureIterator<SimpleFeature> features = collection.features()) {
  83.     //features必须关闭,否则会造成内存泄漏
  84.     while (features.hasNext()) {
  85.         SimpleFeature feature = features.next();
  86.         feature.getProperties().stream().peek(e-> System.out.println(e.getName().getLocalPart() + " = " + e.getValue()));
  87.     }
  88. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

去皮卡多

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表