public object GetValue(int i) { if (i == FidFieldOrdinal) { long fid = _currentFeature.GetFID(); return(fid); } if (i == GeometryFieldOrdinal) { return(GetGeometry()); } switch (_ogrFeatureDefinition.GetFieldDefn(i - 2).GetFieldType()) { case FieldType.OFTString: return(_currentFeature.GetFieldAsString(i - 2)); case FieldType.OFTInteger: return(_currentFeature.GetFieldAsInteger(i - 2)); case FieldType.OFTDateTime: return(GetFieldAsDateTime(i)); case FieldType.OFTReal: return(_currentFeature.GetFieldAsDouble(i - 2)); default: return(null); } }
public object get_mapped_value(OSGeo.OGR.Feature feature, int esriFieldsIndex) { // get the ESRI Field ESRI.ArcGIS.Geodatabase.IField pField = m_fields.get_Field(esriFieldsIndex); if (esriFieldsIndex == m_oidFieldIndex) { return(feature.GetFID()); } if (esriFieldsIndex == m_geometryFieldIndex) { System.Diagnostics.Debug.Assert(false); return(null); // this should never be called for geometries } int ogrIndex = (int)m_fieldMapping[esriFieldsIndex]; if (!feature.IsFieldSet(ogrIndex)) { return(null); } switch (feature.GetFieldType(ogrIndex)) { // must be kept in sync with utilities library case OSGeo.OGR.FieldType.OFTInteger: return(feature.GetFieldAsInteger(ogrIndex)); case OSGeo.OGR.FieldType.OFTReal: return(feature.GetFieldAsDouble(ogrIndex)); case OSGeo.OGR.FieldType.OFTString: return(feature.GetFieldAsString(ogrIndex)); case OSGeo.OGR.FieldType.OFTBinary: // WTF, the C# bindings don't have a blob retrieval until this ticket gets solved // http://trac.osgeo.org/gdal/ticket/4457#comment:2 return(null); case OSGeo.OGR.FieldType.OFTDateTime: { int year, month, day, hour, minute, second, flag; feature.GetFieldAsDateTime(ogrIndex, out year, out month, out day, out hour, out minute, out second, out flag); DateTime date = new DateTime(year, month, day, hour, minute, second); return(date); } default: return(feature.GetFieldAsString(ogrIndex)); //most things coerce as strings } }
/// <summary> /// Returns geometry Object IDs whose bounding box intersects 'bbox' /// </summary> /// <param name="bbox"></param> /// <returns></returns> public Collection <uint> GetObjectIDsInView(SharpMap.Geometries.BoundingBox bbox) { _OgrLayer.SetSpatialFilterRect(bbox.Min.X, bbox.Min.Y, bbox.Max.X, bbox.Max.Y); OSGeo.OGR.Feature _OgrFeature = null; _OgrLayer.ResetReading(); Collection <uint> _ObjectIDs = new Collection <uint>(); while ((_OgrFeature = _OgrLayer.GetNextFeature()) != null) { _ObjectIDs.Add((uint)_OgrFeature.GetFID()); _OgrFeature.Dispose(); } return(_ObjectIDs); }
/// <summary> /// Returns geometry Object IDs whose bounding box intersects 'bbox' /// </summary> /// <param name="bbox"></param> /// <returns></returns> public Collection <uint> GetObjectIDsInView(IEnvelope bbox) { _OgrLayer.SetSpatialFilterRect(bbox.MinX, bbox.MinY, bbox.MaxX, bbox.MaxY); OSGeo.OGR.Feature _OgrFeature = null; _OgrLayer.ResetReading(); Collection <uint> _ObjectIDs = new Collection <uint>(); while ((_OgrFeature = _OgrLayer.GetNextFeature()) != null) { _ObjectIDs.Add((uint)_OgrFeature.GetFID()); _OgrFeature.Dispose(); } return(_ObjectIDs); }
public static void ReportFeature(Feature feat, FeatureDefn def) { Console.WriteLine( "Feature(" + def.GetName() + "): " + feat.GetFID() ); for( int iField = 0; iField < feat.GetFieldCount(); iField++ ) { FieldDefn fdef = def.GetFieldDefn( iField ); Console.Write( fdef.GetNameRef() + " (" + fdef.GetFieldTypeName(fdef.GetFieldType()) + ") = "); if( feat.IsFieldSet( iField ) ) Console.WriteLine( feat.GetFieldAsString( iField ) ); else Console.WriteLine( "(null)" ); } if( feat.GetStyleString() != null ) Console.WriteLine( " Style = " + feat.GetStyleString() ); Geometry geom = feat.GetGeometryRef(); if( geom != null ) Console.WriteLine( " " + geom.GetGeometryName() + "(" + geom.GetGeometryType() + ")" ); Envelope env = new Envelope(); geom.GetEnvelope(env); Console.WriteLine( " ENVELOPE: " + env.MinX + "," + env.MaxX + "," + env.MinY + "," + env.MaxY); string geom_wkt; geom.ExportToWkt(out geom_wkt); Console.WriteLine( " " + geom_wkt ); Console.WriteLine( "" ); }
public void AddAttributeField(string oldFeatureFile) { DriverUtils.RegisterOgrDriver(); DataSource dataSource; Layer layer; var isShapeFile = IsShapeInGdb(oldFeatureFile); if (isShapeFile) { dataSource = Ogr.Open(oldFeatureFile, 1); //second argument in open specifies mode of data, 1 RW & 0 readonly mode layer = dataSource.GetLayerByIndex(0); var layerDefn = layer.GetLayerDefn(); int attrFieldIndex = (int)layerDefn.GetFieldIndex("FID_GDAL"); if (attrFieldIndex == -1) { FieldDefn gdalFiedlDefn = new FieldDefn("FID_GDAL", FieldType.OFTInteger); layer.CreateField(gdalFiedlDefn, 0); Feature feature = layer.GetNextFeature(); while (feature != null) { feature.SetField("FID_GDAL", feature.GetFID()); // Add FID shapefile layer.SetFeature(feature); feature = layer.GetNextFeature(); } layer.SyncToDisk(); } layer.Dispose(); dataSource.FlushCache(); } else { try { EnableEsriLiscences(); string gdbPath = Path.GetDirectoryName(oldFeatureFile); string featureName = Path.GetFileNameWithoutExtension(oldFeatureFile); IWorkspaceFactory workspaceFactory = new FileGDBWorkspaceFactory(); IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspaceFactory.OpenFromFile(gdbPath, 1); IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(featureName); IFields fields = featureClass.Fields; if (fields.FindField("FID_GDAL") == -1) { // Create a Int field called "FID_GDAL" for the fields collection IField gdalField = new FieldClass(); IFieldEdit gField = (IFieldEdit)gdalField; gField.Name_2 = "FID_GDAL"; gField.Type_2 = esriFieldType.esriFieldTypeInteger; //fieldsEdit.AddField(gField); featureClass.AddField(gdalField); } IFeatureCursor featureCursor = featureClass.Search(null, false); IFeature feature = featureCursor.NextFeature(); while (feature != null) { Console.WriteLine(feature.OID); Console.WriteLine(feature.Fields.FindField("FID_GDAL")); feature.set_Value(feature.Fields.FindField("FID_GDAL"), feature.OID); // [feature.Fields.FindFieldByAliasName("FID_GDAL")] feature.Store(); Console.WriteLine(feature.OID); feature = featureCursor.NextFeature(); } featureCursor.Flush(); } catch (Exception e) { Console.WriteLine(e.Message); } } }
public static void ReportFeature(Feature feat, FeatureDefn def) { Console.WriteLine( "Feature(" + def.GetName() + "): " + feat.GetFID() ); for( int iField = 0; iField < feat.GetFieldCount(); iField++ ) { FieldDefn fdef = def.GetFieldDefn( iField ); Console.Write( fdef.GetNameRef() + " (" + fdef.GetFieldTypeName(fdef.GetFieldType()) + ") = "); if( feat.IsFieldSet( iField ) ) { if (fdef.GetFieldType() == FieldType.OFTStringList) { string[] sList = feat.GetFieldAsStringList(iField); foreach (string s in sList) { Console.Write("\"" + s + "\" "); } Console.WriteLine(); } else if (fdef.GetFieldType() == FieldType.OFTIntegerList) { int count; int[] iList = feat.GetFieldAsIntegerList(iField, out count); for (int i = 0; i < count; i++) { Console.Write(iList[i] + " "); } Console.WriteLine(); } else if (fdef.GetFieldType() == FieldType.OFTRealList) { int count; double[] iList = feat.GetFieldAsDoubleList(iField, out count); for (int i = 0; i < count; i++) { Console.Write(iList[i].ToString() + " "); } Console.WriteLine(); } else Console.WriteLine(feat.GetFieldAsString(iField)); } else Console.WriteLine( "(null)" ); } if( feat.GetStyleString() != null ) Console.WriteLine( " Style = " + feat.GetStyleString() ); Geometry geom = feat.GetGeometryRef(); if( geom != null ) { Console.WriteLine( " " + geom.GetGeometryName() + "(" + geom.GetGeometryType() + ")" ); Geometry sub_geom; for (int i = 0; i < geom.GetGeometryCount(); i++) { sub_geom = geom.GetGeometryRef(i); if ( sub_geom != null ) { Console.WriteLine( " subgeom" + i + ": " + sub_geom.GetGeometryName() + "(" + sub_geom.GetGeometryType() + ")" ); } } Envelope env = new Envelope(); geom.GetEnvelope(env); Console.WriteLine(" ENVELOPE: " + env.MinX + "," + env.MaxX + "," + env.MinY + "," + env.MaxY); string geom_wkt; geom.ExportToWkt(out geom_wkt); Console.WriteLine(" " + geom_wkt); } Console.WriteLine( "" ); }