コード例 #1
0
        private static string GetRouteAsStoredFileCore(PropertyLocation source, PropertyLocation destination)
        {
            int width  = 800;
            int height = 800;

            // https://developers.google.com/maps/documentation/directions/?hl=de

            string originText      = source.ToString();
            string destinationText = destination.ToString();

            StringBuilder sbInitialRequest = new StringBuilder();

            sbInitialRequest.Append("http://maps.google.com/maps/api/directions/xml?");
            sbInitialRequest.AppendFormat("origin={0}", HttpUtility.UrlEncode(originText));
            sbInitialRequest.AppendFormat("&destination={0}", HttpUtility.UrlEncode(destinationText));
            sbInitialRequest.Append("&sensor=false");

            WebRequest wreqInitial = WebRequest.Create(sbInitialRequest.ToString());
            XDocument  docResponse = null;

            using (WebResponse wresInitial = wreqInitial.GetResponse())
            {
                using (Stream stream = wresInitial.GetResponseStream())
                {
                    docResponse = XDocument.Load(stream);
                }

                string status = docResponse.Root.Element("status").Value;
                if (status != "OK")
                {
                    Logger.Instance.LogFormat(LogType.Error, typeof(RoutePlanHelper), Properties.Resources.MapsRequestFailed, status);
                    return(null);
                }
            }

            XElement overviewE = docResponse.Root.Element("route").Element("overview_polyline").Element("points");

            StringBuilder sbContinuationRequest = new StringBuilder();

            sbContinuationRequest.Append("http://maps.google.com/maps/api/staticmap?");
            sbContinuationRequest.AppendFormat("size={0}x{1}", width, height);
            sbContinuationRequest.AppendFormat("&sensor=false&path=weight:3|color:red|enc:{0}", overviewE.Value);

            WebRequest wr1 = WebRequest.Create(sbContinuationRequest.ToString());

            using (WebResponse res1 = wr1.GetResponse())
            {
                using (Image image = Image.FromStream(res1.GetResponseStream()))
                {
                    using (FileStream tempFile = File.OpenWrite(Path.GetTempFileName()))
                    {
                        image.Save(tempFile, ImageFormat.Png);
                        return(tempFile.Name);
                    }
                }
            }
        }
コード例 #2
0
        GeocoderLocation IGeoCoder.GeoCode(PropertyLocation address)
        {
            string queryAdress = string.Format(((IGeoCoder)this).UrlPattern, ((IGeoCoder)this).ApiKey, HttpUtility.UrlEncode(address.ToString()));

            WebRequest request = WebRequest.Create(queryAdress);

            using (WebResponse response = request.GetResponse())
            {
                using (Stream stream = response.GetResponseStream())
                {
                    XDocument document = XDocument.Load(new StreamReader(stream));

                    XElement longitudeElement = document.Descendants("lng").FirstOrDefault();
                    XElement latitudeElement  = document.Descendants("lat").FirstOrDefault();

                    if (longitudeElement != null && latitudeElement != null)
                    {
                        return(new GeocoderLocation
                        {
                            Longitude = Double.Parse(longitudeElement.Value, CultureInfo.InvariantCulture),
                            Latitude = Double.Parse(latitudeElement.Value, CultureInfo.InvariantCulture)
                        });
                    }
                }
            }

            return(null);
        }
コード例 #3
0
        private static string GetRouteAsStoredFileCore(PropertyLocation source, PropertyLocation destination)
        {
            int width  = 800;
            int height = 800;

            // https://developers.google.com/maps/documentation/directions/?hl=de

            string originText      = source.ToString();
            string destinationText = destination.ToString();

            // Create initial request
            StringBuilder sbInitialRequest = new StringBuilder();

            sbInitialRequest.Append("http://maps.google.com/maps/api/directions/xml?");
            sbInitialRequest.AppendFormat("origin={0}", originText);
            sbInitialRequest.AppendFormat("&destination={0}", destinationText);
            sbInitialRequest.Append("&sensor=false");

            WebRequest wreqInitial = WebRequest.Create(sbInitialRequest.ToString());
            XDocument  docResponse = null;

            using (WebResponse wresInitial = wreqInitial.GetResponse())
            {
                docResponse = XDocument.Load(wresInitial.GetResponseStream());

                // Load the response XML
                string status = docResponse.Root.Element("status").Value;
                switch (status)
                {
                // TODO: Handle the errors!
                case "NOT_FOUND":
                case "ZERO_RESULTS":
                    Logger.Instance.LogFormat(LogType.Warning, typeof(RoutePlanHelper), "The maps-request failed with status '{0}'. This is an indication that the location could not be retrieved. Sorry, but there's no workaround.", status);
                    return(null);

                case "OVER_QUERY_LIMIT":
                    Logger.Instance.LogFormat(LogType.Error, typeof(RoutePlanHelper), "The maps-request failed with status 'OVER_QUERY_LIMIT'. This indicates too many queries within a short timeframe.");
                    return(null);

                case "MAX_WAYPOINTS_EXCEEDED":
                case "INVALID_REQUEST":
                case "REQUEST_DENIED":
                case "UNKNOWN_ERROR":
                default:
                    Logger.Instance.LogFormat(LogType.Error, typeof(RoutePlanHelper), "The maps-request failed with status '{0}'. Please contact the developers!", status);
                    return(null);

                case "OK":
                    // Everything ok.
                    break;
                }
            }

            // Get the path data
            XElement overviewE = docResponse.Root.Element("route").Element("overview_polyline").Element("points");

            StringBuilder sbContinuationRequest = new StringBuilder();

            sbContinuationRequest.Append("http://maps.google.com/maps/api/staticmap?");
            sbContinuationRequest.AppendFormat("size={0}x{1}", width, height);
            // TODO: Maybe allow configuring the thickness and color, especially for b/w-printers?
            sbContinuationRequest.AppendFormat("&sensor=false&path=weight:3|color:red|enc:{0}", overviewE.Value);

            WebRequest wr1 = WebRequest.Create(sbContinuationRequest.ToString());

            using (WebResponse res1 = wr1.GetResponse())
            {
                using (Image image = Image.FromStream(res1.GetResponseStream()))
                {
                    using (FileStream tempFile = File.OpenWrite(Path.GetTempFileName()))
                    {
                        image.Save(tempFile, ImageFormat.Png);
                        return(tempFile.Name);
                    }
                }
            }
        }
