Example #1
0
        public static object GetFieldValue(this OSGeo.OGR.Feature feature, string fieldName)
        {
            if (feature == null || string.IsNullOrEmpty(fieldName))
            {
                throw new Exception("参数设置错误");
            }
            using var fieldDefn = feature.GetFieldDefnRef(fieldName);
            object value;

            switch (fieldDefn.GetFieldType())
            {
            case OSGeo.OGR.FieldType.OFTString:
                value = feature.GetFieldAsString(fieldName);
                break;

            case OSGeo.OGR.FieldType.OFTInteger:
                value = feature.GetFieldAsInteger(fieldName);
                break;

            case OSGeo.OGR.FieldType.OFTInteger64:
                value = feature.GetFieldAsInteger64(fieldName);
                break;

            case OSGeo.OGR.FieldType.OFTReal:
                value = feature.GetFieldAsDouble(fieldName);
                break;

            default:
                throw new NotImplementedException();
            }
            return(value);
        }
Example #2
0
        public static object GetFieldValue(this OSGeo.OGR.Feature feature, int index)
        {
            if (feature == null || index < 0 || index >= feature.GetFieldCount())
            {
                throw new Exception("参数设置错误");
            }
            using var fieldDefn = feature.GetFieldDefnRef(index);
            object value;

            switch (fieldDefn.GetFieldType())
            {
            case  OSGeo.OGR.FieldType.OFTString:
                value = feature.GetFieldAsString(index);
                break;

            case OSGeo.OGR.FieldType.OFTInteger:
                value = feature.GetFieldAsInteger(index);
                break;

            case OSGeo.OGR.FieldType.OFTInteger64:
                value = feature.GetFieldAsInteger64(index);
                break;

            case OSGeo.OGR.FieldType.OFTReal:
                value = feature.GetFieldAsDouble(index);
                break;

            default:
                throw new NotImplementedException();
            }
            return(value);
        }
Example #3
0
        /// <summary>
        /// Get value from Ogr feature
        /// </summary>
        /// <param name="feature">Ogr feature</param>
        /// <param name="i">Field index</param>
        /// <returns>Value</returns>
        public static object GetOgrValue(OSGeo.OGR.Feature feature, int i)
        {
            switch (feature.GetFieldDefnRef(i).GetFieldType())
            {
            case OSGeo.OGR.FieldType.OFTString:
                return(feature.GetFieldAsString(i));

            case OSGeo.OGR.FieldType.OFTInteger:
                return(feature.GetFieldAsInteger(i));

            case OSGeo.OGR.FieldType.OFTDateTime:
                return(GetFieldAsDateTime(feature, i));

            case OSGeo.OGR.FieldType.OFTReal:
                return(feature.GetFieldAsDouble(i));

            default:
                return(null);
            }
        }
