GeoToolsSpatialOverlay.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //package com.onemap.overlap.utils;
  2. //
  3. //import org.geotools.data.DataStore;
  4. //import org.geotools.data.DataStoreFinder;
  5. //import org.geotools.data.FeatureSource;
  6. //import org.geotools.data.Transaction;
  7. //import org.geotools.data.memory.MemoryDataStore;
  8. //import org.geotools.data.shapefile.ShapefileDataStore;
  9. //import org.geotools.data.shapefile.ShapefileDataStoreFactory;
  10. //import org.geotools.data.simple.SimpleFeatureCollection;
  11. //import org.geotools.data.simple.SimpleFeatureWriter;
  12. //import org.geotools.feature.FeatureCollection;
  13. //import org.geotools.feature.DefaultFeatureCollection; // 引入 DefaultFeatureCollection
  14. //import org.geotools.feature.FeatureIterator;
  15. //import org.geotools.feature.simple.SimpleFeatureBuilder;
  16. //import org.geotools.feature.simple.SimpleFeatureTypeImpl;
  17. //import org.locationtech.jts.geom.Geometry;
  18. //import org.locationtech.jts.geom.GeometryFactory;
  19. //import org.opengis.feature.Feature;
  20. //import org.opengis.feature.simple.SimpleFeature;
  21. //import org.opengis.feature.simple.SimpleFeatureType;
  22. //
  23. //import java.io.File;
  24. //import java.util.HashMap;
  25. //import java.util.Iterator;
  26. //import java.util.Map;
  27. //
  28. //public class GeoToolsSpatialOverlay {
  29. //
  30. // // 加载 Shapefile 文件并返回 FeatureCollection
  31. // public static FeatureCollection loadShapefile(String shapefilePath) throws Exception {
  32. // File file = new File(shapefilePath);
  33. // ShapefileDataStore dataStore = new ShapefileDataStore(file.toURI().toURL());
  34. // String typeName1 = dataStore.getTypeNames()[0];
  35. // FeatureSource<SimpleFeatureType, SimpleFeature> featureSource1 = dataStore.getFeatureSource(typeName1);
  36. // FeatureCollection<SimpleFeatureType, SimpleFeature> featureCollection = featureSource1.getFeatures();
  37. //
  38. // // 将第一个 Shapefile 数据加载到内存
  39. // MemoryDataStore memoryDataStore = new MemoryDataStore();
  40. // memoryDataStore.addFeatures(featureCollection);
  41. //// memoryDataStore.setSpatialIndex(true); // 启用空间索引(例如,R-tree)
  42. //
  43. // return featureCollection;
  44. // }
  45. //
  46. // // 执行交集(Intersection)分析
  47. // public static FeatureCollection performIntersection(FeatureCollection layer1, FeatureCollection layer2) throws Exception {
  48. // // 使用 DefaultFeatureCollection 来存储交集结果
  49. // FeatureCollection resultCollection = new DefaultFeatureCollection();
  50. //
  51. // GeometryFactory geometryFactory = new GeometryFactory();
  52. //
  53. // // 遍历第一个图层
  54. //// for (Feature feature : layer1) {
  55. ////
  56. //// }
  57. // FeatureIterator iterator1 = layer1.features();
  58. // while (iterator1.hasNext()) {
  59. // SimpleFeature feature1 = (SimpleFeature)iterator1.next();
  60. // Geometry geom1 = (Geometry) feature1.getDefaultGeometry();
  61. // // 遍历第二个图层
  62. // FeatureIterator iterator2 = layer2.features();
  63. // while (iterator2.hasNext()) {
  64. // SimpleFeature simpleFeature2 = (SimpleFeature)iterator2.next();
  65. // Geometry geom2 = (Geometry) simpleFeature2.getDefaultGeometry();
  66. // // 如果两者有交集,计算交集
  67. // if (geom1.intersects(geom2)) {
  68. // Geometry intersection = geom1.intersection(geom2);
  69. // // 将交集结果添加到新的 FeatureCollection
  70. // SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(feature1.getFeatureType());
  71. // SimpleFeature newFeature = featureBuilder.buildFeature(null);
  72. // newFeature.setAttribute("the_geom", intersection);
  73. // ((DefaultFeatureCollection) resultCollection).add(newFeature);
  74. // }
  75. // }
  76. // }
  77. //
  78. // return resultCollection;
  79. // }
  80. //
  81. // // 导出结果到新的 Shapefile 文件
  82. // public static void exportToShapefile(FeatureCollection featureCollection, String outputShapefilePath) throws Exception {
  83. // File outputFile = new File(outputShapefilePath);
  84. // Map<String, Object> params = new HashMap<>();
  85. // params.put(ShapefileDataStoreFactory.URLP.key, outputFile.toURI().toURL());
  86. //
  87. // // 创建新的 Shapefile DataStore
  88. // ShapefileDataStore dataStore = (ShapefileDataStore) DataStoreFinder.getDataStore(params);
  89. //
  90. // // 获取 FeatureCollection 的 schema 并创建新的 Shapefile Schema
  91. // SimpleFeatureTypeImpl featureType = (SimpleFeatureTypeImpl) featureCollection.getSchema();
  92. // dataStore.createSchema(featureType);
  93. //
  94. // // 写入新的 Shapefile
  95. // try (SimpleFeatureWriter writer = (SimpleFeatureWriter) dataStore.getFeatureWriterAppend(dataStore.getTypeNames()[0], Transaction.AUTO_COMMIT)) {
  96. // for (int u = 0; u < featureCollection.size(); u++) {
  97. // Feature feature = featureCollection.features().next();
  98. // SimpleFeature simpleFeature = (SimpleFeature) feature;
  99. // //for (SimpleFeature feature : featureCollection) {
  100. // SimpleFeature newFeature = writer.next();
  101. // newFeature.setAttributes(simpleFeature.getAttributes());
  102. // writer.write();
  103. // }
  104. // }
  105. // }
  106. //
  107. // public static void main(String[] args) {
  108. // try {
  109. // // 加载两个输入的 Shapefile 文件
  110. // FeatureCollection layer1 = loadShapefile("E:\\projects\\监测图斑审查上传\\图斑套合\\管控数据\\基本农田.shp");
  111. // FeatureCollection layer2 = loadShapefile("D:\\temp\\wuchuan.shp");
  112. // // 执行空间叠加分析,获取交集部分
  113. // FeatureCollection result = performIntersection(layer1, layer2);
  114. // // 导出交集结果到新的 Shapefile
  115. // exportToShapefile(result, "D:\\temp\\temp.shp");
  116. // System.out.println("空间叠加分析完成,结果已导出到新的 Shapefile。");
  117. // } catch (Exception e) {
  118. // e.printStackTrace();
  119. // }
  120. // }
  121. //}