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); }
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); }
//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.OFTInteger64: values[iField] = ogrFeature.GetFieldAsInteger64(iField); break; case OgrFieldType.OFTIntegerList: values[iField] = ogrFeature.GetFieldAsIntegerList(iField, out count); break; case OgrFieldType.OFTInteger64List: values[iField] = null; // ogrFeature.GetFieldAsInteger64List(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: int 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); }