Example #4
0
        //private static Geometry ParseOgrGeometry(OgrGeometry ogrGeometry, WKBReader reader)
        ////private static Geometry ParseOgrGeometry(OgrGeometry ogrGeometry, IGeometryFactory factory)
        //{
        //    if (ogrGeometry != null)
        //    {

        //        //Just in case it isn't 2D
        //        ogrGeometry.FlattenTo2D();
        //        var wkbBuffer = new byte[ogrGeometry.WkbSize()];
        //        ogrGeometry.ExportToWkb(wkbBuffer);
        //        //var geom = GeometryFromWKB.Parse(wkbBuffer, factory);
        //        var geom = reader.Read(wkbBuffer);
        //        if (geom == null)
        //            _log.Debug(t=> t("Failed to parse '{0}'", ogrGeometry.GetGeometryType()));
        //        return geom;

        //        ogrGeometry.GetGeometry()
        //    }
        //    return null;
        //}

        //private static FeatureDataRow LoadOgrFeatureToFeatureDataRow(FeatureDataTable table, OSGeo.OGR.Feature ogrFeature, GeoAPI.Geometries.IGeometryFactory factory)
        private static FeatureDataRow LoadOgrFeatureToFeatureDataRow(FeatureDataTable table, OSGeo.OGR.Feature ogrFeature, OgrGeometryReader reader)
        {
            var values = new object[ogrFeature.GetFieldCount()];

            for (var iField = 0; iField < ogrFeature.GetFieldCount(); iField++)
            {
                // No need to get field value if there's no value available...
                if (!ogrFeature.IsFieldSet(iField))
                {
                    continue;
                }

                int count;
                switch (ogrFeature.GetFieldType(iField))
                {
                case OgrFieldType.OFTString:
                case OgrFieldType.OFTWideString:
                    values[iField] = ogrFeature.GetFieldAsString(iField);
                    break;

                case OgrFieldType.OFTStringList:
                case OgrFieldType.OFTWideStringList:
                    values[iField] = ogrFeature.GetFieldAsStringList(iField);
                    break;

                case OgrFieldType.OFTInteger:
                    values[iField] = ogrFeature.GetFieldAsInteger(iField);
                    break;

                case OgrFieldType.OFTIntegerList:
                    values[iField] = ogrFeature.GetFieldAsIntegerList(iField, out count);
                    break;

                case OgrFieldType.OFTReal:
                    values[iField] = ogrFeature.GetFieldAsDouble(iField);
                    break;

                case OgrFieldType.OFTRealList:
                    values[iField] = ogrFeature.GetFieldAsDoubleList(iField, out count);
                    break;

                case OgrFieldType.OFTDate:
                case OgrFieldType.OFTDateTime:
                case OgrFieldType.OFTTime:
                    Int32 y, m, d, h, mi, tz;
                    float s;
                    ogrFeature.GetFieldAsDateTime(iField, out y, out m, out d, out h, out mi, out s, out tz);
                    try
                    {
                        if (y == 0 && m == 0 && d == 0)
                        {
                            values[iField] = DateTime.MinValue.AddMinutes(h * 60 + mi);
                        }
                        else
                        {
                            values[iField] = new DateTime(y, m, d, h, mi, (int)s);
                        }
                    }
// ReSharper disable once EmptyGeneralCatchClause
                    catch { }
                    break;

                default:
                    var iTmpField = iField;
                    _log.Debug(t => t("Cannot handle Ogr DataType '{0}'", ogrFeature.GetFieldType(iTmpField)));
                    break;
                }
            }

            var fdr = (FeatureDataRow)table.LoadDataRow(values, true);

            using (var gr = ogrFeature.GetGeometryRef())
            {
                //fdr.Geometry = ParseOgrGeometry(gr, factory);
                fdr.Geometry = reader.Read(gr);
            }
            return(fdr);
        }
        /// <summary>
        /// This is the method that actually does the work.
        /// </summary>
        /// <param name="DA">The DA object is used to retrieve from inputs and store in outputs.</param>
        protected override void SolveInstance(IGH_DataAccess DA)
        {
            // See if gdal and ogr are working first.
            string output = "";

            try
            {
                GdalConfiguration.ConfigureOgr();

                GdalConfiguration.ConfigureGdal();

                output = "It works!";
            }

            catch (Exception e)
            {
                output = "{0} Exception caught. " + e;
            }

            string input = "";

            DA.GetData(0, ref input);

            var driver = OSGeo.OGR.Ogr.GetDriverByName("ESRI Shapefile");

            if (driver == null)
            {
                output = "Driver is null";
            }

            else
            {
                var ds = driver.Open(input, 0);

                if (ds == null)
                {
                    output = "DataSource is null";
                }

                else
                {
                    long numberOfFeatures = 0;
                    int  fieldCount       = 0;

                    int fieldSelector = 0;

                    DA.GetData(1, ref fieldSelector);

                    var layer = ds.GetLayerByIndex(0);

                    string geoType = layer.GetGeomType().ToString();

                    var layerDefinition = layer.GetLayerDefn();

                    numberOfFeatures = unchecked ((int)layer.GetFeatureCount(0));
                    fieldCount       = layerDefinition.GetFieldCount();

                    string[] fields = new string[fieldCount];

                    object[] fieldOut = new object[numberOfFeatures];

                    for (int i = 0; i < fieldCount; ++i)
                    {
                        fields[i] = layerDefinition.GetFieldDefn(i).GetName();
                    }

                    int fieldToSelect = 0;
                    DA.GetData(1, ref fieldToSelect);

                    //bool parallel = false;

                    //DA.GetData( 2, ref parallel );

                    OSGeo.OGR.Feature feature = null;

                    //if( parallel == false )
                    {
                        for (int i = 0; i < numberOfFeatures; ++i)
                        {
                            feature     = layer.GetFeature(i);
                            fieldOut[i] = feature.GetFieldAsString(fieldSelector);
                        }
                    }

                    /*else
                     * {
                     *
                     *  try
                     *  {
                     *
                     *      object locker = new object();
                     *
                     *      System.Threading.Tasks.Parallel.For( 0, numberOfFeatures, i =>
                     *      {
                     *
                     *          feature = layer.GetFeature(i);
                     *
                     *          lock( locker )
                     *          {
                     *
                     *              fieldOut[i] = feature.GetFieldAsString( fieldSelector );
                     *
                     *          }
                     *
                     *          //feature = null;
                     *
                     *      });
                     *
                     *  }
                     *
                     *  catch( Exception e )
                     *  {
                     *
                     *      output = "{0} Exception caught. " + e;
                     *
                     *  }
                     *
                     * }*/

                    DA.SetData(1, numberOfFeatures);
                    DA.SetDataList(2, fields);
                    DA.SetDataList(3, fieldOut);
                    DA.SetData(4, geoType);
                }
            }

            DA.SetData(0, output);
        }
