Creating SDF files

AutoCAD Map 3D Geospatial Platform API

 
Creating SDF files
 
 
 

Many geospatial operations require creating new layers, adding features to the layers, and adding the layers to the map. The layers can be temporary or permanent. In AutoCAD Map 3D this is handled using SDF files.

An SDF file contains a single schema that can define multiple feature classes.

To create an SDF file, first define the schema and any feature classes used in the schema. For each feature class, define a list of properties. In most cases, the properties should include an identity property and a geometry property, as well as any other properties.

Use the FDO API to create the schema.

The following creates a simple schema with an autogenerated ID property, a string property, and a geometry property.

// Create the feature class definition
 
FeatureClass classDefinition = 
  new FeatureClass("newClass", "class description");
 
// Add an autogenerated identity property
 
DataPropertyDefinition idProp = 
  new DataPropertyDefinition("id", "property desc");
idProp.DataType = DataType.DataType_Int32;
idProp.IsAutoGenerated = true;
classDefinition.Properties.Add(idProp);
classDefinition.IdentityProperties.Add(idProp);
 
// Add a name property
 
DataPropertyDefinition nameProp = 
  new DataPropertyDefinition("Name", "property desc");
nameProp.DataType = DataType.DataType_String;
classDefinition.Properties.Add(nameProp);
 
// Add a geometry property
 
GeometricPropertyDefinition geomProp = 
  new GeometricPropertyDefinition("SHPGEOM", "property desc");
geomProp.GeometryTypes = MgGeometryType.Polygon;
classDefinition.Properties.Add(geomProp);
classDefinition.GeometryProperty = geomProp;
 
// Create the schema and add the class definition
 
FeatureSchema schema = 
  new FeatureSchema("SHP_Schema", "Schema description");
schema.Classes.Add(classDefinition);

Once the schema has been created, use the FDO API to create the SDF file, set the spatial context, and apply the schema. The following is modified from the utility classes in the sample applications.

// Set connection properties
 
IConnectionPropertyDictionary connProperties = 
  conn.ConnectionInfo.ConnectionProperties;
connProperties.SetProperty("File", sdfPath);
connProperties.SetProperty("ReadOnly", false.ToString());
 
// Create data store, ie. SDF file
 
ICreateDataStore createDSCmd = 
  conn.CreateCommand(CommandType.CommandType_CreateDataStore)
  as ICreateDataStore;
createDSCmd.DataStoreProperties.SetProperty("File", sdfPath);
createDSCmd.Execute();
 
try
{
  // Open the connection
  ConnectionState connState = conn.Open();
  int retryRound = 0;
  while (++retryRound < 5 // Try 5 times
    && (connState == ConnectionState.ConnectionState_Pending
    || connState == ConnectionState.ConnectionState_Busy))
  {
    connState = conn.Open();
  }
  if (retryRound >= 5)
  {
    throw new 
    InvalidOperationException("Failed to connect to file " 
    + sdfPath);
  }
 
  // Create spatial context, including coordinate
system
 
  AcMapMap currentMap = AcMapMap.GetCurrentMap();
  string mapSRS = currentMap.GetMapSRS();
 
  ICreateSpatialContext createSCCmd = 
  conn.CreateCommand(CommandType.CommandType_CreateSpatialContext)
    as ICreateSpatialContext;
  createSCCmd.CoordinateSystemWkt = mapSRS;
  createSCCmd.Name = "";
  createSCCmd.CoordinateSystem = mapSRS;
  createSCCmd.Extent = new byte[] { 0, 0, 0, 0 };
  createSCCmd.Description = "Description";
  createSCCmd.XYTolerance = 0.0;
  createSCCmd.ZTolerance = 0.0;
  createSCCmd.Execute();
 
  // Apply schema
  if (schema.ElementState != 
    SchemaElementState.SchemaElementState_Unchanged)
  {
    IApplySchema applySchemaCmd = 
      conn.CreateCommand(CommandType.CommandType_ApplySchema)
      as IApplySchema;
    applySchemaCmd.FeatureSchema = schema;
    applySchemaCmd.Execute();
  }
}
catch (System.Exception e)
{
  throw e; // Or add exception handling
}
finally
{
  // Close connection
  if (conn != null)
  {
    conn.Close();
  }
}