示例#1
0
        /// <summary>
        /// Ogr to DotSpatial at layer level
        /// </summary>
        /// <param name="layer">Ogr Layer</param>
        /// <returns>DotSpatial IFeatureSet</returns>
        public static DotSpatial.Data.IFeatureSet Ogr2DSLayer(OSGeo.OGR.Layer layer)
        {
            DotSpatial.Data.IFeatureSet featureSet = new DotSpatial.Data.FeatureSet();
            featureSet.FeatureType = Ogr2DSGeometryType(layer.GetGeomType());
            featureSet.DataTable   = Ogr2DSDataTable(layer.GetLayerDefn());
            featureSet.Projection  = Ogr2DSProjection(layer.GetSpatialRef());

            for (int i = 0; i < layer.GetFeatureCount(0); i++)
            {
                OSGeo.OGR.Geometry       geometry   = layer.GetFeature(i).GetGeometryRef();
                DotSpatial.Data.IFeature feature    = new DotSpatial.Data.Feature(Ogr2DSGeometry(geometry));
                OSGeo.OGR.Feature        ogrFeature = layer.GetFeature(i);
                feature.DataRow = featureSet.DataTable.NewRow();
                for (int j = 0; j < layer.GetLayerDefn().GetFieldCount(); j++)
                {
                    object value = GetOgrValue(ogrFeature, j);
                    if (value == null)
                    {
                        value = DBNull.Value;
                    }
                    feature.DataRow[j] = value;
                }
                featureSet.Features.Add(feature);
            }

            return(featureSet);
        }
示例#2
0
        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);
        }