/// <summary>
        /// Reads the nearest water supply points to a given coordinate.
        /// </summary>
        /// <param name="coordinate">The coordinate to search for.</param>
        /// <returns>A number of water supply points ordered by distance.</returns>
        /// <exception>Throws any exception that cann occur during downloading or processing the data.</exception>
        public List <WaterSupplyPoint> FindWaterSupplyPoints(Coordinate coordinate)
        {
            List <WaterSupplyPoint> waterSupplyPoints;
            string url = CreateUrl(coordinate.Latitude, coordinate.Longitude);

            using (var stream = Network.DownloadStreamData(url, Constants.NetworkTimeouts))
            {
                waterSupplyPoints = mUseWasserkarteInfo ? DeserializeWasserkarteInfoData(stream) : DeserializeCustomData(stream);
            }
            return(waterSupplyPoints.Where(x => x.Distance <= SearchRadius).OrderBy(x => x.Distance).ToList());
        }
        /// <summary>
        /// Downloads a JSON object and returns the token specified by the path if it is set.
        /// </summary>
        /// <param name="url">The URL to download the JSON data.</param>
        /// <param name="path">The path the selects the JSON token. If null, the whole JSON object is returned.</param>
        /// <param name="timeout">The timeout for the web request.</param>
        /// <returns>The specified JSON object.</returns>
        private static JToken GetJsonObject(string url, string path = null, int timeout = 3000)
        {
            var    stream  = Network.DownloadStreamData(url, timeout);
            var    json    = Utilities.StreamToString(stream, Encoding.UTF8);
            JToken jObject = JObject.Parse(json);

            if (path != null)
            {
                jObject = jObject.SelectToken(path);
            }
            return(jObject);
        }
Exemple #3
0
        /// <summary>
        /// Tries to download a (water) map for the provided location.
        /// </summary>
        /// <param name="waterSupplyPoints">A list of water supply points ordered by distance to the start point.</param>
        /// <param name="showDetailMap">If true it shows only the coordinate at zoom factor 15.</param>
        /// <returns>A stream containing the image, null if downloading failed.</returns>
        private Image CreateMapImage(IList <WaterSupplyPoint> waterSupplyPoints = null, bool showDetailMap = false)
        {
            // Create Google Static Maps url
            int    imageWidth  = showDetailMap ? 500 : 640;
            int    imageHeight = showDetailMap ? 640 : 560;
            string url         = Constants.GoogleStaticMapsUrl + "?sensor=false&scale=2&size=" + imageWidth + "x" + imageHeight;

            // Add the map type
            GoogleMapType mapType;

            if (waterSupplyPoints == null && !showDetailMap)
            {
                mapType = AicSettings.Global.RouteMapType;
            }
            else if (waterSupplyPoints != null)
            {
                mapType = AicSettings.Global.WaterMapType;
            }
            else
            {
                mapType = AicSettings.Global.DetailMapType;
            }
            url += "&maptype=" + MapHelper.GetMapTypeString(mapType);

            // Add the route path
            string path = MapHelper.GetPathString(mFireBrigadeCoordinate, mCoordinate);

            if (!string.IsNullOrWhiteSpace(path))
            {
                //path = fireBrigadeCoordinate + "|" + mCoordinate.ToQueryString();
                url += "&path=enc:" + path;
            }

            // Add the fire brigade marker
            string fireBrigadeCoordinate = mFireBrigadeCoordinate.ToQueryString();

            url += "&markers=icon:" + Constants.FireBrigadeMarkerUrl + "|" + fireBrigadeCoordinate;

            // Add the alarm location marker
            url += "&markers=icon:" + Constants.AlarmLocationMarkerUrl + "|" + mCoordinate.ToQueryString();

            if (showDetailMap)
            {
                url += "&zoom=15&center=" + mCoordinate.ToQueryString();
            }
            else if (waterSupplyPoints != null && waterSupplyPoints.Count > 0)
            {
                var coordinates = waterSupplyPoints.Select(x => x.Coordinate).ToList();
                coordinates.Add(mCoordinate);

                // Calculate a suitable zoom level
                int zoom = MapHelper.CalculateZoom(coordinates, imageWidth, imageHeight);
                if (zoom > 16)
                {
                    zoom = 16;
                }
                else if (zoom < 14)
                {
                    zoom = 14;
                }
                var center = MapHelper.CalculateCenter(coordinates);

                url += "&zoom=" + zoom + "&center=" + center.ToQueryString();
                for (int i = 0; i < waterSupplyPoints.Count; i++)
                {
                    var point = waterSupplyPoints[i];
                    url += "&markers=color:blue|label:" + (i + 1) + "|" + point.Coordinate.ToQueryString();
                }
            }



            // Try to download the image from Google Maps using defined web timeouts
            MemoryStream ms = null;

            try
            {
                ms = Network.DownloadStreamData(url, Constants.NetworkTimeouts);
            }
            catch (WebException exc)
            {
                string timeout = "-1";
                if (exc.Data.Contains("Timeout"))
                {
                    timeout = exc.Data["Timeout"].ToString();
                }
                Log.GetInstance().LogError("Error in downloading image after " + timeout + " milliseconds.", exc);
            }
            catch (Exception exc)
            {
                Log.GetInstance().LogError("General error in downloading image. ", exc);
            }

            return(CreateImage(ms));
        }