Exemplo n.º 1
0
        /// <summary>
        /// 获取Feature的最大值和最小值 范围不准
        /// </summary>
        /// <param name="dsmDs"></param>
        /// <param name="oriFeat"></param>
        /// <param name="bufFeat"></param>
        /// <param name="subRasterOff_Size"></param>
        private static void getMaxMinValue(OSGeo.GDAL.Dataset dsmDs, OSGeo.OGR.Feature oriFeat, OSGeo.OGR.Feature bufFeat, int[] subRasterOff_Size)
        {
            //拿到当前Feature对应的栅格值(数组)
            double[] rastValue = new double[subRasterOff_Size[2] * subRasterOff_Size[3]];

            //把SUBimg的值读进数组
            lock (dsmDs)
            {
                dsmDs.GetRasterBand(1).ReadRaster(
                    subRasterOff_Size[0],
                    subRasterOff_Size[1],
                    subRasterOff_Size[2],
                    subRasterOff_Size[3],
                    rastValue,
                    subRasterOff_Size[2],
                    subRasterOff_Size[3],
                    0, 0);
            }
            double max = rastValue.Max();
            double min = rastValue.Min();
            double cay = max - min;

            oriFeat.SetField("TEMP", max - min);
            oriFeat.SetField("MIN", 0);
            oriFeat.SetField("MAX", min);
            oriFeat.SetField("HIGHT", max);
        }
Exemplo n.º 2
0
        /// <summary>
        /// DotSpatial to Ogr at layer level
        /// </summary>
        /// <param name="featureSet">DotSpatial IFeatureSet</param>
        /// <returns>Ogr Layer</returns>
        public static OSGeo.OGR.Layer DS2OrgLayer(DotSpatial.Data.IFeatureSet featureSet)
        {
            using (OSGeo.OGR.Driver driver = OSGeo.OGR.Ogr.GetDriverByName("Memory"))
            {
                OSGeo.OGR.DataSource  dataSource  = driver.CreateDataSource(featureSet.Name, null);
                OSGeo.OGR.Layer       layer       = dataSource.CreateLayer("Result", DS2OgrProjection(featureSet.Projection), DS2OgrGeometryType(featureSet.FeatureType), new string[] { });
                OSGeo.OGR.FeatureDefn featureDefn = DS2OgrDataTable(featureSet.DataTable);

                for (int k = 0; k < featureDefn.GetFieldCount(); k++)
                {
                    layer.CreateField(featureDefn.GetFieldDefn(k), 0);
                }

                for (int i = 0; i < featureSet.NumRows(); i++)
                {
                    DotSpatial.Data.IFeature feature    = featureSet.GetFeature(i);
                    OSGeo.OGR.Feature        ogrFeature = new OSGeo.OGR.Feature(featureDefn);
                    ogrFeature.SetGeometry(DS2OgrGeometry(feature.Geometry, featureSet));
                    for (int j = 0; j < feature.DataRow.ItemArray.Length; j++)
                    {
                        #region Set Value to Feature

                        object value = feature.DataRow.ItemArray.GetValue(j);
                        if (value is int)
                        {
                            ogrFeature.SetField(j, (int)value);
                        }
                        else if (value is double)
                        {
                            ogrFeature.SetField(j, (double)value);
                        }
                        else if (value is string)
                        {
                            ogrFeature.SetField(j, (string)value);
                        }
                        else if (value is DateTime)
                        {
                            DateTime dateTime = (DateTime)value;
                            ogrFeature.SetField(j, dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, (float)dateTime.Second, 0);
                        }

                        #endregion
                    }
                    layer.CreateFeature(ogrFeature);
                }
                dataSource.FlushCache();
                return(layer);
            }
        }
