Exemplo n.º 1
0
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                var fact = new MgdServiceFactory();
                MgFeatureService          featSvc = (MgdFeatureService)fact.CreateService(MgServiceType.FeatureService);
                MgFeatureAggregateOptions query   = new MgFeatureAggregateOptions();
                string[] propNames = txtProps.Text.Split(',');
                foreach (var p in propNames)
                {
                    if (string.IsNullOrEmpty(p))
                    {
                        continue;
                    }

                    query.AddFeatureProperty(p);
                }

                foreach (Pair p in lstComputed.Items)
                {
                    query.AddComputedProperty(p.Name, p.Expr);
                }

                if (!string.IsNullOrEmpty(txtFilter.Text.Trim()))
                {
                    query.SetFilter(txtFilter.Text.Trim());
                }

                query.SelectDistinct(chkDistinct.Checked);

                MgResourceIdentifier fsId = new MgResourceIdentifier(txtFeatureSource.Text);

                MgDataReader reader = featSvc.SelectAggregate(fsId, txtClass.Text, query);
                new ReaderResponseDialog(reader).ShowDialog();
            }
            catch (MgException ex)
            {
                MessageBox.Show(ex.ToString(), "Error from MapGuide");
            }
        }
Exemplo n.º 2
0
        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 = "&quot;" + themeParams.property.Name + "&quot; = "; //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 = "&quot;" + themeParams.property.Name + "&quot; &gt;= " + values[i] + " AND &quot;" + themeParams.property.Name; //NOXLATE
                    if (i == values.Count - 1)
                    {
                        filterText = filterText + "&quot; &lt;= " + values[i + 1]; //NOXLATE
                    }
                    else
                    {
                        filterText = filterText + "&quot; &lt; " + 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);
        }