Exemple #1
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            JObject   jo        = JObject.Load(reader);
            OSMResult osmresult = new OSMResult();

            osmresult.version   = (string)jo["version"];
            osmresult.generator = (string)jo["generator"];
            osm3s osm3s = jo["osm3s"].ToObject <osm3s>();

            osmresult.nodes = new List <node>();
            osmresult.ways  = new List <way>();
            foreach (JObject obj in jo["elements"])
            {
                String type = (string)obj["type"];
                switch (type)
                {
                case "node":
                    osmresult.nodes.Add(obj.ToObject <node>());
                    break;

                case "way":
                    osmresult.ways.Add(obj.ToObject <way>());
                    break;

                default:
                    break;
                }
            }
            return(osmresult);
        }
Exemple #2
0
        public async Task <List <AmenityDTO> > getAmenities(Bound region, AmenityType type)
        {
            List <AmenityDTO>    ret = new List <AmenityDTO>();
            OverpassQueryBuilder oqb = new OverpassQueryBuilder(region);
            var sampleQuery          = "";

            //Caso de excessão para pontos de ônibus
            if (type.Equals(AmenityType.bus_station))
            {
                sampleQuery = oqb.busStopsOverpassQuery();
            }
            else
            {
                sampleQuery = oqb.amenityOverpassQuery(type);
            }
            try
            {
                string encodedURL = overpassUrl + HttpUtility.UrlEncode(sampleQuery);
                using (HttpClient client = new HttpClient())
                {
                    var responseString = await client.GetStringAsync(encodedURL);

                    OSMResult osm = JsonConvert.DeserializeObject <OSMResult>(responseString);
                    if (osm.GetNodesAsDictionary != null && osm.GetNodesAsDictionary.Count() > 0)
                    {
                        var nodes = osm.GetNodesAsDictionary;
                        foreach (var node in nodes)
                        {
                            ret.Add(new AmenityDTO()
                            {
                                lat  = (float)node.Value.lat,
                                lng  = (float)node.Value.lon,
                                name = node.Value.tags.ContainsKey("name") ? node.Value.tags["name"] : null
                            });
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return(ret);
        }
Exemple #3
0
        public async Task <List <StreetDTO> > getStreets(Bound region)
        {
            List <StreetDTO>     ruasDetectadas = new List <StreetDTO>();
            OverpassQueryBuilder oqb            = new OverpassQueryBuilder(region);
            var sampleQuery = oqb.highwayOverpassQuery();

            try
            {
                string encodedURL = overpassUrl + HttpUtility.UrlEncode(sampleQuery);
                using (HttpClient client = new HttpClient())
                {
                    var responseString = await client.GetStringAsync(encodedURL);

                    OSMResult osm = JsonConvert.DeserializeObject <OSMResult>(responseString);
                    if (osm.GetWaysAsDictionary != null && osm.GetWaysAsDictionary.Count() > 0)
                    {
                        foreach (var way in osm.GetWaysAsDictionary)
                        {
                            if (!way.Value.tags.ContainsKey("name") || String.IsNullOrWhiteSpace(way.Value.tags["name"]))
                            {
                                continue;
                            }
                            var name         = way.Value.tags["name"];
                            var ruaExistente = ruasDetectadas.FirstOrDefault(p => p.Name.Equals(name));
                            var novoTrecho   = new List <PointDTO>();
                            if (ruaExistente != null)
                            {
                                foreach (var idNode in way.Value.nodes)
                                {
                                    var ponto = osm.GetNodesAsDictionary[idNode];
                                    novoTrecho.Add(new PointDTO()
                                    {
                                        ID = idNode.ToString(), lat = (float)ponto.lat, lng = (float)ponto.lon
                                    });
                                }
                                ruaExistente.Trechos.Add(novoTrecho);
                                //ruaExistente.Trechos = ruaExistente.Trechos.Distinct(new PointComparer()).ToList();
                                continue;
                            }

                            StreetDTO rua = new StreetDTO();
                            rua.Name = name;
                            foreach (var idNode in way.Value.nodes)
                            {
                                var ponto = osm.GetNodesAsDictionary[idNode];
                                novoTrecho.Add(new PointDTO()
                                {
                                    ID = idNode.ToString(), lat = (float)ponto.lat, lng = (float)ponto.lon
                                });
                            }
                            rua.Trechos.Add(novoTrecho);
                            //rua.Trechos = rua.Trechos.Distinct(new PointComparer()).ToList();
                            ruasDetectadas.Add(rua);
                        }
                    }



                    return(ruasDetectadas);
                }
            }
            catch (Exception ex)
            {
                return(new List <StreetDTO>()
                {
                    new StreetDTO()
                    {
                        Name = "Erro! " + ex.Message
                    }
                });
            }
        }