Exemplo n.º 1
0
    private void MapRasterToTriangulation()
    {
        Console.WriteLine("Mapping elevation raster pixels to triangulation...\n");

        List<River> rivers = model.Rivers.Values.ToList();
        List<WaterSurfacePolygon> waterSurfacePolygons = new List<WaterSurfacePolygon>();

        //Create triangulation for each river
        for (int k = 0; k < rivers.Count; k++)
        {
            River river = rivers[k];

            foreach (Reach reach in river.Reaches.Values)
            {
                reach.CreateGISFeatures();
                reach.CreateTriangulationForWaterSurface();

                foreach (WaterSurfacePolygon polygon in reach.WaterSurfaces)
                {
                    waterSurfacePolygons.Add(polygon);
                }
            }
        }

        float[] mapping = new float[xSize * ySize];
        rasterWaterSurfaceMapping = new int[xSize][];
        rasterTriangleMapping = new int[xSize][];

        for (int i = 0; i < xSize; i++)
        {
            rasterWaterSurfaceMapping[i] = new int[ySize];
            rasterTriangleMapping[i] = new int[ySize];
            for (int j = 0; j < ySize; j++)
            {
                rasterWaterSurfaceMapping[i][j] = -1;
                rasterTriangleMapping[i][j] = -1;
                mapping[j * xSize + i] = noData;
            }
        }

        int count = 0;
        int foundCount = 0;
        int stepSize = (int)Math.Floor(xSize * ySize * 0.01);

        List<List<Coordinate>> coordinates = new List<List<Coordinate>>();
        //Parallel.For(0, waterSurfacePolygons.Count, k =>
        for (int k = 0; k < waterSurfacePolygons.Count; k++)
        {
            WaterSurfacePolygon watersurfacePolygon = waterSurfacePolygons[k];
            Interlocked.Increment(ref count);
            double progress = count * 100.0 / (waterSurfacePolygons.Count * 1.0);

            lock (Console.Out)
            {
                Console.SetCursorPosition(0, Console.CursorTop);
                Console.Write("Progress => " + progress.ToString("###") + " %");
            }

            lock (watersurfacePolygon)
            {
                Point pltc = getCoordIndexes(watersurfacePolygon.MinX, watersurfacePolygon.MaxY);
                Point prbc = getCoordIndexes(watersurfacePolygon.MaxX, watersurfacePolygon.MinY);

                List<Coordinate> coordinate = new List<Coordinate>();

                Point ptt1 = getCoords((int)pltc.X, (int)pltc.Y);
                Point ptt2 = getCoords((int)prbc.X, (int)prbc.Y);

                coordinate.Add(new Coordinate(ptt1.X, ptt1.Y));
                coordinate.Add(new Coordinate(ptt1.X, ptt2.Y));

                coordinate.Add(new Coordinate(ptt2.X, ptt1.Y));
                coordinate.Add(new Coordinate(ptt2.X, ptt2.Y));

                for (int i = (int)pltc.X; i < prbc.X; i++)
                {
                    for (int j = (int)pltc.Y; j < prbc.Y; j++)
                    {
                        Point p2 = getCoords(i, j);

                        lock (watersurfacePolygon)
                        {
                            if (watersurfacePolygon.Contains(p2.X, p2.Y))
                            {
                                int m = watersurfacePolygon.FindTriangleThatContains(p2.X, p2.Y);

                                if (m > -1)
                                {
                                    rasterWaterSurfaceMapping[i][j] = k;
                                    mapping[j * xSize + i] = k;
                                    rasterTriangleMapping[i][j] = m;
                                }
                            }
                        }
                    }
                }
                coordinates.Add(coordinate);
            }
        }

        using(IFeatureSet set = new FeatureSet(FeatureType.MultiPoint))
        {
            foreach(var p in coordinates)
            set.AddFeature(new MultiPoint(p));

            set.SaveAs(localWorkspace + "\\" + name + "_point.shp", true);
        }
        //);

        OSGeo.GDAL.Driver driver = Gdal.GetDriverByName(rasterDriver);
        Dataset newRaster = driver.Create(localWorkspace + "\\" + name + "_mapping.tif", xSize, ySize, 1, dataType, new string[] { "TFW=YES", "COMPRESS=LZW" });
        newRaster.GetRasterBand(1).SetNoDataValue(noData);
        newRaster.SetGeoTransform(geoTransformation);
        newRaster.SetProjection(projection);

        Band newRasterBand = newRaster.GetRasterBand(1);
        newRasterBand.WriteRaster(0, 0, xSize, ySize, mapping, xSize, ySize, 0, 0);
        double min, max, mean, stdev;
        newRasterBand.GetStatistics(0, 1, out min, out max, out mean, out stdev);
        newRasterBand.FlushCache();
        newRaster.FlushCache();
        newRaster.Dispose();
        newRaster = null;

        driver.Dispose();
        driver = null;

        using (IFeatureSet fs = new FeatureSet(DotSpatial.Topology.FeatureType.Polygon))
        {
            fs.DataTable.Columns.AddRange(new DataColumn[]
                    {
                      new DataColumn("Identifier" , typeof(int)),
                    });

            int tcount = 0;

            for (int k = 0; k < waterSurfacePolygons.Count; k++)
            {
                WaterSurfacePolygon surface = waterSurfacePolygons[k];

                foreach (TriangleNet.Data.Triangle pgon in surface.Triangles)
                {
                    TriangleNet.Data.Triangle ts = pgon;
                    List<Coordinate> vertices = new List<Coordinate>();

                    Point p0 = surface.Points[ts.P0];
                    Point p1 = surface.Points[ts.P1];
                    Point p2 = surface.Points[ts.P2];

                    Coordinate c1 = new Coordinate(p0.X, p0.Y, p0.Z);
                    Coordinate c2 = new Coordinate(p1.X, p1.Y, p1.Z);
                    Coordinate c3 = new Coordinate(p2.X, p2.Y, p2.Z);

                    vertices.Add(c1);
                    vertices.Add(c2);
                    vertices.Add(c3);

                    Polygon polygon = new Polygon(vertices);

                    IFeature fset = fs.AddFeature(polygon);

                    fset.DataRow.BeginEdit();

                    fset.DataRow["Identifier"] = k;

                    fset.DataRow.EndEdit();

                    tcount++;
                }
            }

            fs.SaveAs(localWorkspace + "\\" + name + "_polygon.shp", true);
            fs.Close();
            fs.Dispose();
        }

        double temp = 100;
        Console.SetCursorPosition(0, Console.CursorTop);
        Console.WriteLine("Progress => " + temp.ToString("###") + " % \n");

        temp = foundCount * 100.0 / (xSize * ySize * 1.0);
        Console.WriteLine(temp.ToString("###.0") + " %  of pixels were found in triangulation \n");

        Console.WriteLine("Finished mapping elevation raster pixels to triangulation !\n");
    }