import org.gdal.gdal.Dataset; //导入方法依赖的package包/类
private static void renderContours(final String contoursFilePath,
final int imageWidth,
final int imageHeight,
final BoundingBox bounds,
final SpatialReference spatialReference,
final Layer layer,
final Color background,
final Color foreground)
{
@SuppressWarnings("UseOfObsoleteCollectionType")
final Vector imageCreationOptions = new Vector<>(1);
imageCreationOptions.add("COMPRESS=LZW");
final Dataset rasterDataset = gdal.GetDriverByName("GTiff")
.Create(contoursFilePath,
imageWidth,
imageHeight,
4, // RGBA
gdalconstConstants.GDT_Byte,
imageCreationOptions);
try
{
rasterDataset.GetRasterBand(1).Fill(background.getRed());
rasterDataset.GetRasterBand(2).Fill(background.getGreen());
rasterDataset.GetRasterBand(3).Fill(background.getBlue());
rasterDataset.GetRasterBand(4).Fill(background.getAlpha());
rasterDataset.SetGeoTransform(new double[]{ bounds.getTopLeft().getX(), // top left x
bounds.getWidth() / (double)imageWidth, // w-e pixel resolution
0, // rotation, 0 if image is "north up"
bounds.getTopLeft().getY(), // top left y
0, // rotation, 0 if image is "north up"
-bounds.getHeight() / (double)imageHeight // n-s pixel resolution (negative value!)
});
rasterDataset.SetProjection(spatialReference.ExportToWkt());
final int rasterizeError = gdal.RasterizeLayer(rasterDataset,
new int[]{1, 2, 3, 4},
layer,
new double[]{ foreground.getRed(),
foreground.getGreen(),
foreground.getBlue(),
foreground.getAlpha()
},
null, // "options" vector. valid choices are described here: http://gdal.org/gdal__alg_8h.html#adfe5e5d287d6c184aab03acbfa567cb1
null);
if(rasterizeError != gdalconstConstants.CE_None)
{
throw new RuntimeException(new GdalError().getMessage());
}
}
finally
{
rasterDataset.delete();
}
}