[mapguide-users] Thematic Mapping

padmini godavarthi godavarthi.padmini at gmail.com
Fri Jan 30 05:58:52 EST 2009




Hi,
iam using mpguide opensource 2.0 (.net 2.0 +IIS 5.1).
I want to do thematic mapping for polygon layer(for example parcel layer)
i tried to use the code in dotnetviewer sample.its working fine

iam able to create a thematic layer with same color to the all polygons. 
Now my problem is that i want to apply different colours to the different
polygons

for example for parcel_id =1   ------------ green colour
for example for parcel_id =2   ------------ Red colour and
soon.........................

for this can u plz tell me the code for how to do it?


my code was as follows
------------------------------------------------------------------------------------------------------


NameValueCollection serverVars = Request.ServerVariables;
        String strServerVars = "";
        foreach (String str in serverVars.AllKeys)
        {

            strServerVars += "<br>" + str;

        }
        String platform = serverVars["SERVER_SOFTWARE"];
        String queryStr = serverVars["QUERY_STRING"];
        string queryStr1 = serverVars["Form"];
        NameValueCollection requestParams = Request.HttpMethod == "POST" ?
Request.Form : Request.QueryString;
        String sessionId = Request.QueryString["SESSION"];
        string realPath = Request.ServerVariables["APPL_PHYSICAL_PATH"];
        String configPath = realPath + "webconfig.ini";
        MapGuideApi.MgInitializeWebTier(configPath);
        MgUserInformation userInfo = new MgUserInformation(sessionId);
        MgSiteConnection siteConnection = new MgSiteConnection();
        siteConnection.Open(userInfo);
        MgResourceService resourceService =
(MgResourceService)siteConnection.CreateService(MgServiceType.ResourceService);
        MgFeatureService featureService =
(MgFeatureService)siteConnection.CreateService(MgServiceType.FeatureService);

        MgMap map = new MgMap();
        map.Open(resourceService, "mapdata");
        MgLayerCollection layers = map.GetLayers();
        MgLayer layer = (MgLayer)layers.GetItem("property layer");

        MgResourceIdentifier resId = new
MgResourceIdentifier(layer.GetFeatureSourceId());
        MgResourceIdentifier layerDefResId = layer.GetLayerDefinition();
        MgByteReader byteReader =
resourceService.GetResourceContent(layerDefResId);

        // Load the Layer Definition and Navigate to the specified
<VectorScaleRange>

        XmlDocument doc = new XmlDocument();
        String xmlLayerDef = byteReader.ToString();
        doc.LoadXml(xmlLayerDef);
          XmlNodeList nodeList =
doc.GetElementsByTagName("VectorScaleRange");

    XmlElement vectorScaleRangecElement = (XmlElement)nodeList.Item(0);
        XmlElement areaTypeStyle =
(XmlElement)vectorScaleRangecElement.GetElementsByTagName("AreaTypeStyle").Item(0);

        // Remove any existing <AreaRule> elements.

        XmlNodeList areaRuleList =
areaTypeStyle.GetElementsByTagName("AreaRule");
        int count = areaRuleList.Count;
        for (int i = 0; i < count; i++)
        {
            //The areaRuleList shrinks as we remove items, so always
            //remove the first item (don't use the index i)
            areaTypeStyle.RemoveChild(areaRuleList.Item(0));
        }

        // Now create the new <AreaRule> elements.

        String areaRuleTemplate =
File.ReadAllText(GetThemeXmlTemplatePath());
        MgFeatureAggregateOptions aggregateOptions = new
