| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Text.Json; |
| | 3 | |
|
| | 4 | | namespace Itinero.IO.Json.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 feature start. |
| | 35 | | /// </summary> |
| | 36 | | /// <param name="jsonWriter">The json writer.</param> |
| | 37 | | public static void WriteFeatureStart(this Utf8JsonWriter jsonWriter) |
| 0 | 38 | | { |
| 0 | 39 | | jsonWriter.WriteStartObject(); |
| 0 | 40 | | jsonWriter.WriteString("type", "Feature"); |
| 0 | 41 | | } |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Writes feature end. |
| | 45 | | /// </summary> |
| | 46 | | /// <param name="jsonWriter">The json writer.</param> |
| | 47 | | public static void WriteFeatureEnd(this Utf8JsonWriter jsonWriter) |
| 0 | 48 | | { |
| 0 | 49 | | jsonWriter.WriteEndObject(); |
| 0 | 50 | | } |
| | 51 | |
|
| | 52 | | /// <summary> |
| | 53 | | /// Writes a point. |
| | 54 | | /// </summary> |
| | 55 | | /// <param name="jsonWriter">The json writer.</param> |
| | 56 | | /// <param name="location">The location.</param> |
| | 57 | | public static void WritePoint(this Utf8JsonWriter jsonWriter, |
| | 58 | | (double longitude, double latitude, float? e) location) |
| 0 | 59 | | { |
| 0 | 60 | | jsonWriter.WriteStartObject(); |
| 0 | 61 | | jsonWriter.WriteString("type", "Point"); |
| 0 | 62 | | jsonWriter.WritePropertyName("coordinates"); |
| | 63 | |
|
| 0 | 64 | | jsonWriter.WriteStartArray(); |
| 0 | 65 | | jsonWriter.WriteNumberValue(location.longitude); |
| 0 | 66 | | jsonWriter.WriteNumberValue(location.latitude); |
| 0 | 67 | | if (location.e != null) |
| 0 | 68 | | { |
| 0 | 69 | | jsonWriter.WriteNumberValue(location.e.Value); |
| 0 | 70 | | } |
| | 71 | |
|
| 0 | 72 | | jsonWriter.WriteEndArray(); |
| 0 | 73 | | jsonWriter.WriteEndObject(); |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | /// <summary> |
| | 77 | | /// Writes a line string. |
| | 78 | | /// </summary> |
| | 79 | | /// <param name="jsonWriter">The json writer.</param> |
| | 80 | | /// <param name="coordinates">The coordinates.</param> |
| | 81 | | public static void WriteLineString(this Utf8JsonWriter jsonWriter, |
| | 82 | | IEnumerable<(double longitude, double latitude, float? e)> coordinates) |
| 0 | 83 | | { |
| 0 | 84 | | jsonWriter.WriteStartObject(); |
| 0 | 85 | | jsonWriter.WriteString("type", "LineString"); |
| 0 | 86 | | jsonWriter.WritePropertyName("coordinates"); |
| | 87 | |
|
| 0 | 88 | | jsonWriter.WriteStartArray(); |
| 0 | 89 | | foreach (var c in coordinates) |
| 0 | 90 | | { |
| 0 | 91 | | jsonWriter.WriteStartArray(); |
| 0 | 92 | | jsonWriter.WriteNumberValue(c.longitude); |
| 0 | 93 | | jsonWriter.WriteNumberValue(c.latitude); |
| | 94 | | // if (route.Shape[i].Elevation.HasValue) |
| | 95 | | // { |
| | 96 | | // jsonWriter.WriteArrayValue(route.Shape[i].Elevation.Value.ToInvariantString()); |
| | 97 | | // } |
| 0 | 98 | | jsonWriter.WriteEndArray(); |
| 0 | 99 | | } |
| | 100 | |
|
| 0 | 101 | | jsonWriter.WriteEndArray(); |
| 0 | 102 | | jsonWriter.WriteEndObject(); |
| 0 | 103 | | } |
| | 104 | |
|
| | 105 | | /// <summary> |
| | 106 | | /// Writes properties. |
| | 107 | | /// </summary> |
| | 108 | | /// <param name="jsonWriter">The json writer.</param> |
| | 109 | | /// <param name="attributes">The attributes to write as properties.</param> |
| | 110 | | public static void WriteProperties(this Utf8JsonWriter jsonWriter, |
| | 111 | | IEnumerable<(string key, string value)> attributes) |
| 0 | 112 | | { |
| 0 | 113 | | jsonWriter.WritePropertyName("properties"); |
| 0 | 114 | | jsonWriter.WriteStartObject(); |
| 0 | 115 | | foreach (var a in attributes) |
| 0 | 116 | | { |
| 0 | 117 | | jsonWriter.WriteString(a.key, a.value); |
| 0 | 118 | | } |
| | 119 | |
|
| 0 | 120 | | jsonWriter.WriteEndObject(); |
| 0 | 121 | | } |
| | 122 | | } |