コード例 #1
0
        public void Write_DoesntWriteTrackMetadataIfWriteMetadataIsFalse()
        {
            MemoryStream stream = new MemoryStream();

            using (GpxWriter target = new GpxWriter(stream, new GpxWriterSettings()
            {
                WriteMetadata = false
            })) {
                target.Write(_trackWithMetadata);
            }

            XDocument written  = XDocument.Load(new MemoryStream(stream.ToArray()));
            XDocument expected = XDocument.Load(new MemoryStream(GpxTestData.gpx_track_single_track_segment));

            Assert.True(XDocumentExtensions.DeepEqualsWithNormalization(written, expected, null));
        }
コード例 #2
0
        public void Write_WritesWaypointWithoutMetadataIfMetadataIsNull()
        {
            MemoryStream stream = new MemoryStream();

            using (GpxWriter target = new GpxWriter(stream, new GpxWriterSettings()
            {
                WriteMetadata = false
            })) {
                target.Write(_waypoint);
            }

            XDocument written  = XDocument.Load(new MemoryStream(stream.ToArray()));
            XDocument expected = XDocument.Load(new MemoryStream(GpxTestData.gpx_waypoint_simple));

            Assert.True(XDocumentExtensions.DeepEqualsWithNormalization(written, expected, null));
        }
コード例 #3
0
        public void Write_WritesRouteWithMetadata()
        {
            MemoryStream stream = new MemoryStream();

            using (GpxWriter target = new GpxWriter(stream, new GpxWriterSettings()
            {
                WriteMetadata = true
            })) {
                target.Write(_routeWithMetadata);
            }

            XDocument written  = XDocument.Load(new MemoryStream(stream.ToArray()));
            XDocument expected = XDocument.Load(new MemoryStream(GpxTestData.gpx_route_with_metadata));

            Assert.True(XDocumentExtensions.DeepEqualsWithNormalization(written, expected, null));
        }
コード例 #4
0
        public void Write_WritesTrackWithMetadata()
        {
            MemoryStream stream = new MemoryStream();

            using (GpxWriter target = new GpxWriter(stream, new GpxWriterSettings()
            {
                WriteMetadata = true
            })) {
                target.Write(_trackWithMetadata);
            }

            XDocument written  = XDocument.Load(new MemoryStream(stream.ToArray()));
            XDocument expected = XDocument.Load(TestDataReader.Open("gpx-track-with-metadata.gpx"));

            Assert.True(XDocumentExtensions.DeepEqualsWithNormalization(written, expected));
        }
コード例 #5
0
        public void Write_WritesRouteWithoutMetadataIfWriteMetadataIsFalse()
        {
            MemoryStream stream = new MemoryStream();

            using (GpxWriter target = new GpxWriter(stream, new GpxWriterSettings()
            {
                WriteMetadata = false
            })) {
                target.Write(_routeWithMetadata);
            }

            XDocument written  = XDocument.Load(new MemoryStream(stream.ToArray()));
            XDocument expected = XDocument.Load(TestDataReader.Open("gpx-route-single-route.gpx"));

            Assert.True(XDocumentExtensions.DeepEqualsWithNormalization(written, expected));
        }
コード例 #6
0
        public void Write_WritesRouteWithoutUnnecessaryElements()
        {
            _routeWithMetadata.Metadata.Source = null;
            MemoryStream stream = new MemoryStream();

            using (GpxWriter target = new GpxWriter(stream, new GpxWriterSettings()
            {
                WriteMetadata = true
            })) {
                target.Write(_routeWithMetadata);
            }

            XDocument written  = XDocument.Load(new MemoryStream(stream.ToArray()));
            XDocument expected = XDocument.Load(TestDataReader.Open("gpx-route-with-metadata-selection.gpx"));

            Assert.True(XDocumentExtensions.DeepEqualsWithNormalization(written, expected));
        }
コード例 #7
0
        public void Write_TrackWithEntityDetailsButNullValues_WritesTrackWithoutUnnecessaryElements()
        {
            MemoryStream stream = new MemoryStream();

            _trackWithMetadata.Metadata.Source = null;

            using (GpxWriter target = new GpxWriter(stream, new GpxWriterSettings()
            {
                WriteMetadata = true
            })) {
                target.Write(_trackWithMetadata);
            }

            XDocument written  = XDocument.Load(new MemoryStream(stream.ToArray()));
            XDocument expected = XDocument.Load(new MemoryStream(GpxTestData.gpx_track_with_metadata_selection));

            Assert.True(XDocumentExtensions.DeepEqualsWithNormalization(written, expected, null));
        }
コード例 #8
0
        public void Write_WritesWaypointWithoutUnnecessaryElements()
        {
            _waypointWithMetadata.Metadata.SatellitesCount = null;
            _waypointWithMetadata.Metadata.Name            = null;
            MemoryStream stream = new MemoryStream();

            using (GpxWriter target = new GpxWriter(stream, new GpxWriterSettings()
            {
                WriteMetadata = true
            })) {
                target.Write(_waypointWithMetadata);
            }

            XDocument written  = XDocument.Load(new MemoryStream(stream.ToArray()));
            XDocument expected = XDocument.Load(new MemoryStream(GpxTestData.gpx_waypoint_with_metadata_selection));

            Assert.True(XDocumentExtensions.DeepEqualsWithNormalization(written, expected, null));
        }
