In a previous chapter, we created a feature schema and added a feature class to it. The feature class had three properties: an integer data property, a string data property, and a geometric property. We applied this feature schema to the data store. We are now ready to create feature data objects, which are instances of the feature class, and insert them into the data store.
We can now create feature data objects, which are instances of the feature class, by defining a set of property values corresponding to the properties defined for the class and then inserting them into the data store.
An FDO class correspondends roughly to a table definition in a relational database and a property of a class corresponds roughly to a column definition in a table. Adding the property values corresponds roughly to adding a row in the table.
The main distinction between a data value or geometry value and a property value is the order in which they are created. A data value or geometry value object is created first and is then used to create a property value object. The property value object is then added to the value collection object belonging to the Insert command object. Then, the command is executed.
An insert operation consists of the following steps:
A data value object contains data whose type is one of the following:
The data value object is added to the data property value object. The data property value object is added to the property value collection belonging to the Insert command.
A geometry property value object contains a geometry in the form of a byte array. A geometry can be relatively simple, for example, a point (a single pair of ordinates), or quite complex, for example, a polygon (one or more arrays of ordinates). In the latter case, a number of geometry objects are created and then combined together to form the target geometry. Finally, the target geometry is converted to a byte array and incorporated into the geometry property value object.
Creating a geometry value object consists of the following steps:
Example: Inserting an Integer, a String, and a Geometry Value
The following sample code shows how to insert an integer, a string, and a geometry value:
// create the insert command
GisPtr<FdoIInsert> sampleInsert;
sampleInsert = (FdoIInsert *)
connection->CreateCommand(FdoCommandType_Insert);
// index returned by the operation which adds a value to the value
// collection
GisInt32 valueCollectionIndex = 0;
// point the Insert command to the target class
// use a fully qualified class name
// whose format is <schemaName>:<className>
sampleInsert-> SetFeatureClassName(L"SampleFeatureSchema:SampleFeatureClass");
// get the pointer to the value collection used to add properties
// to the Insert command
GisPtr<FdoPropertyValueCollection> samplePropertyValues;
samplePropertyValues = sampleInsert->GetPropertyValues();
// create an FdoDataValue for the identity property value
GisPtr<FdoDataValue> sampleIdentityDataValue;
sampleIdentityDataValue = FdoDataValue::Create(101);
// add the FdoDataValue to the identity property value
GisPtr<FdoPropertyValue> sampleIdentityPropertyValue;
sampleIdentityPropertyValue =
FdoPropertyValue::Create(L"SampleIdentityDataProperty",
sampleIdentityDataValue);
// add the identity property value to the value collection
valueCollectionIndex =
samplePropertyValues->Add(sampleIdentityPropertyValue);
// create an FdoDataValue for the name property value
GisPtr<FdoDataValue> sampleNameDataValue;
sampleNameDataValue = FdoDataValue::Create(L"Blue Lake");
// add the FdoDataValue to the name property value
GisPtr<FdoPropertyValue> sampleNamePropertyValue;
sampleNamePropertyValue =
FdoPropertyValue::Create(L"SampleNameDataProperty",
sampleNameDataValue);
// add the name property value to the value collection
valueCollectionIndex =
samplePropertyValues->Add(sampleNamePropertyValue);
// create an FdoGeometryValue for the geometry property value
// this polygon represents a lake which has an island
// the outer shoreline of the lake is defined as a linear ring
// the shoreline of the island is defined as a linear ring
// the outer shoreline is the external boundary of the polygon
// the island shoreline is an internal linear ring
// a polygon geometry can have zero or more internal rings
GisPtr<FdoGeometryValue> sampleGeometryValue;
sampleGeometryValue = FdoGeometryValue::Create();
// create an instance of a geometry factory used to create the
// geometry objects
GisPtr<GisAgfGeometryFactory> sampleGeometryFactory;
sampleGeometryFactory = GisAgfGeometryFactory::GetInstance();
// define the external boundary of the polygon, the shoreline of
// Blue Lake
GisPtr<GisILinearRing> exteriorRingBlueLake;
GisInt32 numBlueLakeShorelineOrdinates = 10;
double blueLakeExteriorRingOrdinates[] = {52.0, 18.0, 66.0, 23.0,
73.0, 9.0, 48.0, 6.0, 52.0, 18.0};
exteriorRingBlueLake = sampleGeometryFactory->CreateLinearRing(
GisDimensionality_XY, numBlueLakeShorelineOrdinates,
blueLakeExteriorRingOrdinates);
// define the shoreline of Goose Island which is on Blue Lake
// this is the sole member of the list of interior rings
GisPtr<GisILinearRing> linearRingGooseIsland;
GisInt32 numGooseIslandShorelineOrdinates = 10;
double gooseIslandLinearRingOrdinates[] = {59.0, 18.0, 67.0, 18.0,
67.0, 13.0, 59.0, 13.0, 59.0, 18.0};
linearRingGooseIsland = sampleGeometryFactory->CreateLinearRing(
GisDimensionality_XY, numGooseIslandShorelineOrdinates,
gooseIslandLinearRingOrdinates);
// add the Goose Island linear ring to the list of interior rings
GisPtr<GisLinearRingCollection> interiorRingsBlueLake;
interiorRingsBlueLake = GisLinearRingCollection::Create();
interiorRingsBlueLake->Add(linearRingGooseIsland);
// create the Blue Lake polygon
GisPtr<GisIPolygon> blueLake;
blueLake =
sampleGeometryFactory->CreatePolygon(exteriorRingBlueLake,
interiorRingsBlueLake);
// convert the Blue Lake polygon into a byte array
// and set the geometry value to this byte array
GisByteArray * geometryByteArray =
sampleGeometryFactory->GetAgf(blueLake);
sampleGeometryValue->SetGeometry(geometryByteArray);
// add the Blue Lake FdoGeometryValue to the geometry property value
GisPtr<FdoPropertyValue> sampleGeometryPropertyValue;
sampleGeometryPropertyValue =
FdoPropertyValue::Create(L"SampleGeometryProperty",
sampleGeometryValue);
// add the geometry property value to the value collection
valueCollectionIndex =
samplePropertyValues->Add(sampleGeometryPropertyValue);
// do the insertion
// the command returns an FdoIFeatureReader
GisPtr<FdoIFeatureReader sampleFeatureReader;
sampleFeatureReader = sampleInsert->Execute();