| | 1 | | using System.IO; |
| | 2 | | using System.Linq; |
| | 3 | | using System.Text; |
| | 4 | | using System.Text.Json; |
| | 5 | | using Itinero.Network; |
| | 6 | | using Itinero.Snapping; |
| | 7 | |
|
| | 8 | | namespace Itinero.IO.Json.GeoJson; |
| | 9 | |
|
| | 10 | | /// <summary> |
| | 11 | | /// Contains route extensions |
| | 12 | | /// </summary> |
| | 13 | | public static class SnapPointExtensions |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// Returns geojson for the given snap point. |
| | 17 | | /// </summary> |
| | 18 | | /// <param name="snapPoint">The snap point.</param> |
| | 19 | | /// <param name="routerDb">The router db.</param> |
| | 20 | | /// <returns>A geojson string.</returns> |
| | 21 | | public static string ToGeoJson(this SnapPoint snapPoint, RoutingNetwork routerDb) |
| 0 | 22 | | { |
| 0 | 23 | | using var stream = new MemoryStream(); |
| | 24 | |
|
| 0 | 25 | | using (var jsonWriter = new Utf8JsonWriter(stream)) |
| 0 | 26 | | { |
| 0 | 27 | | jsonWriter.WriteFeatureCollectionStart(); |
| 0 | 28 | | WriteFeatures(jsonWriter, snapPoint, routerDb); |
| 0 | 29 | | jsonWriter.WriteFeatureCollectionEnd(); |
| 0 | 30 | | } |
| | 31 | |
|
| 0 | 32 | | return Encoding.UTF8.GetString(stream.ToArray()); |
| 0 | 33 | | } |
| | 34 | |
|
| | 35 | | /// <summary> |
| | 36 | | /// Writes a geojson feature collection to the given json writer. |
| | 37 | | /// </summary> |
| | 38 | | /// <param name="snapPoint">The snap point.</param> |
| | 39 | | /// <param name="routerDb">The router db.</param> |
| | 40 | | /// <param name="jsonWriter">The json writer.</param> |
| | 41 | | public static void WriteFeatures(this Utf8JsonWriter jsonWriter, SnapPoint snapPoint, RoutingNetwork routerDb) |
| 0 | 42 | | { |
| 0 | 43 | | jsonWriter.WriteFeatureStart(); |
| 0 | 44 | | jsonWriter.WriteProperties(Enumerable.Empty<(string key, string value)>()); |
| | 45 | |
|
| 0 | 46 | | jsonWriter.WritePropertyName("geometry"); |
| 0 | 47 | | var locationOnNetwork = snapPoint.LocationOnNetwork(routerDb); |
| 0 | 48 | | jsonWriter.WritePoint(locationOnNetwork); |
| | 49 | |
|
| 0 | 50 | | jsonWriter.WriteFeatureEnd(); |
| 0 | 51 | | } |
| | 52 | | } |