Растрирование полигонов с разными цветами с использованием Gdal - PullRequest
0 голосов
/ 05 июля 2019

Я работаю над решением для автоматической растеризации векторных файлов в формате geojson.Система работает нормально, однако я не могу назначить цветовые коды для каждого многоугольника, и все многоугольники окрашены в один и тот же цвет записи.

После выполнения поиска в Интернете я нашел хороший учебник, в котором объясняется, как интегрировать GDAL.,Однако я не смог создать код для чтения цветовых полей полигонов внутри исходного файла геоджона и рисования.

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
      }
    }
  ]
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...