public static DotSpatial.Data.Raster RasterCheck(DotSpatial.Data.Raster raster, double[] levels) { double eps = (raster.Maximum - raster.Minimum) / 1000; int n = levels.Count(); DotSpatial.Data.Raster rst = raster; for (int i = 0; i <= rst.NumRows - 1; i++) { for (int j = 0; j <= rst.NumColumns - 1; j++) { if (rst.Value[i, j] != rst.NoDataValue) { for (int l = 0; l < n; l++) { if (rst.Value[i, j] == levels[l]) { rst.Value[i, j] += eps; } } } } } return(rst); }
//public static int GetLevel(Polygon p, double[] lev) //{ // double zmin = double.MaxValue; // double zmax = double.MinValue; // foreach (Coordinate c in p.ExteriorRing.Coordinates) // { // if (c.Z < zmin) zmin = c.Z; // if (c.Z > zmax) zmax = c.Z; // } // foreach (LineString ls in p.InteriorRings) // { // foreach (Coordinate c in ls.Coordinates) // { // if (c.Z < zmin) zmin = c.Z; // if (c.Z > zmax) zmax = c.Z; // } // } // if (zmin == zmax) // { // if (zmin == lev[0]) return -1; // if (zmin == lev[lev.Count() - 1]) return lev.Count(); // } // for (int i = 0; i < lev.Count(); i++) // { // if (lev[i] == zmin && lev[i + 1] == zmax) // { // return i; // } // } // return -100; //} public static void CreateMinMaxEvery(DotSpatial.Data.Raster r, ContourType type, out double MinContour, out double MaxContour, out double every) { double min = r.Minimum; double max = r.Maximum; if (min == max) { min = Math.Floor(min); max = Math.Ceiling(max); if (min == max) { max += 1; } } double dz = max - min; double Order = Math.Pow(10, Math.Floor(Math.Log10(dz))); if (Order == dz) { Order /= 10; } if (dz / Order < 2) { Order /= 10; } MinContour = Math.Floor(min / Order) * Order; MaxContour = Math.Ceiling(max / Order) * Order; if (MaxContour < max) { MaxContour += Order; } every = Order; if (type == ContourType.Line) { MinContour += every; MaxContour -= every; if (MaxContour <= MinContour) { MaxContour = MinContour + every; } } }
private void ComputeMinMaxEvery() { double min, max, every; DotSpatial.Data.Raster rst = layers[comboBoxLayerList.SelectedIndex].DataSet as DotSpatial.Data.Raster; Contour.ContourType ContType = new Contour.ContourType(); if (comboBoxType.SelectedIndex == 0) { ContType = Contour.ContourType.Line; } else { ContType = Contour.ContourType.Polygon; } Contour.CreateMinMaxEvery(rst, ContType, out min, out max, out every);; numericUpDownMin.Value = (decimal)min; numericUpDownMax.Value = (decimal)max; numericUpDownEvery.Value = (decimal)every; tomPaletteEditor1.ClearItems(); if (ContType == Contour.ContourType.Line) { tomPaletteEditor1.AddItem(min, Color.Chartreuse); tomPaletteEditor1.AddItem(max, Color.Magenta); //tomPaletteEditor1.AddItem((max + min) / 2, Color.Yellow); } if (ContType == Contour.ContourType.Polygon) { tomPaletteEditor1.AddItem(min + every / 2, Color.Chartreuse); tomPaletteEditor1.AddItem(max - every / 2, Color.Magenta); //tomPaletteEditor1.AddItem((max + min) / 2, Color.Yellow); } tomPaletteEditor1.Invalidate(); }
public static IList <IGeometry> GetContours(ref DotSpatial.Data.Raster rst, double[] x, double[] y, double zlev) { List <LineString> lsList = new List <LineString>(); bool ipari, jpari; double[] xx = new double[3]; double[] yy = new double[3]; double[] zz = new double[3]; for (int j = 0; j < rst.NumColumns - 1; j++) { if (((int)((double)j / 2.0)) * 2 == j) { jpari = true; } else { jpari = false; } for (int i = 0; i < rst.NumRows - 1; i++) { if (((int)((double)i / 2.0)) * 2 == i) { ipari = true; } else { ipari = false; } if (!jpari && !ipari || jpari && ipari) { xx[0] = x[j]; yy[0] = y[i]; zz[0] = rst.Value[i, j]; xx[1] = x[j]; yy[1] = y[i + 1]; zz[1] = rst.Value[i + 1, j]; xx[2] = x[j + 1]; yy[2] = y[i]; zz[2] = rst.Value[i, j + 1]; { Coordinate[] c = Intersect(xx, yy, zz, zlev); if (c != null) { lsList.Add(new LineString(c)); } } xx[0] = x[j + 1]; yy[0] = y[i]; zz[0] = rst.Value[i, j + 1]; xx[1] = x[j]; yy[1] = y[i + 1]; zz[1] = rst.Value[i + 1, j]; xx[2] = x[j + 1]; yy[2] = y[i + 1]; zz[2] = rst.Value[i + 1, j + 1]; { Coordinate[] c = Intersect(xx, yy, zz, zlev); if (c != null) { lsList.Add(new LineString(c)); } } } if (jpari && !ipari || !jpari && ipari) { xx[0] = x[j]; yy[0] = y[i]; zz[0] = rst.Value[i, j]; xx[1] = x[j]; yy[1] = y[i + 1]; zz[1] = rst.Value[i + 1, j]; xx[2] = x[j + 1]; yy[2] = y[i + 1]; zz[2] = rst.Value[i + 1, j + 1]; { Coordinate[] c = Intersect(xx, yy, zz, zlev); if (c != null) { lsList.Add(new LineString(c)); } } xx[0] = x[j]; yy[0] = y[i]; zz[0] = rst.Value[i, j]; xx[1] = x[j + 1]; yy[1] = y[i + 1]; zz[1] = rst.Value[i + 1, j + 1]; xx[2] = x[j + 1]; yy[2] = y[i]; zz[2] = rst.Value[i, j + 1]; { Coordinate[] c = Intersect(xx, yy, zz, zlev); if (c != null) { lsList.Add(new LineString(c)); } } } } } LineMerger lm = new LineMerger(); lm.Add(lsList); IList <IGeometry> merged = (IList <IGeometry>)lm.GetMergedLineStrings(); return(merged); }
public static DotSpatial.Data.FeatureSet Execute(DotSpatial.Data.Raster rst, ContourType contourType, string FieldName = "Value", double[] levels = null) { double[] lev = levels; noData = rst.NoDataValue; type = contourType; DotSpatial.Data.Raster iRst = RasterCheck(rst, lev);; string field; if (FieldName == null) { field = "Value"; } else { field = FieldName; } double[] x = new double[rst.NumColumns]; double[] y = new double[rst.NumRows]; for (int i = 0; i < rst.NumColumns; i++) { x[i] = rst.Extent.MinX + rst.CellWidth * i + rst.CellWidth / 2; } for (int i = 0; i < rst.NumRows; i++) { y[i] = rst.Extent.MaxY - rst.CellHeight * i - rst.CellHeight / 2; } DotSpatial.Data.FeatureSet fs = null; switch (type) { case ContourType.Line: { fs = new DotSpatial.Data.FeatureSet(DotSpatial.Topology.FeatureType.Line); fs.DataTable.Columns.Add(field, typeof(double)); for (int z = 0; z < levels.Length; z++) { IList <IGeometry> cont = GetContours(ref iRst, x, y, lev[z]); foreach (var g in cont) { var f = (DotSpatial.Data.Feature)fs.AddFeature(ToDotSpatialLineString((ILineString)g)); f.DataRow[field] = lev[z]; } } } break; case ContourType.Polygon: { fs = new DotSpatial.Data.FeatureSet(DotSpatial.Topology.FeatureType.Polygon); fs.DataTable.Columns.Add("Lev", typeof(int)); fs.DataTable.Columns.Add("Label", typeof(string)); Collection <IGeometry> Contours = new Collection <IGeometry>(); for (int z = 0; z < levels.Count(); z++) { IList <IGeometry> cont = GetContours(ref iRst, x, y, lev[z]); foreach (var g in cont) { Contours.Add(new LineString(g.Coordinates)); } } Coordinate[] Boundary = new Coordinate[5]; Boundary[0] = new Coordinate(x[0], y[0]); Boundary[1] = new Coordinate(x[0], y[rst.NumRows - 1]); Boundary[2] = new Coordinate(x[rst.NumColumns - 1], y[rst.NumRows - 1]); Boundary[3] = new Coordinate(x[rst.NumColumns - 1], y[0]); Boundary[4] = new Coordinate(x[0], y[0]); Contours.Add(new LineString(Boundary)); Collection <IGeometry> NodedContours = new Collection <IGeometry>(); GeometryNoder geomNoder = new GeometryNoder(new PrecisionModel(1000d)); foreach (var c in geomNoder.Node(Contours)) { NodedContours.Add(c); } Polygonizer polygonizer = new Polygonizer(); polygonizer.Add(NodedContours); foreach (IPolygon p in polygonizer.GetPolygons()) { Point pnt = (Point)p.InteriorPoint; int c = (int)((pnt.X - iRst.Extent.MinX) / iRst.CellWidth); int r = (int)((iRst.Extent.MaxY - pnt.Y) / iRst.CellHeight); double z = ((DotSpatial.Data.Raster)iRst).Value[r, c]; int Cls = GetLevel(z, lev); string label = "Undefined"; if (Cls == -1) { label = "< " + lev[0].ToString(); } if (Cls == lev.Count()) { label = "> " + lev[lev.Count() - 1].ToString(); } if (Cls >= 0 & Cls < lev.Count()) { label = lev[Cls].ToString() + " - " + lev[Cls + 1].ToString(); } DotSpatial.Topology.Polygon dsp = ToDotSpatialPolygon(p); DotSpatial.Data.Feature f = (DotSpatial.Data.Feature)fs.AddFeature(dsp); f.DataRow["Lev"] = Cls; f.DataRow["Label"] = label; } } break; } return(fs); }