Dima C# Sample

deals with computation of distance matrices
Post Reply
HarryM
Posts: 3
Joined: Thu May 14, 2020 5:47 am
Location: D-Wachenroth

Dima C# Sample

Post by HarryM »

Hi,
I'm new to xServer and seek for a working C# sample code which shows who to deal with xDima.

The sample on https://xserver2-europe-eu-test.cloud.p ... ntents.htm doesn't work.
I.e. DistanceMatrixContentsOptions, GetDistanceMatrixByLocationsRequest are not found,
createDistanceMatrixResponse distanceMatrixResponse = xdima.createDistanceMatrix(createDistanceMatrixRequest); drops a NullReference error.

Pleae help!

Harry
HarryM
Posts: 3
Joined: Thu May 14, 2020 5:47 am
Location: D-Wachenroth

Re: Dima C# Sample

Post by HarryM »

Hello,

after struggeling around, I found an other sample using xServer, but the DistanceMatrixRespone.Contents are always NULL. How I understand there should be the response matrix. What's wrong?
Please help!
Harry

Code: Select all

using System;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net;
using XServerClient;
using System.Globalization;
using XServerClient.Models;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;


namespace XServerJson
    {
    class Program
        {
        static void Main()
            {
            ExecAsync().Wait();

            Console.ReadKey();
            }

        static async Task ExecAsync()
            {
            // initialize client for xserver-internet
            var xServer = new XServer(
            new Uri("https://xserver2-europe-eu-test.cloud.ptvgroup.com"), new HttpClientHandler
                {
                Credentials = new NetworkCredential
                    {
                    Password = "<TOKEN>",
                    UserName = "xtok"
                    }
                });

            try
                {
                CreateDistanceMatrixRequest createDistanceMatrixRequest = constructCreateDistanceMatrixRequest();
                var job = await xServer.StartCreateDistanceMatrixAsync(createDistanceMatrixRequest);

                var id = job.Id;
                while (job.Status == JobStatus.QUEUING || job.Status == JobStatus.RUNNING)
                    {
                    await Task.Delay(1000);
                    job = await xServer.WatchJobAsync(new WatchRequest { Id = id });
                    }

                if (job.Status == JobStatus.SUCCEEDED)
                    {
                    var result = await xServer.FetchDistanceMatrixResponseAsync(new JobRequest { Id = id });

                    DistanceMatrixContents con = ((DistanceMatrixResponse)result).Contents;
                    if (con == null)
                        {
                        Console.WriteLine("NULL");
                        }
                    else
                        {
                        Console.WriteLine($@"{con}");
                        }
                    }
                else
                    {
                    Console.WriteLine($@"Failed with status: {job.Status}");
                    }

                await xServer.DeleteJobAsync(new JobRequest { Id = job.Id });
                }
            catch (XServerErrorException ex)
                {
                Console.WriteLine($"Request failed with error: {ex.Response.Content}");
                }
            finally
                {
                xServer.Dispose();
                }
            }
        
        private static CreateDistanceMatrixRequest constructCreateDistanceMatrixRequest()
            {
            List<RouteLocation> startLocations = new List<RouteLocation>();
            List<RouteLocation> destLocations = new List<RouteLocation>();

            startLocations.Add(createOffRoadCoordinate(10.7134, 49.7519));
            startLocations.Add(createOffRoadCoordinate(11.0167, 49.8));
            startLocations.Add(createOffRoadCoordinate(10.8886, 50.015));
            startLocations.Add(createOffRoadCoordinate(11.9305, 51.0801));

            destLocations.Add(createOffRoadCoordinate(11.58198, 48.135125));

            CreateDistanceMatrixRequest createDistanceMatrixRequest = new CreateDistanceMatrixRequest
                {
                StartLocations = startLocations.ToArray(),
                DestinationLocations = destLocations.ToArray(),
                StoredProfile = "truck40t"
                };
            return createDistanceMatrixRequest;
            }

        public static Coordinate createCoordinate(double x, double y)
            {
            Coordinate coordinate = new Coordinate
                {
                X = x,
                Y = y
                };
            return coordinate;
            }
        public static RouteLocation createOffRoadCoordinate(double x, double y)
            {
            OffRoadRouteLocation offRoadRouteLocation = new OffRoadRouteLocation
                {
                OffRoadCoordinate = createCoordinate(x, y),
                };
            return offRoadRouteLocation;
            }

        public static RouteLocation createOnRoadCoordinate(double x, double y)
            {
            OnRoadRouteLocation onRoadRouteLocation = new OnRoadRouteLocation
                {
                Coordinate = createCoordinate(x, y)
                };
            return onRoadRouteLocation;
            }
        }
    }
Joost
Posts: 307
Joined: Fri Apr 25, 2014 1:46 pm

Re: Dima C# Sample

Post by Joost »

You are using CreateDistanceMatrixRequest . This is designed for calculating a dima and leave it on the server so other xServer (like xTour and xCluster) can use it. If you want to get the result you have to use the derived type CreateAndGetDistanceMatrixRequest as request.

Keep in mind that CreateAndGetDistanceMatrixRequest does not leave the dima on the server. If you need the dima to persist but still want the content you can extend your current code with a getDistanceMatrix folow-up.
Joost Claessen
Senior Technical Consultant
PTV Benelux
HarryM
Posts: 3
Joined: Thu May 14, 2020 5:47 am
Location: D-Wachenroth

Re: Dima C# Sample

Post by HarryM »

Thank you!
Now it works ...
Post Reply