我正在研究一种自动栅格化geojson格式的矢量文件的解决方案。系统工作正常,但是我无法为每个多边形分配颜色代码,并且所有多边形都被绘制为相同的燃烧颜色。
进行了一些网络搜索之后,我找到了一个不错的教程,其中解释了如何集成GDAL。但是,我无法编写代码以读取源geojson文件内的多边形的颜色字段并进行绘制。
void Rasterize_Working(string inputFeature, string outRaster, double cellSize)
{
// Define pixel_size and NoData value of new raster
double rasterCellSize = cellSize;
const double noDataValue = -9999;
string outputRasterFile = outRaster;
//Register the vector drivers
Ogr.RegisterAll();
//Register the raster drivers
Gdal.AllRegister();
//Reading the vector data
DataSource dataSource = Ogr.Open(inputFeature, 0);
Layer layer = dataSource.GetLayerByIndex(0);
Envelope envelope = new Envelope();
layer.GetExtent(envelope, 1);
//Compute the out raster cell resolutions
int x_res = Convert.ToInt32((envelope.MaxX - envelope.MinX) / rasterCellSize);
int y_res = Convert.ToInt32((envelope.MaxY - envelope.MinY) / rasterCellSize);
Console.WriteLine("Extent: " + envelope.MaxX + " " + envelope.MinX + " " + envelope.MaxY + " " + envelope.MinY);
Console.WriteLine("X resolution: " + x_res);
Console.WriteLine("X resolution: " + y_res);
//Check if output raster exists & delete (optional)
if (File.Exists(outputRasterFile))
{
File.Delete(outputRasterFile);
}
//Create new tiff
OSGeo.GDAL.Driver outputDriver = Gdal.GetDriverByName("GTiff");
Dataset outputDataset = outputDriver.Create(outputRasterFile, x_res, y_res, 3, DataType.GDT_Byte, null);
//Extrac srs from input feature
string inputShapeSrs;
SpatialReference spatialRefrence = layer.GetSpatialRef();
spatialRefrence.ImportFromEPSG(4326);
spatialRefrence.ExportToWkt(out inputShapeSrs);
//Assign input feature srs to outpur raster
outputDataset.SetProjection(inputShapeSrs);
//Geotransform
double[] argin = new double[] { envelope.MinX, rasterCellSize, 0, envelope.MaxY, 0, -rasterCellSize };
outputDataset.SetGeoTransform(argin);
//Set no data
Band band = outputDataset.GetRasterBand(1);
band.SetNoDataValue(noDataValue);
//close tiff
outputDataset.FlushCache();
outputDataset.Dispose();
//Feature to raster rasterize layer options
//No of bands (1)
int[] bandlist = new int[] { 3,2,1 };
//Values to be burn on raster (10.0)
double[] burnValues = new double[] { 100,100,100};
Dataset myDataset = Gdal.Open(outputRasterFile, Access.GA_Update);
//additional options
string[] rasterizeOptions;
rasterizeOptions = new string[] { "ALL_TOUCHED=TRUE" }; //To set all touched pixels into raster pixel
//Rasterize layer
Gdal.RasterizeLayer(myDataset, 1, bandlist, layer, IntPtr.Zero, IntPtr.Zero, 1, burnValues, rasterizeOptions, null, "Raster conversion");
}
这是我的Json文件:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
39.578125,
59.75
],
[
39.578125,
40
],
[
70.578125,
40
],
[
70.578125,
59.75
],
[
39.578125,
59.75
]
]
]
},
"properties": {
"stroke": "#555555",
"stroke-width": 2,
"stroke-opacity": 1,
"fill": "#00aa22",
"color": "#00aa22",
"fill-opacity": 0.5
}
},
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
18.578125,
27.75
],
[
18.578125,
20
],
[
35.578125,
20
],
[
35.578125,
30.75
],
[
20.578125,
30.75
]
]
]
},
"properties": {
"stroke": "#555555",
"stroke-width": 2,
"stroke-opacity": 1,
"fill": "#00aa22",
"color": "#00aa22",
"fill-opacity": 0.5
}
}
]
}