| | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.gdal.gdal.Dataset; |
| | | import org.gdal.gdal.gdal; |
| | | import org.gdal.gdalconst.gdalconst; |
| | | import org.gdal.ogr.*; |
| | | import org.gdal.osr.SpatialReference; |
| | | |
| | |
| | | } |
| | | } |
| | | |
| | | private static Geometry createPolygon(SpatialReference sr, Double minx, Double miny, Double maxx, Double maxy) { |
| | | public static Geometry createPolygon(SpatialReference sr, Double minx, Double miny, Double maxx, Double maxy) { |
| | | String epsg = sr.GetAuthorityCode(null); |
| | | if (!("4326".equals(epsg) || "4490".equals(epsg))) { |
| | | double[] dmin = GdalHelper.fromWgs84(sr, minx, miny); |
| | |
| | | |
| | | Geometry poly = new Geometry(ogr.wkbPolygon); |
| | | poly.AddGeometry(ring); |
| | | poly.AssignSpatialReference(sr); |
| | | |
| | | return poly; |
| | | } |
| | |
| | | /** |
| | | * 创建Geometry对象 |
| | | */ |
| | | private static Geometry createGeometry(GeLayer geLayer, JSONObject geom) { |
| | | public static Geometry createGeometry(GeLayer geLayer, JSONObject geom) { |
| | | String type = geom.getStr("type"); |
| | | JSONArray cs = geom.getJSONArray("coordinates"); |
| | | |
| | |
| | | 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); |
| | | 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"); |
| | | } |
| | | } |