| | |
| | | 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; |
| | | |
| | |
| | | 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; |
| | |
| | | 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) { |
| | |
| | | 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"); |
| | | } |
| | | } |