Example: Defining a Vector Feature Source

AutoCAD Map 3D Geospatial Platform API

 
Example: Defining a Vector Feature Source
 
 
 

Defining a vector feature source requires creating the feature source definition and storing it in the resource repository using the feature source resource identifier. The following example assumes that SDFpath is a string containing the file path of an SDF file. It creates a feature source with a resource identifier of Library://feature.FeatureSource.

There are many ways to create a feature source definition. One method, used in the sample applications and the following example, uses classes generated using the Visual Studio tool xsd.exe on FeatureSource.xsd. FeatureSourceType and NameValuePairType are classes generated this way.

NoteResource service requires data to be UTF-8 encoded.
// Get the services
 
MgResourceService rs;
rs = AcMapServiceFactory.GetService(MgServiceType.ResourceService)
  as MgResourceService;
MgFeatureService fs;
fs = AcMapServiceFactory.GetService(MgServiceType.FeatureService)
  as MgFeatureService;
MgResourceIdentifier fsId = new MgResourceIdentifier(
  "Library://feature.FeatureSource");
 
// Create the feature source definition with a required
// parameter of File and an optional parameter of ReadOnly
 
string xmlString;
FeatureSourceType fsType = new FeatureSourceType();
 
fsType.Provider = "OSGeo.SDF.3.2";
 
NameValuePairType param = new NameValuePairType();
param.Name = "File";
param.Value = SDFpath;
NameValuePairType param2 = new NameValuePairType();
param2.Name = "ReadOnly";
param2.Value = "false";
fsType.Parameter = new NameValuePairType[] { param, param2 };
 
// Serialize the feature source object model to xml string
using (StringWriter writer = new StringWriter())
{
  XmlSerializer xs = new XmlSerializer(fsType.GetType());
  xs.Serialize(writer, fsType);
  xmlString = writer.ToString();
}
 
// Convert the Unicode string to UTF8 bytes for Resource Service
 
byte[] unicodeBytes = Encoding.Unicode.GetBytes(xmlString);
byte[] utf8Bytes = Encoding.Convert(Encoding.Unicode, 
  Encoding.UTF8, unicodeBytes);
 
// Create a byte reader containing the XML feature 
// source definition. Store the definition in the repository
 
MgByteSource xmlSource = new MgByteSource(utf8Bytes, 
  utf8Bytes.Length);
rs.SetResource(fsId, xmlSource.GetReader(), null);