public override ESRI.ArcGIS.Geodatabase.IFields2 CreateFields(ESRI.ArcGIS.Geometry.ISpatialReference spatialReference) { ESRI.ArcGIS.Geodatabase.IFields2 fields = new ESRI.ArcGIS.Geodatabase.FieldsClass(); ESRI.ArcGIS.Geodatabase.IFieldsEdit fieldsEdit = (ESRI.ArcGIS.Geodatabase.IFieldsEdit)fields; ESRI.ArcGIS.Geodatabase.IFieldEdit2 fieldedit = CreateField("ObjectID", "FID", ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeOID, 0); fieldsEdit.AddField(fieldedit); //add id fieldedit = CreateField("LineID", "LineID", ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeGUID, 36); fieldedit.DefaultValue_2 = 0; // add default for letter fieldsEdit.AddField(fieldedit); // place holder for letter fieldedit = CreateField("LETTER", "LETTER", ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeString, 20); fieldedit.DefaultValue_2 = "X"; // add default for letter fieldsEdit.AddField(fieldedit); // just another field fieldedit = CreateField("NOTES", "NOTES", ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeString, 225); fieldsEdit.AddField(fieldedit); // add geomtype ESRI.ArcGIS.Geodatabase.IGeometryDef geometryDef = new ESRI.ArcGIS.Geodatabase.GeometryDefClass(); ESRI.ArcGIS.Geodatabase.IGeometryDefEdit geometryDefEdit = (ESRI.ArcGIS.Geodatabase.IGeometryDefEdit)geometryDef; geometryDefEdit.GeometryType_2 = ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline; ESRI.ArcGIS.Geometry.ISpatialReferenceResolution spatialReferenceResolution = (ESRI.ArcGIS.Geometry.ISpatialReferenceResolution)spatialReference; ESRI.ArcGIS.Geometry.ISpatialReferenceTolerance spatialReferenceTolerance = (ESRI.ArcGIS.Geometry.ISpatialReferenceTolerance)spatialReference; spatialReferenceTolerance.SetDefaultXYTolerance(); geometryDefEdit.SpatialReference_2 = spatialReference; ESRI.ArcGIS.Geodatabase.IField geometryField = new ESRI.ArcGIS.Geodatabase.FieldClass(); ESRI.ArcGIS.Geodatabase.IFieldEdit geometryFieldEdit = (ESRI.ArcGIS.Geodatabase.IFieldEdit)geometryField; geometryFieldEdit.Name_2 = "Shape"; geometryFieldEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeGeometry; geometryFieldEdit.GeometryDef_2 = geometryDef; fieldsEdit.AddField(geometryField); return(fields); }
public System.Boolean BuildShapefileSpatialIndex(ESRI.ArcGIS.Geodatabase.IFeatureClass ShapefileFeatureClass) { ESRI.ArcGIS.Geodatabase.IFields indexShapefileFields = null; ESRI.ArcGIS.Geodatabase.IFieldsEdit indexShapefileFieldsEdit = null; ESRI.ArcGIS.Geodatabase.IField shapefileGeometryField = null; ESRI.ArcGIS.Geodatabase.IIndex shapefileSpatialIndex = null; ESRI.ArcGIS.Geodatabase.IIndexEdit shapefileSpatialIndexEdit = null; ESRI.ArcGIS.Geodatabase.IEnumIndex shapefileExistingIndices = null; ESRI.ArcGIS.Geodatabase.IIndex shapefileDeleteIndex = null; try { // Make sure the Feature Class Object is not a NULL Pointer before moving on. if (ShapefileFeatureClass == null) { // Let the User know that a valid Shapefile Name MUST be passed before the index can // be created. if (ErrorMessage != null) { ErrorMessage("A Valid Shapefile Object must be passed to this method! The MaintTools.FeatureClassUtilities.BuildShapefileSpatialIndex() Method failed!"); } // Return FALSE to the calling method to indicate that this method failed. return false; } // If the Process Message Event has been instantiated, let the user know what is happening. if (ProcessMessage != null) { ProcessMessage(" - Opening the Shapefile Geometry Field and preparing it to be indexed..."); } // Create a Fields Object and open an Editing Object on it so that new fields can be added. indexShapefileFields = new ESRI.ArcGIS.Geodatabase.FieldsClass(); indexShapefileFieldsEdit = (ESRI.ArcGIS.Geodatabase.IFieldsEdit)indexShapefileFields; // Find the Geometry Field in the Field in the Shapefile Fields Object and add it to the // Fields Collection. indexShapefileFieldsEdit.FieldCount_2 = 1; int l = ShapefileFeatureClass.FindField(ShapefileFeatureClass.ShapeFieldName); if (l < 0) { // Let the user know that the Geometry Field of the Shapefile Feature Class could not // be found. if (ErrorMessage != null) { ErrorMessage("The Shapefile Geometry Field could not be found! The MaintTools.FeatureClassUtilities.BuildShapefileSpatialIndex() Method failed!"); } // Return FALSE to the calling method to indicate that this process failed. return false; } shapefileGeometryField = ShapefileFeatureClass.Fields.get_Field(l); indexShapefileFieldsEdit.set_Field(0, shapefileGeometryField); // If the Process Message Event has been instantiated, let the user know what is happening. if (ProcessMessage != null) { ProcessMessage(" - Creating the Index and applying it to the Shapefile Feature Class Geometry Field..."); } // Create a new index and prepare it to be added to the table. shapefileSpatialIndex = new ESRI.ArcGIS.Geodatabase.IndexClass(); shapefileSpatialIndexEdit = (ESRI.ArcGIS.Geodatabase.IIndexEdit)shapefileSpatialIndex; shapefileSpatialIndexEdit.Fields_2 = indexShapefileFields; shapefileSpatialIndexEdit.Name_2 = "Idx_1"; // Remove all Indices from the table. shapefileExistingIndices = ShapefileFeatureClass.Indexes.FindIndexesByFieldName(ShapefileFeatureClass.ShapeFieldName); shapefileDeleteIndex = shapefileExistingIndices.Next(); while (shapefileDeleteIndex != null) { ShapefileFeatureClass.DeleteIndex(shapefileDeleteIndex); shapefileDeleteIndex = shapefileExistingIndices.Next(); } // Add the index to the Shapefile. ShapefileFeatureClass.AddIndex(shapefileSpatialIndex); // If the process made it to here it was successful so return TRUE to the calling method. return true; } catch (System.Exception caught) { // Determine the Line Number from which the exception was thrown. System.Diagnostics.StackTrace stackTrace = new System.Diagnostics.StackTrace(caught, true); System.Diagnostics.StackFrame stackFrame = stackTrace.GetFrame(stackTrace.FrameCount - 1); int lineNumber = stackFrame.GetFileLineNumber(); // Let the user know that this method failed. if (ErrorMessage != null) { ErrorMessage("The MaintTools.FeatureClassUtilities.BuildShapefileSpatialIndex() Method failed with error message - " + caught.Message + " (Line: " + lineNumber.ToString() + ")!"); } // Return FALSE to the calling method to indicate that this method Failed. return false; } finally { // Close any Object Pointers that were created by this method. if (indexShapefileFields != null) { // Close the Object Pointer. indexShapefileFields = null; } if (indexShapefileFieldsEdit != null) { // Close the Object Pointer. indexShapefileFieldsEdit = null; } if (shapefileGeometryField != null) { // Close the Object Pointer. shapefileGeometryField = null; } if (shapefileSpatialIndex != null) { // Close the Object Pointer. shapefileSpatialIndex = null; } if (shapefileSpatialIndexEdit != null) { // Close the Object Pointer. shapefileSpatialIndexEdit = null; } if (shapefileExistingIndices != null) { // Close the Object Pointer. shapefileExistingIndices = null; } if (shapefileDeleteIndex != null) { // Close the Object Pointer. shapefileDeleteIndex = null; } } }
public static void map_fields(OSGeo.OGR.Layer ogr_layer, out System.Collections.Hashtable outFieldMap, out ESRI.ArcGIS.Geodatabase.IFields outFields, out ESRI.ArcGIS.Geodatabase.esriDatasetType outDatasetType, out ESRI.ArcGIS.Geometry.esriGeometryType outGeometryType, out int outShapeIndex, out int outOIDFieldIndex, out ISpatialReference outSpatialReference) { outSpatialReference = null; outFields = null; outDatasetType = ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTTable; // start assuming it is a table outGeometryType = esriGeometryType.esriGeometryNull; //don't know what it is outOIDFieldIndex = -1; outShapeIndex = -1; outFieldMap = new System.Collections.Hashtable(); System.Collections.ArrayList fieldArray = new System.Collections.ArrayList(); OSGeo.OGR.FeatureDefn featDef = ogr_layer.GetLayerDefn(); int fieldsInserted = 0; // OIDs and Geometries can be pseudo fields in GDAL and are thus *may* not included in the OGR FieldDef // To account for that add those first (if they exist) and keep a mapping of fields using // fieldsInserted ////////////////////////////// // // handle oid field pseudo column // ESRI.ArcGIS.Geodatabase.IFieldEdit2 oidFieldEdit = new ESRI.ArcGIS.Geodatabase.FieldClass(); if (ogr_layer.GetFIDColumn().Length > 0) { oidFieldEdit.Name_2 = ogr_layer.GetFIDColumn(); oidFieldEdit.AliasName_2 = ogr_layer.GetFIDColumn(); } else { oidFieldEdit.Name_2 = "FID"; oidFieldEdit.AliasName_2 = "FID"; } oidFieldEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeOID; fieldArray.Add(oidFieldEdit); outOIDFieldIndex = fieldsInserted; fieldsInserted++; ////////////////////////////////////// // // handle (optional) geometry field pseudo column // if (!(ogr_layer.GetGeomType() == OSGeo.OGR.wkbGeometryType.wkbNone || ogr_layer.GetGeomType() == OSGeo.OGR.wkbGeometryType.wkbUnknown)) { ESRI.ArcGIS.Geodatabase.IFieldEdit2 geomFieldEdit = new ESRI.ArcGIS.Geodatabase.FieldClass(); if (ogr_layer.GetGeometryColumn().Length > 0) { geomFieldEdit.Name_2 = ogr_layer.GetGeometryColumn(); geomFieldEdit.AliasName_2 = ogr_layer.GetGeometryColumn(); } else { geomFieldEdit.Name_2 = "Shape"; geomFieldEdit.AliasName_2 = "Shape"; } geomFieldEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeGeometry; // add geometry def ESRI.ArcGIS.Geometry.esriGeometryType gdbType; bool hasZ; ogr_geo_type_to_esri_geo_type(ogr_layer.GetGeomType(), out gdbType, out hasZ); ESRI.ArcGIS.Geodatabase.IGeometryDefEdit geomDef = new ESRI.ArcGIS.Geodatabase.GeometryDefClass(); geomDef.GeometryType_2 = gdbType; geomDef.HasM_2 = false; //no M support on OGR geomDef.HasZ_2 = hasZ; geomDef.SpatialReference_2 = outSpatialReference = ogr_utils.get_spatialReference(ogr_layer.GetSpatialRef()); geomFieldEdit.GeometryDef_2 = geomDef; fieldArray.Add(geomFieldEdit); outDatasetType = ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureClass; // upgrade to featureclass outGeometryType = gdbType; outShapeIndex = fieldsInserted; fieldsInserted++; } int fieldCount = featDef.GetFieldCount(); for (int i = 0; i < fieldCount; i++) { // map OGR field to ArcObjects OSGeo.OGR.FieldDefn fieldDef = featDef.GetFieldDefn(i); ESRI.ArcGIS.Geodatabase.IFieldEdit2 fieldEdit = new ESRI.ArcGIS.Geodatabase.FieldClass(); fieldEdit.Name_2 = fieldDef.GetName(); fieldEdit.AliasName_2 = fieldDef.GetName(); // map type OSGeo.OGR.FieldType ogrFieldType = fieldDef.GetFieldType(); ESRI.ArcGIS.Geodatabase.esriFieldType mappedType; switch (ogrFieldType) { case OSGeo.OGR.FieldType.OFTInteger: mappedType = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeInteger; break; case OSGeo.OGR.FieldType.OFTReal: mappedType = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeDouble; break; case OSGeo.OGR.FieldType.OFTString: mappedType = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeString; break; case OSGeo.OGR.FieldType.OFTBinary: mappedType = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeBlob; break; case OSGeo.OGR.FieldType.OFTDateTime: mappedType = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeDate; break; default: mappedType = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeString; break; } fieldEdit.Type_2 = mappedType; outFieldMap.Add(fieldsInserted, i); fieldArray.Add(fieldEdit); fieldsInserted++; } // Add all the fields from the array to an ESRI fields class object. The reason that we do that // here is that we need to know the count in advance ESRI.ArcGIS.Geodatabase.IFieldsEdit fields = new ESRI.ArcGIS.Geodatabase.FieldsClass(); fields.FieldCount_2 = fieldArray.Count; for (int i = 0; i < fieldArray.Count; i++) { fields.set_Field(i, fieldArray[i] as ESRI.ArcGIS.Geodatabase.IField); } outFields = fields; }
public static void map_fields(OSGeo.OGR.Layer ogr_layer, out System.Collections.Hashtable outFieldMap, out ESRI.ArcGIS.Geodatabase.IFields outFields, out ESRI.ArcGIS.Geodatabase.esriDatasetType outDatasetType, out ESRI.ArcGIS.Geometry.esriGeometryType outGeometryType, out int outShapeIndex, out int outOIDFieldIndex, out ISpatialReference outSpatialReference) { outSpatialReference = null; outFields = null; outDatasetType = ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTTable; // start assuming it is a table outGeometryType = esriGeometryType.esriGeometryNull; //don't know what it is outOIDFieldIndex = -1; outShapeIndex = -1; outFieldMap = new System.Collections.Hashtable(); System.Collections.ArrayList fieldArray = new System.Collections.ArrayList(); OSGeo.OGR.FeatureDefn featDef = ogr_layer.GetLayerDefn(); int fieldsInserted = 0; // OIDs and Geometries can be pseudo fields in GDAL and are thus *may* not included in the OGR FieldDef // To account for that add those first (if they exist) and keep a mapping of fields using // fieldsInserted ////////////////////////////// // // handle oid field pseudo column // ESRI.ArcGIS.Geodatabase.IFieldEdit2 oidFieldEdit = new ESRI.ArcGIS.Geodatabase.FieldClass(); if (ogr_layer.GetFIDColumn().Length > 0) { oidFieldEdit.Name_2 = ogr_layer.GetFIDColumn(); oidFieldEdit.AliasName_2 = ogr_layer.GetFIDColumn(); } else { oidFieldEdit.Name_2 = "FID"; oidFieldEdit.AliasName_2 = "FID"; } oidFieldEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeOID; fieldArray.Add(oidFieldEdit); outOIDFieldIndex = fieldsInserted; fieldsInserted++; ////////////////////////////////////// // // handle (optional) geometry field pseudo column // if (!(ogr_layer.GetGeomType() == OSGeo.OGR.wkbGeometryType.wkbNone || ogr_layer.GetGeomType() == OSGeo.OGR.wkbGeometryType.wkbUnknown)) { ESRI.ArcGIS.Geodatabase.IFieldEdit2 geomFieldEdit = new ESRI.ArcGIS.Geodatabase.FieldClass(); if (ogr_layer.GetGeometryColumn().Length > 0) { geomFieldEdit.Name_2 = ogr_layer.GetGeometryColumn(); geomFieldEdit.AliasName_2 = ogr_layer.GetGeometryColumn(); } else { geomFieldEdit.Name_2 = "Shape"; geomFieldEdit.AliasName_2 = "Shape"; } geomFieldEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeGeometry; // add geometry def ESRI.ArcGIS.Geometry.esriGeometryType gdbType; bool hasZ; ogr_geo_type_to_esri_geo_type(ogr_layer.GetGeomType(), out gdbType, out hasZ); ESRI.ArcGIS.Geodatabase.IGeometryDefEdit geomDef = new ESRI.ArcGIS.Geodatabase.GeometryDefClass(); geomDef.GeometryType_2 = gdbType; geomDef.HasM_2 = false; //no M support on OGR geomDef.HasZ_2 = hasZ; geomDef.SpatialReference_2 = outSpatialReference = ogr_utils.get_spatialReference(ogr_layer.GetSpatialRef()); geomFieldEdit.GeometryDef_2 = geomDef; fieldArray.Add(geomFieldEdit); outDatasetType = ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureClass; // upgrade to featureclass outGeometryType = gdbType; outShapeIndex = fieldsInserted; fieldsInserted++; } int fieldCount = featDef.GetFieldCount(); for (int i = 0; i < fieldCount; i++) { // map OGR field to ArcObjects OSGeo.OGR.FieldDefn fieldDef = featDef.GetFieldDefn(i); ESRI.ArcGIS.Geodatabase.IFieldEdit2 fieldEdit = new ESRI.ArcGIS.Geodatabase.FieldClass(); fieldEdit.Name_2 = fieldDef.GetName(); fieldEdit.AliasName_2 = fieldDef.GetName(); // map type OSGeo.OGR.FieldType ogrFieldType = fieldDef.GetFieldType(); ESRI.ArcGIS.Geodatabase.esriFieldType mappedType; switch (ogrFieldType) { case OSGeo.OGR.FieldType.OFTInteger: mappedType = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeInteger; break; case OSGeo.OGR.FieldType.OFTReal: mappedType = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeDouble; break; case OSGeo.OGR.FieldType.OFTString: mappedType = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeString; break; case OSGeo.OGR.FieldType.OFTBinary: mappedType = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeBlob; break; case OSGeo.OGR.FieldType.OFTDateTime: mappedType = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeDate; break; default: mappedType = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeString; break; } fieldEdit.Type_2 = mappedType; outFieldMap.Add(fieldsInserted, i); fieldArray.Add(fieldEdit); fieldsInserted++; } // Add all the fields from the array to an ESRI fields class object. The reason that we do that // here is that we need to know the count in advance ESRI.ArcGIS.Geodatabase.IFieldsEdit fields = new ESRI.ArcGIS.Geodatabase.FieldsClass(); fields.FieldCount_2 = fieldArray.Count; for (int i = 0; i < fieldArray.Count; i++) { fields.set_Field(i, fieldArray[i] as ESRI.ArcGIS.Geodatabase.IField); } outFields = fields; }