Beispiel #1
0
        /// <summary>
        /// Get date and time from Ogr feature
        /// </summary>
        /// <param name="feature">Ogr feature</param>
        /// <param name="i">Field index</param>
        /// <returns>DateTime value</returns>
        public static DateTime GetFieldAsDateTime(OSGeo.OGR.Feature feature, int i)
        {
            int year;
            int month;
            int day;

            int   h;
            int   m;
            float s;
            int   flag;

            feature.GetFieldAsDateTime(i, out year, out month, out day, out h, out m, out s, out flag);
            try
            {
                return(new DateTime(year, month, day, h, m, (int)s));
            }
            catch (ArgumentOutOfRangeException)
            {
                return(DateTime.MinValue);
            }
        }
Beispiel #2
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);
        }
Beispiel #3
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);
        }
Beispiel #4
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);
        }