/// <summary> /// Generates GeoJson for the given attribute collection. /// </summary> /// <param name="writer"></param> /// <param name="attributes"></param> internal static void Write(JsonWriter writer, GeometryAttributeCollection attributes) { if (writer == null) { throw new ArgumentNullException("writer"); } if (attributes == null) { throw new ArgumentNullException("attributes"); } writer.WriteStartObject(); var keys = new HashSet <string>(); foreach (var attribute in attributes) { if (!keys.Contains(attribute.Key)) { writer.WritePropertyName(attribute.Key); writer.WriteValue(attribute.Value); keys.Add(attribute.Key); } } writer.WriteEndObject(); }
public static Feature ReadFeature(JsonReader jsonReader) { string str = string.Empty; Geometry geometry = (Geometry)null; GeometryAttributeCollection attributes = (GeometryAttributeCollection)null; while (jsonReader.Read()) { if (jsonReader.TokenType == JsonToken.EndArray) { return((Feature)null); } if (jsonReader.TokenType != JsonToken.EndObject) { if (jsonReader.TokenType == JsonToken.PropertyName) { if ((string)jsonReader.Value == "type") { str = jsonReader.ReadAsString(); } else if ((string)jsonReader.Value == "geometry") { geometry = GeoJsonConverter.ReadGeometry(jsonReader); } else if ((string)jsonReader.Value == "properties") { attributes = GeoJsonConverter.ReadAttributes(jsonReader); } } } else { break; } } if (!(str == "Feature")) { throw new Exception("Invalid type."); } if (geometry == null) { throw new Exception("No geometry found."); } if (attributes != null) { return(new Feature(geometry, attributes)); } return(new Feature(geometry)); }
/// <summary> /// Reads GeoJson and returns the feature. /// </summary> /// <param name="jsonReader"></param> /// <returns></returns> public static Feature ReadFeature(JsonReader jsonReader) { var type = string.Empty; Geometry geometry = null; GeometryAttributeCollection attributes = null; while (jsonReader.Read()) { if (jsonReader.TokenType == JsonToken.EndArray) { return(null); } if (jsonReader.TokenType == JsonToken.EndObject) { // end of geometry. break; } if (jsonReader.TokenType == JsonToken.PropertyName) { if ((string)jsonReader.Value == "type") { // the geometry type. type = jsonReader.ReadAsString(); } else if ((string)jsonReader.Value == "geometry") { // the geometry. geometry = GeoJsonConverter.ReadGeometry(jsonReader); } else if ((string)jsonReader.Value == "properties") { // the properties/attributes. attributes = GeoJsonConverter.ReadAttributes(jsonReader); } } } switch (type) { case "Feature": if (geometry == null) { throw new Exception("No geometry found."); } if (attributes != null) { return(new Feature(geometry, attributes)); } return(new Feature(geometry)); } throw new Exception("Invalid type."); }
/// <summary> /// Generates GeoJson for the given attribute collection. /// </summary> /// <param name="writer"></param> /// <param name="attributes"></param> internal static void Write(JsonWriter writer, GeometryAttributeCollection attributes) { if (writer == null) { throw new ArgumentNullException("writer"); } if (attributes == null) { throw new ArgumentNullException("attributes"); } writer.WriteStartObject(); foreach (var attribute in attributes) { writer.WritePropertyName(attribute.Key); writer.WriteValue(attribute.Value); } writer.WriteEndObject(); }
/// <summary> /// Generates GeoJson for the given attribute collection. /// </summary> /// <param name="writer"></param> /// <param name="attributes"></param> internal static void Write(JsonWriter writer, GeometryAttributeCollection attributes) { if (writer == null) { throw new ArgumentNullException("writer"); } if (attributes == null) { throw new ArgumentNullException("attributes"); } writer.WriteStartObject(); var keys = new HashSet<string>(); foreach (var attribute in attributes) { if (!keys.Contains(attribute.Key)) { writer.WritePropertyName(attribute.Key); writer.WriteValue(attribute.Value); keys.Add(attribute.Key); } } writer.WriteEndObject(); }
public Feature(Geometry geometry, GeometryAttributeCollection attributes) { this.Geometry = geometry; this.Attributes = attributes; }
public Feature(Geometry geometry) { this.Geometry = geometry; this.Attributes = (GeometryAttributeCollection) new SimpleGeometryAttributeCollection(); }
/// <summary> /// Generates GeoJson for the given attribute collection. /// </summary> /// <param name="writer"></param> /// <param name="attributes"></param> internal static void Write(JsonWriter writer, GeometryAttributeCollection attributes) { if (writer == null) { throw new ArgumentNullException("writer"); } if (attributes == null) { throw new ArgumentNullException("attributes"); } writer.WriteStartObject(); foreach (var attribute in attributes) { writer.WritePropertyName(attribute.Key); writer.WriteValue(attribute.Value); } writer.WriteEndObject(); }