コード例 #1
0
 public bool Equals(Rota obj) => Origem.Equals(obj.Origem) && Destino.Equals(obj.Destino);
コード例 #2
0
        public virtual async Task Prepare(IEnumerable <Local> locals)
        {
            routeMatrix = new Dictionary <string, Dictionary <string, Rota> >();
            var bufferedLocals = locals.Buffer(5).ToArray();
            var depot          = locals.First();

            for (int b = 0; b < bufferedLocals.Length; b++)
            {
                for (int c = 0; c < bufferedLocals.Length; c++)
                {
                    var bufferOrigin = bufferedLocals[b];
                    var bufferDest   = bufferedLocals[c];

                    var     url  = GetRequestMatrixUrl(bufferOrigin, bufferDest, depot);
                    dynamic data = JsonConvert.DeserializeObject(await httpClient.GetStringAsync(url));

                    if (data.status == "OVER_QUERY_LIMIT")
                    {
                        throw new Exception("Estourou o limite diário!");
                    }


                    var rows = data.rows;
                    for (int i = 0; i < rows.Count; i++)
                    {
                        var cols     = rows[i].elements;
                        var destinos = new Dictionary <string, Rota>();
                        for (int j = 0; j < cols.Count; j++)
                        {
                            var    col      = cols[j];
                            double metros   = col.distance.value;
                            double segundos = col.duration_in_traffic.value;

                            var rotaBase = new Rota
                            {
                                Origem   = bufferOrigin[i],
                                Destino  = bufferDest[j],
                                Metros   = metros,
                                Segundos = segundos
                            };

                            destinos.Add(ParseLocal(bufferDest[j]), rotaBase);
                        }

                        var key = ParseLocal(bufferOrigin[i]);
                        if (routeMatrix.ContainsKey(key))
                        {
                            var dict = routeMatrix[key];
                            foreach (var r in destinos)
                            {
                                dict.Add(r.Key, r.Value);
                            }
                        }
                        else
                        {
                            routeMatrix.Add(key, destinos);
                        }
                    }
                }
            }
        }