コード例 #9
0
ファイル: Program.cs プロジェクト: cuulee/streetscan-planner
        static void Main(string[] args)
        {
            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Verbose()
                         .Enrich.FromLogContext()
                         .WriteTo.Console()
                         .CreateLogger();

#if DEBUG
            if (args == null || args.Length < 1)
            {
                args = new[] { "test" };
            }
#endif

            if (args == null || args.Length < 1)
            { // show help.
                Log.Fatal("Could not parse arguments");
                ShowHelp();
                return;
            }
            if (args[0] == "help")
            {
                ShowHelp();
                return;
            }
            if (args[0] == "test")
            {
                args = new[]
                {
                    "test.geojson",
                    "test.gpx"
                };
                Log.Information($"Running test using: {args[0]}");
            }

            if (!string.IsNullOrEmpty(args[0]))
            { // check if the input exists.
                if (!File.Exists(args[0]))
                {
                    Log.Fatal($"Input file not found: {args[0]}");
                    ShowHelp();
                    return;
                }

                if (args.Length == 1)
                {
                    args = new[]
                    {
                        args[0],
                        args[0] + ".gpx"
                    };
                    Log.Information($"No second argument found, using output: {args[1]}");
                }
            }

            // enable logging.
            OsmSharp.Logging.Logger.LogAction = (origin, level, message, parameters) =>
            {
                var formattedMessage = $"{origin} - {message}";
                switch (level)
                {
                case "critical":
                    Log.Fatal(formattedMessage);
                    break;

                case "error":
                    Log.Error(formattedMessage);
                    break;

                case "warning":
                    Log.Warning(formattedMessage);
                    break;

                case "verbose":
                    Log.Verbose(formattedMessage);
                    break;

                case "information":
                    Log.Information(formattedMessage);
                    break;

                case "debug":
                    Log.Debug(formattedMessage);
                    break;
                }
            };
            Itinero.Logging.Logger.LogAction = (origin, level, message, parameters) =>
            {
                var formattedMessage = $"{origin} - {message}";
                switch (level)
                {
                case "critical":
                    Log.Fatal(formattedMessage);
                    break;

                case "error":
                    Log.Error(formattedMessage);
                    break;

                case "warning":
                    Log.Warning(formattedMessage);
                    break;

                case "verbose":
                    Log.Verbose(formattedMessage);
                    break;

                case "information":
                    Log.Information(formattedMessage);
                    break;

                case "debug":
                    Log.Debug(formattedMessage);
                    break;
                }
            };

            // validate arguments.
            if (args.Length < 2)
            {
                Log.Fatal("At least two arguments expected.");
                return;
            }

            var inputFile = args[0];
            if (!File.Exists(inputFile))
            {
                Log.Fatal($"Input file {inputFile} not found!");
                return;
            }

            var outputPath = new FileInfo(args[1]).DirectoryName;
            if (!Directory.Exists(outputPath))
            {
                Log.Fatal($"Output path {outputPath} not found!");
                return;
            }

            var profileName = "car.shortest";
            if (args.Length >= 3 &&
                args[2] == "fastest")
            { // apply fastest only on request.
                profileName = "car";
            }
            Log.Information($"Using profile '{profileName}'");

            // read the locations.
            Coordinate[] locations;
            if (inputFile.ToLowerInvariant().EndsWith(".geojson"))
            {
                locations = GeoJson.GeoJsonReader1.Read(args[0]).ToArray();
            }
            else
            {
                locations = CSV.CSVReader.Read(args[0]).Select(r => new Coordinate((float)r.Latitude, (float)r.Longitude)).ToArray();
            }

            // build router db if needed.
            var routerDb = RouterDbBuilder.BuildRouterDb(profileName);

            // cut out a part of the router db.
            var box = locations.BuildBoundingBox().Value.Resize(0.01f);
            routerDb = routerDb.ExtractArea((l) => box.Overlaps(l.Latitude, l.Longitude));

            // create and configure the optimizer.
            var router    = new Router(routerDb);
            var optimizer = router.Optimizer(new OptimizerConfiguration(modelMapperRegistry: new ModelMapperRegistry(
                                                                            new ByEdgeDirectedModelMapper(1000))));

            // run the optimization.
            var profile = routerDb.GetSupportedProfile(profileName);
            var route   = optimizer.Optimize(profile.FullName, locations, out _, 0, 0, turnPenalty: 60);
            if (route.IsError)
            {
                Log.Fatal("Calculating route failed {@errorMessage}", route.ErrorMessage);
                return;
            }
            File.WriteAllText(args[1] + ".geojson", route.Value.ToGeoJson());

            // set a description/name on stops.
            foreach (var stop in route.Value.Stops)
            {
                if (!stop.Attributes.TryGetValue("order", out var order) ||
                    !stop.Attributes.TryGetValue("index", out var index))
                {
                    continue;
                }
                stop.Attributes.AddOrReplace("Name",
                                             $"{order}");
                stop.Attributes.AddOrReplace("Description",
                                             $"Stop {index} @ {order}");
            }
            route.Value.ShapeMeta = null;

            // convert to GPX.
            var features = route.Value.ToFeatureCollection();
            using (var stream = File.Open(args[1], FileMode.Create))
            {
                var writerSettings = new XmlWriterSettings {
                    Encoding = Encoding.UTF8, CloseOutput = true
                };
                using (var wr = XmlWriter.Create(stream, writerSettings))
                {
                    GpxWriter.Write(wr, null, new GpxMetadata("StreetScan"), features.Features, null);
                }
            }
        }