Example #6
0
        private static FeatureDataRow OgrFeatureToFeatureDataRow(FeatureDataTable table, OSGeo.OGR.Feature ogrFeature, GeoAPI.Geometries.IGeometryFactory factory)
        {
            FeatureDataRow fdr      = table.NewRow();
            Int32          fdrIndex = 0;

            for (int iField = 0; iField < ogrFeature.GetFieldCount(); iField++)
            {
                //*** BEGIN FIX: Ensure fdrIndex is incremented if field value is not set and field is of a supported type.
                bool         valueAvailable = ogrFeature.IsFieldSet(iField);
                OgrFieldType fieldType      = ogrFeature.GetFieldType(iField);

                // No need to get field value if there's no value available...
                if (!valueAvailable)
                {
                    // We need to increment the data row column index if this is a supported field type
                    // otherwise the next field will be written to the wrong column...
                    // We support all types except OFTBinary...
                    // Supported field types must be consistent with the code in ReadColumnDefinition and the switch case below...
                    if (fieldType != OgrFieldType.OFTBinary)
                    {
                        fdrIndex++;
                    }

                    continue;
                }
                //*** END FIX
                //if (!ogrFeature.IsFieldSet(iField)) continue;
                try
                {
                    switch (ogrFeature.GetFieldType(iField))
                    {
                    case OgrFieldType.OFTString:
                    case OgrFieldType.OFTWideString:
                    {
                        fdr[fdrIndex++] = ogrFeature.GetFieldAsString(iField);
                        break;
                    }

                    case OgrFieldType.OFTStringList:
                    case OgrFieldType.OFTWideStringList:
                    {
                        fdr[fdrIndex++] = ogrFeature.GetFieldAsStringList(iField);
                        break;
                    }

                    case OgrFieldType.OFTInteger:
                    {
                        fdr[fdrIndex++] = ogrFeature.GetFieldAsInteger(iField);
                        break;
                    }

                    case OgrFieldType.OFTIntegerList:
                    {
                        int count = 0;
                        fdr[fdrIndex++] = ogrFeature.GetFieldAsIntegerList(iField, out count);
                        break;
                    }

                    case OgrFieldType.OFTReal:
                    {
                        fdr[fdrIndex++] = ogrFeature.GetFieldAsDouble(iField);
                        break;
                    }

                    case OgrFieldType.OFTRealList:
                    {
                        int count = 0;
                        fdr[fdrIndex++] = ogrFeature.GetFieldAsDoubleList(iField, out count);
                        break;
                    }

                    case OgrFieldType.OFTDate:
                    case OgrFieldType.OFTDateTime:
                    case OgrFieldType.OFTTime:
                        Int32 y, m, d, h, mi, s, tz;
                        ogrFeature.GetFieldAsDateTime(iField, out y, out m, out d, out h, out mi, out s, out tz);
                        try
                        {
                            if (y == 0 && m == 0 && d == 0)
                            {
                                fdr[fdrIndex++] = DateTime.MinValue.AddMinutes(h * 60 + mi);
                            }
                            else
                            {
                                fdr[fdrIndex++] = new DateTime(y, m, d, h, mi, s);
                            }
                        }
                        catch
                        {
                            throw;
                        }
                        break;

                    default:
                        Debug.WriteLine(string.Format("Cannot handle Ogr DataType '{0}', '{1}'", ogrFeature.GetFieldType(iField), iField));
                        break;
                    }
                }
                catch { }
            }

            using (var gr = ogrFeature.GetGeometryRef())
            {
                fdr.Geometry = ParseOgrGeometry(gr, factory);
                gr.Dispose();
            }
            return(fdr);
        }