Exemplo n.º 3
0
        public static OSGeo.OGR.Layer DS2OrgLayer(DotSpatial.Data.IFeatureSet featureSet, OSGeo.OGR.Layer layer)
        {
            using (OSGeo.OGR.FeatureDefn featureDefn = DS2OgrDataTable(featureSet.DataTable))
            {
                for (int k = 0; k < featureDefn.GetFieldCount(); k++)
                {
                    layer.CreateField(featureDefn.GetFieldDefn(k), 0);
                }

                for (int i = 0; i < featureSet.NumRows(); i++)
                {
                    DotSpatial.Data.IFeature feature = featureSet.GetFeature(i);
                    using (OSGeo.OGR.Feature ogrFeature = new OSGeo.OGR.Feature(featureDefn))
                    {
                        ogrFeature.SetGeometry(DS2OgrGeometry(feature.Geometry, featureSet));
                        for (int j = 0; j < feature.DataRow.ItemArray.Length; j++)
                        {
                            #region Set Value to Feature

                            object value = feature.DataRow.ItemArray.GetValue(j);
                            if (value is int)
                            {
                                ogrFeature.SetField(j, (int)value);
                            }
                            else if (value is double)
                            {
                                ogrFeature.SetField(j, (double)value);
                            }
                            else if (value is string)
                            {
                                ogrFeature.SetField(j, (string)value);
                            }
                            else if (value is DateTime)
                            {
                                DateTime dateTime = (DateTime)value;
                                ogrFeature.SetField(j, dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, (float)dateTime.Second, 0);
                            }

                            #endregion
                        }
                        layer.CreateFeature(ogrFeature);
                    }
                }
            }
            return(layer);
        }
Exemplo n.º 4
0
        public static void SetField(this OSGeo.OGR.Feature feature, OSGeo.OGR.FieldDefn fieldDefn, object value)
        {
            if (feature == null || fieldDefn == null)
            {
                return;
            }
            var fieldName = fieldDefn.GetName();
            var fieldType = fieldDefn.GetFieldType();

            if (!DBNull.Value.Equals(value))
            {
                switch (fieldType)
                {
                case OSGeo.OGR.FieldType.OFTString:
                    feature.SetField(fieldName, value.ToString());
                    break;

                case OSGeo.OGR.FieldType.OFTInteger:
                    if (value is int intValue)
                    {
                        feature.SetField(fieldName, intValue);
                    }
                    break;

                case OSGeo.OGR.FieldType.OFTReal:
                    if (value is double doubleValue)
                    {
                        feature.SetField(fieldName, doubleValue);
                    }
                    break;

                default:
                    throw new NotImplementedException();
                }
            }
        }
