< Summary

Class:Itinero.MapMatching.IO.GeoJson.GraphModelExtensions
Assembly:Itinero.MapMatching
File(s):/home/runner/work/routing2/routing2/src/Itinero.MapMatching/IO/GeoJson/GraphModelExtensions.cs
Covered lines:0
Uncovered lines:87
Coverable lines:87
Total lines:113
Line coverage:0% (0 of 87)
Covered branches:0
Total branches:26
Branch coverage:0% (0 of 26)
Tag:251_23667616543

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
ToGeoJson(...)100%10%
WriteFeatures(...)0%260%

File(s)

/home/runner/work/routing2/routing2/src/Itinero.MapMatching/IO/GeoJson/GraphModelExtensions.cs

#LineLine coverage
 1using System.Collections.Generic;
 2using System.Globalization;
 3using System.IO;
 4using System.Text;
 5using System.Text.Json;
 6using Itinero.Geo;
 7using Itinero.MapMatching.Model;
 8using Itinero.Network;
 9using Itinero.Snapping;
 10
 11namespace Itinero.MapMatching.IO.GeoJson;
 12
 13public static class GraphModelExtensions
 14{
 15    public static string ToGeoJson(this GraphModel graphModel, RoutingNetwork routingNetwork)
 016    {
 017        using var stream = new MemoryStream();
 018        using (var jsonWriter = new Utf8JsonWriter(stream))
 019        {
 020            jsonWriter.WriteFeatureCollectionStart();
 021            jsonWriter.WriteFeatures(graphModel, routingNetwork);
 022            jsonWriter.WriteFeatureCollectionEnd();
 023        }
 24
 025        return Encoding.UTF8.GetString(stream.ToArray());
 026    }
 27
 28    public static void WriteFeatures(this Utf8JsonWriter jsonWriter, GraphModel graphModel, RoutingNetwork routingNetwor
 029    {
 030        var track = graphModel.Track;
 031        jsonWriter.WriteFeatures(track);
 32
 033        for (var n = 0; n < graphModel.Count; n++)
 034        {
 035            var node = graphModel.GetNode(n);
 036            if (node.SnapPoint == null) continue;
 37
 038            var nodeLocation = node.SnapPoint.Value.LocationOnNetwork(routingNetwork);
 039            jsonWriter.WriteFeatureStart();
 040            jsonWriter.WriteGeometryStart();
 041            jsonWriter.WritePoint((nodeLocation));
 042            jsonWriter.WriteProperties(new (string key, string value)[]
 043            {
 044                ("type", "node"),
 045                ("node-id", n.ToString()),
 046                ("track-point-id", node.TrackPoint?.ToString() ?? string.Empty),
 047                ("snappoint-edge-id", node.SnapPoint.Value.EdgeId.ToString()),
 048                ("snappoint-offset", node.SnapPoint.Value.Offset.ToString()),
 049                ("cost", node.Cost.ToString(CultureInfo.InvariantCulture))
 050            });
 051            jsonWriter.WriteFeatureEnd();
 52
 053            if (node.TrackPoint == null) continue;
 054            var trackPoint = track[node.TrackPoint.Value];
 55
 056            jsonWriter.WriteFeatureStart();
 057            jsonWriter.WriteGeometryStart();
 058            jsonWriter.WriteLineString(new[]
 059            {
 060                nodeLocation,
 061                (trackPoint.Location.longitude, trackPoint.Location.latitude, null)
 062            });
 063            var length =
 064                nodeLocation.DistanceEstimateInMeter(
 065                    (trackPoint.Location.longitude, trackPoint.Location.latitude, null));
 066            jsonWriter.WriteProperties(new (string key, string value)[]
 067            {
 068                ("type", "node-offset"),
 069                ("node-id", n.ToString()),
 070                ("track-point-id", node.TrackPoint.Value.ToString()),
 071                ("snappoint-offset", node.SnapPoint.Value.Offset.ToString()),
 072                ("length", length.ToString(CultureInfo.InvariantCulture)),
 073                ("cost", node.Cost.ToString(CultureInfo.InvariantCulture))
 074            });
 075            jsonWriter.WriteFeatureEnd();
 76
 077            var edges = graphModel.GetNeighbours(n);
 078            foreach (var edge in edges)
 079            {
 080                var node1 = graphModel.GetNode(edge.Node1);
 081                if (node1.SnapPoint == null) continue;
 082                var node2 = graphModel.GetNode(edge.Node2);
 083                if (node2.SnapPoint == null) continue;
 84
 085                var node1Location = node1.SnapPoint.Value.LocationOnNetwork(routingNetwork);
 086                var node2Location = node2.SnapPoint.Value.LocationOnNetwork(routingNetwork);
 087                length =
 088                    node1Location.DistanceEstimateInMeter(
 089                        node2Location);
 90
 091                jsonWriter.WriteFeatureStart();
 092                jsonWriter.WriteGeometryStart();
 093                jsonWriter.WriteLineString(new[]
 094                {
 095                    node1Location,
 096                    node2Location
 097                });
 098                var attributes = new List<(string key, string value)>
 099                {
 0100                    ("cost", edge.Cost.ToString(CultureInfo.InvariantCulture)),
 0101                    ("node-id1", edge.Node1.ToString()),
 0102                    ("node-id2", edge.Node2.ToString()),
 0103                    ("track-point-1", node1.TrackPoint?.ToString() ?? string.Empty),
 0104                    ("track-point-2", node2.TrackPoint?.ToString() ?? string.Empty),
 0105                    ("length", length.ToString(CultureInfo.InvariantCulture))
 0106                };
 0107                if (edge.Attributes != null) attributes.AddRange(edge.Attributes);
 0108                jsonWriter.WriteProperties(attributes);
 0109                jsonWriter.WriteFeatureEnd();
 0110            }
 0111        }
 0112    }
 113}

Methods/Properties

ToGeoJson(...)
WriteFeatures(...)