MgFeatureAggregateOptions();

        String value = null;
        String filterText = null;
        String areaRuleXML = null;
        XmlDocument areaDoc = null;
        XmlNode areaNode = null;
        double portion = 0.0;
         
            aggregateOptions.AddFeatureProperty("FeatId");
            aggregateOptions.SelectDistinct(true);
                    aggregateOptions.SetFilter(totquery);
                  double increment = (5 > 1) ? 1.0 / (5 - 1) : 1.0;
            MgDataReader dataReader = featureService.SelectAggregate(resId,
layer.GetFeatureClassName(), aggregateOptions);
            while (dataReader.ReadNext())
            {
                value = GetFeaturePropertyValue(dataReader, "FeatId");

                filterText = "&quot;" + "FeatId" + "&quot; = ";
              
                    filterText = filterText + value;

                areaRuleXML = String.Format(areaRuleTemplate,
                                          "FeatId" + ":" + value,
                                            filterText,
InterpolateColor(portion, "C2C3C4", "C2C3C4", 0), InterpolateColor(portion,
"C2C3C4", "C2C3C4", 0));
                areaDoc = new XmlDocument();
                areaDoc.LoadXml(areaRuleXML);
                areaDoc.Save("C:\\theme.xml");
                areaNode = doc.ImportNode(areaDoc.DocumentElement, true);
                areaTypeStyle.AppendChild(areaNode);


             portion = portion + increment;
            }
            dataReader.Close();
       

        // Now save our new layer definition to the session and add it to
the map.

        String xmlString = doc.DocumentElement.OuterXml;
        String uniqueName = this.MakeUniqueLayerName(map, "property layer",
"sample");
        String legendLabel = layer.GetLegendLabel();
        if ("sample".Length > 0)
            legendLabel = legendLabel + " (" + "sample" + ")";

        MgResourceIdentifier layerResId = new
MgResourceIdentifier("Session:" + sessionId + "//" + uniqueName +
".LayerDefinition");
        resourceService.SetResource(layerResId, new MgByteReader(xmlString,
"text/xml") , null);

        MgLayer newLayer = new MgLayer(layerResId, resourceService);
        newLayer.SetName(uniqueName);
        newLayer.SetLegendLabel(legendLabel);
        newLayer.SetDisplayInLegend(layer.GetDisplayInLegend());
        newLayer.SetVisible(true);
        newLayer.SetSelectable(layer.GetSelectable());
        layers.Insert(layers.IndexOf(layer), newLayer);

        map.Save(resourceService);

        return uniqueName;
    }
---------------------------------------------
 private String InterpolateColor(double portion, String startColor, String
endColor, int percentTransparent)
    {
        int alpha = (int)(255 * (100.0 - percentTransparent) / 100.0);
        String result = "";
        if (startColor.Equals(endColor))
        {
            result = String.Format("{0:X2}{1}", alpha, startColor);
        }
        else
        {
            int red = CalculateRGB(portion, startColor.Substring(0, 2),
endColor.Substring(0, 2));
            int green = CalculateRGB(portion, startColor.Substring(2, 2),
endColor.Substring(2, 2));
            int blue = CalculateRGB(portion, startColor.Substring(4, 2),
endColor.Substring(4, 2));
            result = String.Format("{0:X2}{1:X2}{2:X2}{3:X2}", alpha, red,
green, blue);
        }
        return result;
    }

    private String MakeUniqueLayerName(MgMap map, String layerName, String
themeName)
    {
        String desiredName = "_" + layerName + themeName;
        String uniqueName = desiredName;
        int index = 1;

        while (map.GetLayers().Contains(uniqueName))
        {
            uniqueName = desiredName + index.ToString();
            index++;
        }
        return uniqueName;
    }
    private int CalculateRGB(double portion, String startRGB, String endRGB)
    {
        double result = Int32.Parse(startRGB, NumberStyles.HexNumber) +
portion * (Int32.Parse(endRGB, NumberStyles.HexNumber) -
Int32.Parse(startRGB, NumberStyles.HexNumber));
        return (int)result;
    }
    String GetThemeXmlTemplatePath()
    {
        String xmlTemplatePath ="D:\\arearuletemplate.xml";
        return xmlTemplatePath;
    }
---------------------------------------
can u plz tell me the solution?

Regards,
Padmini.





-- 
View this message in context: http://n2.nabble.com/Thematic-Mapping-tp2244230p2244230.html
Sent from the MapGuide Users mailing list archive at Nabble.com.



More information about the mapguide-users mailing list