Example #7
0
        private static FeatureDataRow OgrFeatureToFeatureDataRow(FeatureDataTable table, OSGeo.OGR.Feature ogrFeature, GeoAPI.Geometries.IGeometryFactory factory)
        {
            FeatureDataRow fdr      = table.NewRow();
            Int32          fdrIndex = 0;

            for (int iField = 0; iField < ogrFeature.GetFieldCount(); iField++)
            {
                if (!ogrFeature.IsFieldSet(iField))
                {
                    continue;
                }

                switch (ogrFeature.GetFieldType(iField))
                {
                case OgrFieldType.OFTString:
                case OgrFieldType.OFTWideString:
                    fdr[fdrIndex++] = ogrFeature.GetFieldAsString(iField);
                    break;

                case OgrFieldType.OFTStringList:
                case OgrFieldType.OFTWideStringList:
                    break;

                case OgrFieldType.OFTInteger:
                    fdr[fdrIndex++] = ogrFeature.GetFieldAsInteger(iField);
                    break;

                case OgrFieldType.OFTIntegerList:
                    break;

                case OgrFieldType.OFTReal:
                    fdr[fdrIndex++] = ogrFeature.GetFieldAsDouble(iField);
                    break;

                case OgrFieldType.OFTRealList:
                    break;

                case OgrFieldType.OFTDate:
                case OgrFieldType.OFTDateTime:
                case OgrFieldType.OFTTime:
                    Int32 y, m, d, h, mi, s, tz;
                    ogrFeature.GetFieldAsDateTime(iField, out y, out m, out d, out h, out mi, out s, out tz);
                    try
                    {
                        if (y == 0 && m == 0 && d == 0)
                        {
                            fdr[fdrIndex++] = DateTime.MinValue.AddMinutes(h * 60 + mi);
                        }
                        else
                        {
                            fdr[fdrIndex++] = new DateTime(y, m, d, h, mi, s);
                        }
                    }
                    catch { }
                    break;

                default:
                    Debug.WriteLine(string.Format("Cannot handle Ogr DataType '{0}'", ogrFeature.GetFieldType(iField)));
                    break;
                }
            }

            using (var gr = ogrFeature.GetGeometryRef())
            {
                fdr.Geometry = ParseOgrGeometry(gr, factory);
                gr.Dispose();
            }
            return(fdr);
        }
Example #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);
        }