private void CreateDistrictsLayer(IServerConnection conn, string resId, string layerId) { //We use the Utility class to create our layer. You can also use ObjectFactory, but //that requires explicitly specifying the resource version. Using Utility will pick //the latest supported version ILayerDefinition ldf = Utility.CreateDefaultLayer(conn, LayerType.Vector); IVectorLayerDefinition vldf = (IVectorLayerDefinition)ldf.SubLayer; //Set feature source vldf.ResourceId = resId; //Set the feature class // //Note: In versions of the Sheboygan Dataset before 2.6, this used to be // // - Feature Class: SDF_2_Schema:VotingDistricts // - Identity Property: Autogenerated_SDF_ID // - Geometry Property: Data string featureClass = "Default:VotingDistricts"; string idProp = "FeatId"; string geometryProp = "Geometry"; vldf.FeatureName = featureClass; //Set the designated geometry vldf.Geometry = geometryProp; //Get the first vector scale range. This will have been created for us and is 0 to infinity IVectorScaleRange vsr = vldf.GetScaleRangeAt(0); //What are we doing here? We're checking if this vector scale range is a //IVectorScaleRange2 instance. If it is, it means this layer definition //has a composite style attached, which takes precedence over point/area/line //styles. We don't want this, so this removes the composite styles if they //exist. IVectorScaleRange2 vsr2 = vsr as IVectorScaleRange2; if (vsr2 != null) { vsr2.CompositeStyle = null; } //Get the area style IAreaVectorStyle astyle = vsr.AreaStyle; //Remove the default rule astyle.RemoveAllRules(); IFeatureService featSvc = conn.FeatureService; //Generate a random color for each distinct feature id //Perform a distinct value query IReader valueReader = featSvc.AggregateQueryFeatureSource(resId, featureClass, null, new NameValueCollection() { { "Value", "UNIQUE(" + idProp + ")" } //UNIQUE() is the aggregate function that collects all distinct values of FeatId }); while (valueReader.ReadNext()) { //The parent Layer Definition provides all the methods needed to create the necessary child elements IAreaRule rule = ldf.CreateDefaultAreaRule(); //Set the filter for this rule rule.Filter = idProp + " = " + valueReader["Value"].ToString(); //IReader allows object access by name in case you don't care to determine the data type rule.LegendLabel = valueReader["Value"].ToString(); //Assign a random color fill rule.AreaSymbolization2D.Fill.ForegroundColor = Utility.SerializeHTMLColor(RandomColor(), true); //Add this rule astyle.AddRule(rule); } valueReader.Close(); //Now save it conn.ResourceService.SaveResourceAs(ldf, layerId); }
private void LookupValues_Click(object sender, EventArgs e) { //Use UNIQUE() method first. This should work in most cases using (new WaitCursor(this)) { string filter = null; var expr = $"UNIQUE({ColumnName.Text})"; //NOXLATE bool bFallback = false; ColumnValue.Items.Clear(); ColumnValue.Tag = null; try { using (var rdr = _featSvc.AggregateQueryFeatureSource(m_featureSource, _cls.QualifiedName, filter, new System.Collections.Specialized.NameValueCollection() { { UNIQ_VALS, expr } })) { ColumnValue.Tag = rdr.GetPropertyType(UNIQ_VALS); while (rdr.ReadNext()) { if (!rdr.IsNull(UNIQ_VALS)) { object value = rdr[UNIQ_VALS]; ColumnValue.Items.Add(value); } } rdr.Close(); } } catch { ColumnValue.Items.Clear(); bFallback = true; } if (!bFallback) { ColumnValue.Enabled = true; ColumnValue.SelectedIndex = -1; ColumnValue.DroppedDown = true; return; } try { SortedList <string, PropertyDefinition> cols = (SortedList <string, PropertyDefinition>)ColumnName.Tag; PropertyDefinition col = cols[ColumnName.Text]; bool retry = true; Exception rawEx = null; SortedList <string, string> values = new SortedList <string, string>(); bool hasNull = false; while (retry) { try { retry = false; using (var rd = _featSvc.QueryFeatureSource(m_featureSource, _cls.QualifiedName, filter, new string[] { ColumnName.Text })) { while (rd.ReadNext()) { if (rd.IsNull(ColumnName.Text)) { hasNull = true; } else { values[Convert.ToString(rd[ColumnName.Text], System.Globalization.CultureInfo.InvariantCulture)] = null; } } rd.Close(); } } catch (Exception ex) { if (filter == null && ex.Message.IndexOf("MgNullPropertyValueException") >= 0) //NOXLATE { hasNull = true; rawEx = ex; retry = true; filter = $"{ColumnName.Text} != NULL"; //NOXLATE } else if (rawEx != null) { throw rawEx; } else { throw; } } } ColumnValue.Items.Clear(); if (hasNull) { ColumnValue.Items.Add("NULL"); //NOXLATE } foreach (string s in values.Keys) { ColumnValue.Items.Add(s); } ColumnValue.Tag = col.Type; if (ColumnValue.Items.Count == 0) { MessageBox.Show(this, Strings.NoColumnValuesError, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information); } else { ColumnValue.Enabled = true; ColumnValue.SelectedIndex = -1; ColumnValue.DroppedDown = true; } } catch (Exception ex) { string msg = NestedExceptionMessageProcessor.GetFullMessage(ex); MessageBox.Show(this, string.Format(Strings.ColumnValueError, msg), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information); } } }