Esri ArcObjects Snippets - Series
Create Feature Class
// Create Feature Class
static IFeatureClass createFeatureClass(IFeatureDataset featureDataset,
String nameOfFeatureClass) throws Exception {
// This function creates a new feature class in a supplied feature
// dataset by creating all fields from scratch. IFeatureClassDescription (or
// IObjectClassDescription if the table is
// created at the workspace level) can be used to get the required
// fields and are used to
// get the InstanceClassID and ExtensionClassID.
// Create new fields collection with the number of fields you plan to
// add. Must add at least two fields
// for a feature class: Object ID and Shape field.
IFields fields = new Fields();
IFieldsEdit fieldsEdit = (IFieldsEdit) fields;
fieldsEdit.setFieldCount(3);
// Create Object ID field.
IField fieldUserDefined = new Field();
IFieldEdit fieldEdit = (IFieldEdit) fieldUserDefined;
fieldEdit.setName("OBJECTID");
fieldEdit.setAliasName("OBJECT ID");
fieldEdit.setType(esriFieldType.esriFieldTypeOID);
fieldsEdit.setFieldByRef(0, fieldUserDefined);
// Create Shape field.
fieldUserDefined = new Field();
fieldEdit = (IFieldEdit) fieldUserDefined;
// Set up geometry definition for the Shape field.
// You do not have to set the spatial reference, as it is inherited from
// the feature dataset.
IGeometryDef geometryDef = new GeometryDef();
IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit) geometryDef;
geometryDefEdit.setGeometryType(esriGeometryType.esriGeometryPolyline);
// By setting the grid size to 0, you are allowing ArcGIS to determine
// the appropriate grid sizes for the feature class.
// If in a personal geodatabase, the grid size is 1,000. If in a file or
// ArcSDE geodatabase, the grid size
// is based on the initial loading or inserting of features.
geometryDefEdit.setGridCount(1);
geometryDefEdit.setGridSize(0, 0);
geometryDefEdit.setHasM(false);
geometryDefEdit.setHasZ(false);
// Set standard field properties.
fieldEdit.setName("SHAPE");
fieldEdit.setType(esriFieldType.esriFieldTypeGeometry);
fieldEdit.setGeometryDefByRef(geometryDef);
fieldEdit.setIsNullable(true);
fieldEdit.setRequired(true);
fieldsEdit.setFieldByRef(1, fieldUserDefined);
// Create a field of type double to hold some information for the
// features.
fieldUserDefined = new Field();
fieldEdit = (IFieldEdit) fieldUserDefined;
fieldEdit.setName("Avg_income");
fieldEdit.setAliasName("Average income for 1999-2000");
fieldEdit.setEditable(true);
fieldEdit.setIsNullable(false);
fieldEdit.setPrecision(2);
fieldEdit.setScale(5);
fieldEdit.setType(esriFieldType.esriFieldTypeDouble);
fieldsEdit.setFieldByRef(2, fieldUserDefined);
// Create a feature class description object to use for specifying the
// CLSID and EXTCLSID.
IFeatureClassDescription fcDesc = new FeatureClassDescription();
IObjectClassDescription ocDesc = (IObjectClassDescription) fcDesc;
return featureDataset.createFeatureClass(nameOfFeatureClass, fields,
ocDesc.getInstanceCLSID(), ocDesc.getClassExtensionCLSID(),
esriFeatureType.esriFTSimple, "SHAPE", "");
}
This code has been made using ESRI Resources and other Help Content and is provided here just for educational purpose.
// Create Feature Class
static IFeatureClass createFeatureClass(IFeatureDataset featureDataset,
String nameOfFeatureClass) throws Exception {
// This function creates a new feature class in a supplied feature
// dataset by creating all fields from scratch. IFeatureClassDescription (or
// IObjectClassDescription if the table is
// created at the workspace level) can be used to get the required
// fields and are used to
// get the InstanceClassID and ExtensionClassID.
// Create new fields collection with the number of fields you plan to
// add. Must add at least two fields
// for a feature class: Object ID and Shape field.
IFields fields = new Fields();
IFieldsEdit fieldsEdit = (IFieldsEdit) fields;
fieldsEdit.setFieldCount(3);
// Create Object ID field.
IField fieldUserDefined = new Field();
IFieldEdit fieldEdit = (IFieldEdit) fieldUserDefined;
fieldEdit.setName("OBJECTID");
fieldEdit.setAliasName("OBJECT ID");
fieldEdit.setType(esriFieldType.esriFieldTypeOID);
fieldsEdit.setFieldByRef(0, fieldUserDefined);
// Create Shape field.
fieldUserDefined = new Field();
fieldEdit = (IFieldEdit) fieldUserDefined;
// Set up geometry definition for the Shape field.
// You do not have to set the spatial reference, as it is inherited from
// the feature dataset.
IGeometryDef geometryDef = new GeometryDef();
IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit) geometryDef;
geometryDefEdit.setGeometryType(esriGeometryType.esriGeometryPolyline);
// By setting the grid size to 0, you are allowing ArcGIS to determine
// the appropriate grid sizes for the feature class.
// If in a personal geodatabase, the grid size is 1,000. If in a file or
// ArcSDE geodatabase, the grid size
// is based on the initial loading or inserting of features.
geometryDefEdit.setGridCount(1);
geometryDefEdit.setGridSize(0, 0);
geometryDefEdit.setHasM(false);
geometryDefEdit.setHasZ(false);
// Set standard field properties.
fieldEdit.setName("SHAPE");
fieldEdit.setType(esriFieldType.esriFieldTypeGeometry);
fieldEdit.setGeometryDefByRef(geometryDef);
fieldEdit.setIsNullable(true);
fieldEdit.setRequired(true);
fieldsEdit.setFieldByRef(1, fieldUserDefined);
// Create a field of type double to hold some information for the
// features.
fieldUserDefined = new Field();
fieldEdit = (IFieldEdit) fieldUserDefined;
fieldEdit.setName("Avg_income");
fieldEdit.setAliasName("Average income for 1999-2000");
fieldEdit.setEditable(true);
fieldEdit.setIsNullable(false);
fieldEdit.setPrecision(2);
fieldEdit.setScale(5);
fieldEdit.setType(esriFieldType.esriFieldTypeDouble);
fieldsEdit.setFieldByRef(2, fieldUserDefined);
// Create a feature class description object to use for specifying the
// CLSID and EXTCLSID.
IFeatureClassDescription fcDesc = new FeatureClassDescription();
IObjectClassDescription ocDesc = (IObjectClassDescription) fcDesc;
return featureDataset.createFeatureClass(nameOfFeatureClass, fields,
ocDesc.getInstanceCLSID(), ocDesc.getClassExtensionCLSID(),
esriFeatureType.esriFTSimple, "SHAPE", "");
}
This code has been made using ESRI Resources and other Help Content and is provided here just for educational purpose.
No comments:
Post a Comment