using System; using System.Collections; namespace GisSharpBlog.NetTopologySuite.Features { /// /// Stores all attributes associated with a single Geometry feature. /// [Serializable] public class AttributesTable : IAttributesTable { private const string IndexField = "_NTS_ID_"; private const int IndexValue = 0; private readonly Hashtable attributes = new Hashtable(); /// /// Initialize a new attribute table. /// public AttributesTable() { // Add ID with fixed value of 0 // AddAttribute(IndexField, typeof(Int32)); // this[IndexField] = IndexValue; } /// /// Get / Set the value of the specified attribute. /// /// /// public object this[string attributeName] { get { return GetValue(attributeName); } set { SetValue(attributeName, value); } } /// /// /// public int Count { get { return attributes.Count; } } /// /// Returns a string array containing /// all names of present attributes. /// /// public string[] GetNames() { int index = 0; string[] names = new string[attributes.Count]; foreach (string name in attributes.Keys) { names[index++] = name; } return names; } /// /// Returns a object array containing /// all values of present attributes. /// /// public object[] GetValues() { int index = 0; object[] values = new object[attributes.Count]; foreach (object val in attributes.Values) { values[index++] = val; } return values; } /// /// Verifies if attribute specified already exists. /// /// /// public bool Exists(string attributeName) { return attributes.ContainsKey(attributeName); } /// /// Build a field with the given value and add it to attributes table. /// /// Name of the new attribute. /// Value for attribute (can be null). /// If attribute already exists. public void AddAttribute(string attributeName, object attributeValue) { if (Exists(attributeName)) { throw new ArgumentException("Attribute " + attributeName + " already exists!"); } attributes.Add(attributeName, attributeValue); } /// /// Delete the specified attribute from the table. /// /// public virtual void DeleteAttribute(string attributeName) { if (!Exists(attributeName)) { throw new ArgumentException("Attribute " + attributeName + " not exists!"); } attributes.Remove(attributeName); } /// /// Return the System.Type of the specified attribute, /// useful for casting values retrieved with GetValue methods. /// /// /// public Type GetType(string attributeName) { if (!Exists(attributeName)) { throw new ArgumentException("Attribute " + attributeName + " not exists!"); } return attributes[attributeName].GetType(); } /// /// Get the value of the specified attribute. /// /// /// protected object GetValue(string attributeName) { if (!Exists(attributeName)) { throw new ArgumentException("Attribute " + attributeName + " not exists!"); } return attributes[attributeName]; } /// /// Set the value of the specified attribute. /// /// /// protected void SetValue(string attributeName, object attributeValue) { if (!Exists(attributeName)) { throw new ArgumentException("Attribute " + attributeName + " not exists!"); } attributes[attributeName] = attributeValue; } } }