[mapguide-users] change layer definition runtime

Liglio liglio at pobox.com
Thu Sep 8 12:18:25 PDT 2016


Hi,

I made an example of a class to manipulate the atribute Filter of a layer,
you can use as an example to manipulate any atribute of a layer. It´s made
in c#.

 public class UtilMap
    {
        private string strSessionMap;
        private string strMapDefinition;
        private MgSiteConnection siteConnection;
        private MgUserInformation userInfo;
        private MgFeatureService featureService;
        private MgResourceService resourceService;
        private MgMap map;
        private string mapName;
        private string strTempLayerSufix;

        /// <summary>
        /// Constructor of class to manipulate the RunTime Mapguide Map 
        /// </summary>
        /// Map Session ID
        /// Map Library Definition
        public UtilMap(string pstrSessionMap, string pstrMapDefinition)
        {
            try
            {
                strSessionMap = pstrSessionMap;
                strMapDefinition = pstrMapDefinition;
                strTempLayerSufix = "-" + strSessionMap;

                siteConnection = new MgSiteConnection();
                userInfo = new MgUserInformation(strSessionMap);

                siteConnection.Open(userInfo);
                featureService =
(MgFeatureService)siteConnection.CreateService(MgServiceType.FeatureService);
                resourceService =
(MgResourceService)siteConnection.CreateService(MgServiceType.ResourceService);

                mapName = strMapDefinition;

                //if mapName is empty, I just want to work with session
repository, no map operations
                if (mapName != "")
                {
                    map = new MgMap(siteConnection);
                    map.Open(resourceService, mapName);
                }

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        
        /// <summary>
        /// Set a filter to a Layer in session repository
        /// </summary>
        /// Layer Name
        /// Filter condition
        /// <returns>Operation success</returns>
        public bool SetLayerFilter(string strLayerName, string strFilter)
        {
            try
            {
                //
                // Set/Add a filter to a temporary layer created based on an
original one. 
                //
                MgResourceIdentifier resId, resIdTemp;
                MgLayerBase layer, layerTemp;
                string tempResource;
                string strLayerNameForSession;
                string strXml;

                //Verify if Layer Name is the temporary layer. If It is,
correct to original Layer Name.
                if (strLayerName.IndexOf(strTempLayerSufix) > 0)
                {
                    strLayerName = strLayerName.Replace(strTempLayerSufix,
"");
                }

                //Use random name to avoid overwrites
                strLayerNameForSession = strLayerName +
this.strTempLayerSufix;

                //Test if the original or temporary layers existis
                if (GetLayerByName(map, strLayerName) == null &&
GetLayerByName(map, strLayerNameForSession) == null)
                    return false;

                //Test if the filter is not null
                if (strFilter == null)
                    strFilter = "";

                //Get the temporary layer
                layer = GetLayerByName(map, strLayerNameForSession);

                //Get the original layer, if temporary later not exist
                if (layer == null)
                    layer = GetLayerByName(map, strLayerName);

                //If the temp resource was not created, read the original
xml
                tempResource = "Session:" + strSessionMap + "//" +
strLayerNameForSession + "." + MgResourceType.LayerDefinition;
                resIdTemp = new MgResourceIdentifier(tempResource);

                //Read the resource original layer
                resId = layer.GetLayerDefinition();
                strXml =
(resourceService.GetResourceContent(resId)).ToString();

                //Manipulate xml, set the Tags Filter

                strXml = Util.SetTagTextContent(strXml,
"/LayerDefinition/VectorLayerDefinition/Filter", strFilter, true,
"/LayerDefinition/VectorLayerDefinition/FeatureNameType");

                //Write the resource
                System.Text.ASCIIEncoding encoding = new
System.Text.ASCIIEncoding();
                MgByteSource ByteSource = new
MgByteSource(encoding.GetBytes(strXml), strXml.Length);
                resourceService.SetResource(resIdTemp,
ByteSource.GetReader(), null);

                //Get the temp layer
                layerTemp = GetLayerByName(map, strLayerNameForSession);
                if (layerTemp == null)
                {
                    layerTemp = new MgLayer(resIdTemp, resourceService);
                    layerTemp.SetGroup(layer.GetGroup());
                    layerTemp.SetName(strLayerNameForSession);
                    layerTemp.SetLegendLabel(strLayerName);
                    layerTemp.SetSelectable(true);
                    layerTemp.SetVisible(true);

                    MgLayerCollection layers = map.GetLayers();
                    //Insert at the same position of the original layer
                    layers.Insert(layers.IndexOf(strLayerName), layerTemp);
                }

                layerTemp.ForceRefresh();
                map.Save(resourceService);

                return true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }
        
      }  

********************************************************

objUtilMap = new UtilMap(SessionMapLightining, MapNameLightining);
string strFiltroLayer = "time_lightining >= ToDate('2016-09-08 10:00:00')
AND time_lightining < ToDate('2016-09-08 10:05:00')";
objUtilMap.SetLayerFilter("Lightining", strFiltroLayer);



--
View this message in context: http://osgeo-org.1560.x6.nabble.com/change-layer-definition-runtime-tp5284382p5284814.html
Sent from the MapGuide Users mailing list archive at Nabble.com.


More information about the mapguide-users mailing list