Ejemplo n.º 1
0
        /// <summary>
        /// Create the document for printing.
        /// </summary>
        public FlowDocument CreatePrintout()
        {
            Image locationMap = null;
            Image waterMap    = null;
            Image detailMap   = null;
            List <WaterSupplyPoint> waterSupplyPoints = null;

            #region Download all the data for the printing

            try
            {
                // Check if the alarm location is valid (it must not be the same as the center of the city)
                var  postalCodeAndCity  = MapHelper.GetPostalCodeAndCity(AicSettings.Global.FireBrigadeName);
                bool destinationIsValid = MapHelper.IsDestinationValid(mAlarm.MapLocation, postalCodeAndCity.Item1, postalCodeAndCity.Item2);

                if (destinationIsValid)
                {
                    mCoordinate = MapHelper.Geocode(mAlarm.MapLocation);                     // Try to get the coordinate of the alarm location

                    if (mCoordinate != null || mFireBrigadeCoordinate == null)
                    {
                        try
                        {
                            // Location map
                            locationMap = CreateMapImage();
                        }
                        catch (Exception ex)
                        {
                            Log.GetInstance().LogError("Error in downloading location map data.", ex);
                        }

                        // Water/Detail map
                        try
                        {
                            // Show the water map if water supply points are found and the nearest one is not more than 2000 m away
                            // otherwise a detail map is shown
                            if (AicSettings.Global.WaterMapMode != 0)
                            {
                                bool useWassserkarteInfo = AicSettings.Global.WaterMapMode == 1;
                                var  waterMapQuery       = new WaterMapQuery(useWassserkarteInfo, AicSettings.Global.WasserkarteInfoToken, AicSettings.Global.WaterMapApiUrl);
                                waterSupplyPoints = waterMapQuery.FindWaterSupplyPoints(mCoordinate);
                                if (waterSupplyPoints.Count > 0)
                                {
                                    waterMap = CreateMapImage(waterSupplyPoints);
                                }
                            }

                            if (waterMap == null)
                            {
                                detailMap = CreateMapImage(null, true);
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.GetInstance().LogError("Error in downloading water map data.", ex);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.GetInstance().LogError("Error in downloading map data.", ex);
            }

            #endregion

            #region Document

            mDocument.PagePadding = new Thickness(2 * 96 / 2.54);                       // 2 cm
            mDocument.FontFamily  = new FontFamily("Arial");
            mDocument.PageWidth   = 8.27 * 96;
            mDocument.PageHeight  = 11.69 * 96;
            mDocument.ColumnWidth = mDocument.PageWidth;
            mDocument.LineHeight  = 10;

            #endregion

            #region Title

            AddParagraph("Alarmausdruck - FF " + AicSettings.Global.FireBrigadeName, 32, true, false, TextAlignment.Center);
            mDocument.Blocks.Add(new Paragraph(new LineBreak {
                FontSize = 2
            }));

            #endregion

            #region Subject, location proposition, location, and additional information

            const int infoFontSize = 24;
            AddParagraph(mAlarm.Subject, infoFontSize);
            if (!string.IsNullOrWhiteSpace(mAlarm.LocationProposition))
            {
                AddParagraph(mAlarm.LocationProposition, infoFontSize, false, true);
            }
            else
            {
                AddParagraph(mAlarm.Location, infoFontSize);
            }
            AddParagraph(mAlarm.AdditionalInformation, infoFontSize);

            #endregion

            AddHorizontalLine();

            #region Info table

            TableColumn col1 = new TableColumn();
            TableColumn col2 = new TableColumn();
            col1.Width = new GridLength(220);
            col2.Width = GridLength.Auto;
            Table table = new Table();
            table.Columns.Add(col1);
            table.Columns.Add(col2);
            table.FontSize = 16;

            string fireBrigades = "";
            if (mAlarm.FireBrigades.Count > 0)
            {
                fireBrigades += mAlarm.FireBrigades.Aggregate((x, y) => x + ", " + y);
            }
            table.RowGroups.Add(new TableRowGroup());
            table.RowGroups[0].Rows.Add(new TableRow());
            table.RowGroups[0].Rows.Add(new TableRow());
            table.RowGroups[0].Rows.Add(new TableRow());
            table.RowGroups[0].Rows[0].Cells.Add(new TableCell(new Paragraph(new Run(mAlarm.StartTime.ToShortDateString() + ", " + mAlarm.StartTime.ToLongTimeString() + " Uhr"))));
            table.RowGroups[0].Rows[0].Cells.Add(new TableCell(new Paragraph(new Run(mAlarm.Caller))));
            table.RowGroups[0].Rows[1].Cells.Add(new TableCell(new Paragraph(new Run(mAlarm.AlarmStation + " (Alarmstufe " + mAlarm.AlarmLevel + ")"))));
            table.RowGroups[0].Rows[1].Cells.Add(new TableCell(new Paragraph(new Run("Sirenenprogramm: " + mAlarm.SirenProgram))));
            var cell = new TableCell(new Paragraph(new Run(fireBrigades)));
            cell.ColumnSpan = 2;
            table.RowGroups[0].Rows[2].Cells.Add(cell);
            mDocument.Blocks.Add(table);

            #endregion

            #region Location map

            if (locationMap != null)
            {
                AddHorizontalLine();                 // Horizontal line
                try
                {
                    mDocument.Blocks.Add(new BlockUIContainer(locationMap)
                    {
                        Padding = new Thickness(0, 20, 0, 0)
                    });
                }
                catch (Exception ex)
                {
                    Log.GetInstance().LogError("Error in creating the image for the printout.", ex);
                }
            }

            #endregion

            #region Water map

            if (waterMap != null)
            {
                Table waterTable = CreateWaterSupplyPointTable(waterSupplyPoints);
                AddMapPage("Wasserkarte - " + mAlarm.MapLocation, waterMap, waterTable);
            }

            #endregion

            #region Detail map

            if (detailMap != null)
            {
                AddMapPage("Detailkarte - " + mAlarm.MapLocation, detailMap);
            }

            #endregion

            #region Footer

            //TableColumn footerCol1 = new TableColumn();
            //TableColumn footerCol2 = new TableColumn();
            //Table footerTable = new Table();
            //footerTable.Columns.Add(footerCol1);
            //footerTable.Columns.Add(footerCol2);
            //footerTable.FontSize = 12;
            //footerTable.RowGroups.Add(new TableRowGroup());
            //footerTable.RowGroups[0].Rows.Add(new TableRow());
            //footerTable.RowGroups[0].Rows[0].Cells.Add(new TableCell((new Paragraph(new Run(alarm.Id)))));
            //footerTable.RowGroups[0].Rows[0].Cells.Add(new TableCell((new Paragraph(new Run("AIC FF Marchtrenk")) { TextAlignment = TextAlignment.Right})));
            //doc.Blocks.Add(footerTable);

            #endregion

            // Create XPS, just for testing
            //string filename = @"C:\Users\Thomas\Downloads\" + alarm.Id + ".xps";
            //XpsDocument xpsDoc = new XpsDocument(filename, FileAccess.Write);
            //XpsDocumentWriter xpsWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
            //xpsWriter.Write((doc as IDocumentPaginatorSource).DocumentPaginator);
            //xpsDoc.Close();

            return(mDocument);
        }
Ejemplo n.º 2
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));
        }