[fdo-users] How to add WMS layer (transparent) to MapGuide?

Patrick Tsang pt at pat.ca
Sat Jun 30 18:10:08 EDT 2007


Hi All,

I want to add WMS layer on the fly not through MapStudio.I am able to add
WMS layer on the fly (Thank you to Hidekazu and Greg's help) but the layer
come back is not transparent but i cannot figure out how to request the
layer as transparent... here is my code (C#):

        private string formatWMSProviderXml =
            "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
System.Environment.NewLine +
            "  <FeatureSource
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
xsi:noNamespaceSchemaLocation=\"FeatureSource-1.0.0.xsd\">" +
System.Environment.NewLine +
            "    <Provider>OSGeo.WMS</Provider>" +
System.Environment.NewLine +
            "    <Parameter>" + System.Environment.NewLine +
            "      <Name>FeatureServer</Name>" + System.Environment.NewLine
+
            "      <Value>{0}</Value>" + System.Environment.NewLine +
            "    </Parameter>" + System.Environment.NewLine +
            "  </FeatureSource>";

        private string formatWMSLayerXml =
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?> " +
System.Environment.NewLine +
            "  <LayerDefinition
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
xsi:noNamespaceSchemaLocation=\"LayerDefinition-1.0.0.xsd\"
version=\"1.0.0\"> " + System.Environment.NewLine +
            "    <GridLayerDefinition> " + System.Environment.NewLine +
            "      <ResourceId>{0}</ResourceId> " +
System.Environment.NewLine +
            "      <FeatureName>WMS_Schema:{1}</FeatureName> " +
System.Environment.NewLine +
            "      <Geometry>Raster</Geometry> " +
System.Environment.NewLine +
            "      <GridScaleRange> " + System.Environment.NewLine +
            "        <ColorStyle> " + System.Environment.NewLine +
            "          <ColorRule> " + System.Environment.NewLine +
            "            <LegendLabel></LegendLabel> " +
System.Environment.NewLine +
            "            <Color> " + System.Environment.NewLine +
            "              <ExplicitColor>000000</ExplicitColor> " +
System.Environment.NewLine +
            "            </Color> " + System.Environment.NewLine +
            "          </ColorRule> " + System.Environment.NewLine +
            "          <ColorRule> " + System.Environment.NewLine +
            "            <LegendLabel></LegendLabel> " +
System.Environment.NewLine +
            "            <Color> " + System.Environment.NewLine +
            "              <ExplicitColor>FFFFFF</ExplicitColor> " +
System.Environment.NewLine +
            "            </Color> " + System.Environment.NewLine +
            "          </ColorRule> " + System.Environment.NewLine +
            "        </ColorStyle> " + System.Environment.NewLine +
            "        <RebuildFactor>1</RebuildFactor> " +
System.Environment.NewLine +
            "      </GridScaleRange> " + System.Environment.NewLine +
            "    </GridLayerDefinition> " + System.Environment.NewLine +
            "  </LayerDefinition>";

        public void AddLayer(string SessionId, string ClassName, Server
Server, string MapName)
        {
            // Create MgSiteConnection
            MgSiteConnection siteConnection = new MgSiteConnection();
            siteConnection.Open(new MgUserInformation(SessionId));

            //create the services we will use
            MgResourceService resourceService =
siteConnection.CreateService(MgServiceType.ResourceService) as
MgResourceService;

            //create the wms feature source
            // NOTES: Server.URL is something like
"http://www.geographynetwork.ca/wmsconnector/com.esri.wms.Esrimap/OBM_Full_I?request=getcapabilities&service=WMS&version=1.1.1"
            string formatedWmsProviderXml =
string.Format(formatWMSProviderXml, XmlEscape(Server.URL));

            MgByteSource wmsFeatureSourceByteSource = new
MgByteSource(System.Text.Encoding.ASCII.GetBytes(formatedWmsProviderXml),
formatedWmsProviderXml.Length);
            wmsFeatureSourceByteSource.SetMimeType(MgMimeType.Xml);
            string featureSourceName =
string.Format("Session:{0}//{1}.FeatureSource", SessionId, Guid.NewGuid());
            MgResourceIdentifier WmsFeatureSourceResourceIdentifier = new
MgResourceIdentifier(featureSourceName);
            resourceService.SetResource(WmsFeatureSourceResourceIdentifier,
wmsFeatureSourceByteSource.GetReader(), null);

            //create the wms layer
            // NOTES: ClassName is the layer name user pick from a list of
layers from the Server.URL
            string layerWmsName = ClassName;
            string formatedWmsLayerXml = string.Format(formatWMSLayerXml,
XmlEscape(featureSourceName), XmlEscape(layerWmsName));
            MgByteSource wmsLayerByteSource = new
MgByteSource(System.Text.Encoding.ASCII.GetBytes(formatedWmsLayerXml),
formatedWmsLayerXml.Length);
            wmsLayerByteSource.SetMimeType(MgMimeType.Xml);

            MgResourceIdentifier wmsLayerResourceIdentifier = new
MgResourceIdentifier(string.Format("Session:{0}//{1}.LayerDefinition",
SessionId, layerWmsName));
            resourceService.SetResource(wmsLayerResourceIdentifier,
wmsLayerByteSource.GetReader(), null);

            //get a reference to the map
            MgResourceService l_oResourceService =
siteConnection.CreateService(MgServiceType.ResourceService) as
MgResourceService;
            MgMap map = new MgMap();
            map.Open(l_oResourceService, MapName);

            // Add the new layer to the map's layer collection
            MgLayer newLayer = new MgLayer(wmsLayerResourceIdentifier,
resourceService);

            newLayer.SetName(layerWmsName);
            newLayer.SetVisible(true);
            newLayer.SetLegendLabel(layerWmsName);
            newLayer.SetDisplayInLegend(true);


            MgLayerCollection layerCollection = map.GetLayers();
            if (!layerCollection.Contains(layerWmsName))
            {
                // Insert the new layer at position 0 so it is at
                // the top of the drawing order
                layerCollection.Insert(0, newLayer);

                Server.Layers.Find(delegate(Layer l) { return l.ClassName ==
ClassName; }).IsVisible = true;
            }

            //  Save the map back to the session repository
            string mapResourceName = string.Format("Session:{0}//{1}.Map",
SessionId, MapName);
            MgResourceIdentifier mapResourceID = new
MgResourceIdentifier(mapResourceName);
            mapResourceID.Validate();
            map.Save(resourceService, mapResourceID);

            // return
            //return Server;
        }

There has objects not defined, try to keep it short. Sorry about it...
Thanks in advance!!!

Cheers,
Patrick 
-- 
View this message in context: http://www.nabble.com/How-to-add-WMS-layer-%28transparent%29-to-MapGuide--tf4006197s18162.html#a11377502
Sent from the fdo-users mailing list archive at Nabble.com.



More information about the fdo-users mailing list