From 2164262d9b15d2dcf0dbdc30ef9d829ba99e37af Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期三, 06 十一月 2024 16:12:34 +0800
Subject: [PATCH] 1

---
 src/main/java/com/se/simu/helper/ShpHelper.java |   44 ++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 40 insertions(+), 4 deletions(-)

diff --git a/src/main/java/com/se/simu/helper/ShpHelper.java b/src/main/java/com/se/simu/helper/ShpHelper.java
index 2af67f2..fb31cef 100644
--- a/src/main/java/com/se/simu/helper/ShpHelper.java
+++ b/src/main/java/com/se/simu/helper/ShpHelper.java
@@ -6,8 +6,10 @@
 import com.se.simu.domain.dto.GeField;
 import com.se.simu.domain.dto.GeLayer;
 import lombok.extern.slf4j.Slf4j;
+import org.gdal.gdal.Band;
 import org.gdal.gdal.Dataset;
 import org.gdal.gdal.gdal;
+import org.gdal.gdalconst.gdalconst;
 import org.gdal.ogr.*;
 import org.gdal.osr.SpatialReference;
 
@@ -302,7 +304,7 @@
         return sr.GetAuthorityCode(null);
     }
 
-    public static boolean polygonize(Dataset ds, String filePath) {
+    public static boolean polygonize2Shp(Dataset ds, String filePath) {
         Driver driver = null;
         DataSource dataSource = null;
         Layer layer = null;
@@ -316,10 +318,9 @@
             layer = dataSource.CreateLayer(FileUtil.getName(filePath), ds.GetSpatialRef(), ogr.wkbPolygon, getOptions());
             if (null == layer) return false;
 
-            FieldDefn field = new FieldDefn("val", ogr.OFTReal);
-            layer.CreateField(field);
+            layer.CreateField(new FieldDefn("val", ogr.OFTReal));
 
-            gdal.Polygonize(ds.GetRasterBand(1), null, layer, 0, null);
+            gdal.Polygonize(ds.GetRasterBand(1), ds.GetRasterBand(1).GetMaskBand(), layer, 0);
 
             return true;
         } catch (Exception ex) {
@@ -329,4 +330,39 @@
             GdalHelper.delete(layer, dataSource, driver);
         }
     }
+
+    public static boolean polygonize2Geojson(Dataset ds, String filePath) {
+        Driver driver = null;
+        DataSource dataSource = null;
+        Layer layer = null;
+        try {
+            driver = ogr.GetDriverByName("GeoJSON");
+            if (null == driver) return false;
+
+            dataSource = driver.CreateDataSource(filePath, null);
+            if (null == dataSource) return false;
+
+            layer = dataSource.CreateLayer(FileUtil.getName(filePath), ds.GetSpatialRef(), ogr.wkbPolygon, getOptions());
+            if (null == layer) return false;
+
+            layer.CreateField(new FieldDefn("val", ogr.OFTReal));
+
+            gdal.Polygonize(ds.GetRasterBand(1), ds.GetRasterBand(1).GetMaskBand(), layer, 0);
+
+            return true;
+        } catch (Exception ex) {
+            log.error(ex.getMessage(), ex);
+            return false;
+        } finally {
+            GdalHelper.delete(layer, dataSource, driver);
+        }
+    }
+
+    public static void test() {
+        String path = "D:\\simu\\out\\20241010095328\\waters\\1730217635000\\";
+
+        Dataset ds = gdal.Open(path + "water.tif", gdalconst.GA_ReadOnly);
+        ds.SetSpatialRef(GdalHelper.createSpatialReference(4548));
+        polygonize2Geojson(ds, path + "water.geojson");
+    }
 }

--
Gitblit v1.9.3