I am trying to code gdal_polygonize source.png -f "GeoJSON" destination.geojson
with the java-bindings of gdal. I don't know how to make gdal.Polygonize(...) create the "DN"-Fields one obtains when you use gdal_polygonize. Here is my code so far:
public static void gdalPolygonize(String[] args){
gdal.AllRegister();
ogr.RegisterAll();
args = gdal.GeneralCmdLineProcessor(args);
Dataset hDataset = gdal.Open(args[0], gdalconstConstants.GA_ReadOnly);
Band rasterBand = hDataset.GetRasterBand(1);
Driver driver = ogr.GetDriverByName("GeoJSON");
DataSource dataSource = driver.CreateDataSource("C:\\Users\\User\\Desktop\\destination.geojson");
Layer outputLayer =dataSource.CreateLayer("destination");
gdal.Polygonize(rasterBand, null, outputLayer, 0);
}
the created polygons in the geojsonfiles look like this:
{ "type": "Feature", "properties": { }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 7858.0, 964.0 ], [ 7858.0, 965.0 ], [ 7859.0, 965.0 ], [ 7858.0, 964.0 ] ] ] } }
but they should look like this:
{ "type": "Feature", "properties": {"DN": 2}, "geometry": { "type": "Polygon", "coordinates": [ [ [ 7858.0, 964.0 ], [ 7858.0, 965.0 ], [ 7859.0, 965.0 ], [ 7858.0, 964.0 ] ] ] } }
So the difference it the "DN" in "properties".