Exemplo n.º 5
0
        private void pdfToolStripMenuItem_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < mapBox1.Map.Layers.Count; i++)
            {
                if (mapBox1.Map.Layers[i].LayerName == SelectedLayer)
                {
                    var layer = mapBox1.Map.Layers[i] as VectorLayer;

                    var dataSource = layer.DataSource as GeometryFeatureProvider;

                    OSGeo.OGR.Driver driverSH = OSGeo.OGR.Ogr.GetDriverByName("PDF");



                    var referance = new SpatialReference("PROJCS[\"WGS 84 / Pseudo-Mercator\",GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9122\"]],AUTHORITY[\"EPSG\",\"4326\"]],PROJECTION[\"Mercator_1SP\"],PARAMETER[\"central_meridian\",0],PARAMETER[\"scale_factor\",1],PARAMETER[\"false_easting\",0],PARAMETER[\"false_northing\",0],UNIT[\"metre\",1,AUTHORITY[\"EPSG\",\"9001\"]],AXIS[\"X\",EAST],AXIS[\"Y\",NORTH],EXTENSION[\"PROJ4\",\"+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs\"],AUTHORITY[\"EPSG\",\"3857\"]]");


                    OSGeo.OGR.DataSource dataSourceSH = driverSH.CreateDataSource(Application.StartupPath + "\\" + SelectedLayer + ".pdf", new string[] { "ENCODING=UTF-16" });


                    var layerSH = dataSourceSH.CreateLayer("PointLayer", referance, OSGeo.OGR.wkbGeometryType.wkbPoint, new string[] { "ENCODING=UTF-16" });


                    layerSH.StartTransaction();

                    for (int k = 0; k < dataSource.Features.Columns.Count; k++)
                    {
                        OSGeo.OGR.FieldDefn field = new OSGeo.OGR.FieldDefn(dataSource.Features.Columns[k].Caption, OSGeo.OGR.FieldType.OFTString);


                        layerSH.CreateField(field, 1);
                    }

                    for (int k = 0; k < dataSource.Features.Rows.Count; k++)
                    {
                        OSGeo.OGR.Feature feature = new OSGeo.OGR.Feature(layerSH.GetLayerDefn());

                        if (!string.IsNullOrEmpty(dataSource.Features.Rows[k]["geom_wkt"].ToString()))
                        {
                            OSGeo.OGR.Geometry geom = OSGeo.OGR.Geometry.CreateFromWkt(dataSource.Features.Rows[k]["geom_wkt"].ToString());
                            feature.SetGeometry(geom);
                        }

                        for (int x = 0; x < dataSource.Features.Columns.Count; x++)
                        {
                            var data = dataSource.Features.Rows[k][x].ToString();

                            feature.SetField(dataSource.Features.Columns[x].Caption, data);
                        }
                        try
                        {
                            layerSH.CreateFeature(feature);
                            feature.Dispose();
                        }
                        catch (Exception)
                        {
                        }
                    }
                    layerSH.CommitTransaction();
                    layerSH.SyncToDisk();
                    layerSH.Dispose();

                    dataSourceSH.FlushCache();
                    dataSourceSH.Dispose();

                    driverSH.Dispose();
                }
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Creates an OGR data source from a FeatureDataTable
        /// </summary>
        /// <param name="table">The name of the table</param>
        /// <param name="geometryType">The geometry type</param>
        /// <param name="driver">The driver</param>
        /// <param name="connection">The connection string</param>
        /// <param name="driverOptions">The options for the driver</param>
        /// <param name="layerOptions">The options for the layer</param>
        public static void CreateFromFeatureDataTable(FeatureDataTable table,
                                                      OgcGeometryType geometryType, int srid, string driver, string connection, string[] driverOptions = null, string[] layerOptions = null)
        {
            if (table == null)
            {
                throw new ArgumentNullException("table");
            }

            if (table.Rows.Count == 0)
            {
                throw new ArgumentException("The table contains no rows", "table");
            }

            if (geometryType < OgcGeometryType.Point || geometryType > OgcGeometryType.MultiPolygon)
            {
                throw new ArgumentException("Invalid geometry type", "geometryType");
            }

            if (string.IsNullOrWhiteSpace(driver))
            {
                throw new ArgumentException("No driver specified", "driver");
            }

            var dr = OSGeo.OGR.Ogr.GetDriverByName(driver);

            if (dr == null)
            {
                throw new Exception(string.Format("Cannot load driver '{0}'!", driver));
            }

            //if (!dr.TestCapability("ODrCCreateDataSource"))
            //    throw new Exception(string.Format("Driver '{0}' cannot create a data source!", driver));

            // Create the data source
            var ds = dr.CreateDataSource(connection, driverOptions);
            //if (!ds.TestCapability("ODsCCreateLayer"))
            //    throw new Exception(string.Format("Driver '{0}' cannot create a layer!", driver));

            // Create the spatial reference
            var sr = new OSGeo.OSR.SpatialReference(string.Empty);

            sr.ImportFromEPSG(srid);

            // Create the layer
            var lyr = ds.CreateLayer(table.TableName, sr, (OgrGeometryType)geometryType, layerOptions);

            sr.Dispose();

            //lyr.GetSpatialRef();
            foreach (System.Data.DataColumn dc in table.Columns)
            {
                using (var fldDef = GetFieldDefinition(dc))
                    lyr.CreateField(fldDef, 0);
            }

            using (var ld = lyr.GetLayerDefn())
            {
                foreach (FeatureDataRow fdr in table.Rows)
                {
                    if ((int)fdr.Geometry.OgcGeometryType != (int)geometryType)
                    {
                        continue;
                    }

                    using (var feature = new OgrFeature(ld))
                    {
                        feature.SetGeometry(OgrGeometry.CreateFromWkb(fdr.Geometry.AsBinary()));
                        var idx = -1;
                        foreach (System.Data.DataColumn dc in table.Columns)
                        {
                            idx++;
                            var      fd = ld.GetFieldDefn(idx);
                            DateTime dt;
                            switch (fd.GetFieldType())
                            {
                            case OgrFieldType.OFTBinary:
                                //Nothing
                                break;

                            case OgrFieldType.OFTDate:
                                dt = ((DateTime)fdr[dc]).Date;
                                feature.SetField(idx, dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, 0);
                                break;

                            case OgrFieldType.OFTDateTime:
                                dt = (DateTime)fdr[dc];
                                feature.SetField(idx, dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, 0);
                                break;

                            case OgrFieldType.OFTTime:
                                var tod = ((DateTime)fdr[dc]).TimeOfDay;
                                feature.SetField(idx, 0, 0, 0, tod.Hours, tod.Minutes, tod.Seconds, 0);
                                break;

                            case OgrFieldType.OFTInteger:
                                feature.SetField(idx, Convert.ToInt32(fdr[dc]));
                                break;

                            case OgrFieldType.OFTIntegerList:
                                var il = GetIntegerList(fdr[dc], dc.DataType);
                                feature.SetFieldIntegerList(idx, il.Length, il);
                                break;

                            case OgrFieldType.OFTReal:
                                feature.SetField(idx, Convert.ToDouble(fdr[dc]));
                                break;

                            case OgrFieldType.OFTRealList:
                                var dl = GetDoubleList(fdr[dc], dc.DataType);
                                feature.SetFieldDoubleList(idx, dl.Length, dl);
                                break;

                            case OgrFieldType.OFTString:
                                feature.SetField(idx, Convert.ToString(fdr[dc]));
                                break;

                            case OgrFieldType.OFTStringList:
                                var sl = (string[])fdr[dc];
                                feature.SetFieldStringList(idx, sl);
                                break;
                            }
                            fd.Dispose();
                        }
                        lyr.CreateFeature(feature);
                        feature.Dispose();
                    }
                    //ld.Dispose();
                }
            }

            lyr.Dispose();
            ds.Dispose();
            dr.Dispose();
        }
Exemplo n.º 7
0
        /// <summary>
        /// Creates an OGR data source from a FeatureDataTable
        /// </summary>
        /// <param name="table">The name of the table</param>
        /// <param name="geometryType">The geometry type</param>
        /// <param name="driver">The driver</param>
        /// <param name="connection">The connection string</param>
        /// <param name="driverOptions">The options for the driver</param>
        /// <param name="layerOptions">The options for the layer</param>
        public static void CreateFromFeatureDataTable(FeatureDataTable table, 
            OgcGeometryType geometryType, int srid, string driver, string connection, string[] driverOptions = null, string[] layerOptions = null)
        {
            if (table == null)
                throw new ArgumentNullException("table");

            if (table.Rows.Count == 0)
                throw new ArgumentException("The table contains no rows", "table");

            if (geometryType < OgcGeometryType.Point || geometryType > OgcGeometryType.MultiPolygon)
                throw new ArgumentException("Invalid geometry type", "geometryType");

            if (string.IsNullOrWhiteSpace(driver))
                throw new ArgumentException("No driver specified", "driver");

            var dr = OSGeo.OGR.Ogr.GetDriverByName(driver);
            if (dr == null)
                throw new Exception(string.Format("Cannot load driver '{0}'!", driver));

            //if (!dr.TestCapability("ODrCCreateDataSource"))
            //    throw new Exception(string.Format("Driver '{0}' cannot create a data source!", driver));

            // Create the data source
            var ds = dr.CreateDataSource(connection, driverOptions);
            //if (!ds.TestCapability("ODsCCreateLayer"))
            //    throw new Exception(string.Format("Driver '{0}' cannot create a layer!", driver));

            // Create the spatial reference
            var sr = new OSGeo.OSR.SpatialReference(string.Empty);
            sr.ImportFromEPSG(srid);

            // Create the layer
            var lyr = ds.CreateLayer(table.TableName, sr, (OgrGeometryType)geometryType, layerOptions);
            sr.Dispose();

            //lyr.GetSpatialRef();
            foreach (System.Data.DataColumn dc in table.Columns)
            {
                using (var fldDef = GetFieldDefinition(dc))
                    lyr.CreateField(fldDef, 0);
            }

            using (var ld = lyr.GetLayerDefn())
            {
                foreach (FeatureDataRow fdr in table.Rows)
                {
                    if ((int)fdr.Geometry.OgcGeometryType != (int)geometryType)
                        continue;

                    using (var feature = new OgrFeature(ld))
                    {
                        feature.SetGeometry(OgrGeometry.CreateFromWkb(fdr.Geometry.AsBinary()));
                        var idx = -1;
                        foreach (System.Data.DataColumn dc in table.Columns)
                        {
                            idx++;
                            var fd = ld.GetFieldDefn(idx);
                            DateTime dt;
                            switch (fd.GetFieldType())
                            {
                                case OgrFieldType.OFTBinary:
                                    //Nothing
                                    break;
                                case OgrFieldType.OFTDate:
                                    dt = ((DateTime)fdr[dc]).Date;
                                    feature.SetField(idx, dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, 0);
                                    break;
                                case OgrFieldType.OFTDateTime:
                                    dt = (DateTime)fdr[dc];
                                    feature.SetField(idx, dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, 0);
                                    break;
                                case OgrFieldType.OFTTime:
                                    var tod = ((DateTime)fdr[dc]).TimeOfDay;
                                    feature.SetField(idx, 0, 0, 0, tod.Hours, tod.Minutes, tod.Seconds, 0);
                                    break;
                                case OgrFieldType.OFTInteger:
                                    feature.SetField(idx, Convert.ToInt32(fdr[dc]));
                                    break;
                                case OgrFieldType.OFTIntegerList:
                                    var il = GetIntegerList(fdr[dc], dc.DataType);
                                    feature.SetFieldIntegerList(idx, il.Length, il);
                                    break;
                                case OgrFieldType.OFTReal:
                                    feature.SetField(idx, Convert.ToDouble(fdr[dc]));
                                    break;
                                case OgrFieldType.OFTRealList:
                                    var dl = GetDoubleList(fdr[dc], dc.DataType);
                                    feature.SetFieldDoubleList(idx, dl.Length, dl);
                                    break;
                                case OgrFieldType.OFTString:
                                    feature.SetField(idx, Convert.ToString(fdr[dc]));
                                    break;
                                case OgrFieldType.OFTStringList:
                                    var sl = (string[])fdr[dc];
                                    feature.SetFieldStringList(idx, sl);
                                    break;

                            }
                            fd.Dispose();
                        }
                        lyr.CreateFeature(feature);
                        feature.Dispose();
                    }
                    //ld.Dispose();
                }
            }

            lyr.Dispose();
            ds.Dispose();
            dr.Dispose();
        }
Exemplo n.º 8
0
        /// <summary>
        /// 以前的代码
        /// </summary>
        /// <param name="dsmDs"></param>
        /// <param name="oriFeat"></param>
        /// <param name="bufFeat"></param>
        /// <param name="subRasterOff_Size"></param>
        private static void getMaxMinValue_(OSGeo.GDAL.Dataset dsmDs, OSGeo.OGR.Feature oriFeat, OSGeo.OGR.Feature bufFeat, int[] subRasterOff_Size)
        {
            //拿到当前Feature对应的栅格值(数组)
            double[] rastValue = new double[subRasterOff_Size[2] * subRasterOff_Size[3]];
            //拿到全局Transfrom
            double[] Transfrom = new double[6];
            dsmDs.GetGeoTransform(Transfrom);

            //把SUBimg的值读进数组
            dsmDs.GetRasterBand(1).ReadRaster(
                subRasterOff_Size[0],
                subRasterOff_Size[1],
                subRasterOff_Size[2],
                subRasterOff_Size[3],
                rastValue,
                subRasterOff_Size[2],
                subRasterOff_Size[3],
                0, 0);

            OSGeo.OGR.Geometry oriGeom = oriFeat.GetGeometryRef();
            string             ddd     = oriFeat.GetFieldAsString("MAX");

            while (oriFeat.GetFieldAsString("MAX") == "")
            {
                //拿到数组中最大值的索引
                int maxId = Array.IndexOf(rastValue, rastValue.Max());

                //通过索引拿到空间坐标
                double maxX, maxY;
                StaticTools.indexToGeoSpace(maxId, subRasterOff_Size, Transfrom, out maxX, out maxY);

                //把空间坐标写进Geomtry
                OSGeo.OGR.Geometry maxGeom = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbPoint);
                maxGeom.AddPoint_2D(maxX, maxY);

                //判断坐标是否在Feature中,在则把值写入对应字段,不在则把当前值改为平均值

                if (maxGeom.Within(oriGeom))
                {
                    oriFeat.SetField("MAX", rastValue.Max());
                }
                else
                {
                    rastValue[maxId] = rastValue.Average();
                }
            }
            string dd = oriFeat.GetFieldAsString("MIN");

            while (oriFeat.GetFieldAsString("MIN") == "")
            {
                //拿到数组中最小值的索引
                double ffdf  = rastValue.Min();
                int    minId = Array.IndexOf(rastValue, rastValue.Min());

                //通过索引拿到空间坐标
                double minX, minY;
                StaticTools.indexToGeoSpace(minId, subRasterOff_Size, Transfrom, out minX, out minY);

                //把空间坐标写进Geomtry
                OSGeo.OGR.Geometry minGeom = new OSGeo.OGR.Geometry(OSGeo.OGR.wkbGeometryType.wkbPoint);
                minGeom.AddPoint_2D(minX, minY);

                //判断坐标是否在Feature中,在则把值写入对应字段,不在则把当前值改为平均值
                OSGeo.OGR.Geometry bufGeom = bufFeat.GetGeometryRef();
                if (minGeom.Within(bufGeom))
                {
                    oriFeat.SetField("MIN", rastValue.Min());
                }
                else
                {
                    rastValue[minId] = rastValue.Average();
                }
            }
            double max = oriFeat.GetFieldAsDouble("MAX");
            double min = oriFeat.GetFieldAsDouble("MIN");

            oriFeat.SetField("HIGHT", max - min);
        }