Creating Layers By Modifying XML

The easiest way to programmatically create new layers is to

  1. Build a prototype layer through the Infrastructure Studio UI. To make the scripting simpler, this layer should have as many of the correct settings as can be determined in advance.
  2. Use Infrastructure Studio’s Save as Xml command to save the layer as an XML file.
  3. Have the script load the XML file and then use the DOM (Document Object Model) to change the necessary XML elements.
  4. Add the modified layer to the map.

The XML schema for layer definitions is defined by the LayerDefinition-version.xsd schema, which is documented in the WebApiRef . This schema closely parallels the UI in Infrastructure Studio’s Layer Editor, as described in the Studio Help.

This example

You can use the DOM to modify any layers, including ones that already exist in the map, not just new layers that you are adding to the map. You can also use the DOM to modify other resources; the XML schemas are described in the WebApiRef .

// (initialization etc. not shown here)
// Open the map
 $map = new MgMap();
 $map->Open($resourceService, $mapName);
 // --------------------------------------------------//
 // Load a layer from XML, and use the DOM to change it
 // Load the prototype layer definition into
 // a PHP DOM object.
 $domDocument =
   DOMDocument::load('RecentlyBuilt.LayerDefinition');
 if ($domDocument == NULL)
 {
     echo "The layer definition
       'RecentlyBuilt.LayerDefinition' could not be
       found.<BR>\n";
     return;
 }
 // Change the filter
 $xpath = new DOMXPath($domDocument);
 $query = '//AreaRule/Filter';
 // Get a list of all the <AreaRule><Filter> elements in
 // the XML.   
 $nodes = $xpath->query($query);
 // Find the correct node and change it
 foreach ($nodes as $node )
 {
     if ($node->nodeValue == 'YRBUILT > 1950')
     {
         $node->nodeValue = 'YRBUILT > 1980';
     }
 }
 // Change the legend label
 $query = '//LegendLabel';
 // Get a list of all the <LegendLabel> elements in the
 // XML.
 $nodes = $xpath->query($query);
 // Find the correct node and change it
 foreach ($nodes as $node )
 {
     if ($node->nodeValue == 'Built after 1950')
     {
         $node->nodeValue = 'Built after 1980';
     }
 }
// ...

The page then goes on to save the XML to a resource and loads that resource into the map, as described in Adding Layers To A Map.

If you wish to modify an existing layer that is visible in other users’ maps, without affecting those maps:

  1. Copy the layer to the user’s session repository.
  2. Modify the layer and save it back to the session repository.
  3. Change the user’s map to refer to the modified layer.

See Adding Layers To A Map.