Layer Definition

AutoCAD Map 3D Geospatial Platform API

Layer Definition

The feature source and styling for a layer are set using a LayerDefinition, stored in the resource repository. The layer definition conforms to the LayerDefinition.xsdLayerDefinition.xsd schema. Raster layers and vector layers have different styling requirements and capabilities.

For both raster and vector data, a single layer definition can contain multiple scale ranges, which define the styling for particular view scales. For example, a vector layer could have the following scale ranges:

  • 0 to 10,000
  • 10,000 to 500,000
  • 500,000 to infinity

Features in the layer could be styled differently or be hidden completely for the different scale ranges.

For a a vector layer, each scale range can contain styling information for the following:

  • AreaTypeStyle, used for polygons
  • LineTypeStyle, used for lines and curves
  • PointTypeStyle, used for points

The layer definition can be any valid XML that conforms to the schema. To help generate the XML, the AutoCAD Map 3D samples use classes generated from LayerDefinition.xsd by xsd.exe.

For example, to create a vector layer definition that contains 2 scale ranges, do the following:

  • Create the two scale ranges. For each, specify the minimum and maximum scale for the range. Define the necessary elements, like fill and stroke settings.
  • Create the vector layer definition. Add the scale ranges.
  • Set the feature name to the schema/feature class in the feature source.
  • Set the resource id to point to the feature source containing the layer data.
  • Set the geometry to the geometry property of the feature class.
  • Create a layer definition that contains the vector layer definition.
  • Store the layer definition in the resource repository.
  • Create a new layer that references the layer definition. Add the layer to the layer collection for the current map.

The example code that follows uses the following namespace declarations:

  • using System.IO (for the StringWriter class)
  • using System.Text (for the Encoding class)
  • using System.Xml.Serialization (for XML serialization routines)
  • using OSGeo.MapGuide.Schema.LayerDefinition (generated by xsd.exe. Code for this class is available with the AutoCAD Map 3D samples.)

The following assumes that the feature source contains only polygons. Feature sources containing line or point data would require styling for those feature types as well.

// Create the first scale range
VectorScaleRangeType range = new VectorScaleRangeType();
AreaRule areaRule = new AreaRule();
AreaSymbolizationFillType areaSymFillType = 
  new AreaSymbolizationFillType();
FillType fillType = new FillType();
fillType.FillPattern = "Solid";
fillType.ForegroundColor = "9900ffaa";
fillType.BackgroundColor = "FF000000";
StrokeType strokeType = new StrokeType();
strokeType.LineStyle = "Solid";
strokeType.Thickness = "0.0";
strokeType.Color = "FF000000";
strokeType.Unit = LengthUnitType.Centimeters;
areaSymFillType.Fill = fillType;
areaSymFillType.Stroke = strokeType;
areaRule.Item = areaSymFillType;
areaRule.LegendLabel = "";
object[] items = new object[1];
items[0] = new AreaRule[] { areaRule };
range.Items = items;
range.MinScale = 0.0;
range.MaxScale = 10000000.0;
range.MinScaleSpecified = true;
range.MaxScaleSpecified = true;
// Second scale range
VectorScaleRangeType range2 = new VectorScaleRangeType();
AreaRule areaRule2 = new AreaRule();
AreaSymbolizationFillType areaSymFillType2 = 
  new AreaSymbolizationFillType();
FillType fillType2 = new FillType();
fillType2.FillPattern = "Solid";
fillType2.ForegroundColor = "ffffff00";
fillType2.BackgroundColor = "FF000000";
StrokeType strokeType2 = new StrokeType();
strokeType2.LineStyle = "Solid";
strokeType2.Thickness = "0.0";
strokeType2.Color = "FF000000";
strokeType2.Unit = LengthUnitType.Centimeters;
areaSymFillType2.Fill = fillType2;
areaSymFillType2.Stroke = strokeType2;
areaRule2.Item = areaSymFillType2;
areaRule2.LegendLabel = "";
object[] items2 = new object[1];
items2[0] = new AreaRule[] { areaRule2 };
range2.Items = items2;
range2.MinScale = 10000000.0;
range2.MaxScale = Double.MaxValue;
range2.MinScaleSpecified = true;
range2.MaxScaleSpecified = true;
// Now create a vector layer definition and add
the scale ranges.
VectorLayerDefinitionType vectorLayerDef = 
  new VectorLayerDefinitionType();
vectorLayerDef.VectorScaleRange = new VectorScaleRangeType[2];
vectorLayerDef.VectorScaleRange.SetValue(range, 0);
vectorLayerDef.VectorScaleRange.SetValue(range2, 1);
// Set the resource id to point to the feature
source for
// the layer. Set the feature name to a feature
class in 
// the feature source. Set the geometry property
to the
// default geometry property for the feature
vectorLayerDef.ResourceId = "Library://Data/SAMPLE.FeatureSource";
vectorLayerDef.FeatureName = "Schema1:FeatureClass1";
vectorLayerDef.Geometry = "Geometry";
// Create a layer definition containing the vector
// definition.
LayerDefinition layerDef = new LayerDefinition();
layerDef.Item = vectorLayerDef;
layerDef.version = "1.0.0";
// Convert to an XML string.
using (StringWriter writer = new StringWriter())
  XmlSerializer xs = new XmlSerializer(typeof(LayerDefinition));
  xs.Serialize(writer, layerDef);
  // Convert to UTF-8.
  byte[] unicodeBytes = 
  byte[] utf8Bytes = 
    Encoding.Convert(Encoding.Unicode, Encoding.UTF8, 
  MgByteSource source = 
    new MgByteSource(utf8Bytes, utf8Bytes.Length);
  // Update the resource in Map.
  MgResourceService rs =
    as MgResourceService;
  // Store the layer definition in the repository.
  MgResourceIdentifier newLayerDefId = 
    new MgResourceIdentifier(
  rs.SetResource(newLayerDefId, source.GetReader(), null);
  // Create a new layer that references the layer
  // Add this to the layers in the current map.
  AcMapMap currentMap = AcMapMap.GetCurrentMap();
  MgLayerCollection layers = currentMap.GetLayers();
  AcMapLayer layer;
  layer = new AcMapLayer(newLayerDefId, rs);
  layer.Name = "testlayer";

This creates the following XML:

<?xml version="1.0" encoding="utf-16"?>
          <LegendLabel />
          <LegendLabel />