コード例 #4
0
ファイル: Google.cs プロジェクト: pawan9/AlarmWorkflow
        GeocoderLocation IGeoCoder.Geocode(PropertyLocation address)
        {
            string queryAddress = string.Format(((IGeoCoder)this).UrlPattern, HttpUtility.UrlEncode(address.ToString()));

            if (!string.IsNullOrWhiteSpace(address.ZipCode))
            {
                queryAddress = string.Format("{0}&components=postal_code:{1}|country:DE", queryAddress, address.ZipCode);
            }
            WebRequest request = WebRequest.Create(queryAddress);

            using (WebResponse response = request.GetResponse())
            {
                using (Stream stream = response.GetResponseStream())
                {
                    XDocument document = XDocument.Load(stream);

                    XElement longitudeElement = document.Descendants("lng").FirstOrDefault();
                    XElement latitudeElement  = document.Descendants("lat").FirstOrDefault();

                    if (longitudeElement != null && latitudeElement != null)
                    {
                        return(new GeocoderLocation()
                        {
                            Longitude = double.Parse(longitudeElement.Value, CultureInfo.InvariantCulture),
                            Latitude = double.Parse(latitudeElement.Value, CultureInfo.InvariantCulture)
                        });
                    }
                }
            }

            return(null);
        }
コード例 #5
0
        /// <summary>
        /// Downloads the route planning info if it is enabled and the location datas are meaningful enough.
        /// </summary>
        /// <param name="operation"></param>
        private void DownloadRoutePlan(Operation operation)
        {
            if (!AlarmWorkflowConfiguration.Instance.DownloadRoutePlan)
            {
                return;
            }

            // Get start address and check if it is meaningful enough (if not then bail out)
            PropertyLocation source = AlarmWorkflowConfiguration.Instance.FDInformation.Location;

            if (!source.IsMeaningful)
            {
                Logger.Instance.LogFormat(LogType.Warning, this, "Cannot download route plan because the location information for this fire department is not meaningful enough: '{0}'. Please fill the correct address!", source);
                return;
            }

            // Get destination address and check if it is meaningful enough (if not then bail out)
            PropertyLocation destination = operation.GetDestinationLocation();

            if (!operation.GetDestinationLocation().IsMeaningful)
            {
                Logger.Instance.LogFormat(LogType.Warning, this, "Destination location is unknown! Cannot download route plan!");
            }
            else
            {
                Logger.Instance.LogFormat(LogType.Trace, this, "Downloading route plan to destination '{0}'...", destination.ToString());

                Stopwatch sw = Stopwatch.StartNew();
                try
                {
                    Image image = _routePlanProvider.GetRouteImage(source, destination);

                    if (image != null)
                    {
                        // Save the image as PNG
                        using (MemoryStream ms = new MemoryStream())
                        {
                            image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);

                            operation.RouteImage = ms.ToArray();
                        }
                    }

                    sw.Stop();

                    if (operation.RouteImage == null)
                    {
                        Logger.Instance.LogFormat(LogType.Warning, this, "The download of the route plan did not succeed. Please check the log for information!");
                    }
                    else
                    {
                        Logger.Instance.LogFormat(LogType.Trace, this, "Downloaded route plan in '{0}' milliseconds.", sw.ElapsedMilliseconds);
                    }
                }
                catch (Exception ex)
                {
                    sw.Stop();
                    Logger.Instance.LogFormat(LogType.Error, this, "An error occurred while trying to download the route plan! The image will not be available.");
                    Logger.Instance.LogException(this, ex);
                }
            }
        }
コード例 #6
0
        private byte[] DownloadRoutePlan(Operation operation)
        {
            PropertyLocation source = GetFDLocation();

            if (!source.IsMeaningful)
            {
                Logger.Instance.LogFormat(LogType.Warning, this, Resources.RoutePlanningSourceLocationNotMeaningful, source);
                return(null);
            }

            PropertyLocation destination = operation.GetDestinationLocation();

            if (!operation.GetDestinationLocation().IsMeaningful)
            {
                Logger.Instance.LogFormat(LogType.Warning, this, Resources.DestinationLocationIsUnknown);
                return(null);
            }

            Logger.Instance.LogFormat(LogType.Trace, this, Resources.DownloadRoutePlanBegin, destination.ToString());

            Stopwatch sw = Stopwatch.StartNew();

            try
            {
                byte[] imageBuffer = GoogleMapsProvider.GetRouteImage(source, destination);

                sw.Stop();

                if (imageBuffer == null)
                {
                    Logger.Instance.LogFormat(LogType.Warning, this, Resources.DownloadRoutePlanFailed);
                }
                else
                {
                    Logger.Instance.LogFormat(LogType.Trace, this, Resources.DownloadRoutePlanSuccess, sw.ElapsedMilliseconds);
                }

                return(imageBuffer);
            }
            catch (Exception ex)
            {
                sw.Stop();
                Logger.Instance.LogFormat(LogType.Error, this, Resources.DownloadRoutePlanError);
                Logger.Instance.LogException(this, ex);
            }

            return(null);
        }