private void btnApply_Click(object sender, EventArgs e) { var tp = new ThemeParams() { borderFrom = Util.ToHtmlColor(pnlFromBorderColor.BackColor), borderTo = Util.ToHtmlColor(pnlToBorderColor.BackColor), distro = cmbDistribution.SelectedValue.ToString(), fillFrom = Util.ToHtmlColor(pnlFromFillColor.BackColor), fillTo = Util.ToHtmlColor(pnlToFillColor.BackColor), fillTrans = Convert.ToInt32(numFillTransparency.Value), layer = (MgLayerBase)cmbLayer.SelectedItem, maxValue = txtMax.Text, minValue = txtMin.Text, numRules = Convert.ToInt32(numRules.Value), property = (MgDataPropertyDefinition)cmbProperty.SelectedItem, scaleRangeIndex = cmbScaleRange.SelectedIndex, themeName = txtThemeName.Text }; string name = ApplyTheme(tp); _viewer.RefreshMap(); MessageBox.Show(string.Format(Strings.MsgThemeLayerCreated, name)); }
private string ApplyTheme(ThemeParams themeParams) { var provider = _viewer.GetProvider(); var map = _viewer.GetMap(); var layers = map.GetLayers(); MgResourceService resourceService = (MgResourceService)provider.CreateService(MgServiceType.ResourceService); MgFeatureService featureService = (MgFeatureService)provider.CreateService(MgServiceType.FeatureService); MgResourceIdentifier resId = new MgResourceIdentifier(themeParams.layer.GetFeatureSourceId()); MgResourceIdentifier layerDefResId = themeParams.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"); //NOXLATE XmlElement vectorScaleRangecElement = (XmlElement)nodeList.Item(themeParams.scaleRangeIndex); XmlElement areaTypeStyle = (XmlElement)vectorScaleRangecElement.GetElementsByTagName("AreaTypeStyle").Item(0); //NOXLATE // Remove any existing <AreaRule> elements. XmlNodeList areaRuleList = areaTypeStyle.GetElementsByTagName("AreaRule"); //NOXLATE 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 = Properties.Resources.AreaRuleTemplate; MgFeatureAggregateOptions aggregateOptions = new MgFeatureAggregateOptions(); String value = null; String filterText = null; String areaRuleXML = null; XmlDocument areaDoc = null; XmlNode areaNode = null; double portion = 0.0; double increment = (themeParams.numRules > 1) ? 1.0 / (themeParams.numRules - 1) : 1.0; if (THEME_INDIVIDUAL == themeParams.distro) { aggregateOptions.AddComputedProperty("THEME_VALUE", "UNIQUE(\"" + themeParams.property.Name + "\")"); //NOXLATE MgDataReader dataReader = featureService.SelectAggregate(resId, themeParams.layer.GetFeatureClassName(), aggregateOptions); while (dataReader.ReadNext()) { value = Util.GetFeaturePropertyValue(dataReader, "THEME_VALUE"); //NOXLATE filterText = """ + themeParams.property.Name + "" = "; //NOXLATE if (themeParams.property.DataType == MgPropertyType.String) filterText = filterText + "'" + value + "'"; //NOXLATE else filterText = filterText + value; areaRuleXML = String.Format(areaRuleTemplate, themeParams.property.Name + ":" + value, //NOXLATE filterText, Util.InterpolateColor(portion, themeParams.fillFrom, themeParams.fillTo, themeParams.fillTrans), Util.InterpolateColor(portion, themeParams.borderFrom, themeParams.borderTo, 0)); areaDoc = new XmlDocument(); areaDoc.LoadXml(areaRuleXML); areaNode = doc.ImportNode(areaDoc.DocumentElement, true); areaTypeStyle.AppendChild(areaNode); portion = portion + increment; } dataReader.Close(); } else { var values = new List<string>(); var expr = themeParams.distro + "(\"" + themeParams.property.Name + "\"," + themeParams.numRules + "," + themeParams.minValue + "," + themeParams.maxValue + ")"; //NOXLATE aggregateOptions.AddComputedProperty("THEME_VALUE", expr); //NOXLATE MgDataReader dataReader = featureService.SelectAggregate(resId, themeParams.layer.GetFeatureClassName(), aggregateOptions); while (dataReader.ReadNext()) { value = Util.GetFeaturePropertyValue(dataReader, "THEME_VALUE"); //NOXLATE values.Add(value); } dataReader.Close(); for (int i = 0; i < values.Count - 1; i++) { filterText = """ + themeParams.property.Name + "" >= " + values[i] + " AND "" + themeParams.property.Name; //NOXLATE if (i == values.Count - 1) filterText = filterText + "" <= " + values[i + 1]; //NOXLATE else filterText = filterText + "" < " + values[i + 1]; //NOXLATE areaRuleXML = String.Format(areaRuleTemplate, themeParams.property.Name + ":" + values[i] + " - " + values[i + 1], //NOXLATE filterText, Util.InterpolateColor(portion, themeParams.fillFrom, themeParams.fillTo, themeParams.fillTrans), Util.InterpolateColor(portion, themeParams.borderFrom, themeParams.borderTo, 0)); areaDoc = new XmlDocument(); areaDoc.LoadXml(areaRuleXML); areaNode = doc.ImportNode(areaDoc.DocumentElement, true); areaTypeStyle.AppendChild(areaNode); portion = portion + increment; } } // Now save our new layer definition to the session and add it to the map. String xmlString = doc.DocumentElement.OuterXml; String uniqueName = Util.MakeUniqueLayerName(map, themeParams.layer.Name, themeParams.themeName); String legendLabel = themeParams.layer.GetLegendLabel(); if (!string.IsNullOrEmpty(themeParams.themeName)) legendLabel = legendLabel + " (" + themeParams.themeName + ")"; //NOXLATE MgResourceIdentifier layerResId = new MgResourceIdentifier("Session:" + _sessionId + "//" + uniqueName + ".LayerDefinition"); //NOXLATE resourceService.SetResource(layerResId, new MgByteReader(xmlString, "text/xml"), null); //NOXLATE var newLayer = provider.CreateLayer(layerResId); newLayer.SetName(uniqueName); newLayer.SetLegendLabel(legendLabel); newLayer.SetDisplayInLegend(themeParams.layer.GetDisplayInLegend()); newLayer.SetVisible(true); newLayer.SetSelectable(themeParams.layer.GetSelectable()); //HACK: This has to be true otherwise owner-drawn nodes will not display its children (the theme rules) provider.SetLayerExpandInLegend(newLayer, true); layers.Insert(layers.IndexOf(themeParams.layer), newLayer); //map.Save(resourceService); return uniqueName; }
private string ApplyTheme(ThemeParams themeParams) { var provider = _viewer.GetProvider(); var map = _viewer.GetMap(); var layers = map.GetLayers(); MgResourceService resourceService = (MgResourceService)provider.CreateService(MgServiceType.ResourceService); MgFeatureService featureService = (MgFeatureService)provider.CreateService(MgServiceType.FeatureService); MgResourceIdentifier resId = new MgResourceIdentifier(themeParams.layer.GetFeatureSourceId()); MgResourceIdentifier layerDefResId = themeParams.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"); //NOXLATE XmlElement vectorScaleRangecElement = (XmlElement)nodeList.Item(themeParams.scaleRangeIndex); XmlElement areaTypeStyle = (XmlElement)vectorScaleRangecElement.GetElementsByTagName("AreaTypeStyle").Item(0); //NOXLATE // Remove any existing <AreaRule> elements. XmlNodeList areaRuleList = areaTypeStyle.GetElementsByTagName("AreaRule"); //NOXLATE 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 = Properties.Resources.AreaRuleTemplate; MgFeatureAggregateOptions aggregateOptions = new MgFeatureAggregateOptions(); String value = null; String filterText = null; String areaRuleXML = null; XmlDocument areaDoc = null; XmlNode areaNode = null; double portion = 0.0; double increment = (themeParams.numRules > 1) ? 1.0 / (themeParams.numRules - 1) : 1.0; if (THEME_INDIVIDUAL == themeParams.distro) { aggregateOptions.AddComputedProperty("THEME_VALUE", "UNIQUE(\"" + themeParams.property.Name + "\")"); //NOXLATE MgDataReader dataReader = featureService.SelectAggregate(resId, themeParams.layer.GetFeatureClassName(), aggregateOptions); while (dataReader.ReadNext()) { value = Util.GetFeaturePropertyValue(dataReader, "THEME_VALUE"); //NOXLATE filterText = """ + themeParams.property.Name + "" = "; //NOXLATE if (themeParams.property.DataType == MgPropertyType.String) { filterText = filterText + "'" + value + "'"; //NOXLATE } else { filterText = filterText + value; } areaRuleXML = String.Format(areaRuleTemplate, themeParams.property.Name + ":" + value, //NOXLATE filterText, Util.InterpolateColor(portion, themeParams.fillFrom, themeParams.fillTo, themeParams.fillTrans), Util.InterpolateColor(portion, themeParams.borderFrom, themeParams.borderTo, 0)); areaDoc = new XmlDocument(); areaDoc.LoadXml(areaRuleXML); areaNode = doc.ImportNode(areaDoc.DocumentElement, true); areaTypeStyle.AppendChild(areaNode); portion = portion + increment; } dataReader.Close(); } else { var values = new List <string>(); var expr = themeParams.distro + "(\"" + themeParams.property.Name + "\"," + themeParams.numRules + "," + themeParams.minValue + "," + themeParams.maxValue + ")"; //NOXLATE aggregateOptions.AddComputedProperty("THEME_VALUE", expr); //NOXLATE MgDataReader dataReader = featureService.SelectAggregate(resId, themeParams.layer.GetFeatureClassName(), aggregateOptions); while (dataReader.ReadNext()) { value = Util.GetFeaturePropertyValue(dataReader, "THEME_VALUE"); //NOXLATE values.Add(value); } dataReader.Close(); for (int i = 0; i < values.Count - 1; i++) { filterText = """ + themeParams.property.Name + "" >= " + values[i] + " AND "" + themeParams.property.Name; //NOXLATE if (i == values.Count - 1) { filterText = filterText + "" <= " + values[i + 1]; //NOXLATE } else { filterText = filterText + "" < " + values[i + 1]; //NOXLATE } areaRuleXML = String.Format(areaRuleTemplate, themeParams.property.Name + ":" + values[i] + " - " + values[i + 1], //NOXLATE filterText, Util.InterpolateColor(portion, themeParams.fillFrom, themeParams.fillTo, themeParams.fillTrans), Util.InterpolateColor(portion, themeParams.borderFrom, themeParams.borderTo, 0)); areaDoc = new XmlDocument(); areaDoc.LoadXml(areaRuleXML); areaNode = doc.ImportNode(areaDoc.DocumentElement, true); areaTypeStyle.AppendChild(areaNode); portion = portion + increment; } } // Now save our new layer definition to the session and add it to the map. String xmlString = doc.DocumentElement.OuterXml; String uniqueName = Util.MakeUniqueLayerName(map, themeParams.layer.Name, themeParams.themeName); String legendLabel = themeParams.layer.GetLegendLabel(); if (!string.IsNullOrEmpty(themeParams.themeName)) { legendLabel = legendLabel + " (" + themeParams.themeName + ")"; //NOXLATE } MgResourceIdentifier layerResId = new MgResourceIdentifier("Session:" + _sessionId + "//" + uniqueName + ".LayerDefinition"); //NOXLATE resourceService.SetResource(layerResId, new MgByteReader(xmlString, "text/xml"), null); //NOXLATE var newLayer = provider.CreateLayer(layerResId); newLayer.SetName(uniqueName); newLayer.SetLegendLabel(legendLabel); newLayer.SetDisplayInLegend(themeParams.layer.GetDisplayInLegend()); newLayer.SetVisible(true); newLayer.SetSelectable(themeParams.layer.GetSelectable()); //HACK: This has to be true otherwise owner-drawn nodes will not display its children (the theme rules) provider.SetLayerExpandInLegend(newLayer, true); layers.Insert(layers.IndexOf(themeParams.layer), newLayer); //map.Save(resourceService); return(uniqueName); }