Creating Layers By Modifying XML

The easiest way to programmatically create new layers is to

1. Build a prototype layer through the 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 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 MapGuide Web API Reference. This schema closely parallels the UI in the Studio’s Layer Editor, as described in the Studio Help.

This example

- loads a layer that has been created through Studio
- uses the DOM to change the filter and its associated legend label

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 MapGuide Web API Reference.

    // (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.