| | | 1 | | using System.Collections.Generic; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | |
| | | 4 | | namespace Itinero.MapMatching.IO.GeoJson; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Geojson writing extensions. |
| | | 8 | | /// </summary> |
| | | 9 | | public static class GeoJsonExtensions |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Writes feature collection start. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="jsonWriter">The json writer.</param> |
| | | 15 | | public static void WriteFeatureCollectionStart(this Utf8JsonWriter jsonWriter) |
| | 0 | 16 | | { |
| | 0 | 17 | | jsonWriter.WriteStartObject(); |
| | 0 | 18 | | jsonWriter.WriteString("type", "FeatureCollection"); |
| | 0 | 19 | | jsonWriter.WritePropertyName("features"); |
| | 0 | 20 | | jsonWriter.WriteStartArray(); |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Writes feature collection end. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="jsonWriter">The json writer.</param> |
| | | 27 | | public static void WriteFeatureCollectionEnd(this Utf8JsonWriter jsonWriter) |
| | 0 | 28 | | { |
| | 0 | 29 | | jsonWriter.WriteEndArray(); |
| | 0 | 30 | | jsonWriter.WriteEndObject(); |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Writes a geometry start. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="jsonWriter">The json writer.</param> |
| | | 37 | | public static void WriteGeometryStart(this Utf8JsonWriter jsonWriter) |
| | 0 | 38 | | { |
| | 0 | 39 | | jsonWriter.WritePropertyName("geometry"); |
| | 0 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Writes a feature start. |
| | | 44 | | /// </summary> |
| | | 45 | | /// <param name="jsonWriter">The json writer.</param> |
| | | 46 | | public static void WriteFeatureStart(this Utf8JsonWriter jsonWriter) |
| | 0 | 47 | | { |
| | 0 | 48 | | jsonWriter.WriteStartObject(); |
| | 0 | 49 | | jsonWriter.WriteString("type", "Feature"); |
| | 0 | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Writes feature end. |
| | | 54 | | /// </summary> |
| | | 55 | | /// <param name="jsonWriter">The json writer.</param> |
| | | 56 | | public static void WriteFeatureEnd(this Utf8JsonWriter jsonWriter) |
| | 0 | 57 | | { |
| | 0 | 58 | | jsonWriter.WriteEndObject(); |
| | 0 | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Writes a point. |
| | | 63 | | /// </summary> |
| | | 64 | | /// <param name="jsonWriter">The json writer.</param> |
| | | 65 | | /// <param name="location">The location.</param> |
| | | 66 | | public static void WritePoint(this Utf8JsonWriter jsonWriter, |
| | | 67 | | (double longitude, double latitude, float? e) location) |
| | 0 | 68 | | { |
| | 0 | 69 | | jsonWriter.WriteStartObject(); |
| | 0 | 70 | | jsonWriter.WriteString("type", "Point"); |
| | 0 | 71 | | jsonWriter.WritePropertyName("coordinates"); |
| | | 72 | | |
| | 0 | 73 | | jsonWriter.WriteStartArray(); |
| | 0 | 74 | | jsonWriter.WriteNumberValue(location.longitude); |
| | 0 | 75 | | jsonWriter.WriteNumberValue(location.latitude); |
| | 0 | 76 | | if (location.e != null) |
| | 0 | 77 | | { |
| | 0 | 78 | | jsonWriter.WriteNumberValue(location.e.Value); |
| | 0 | 79 | | } |
| | | 80 | | |
| | 0 | 81 | | jsonWriter.WriteEndArray(); |
| | 0 | 82 | | jsonWriter.WriteEndObject(); |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Writes a line string. |
| | | 87 | | /// </summary> |
| | | 88 | | /// <param name="jsonWriter">The json writer.</param> |
| | | 89 | | /// <param name="coordinates">The coordinates.</param> |
| | | 90 | | public static void WriteLineString(this Utf8JsonWriter jsonWriter, |
| | | 91 | | IEnumerable<(double longitude, double latitude, float? e)> coordinates) |
| | 0 | 92 | | { |
| | 0 | 93 | | jsonWriter.WriteStartObject(); |
| | 0 | 94 | | jsonWriter.WriteString("type", "LineString"); |
| | 0 | 95 | | jsonWriter.WritePropertyName("coordinates"); |
| | | 96 | | |
| | 0 | 97 | | jsonWriter.WriteStartArray(); |
| | 0 | 98 | | foreach (var c in coordinates) |
| | 0 | 99 | | { |
| | 0 | 100 | | jsonWriter.WriteStartArray(); |
| | 0 | 101 | | jsonWriter.WriteNumberValue(c.longitude); |
| | 0 | 102 | | jsonWriter.WriteNumberValue(c.latitude); |
| | 0 | 103 | | jsonWriter.WriteEndArray(); |
| | 0 | 104 | | } |
| | | 105 | | |
| | 0 | 106 | | jsonWriter.WriteEndArray(); |
| | 0 | 107 | | jsonWriter.WriteEndObject(); |
| | 0 | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Writes properties. |
| | | 112 | | /// </summary> |
| | | 113 | | /// <param name="jsonWriter">The json writer.</param> |
| | | 114 | | /// <param name="attributes">The attributes to write as properties.</param> |
| | | 115 | | public static void WriteProperties(this Utf8JsonWriter jsonWriter, |
| | | 116 | | IEnumerable<(string key, string value)> attributes) |
| | 0 | 117 | | { |
| | 0 | 118 | | jsonWriter.WritePropertyName("properties"); |
| | 0 | 119 | | jsonWriter.WriteStartObject(); |
| | 0 | 120 | | foreach (var a in attributes) |
| | 0 | 121 | | { |
| | 0 | 122 | | jsonWriter.WriteString(a.key, a.value); |
| | 0 | 123 | | } |
| | | 124 | | |
| | 0 | 125 | | jsonWriter.WriteEndObject(); |
| | 0 | 126 | | } |
| | | 127 | | } |