Exemplo n.º 1
0
        private static void GenerateKmlFile(KmlThreadArg arg, string kmlFolder, List <GroundOverlayType> fileOverlays, int kmlfileCount)
        {
            FolderType folder = new FolderType();

            folder.name = String.Format("{0}_{1}", arg.KmlName, kmlfileCount);

            folder.Items1 = fileOverlays.ToArray();

            KmlType kml = new KmlType();

            kml.Item = folder;

            string fileNameKmz = String.Format("{0}\\{1}{2}.kml", kmlFolder, arg.KmlName, kmlfileCount);

            using (FileStream fs = File.Create(fileNameKmz))
            {
                XmlSerializer ser = new XmlSerializer(typeof(KmlType));

                ser.Serialize(fs, kml);
            }
        }
Exemplo n.º 2
0
        void m_kmlWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            KmlThreadArg arg = e.Argument as KmlThreadArg;

            e.Result = false;

            MultiDictionary <string, ImageInfo> filesGrouped4x4 = new MultiDictionary <string, ImageInfo>(false);

            {
                List <ImageInfo> allFiles = new List <ImageInfo>();
                List <string>    rows     = new List <string>();
                List <string>    columns  = new List <string>();

                Regex filePattern = new Regex(@"Img_(?<Row>\d+)_(?<Column>\d+)_(?<Zoom>\d+)\.*", RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.Singleline);

                foreach (string item in Directory.GetFiles(arg.SelectedPath, "*.*", SearchOption.TopDirectoryOnly))
                {
                    Match match = filePattern.Match(item);
                    if (match.Success)
                    {
                        string row    = match.Groups["Row"].Value;
                        string column = match.Groups["Column"].Value;

                        allFiles.Add(new ImageInfo(item, row, column, match.Groups["Zoom"].Value));

                        if (!rows.Contains(row))
                        {
                            rows.Add(row);
                        }

                        if (!columns.Contains(column))
                        {
                            columns.Add(column);
                        }
                    }
                }

                rows.Sort();
                columns.Sort();

                int totalRowRun    = 0;
                int totalColumnRun = 0;

                if (rows.Count > 0)
                {
                    totalRowRun = (int)rows.Count / 4;
                    if (rows.Count % 4 > 0)
                    {
                        totalRowRun++;
                    }
                }

                if (columns.Count > 0)
                {
                    totalColumnRun = (int)columns.Count / 4;
                    if (columns.Count % 4 > 0)
                    {
                        totalColumnRun++;
                    }
                }

                string rowKey;
                string columnKey;

                string _4x4Key;

                for (int indexRow = 0; indexRow < totalRowRun; indexRow++)
                {
                    int    actualStartRow = indexRow * 4;
                    string rowStartKey    = rows[actualStartRow];

                    for (int indexX = actualStartRow; indexX < rows.Count && indexX < actualStartRow + 4; indexX++)
                    {
                        rowKey = rows[indexX];

                        for (int indexColumn = 0; indexColumn < totalColumnRun; indexColumn++)
                        {
                            int    actualStartColumn = indexColumn * 4;
                            string startColumnKey    = columns[actualStartColumn];
                            _4x4Key = String.Format("{0}x{1}", rowStartKey, startColumnKey);


                            for (int indexY = actualStartColumn; indexY < columns.Count && indexY < actualStartColumn + 4; indexY++)
                            {
                                columnKey = columns[indexY];
                                ImageInfo data = allFiles.GetData <ImageInfo>(rowKey, columnKey);

                                filesGrouped4x4.Add(_4x4Key, data);
                            }
                        }
                    }
                }
            }
            {
                string kmlFolder = String.Format("{0}\\{1}\\", arg.SelectedPath, arg.KmlName);

                if (Directory.Exists(kmlFolder))
                {
                    System.Windows.MessageBox.Show("Folder already exits, select diffrent folder");
                    return;
                }

                Directory.CreateDirectory(kmlFolder);

                ImageInfo imageInfo;

                TileDownLoader.Projection.MercatorProjection projections = new TileDownLoader.Projection.MercatorProjection();

                List <GroundOverlayType> overlays = new List <GroundOverlayType>();
                int index = 0;
                foreach (string imgkey in filesGrouped4x4.Keys)
                {
                    if (this.KmlWorker.CancellationPending)
                    {
                        return;
                    }

                    this.KmlWorker.ReportProgress((int)(((double)index / (double)filesGrouped4x4.Keys.Count) * 100.0));

                    imageInfo = filesGrouped4x4[imgkey].First <ImageInfo>();

                    string fileName = String.Format("{0}\\Img_{1}_{2}_{3}.jpeg", kmlFolder, imageInfo.Row, imageInfo.Column, imageInfo.Zoom);

                    MultiDictionary <string, ImageInfo> tempData = new MultiDictionary <string, ImageInfo>(false);

                    foreach (ImageInfo item in filesGrouped4x4[imgkey])
                    {
                        tempData.Add(item.Row, item);
                    }

                    int rowCount    = tempData.Keys.Count;
                    int columnCount = tempData[tempData.Keys.First <string>()].Count;
                    int imageWidth  = rowCount * 256;
                    int imageHeight = columnCount * 256;

                    TileDownLoader.Projection.Point imgPt = new TileDownLoader.Projection.Point(int.Parse(imageInfo.Row), int.Parse(imageInfo.Column));

                    TileDownLoader.Projection.Point imgXYInVirtualSingleImageStPoint = projections.FromTileXYToPixel(imgPt);

                    TileDownLoader.Projection.Point imgXYInVirtualSingleImageEtPoint = new TileDownLoader.Projection.Point(imgXYInVirtualSingleImageStPoint.X + imageWidth,
                                                                                                                           imgXYInVirtualSingleImageStPoint.Y + imageHeight);

                    TileDownLoader.Projection.Location latlng1 = projections.FromPixelToLatLng(imgXYInVirtualSingleImageStPoint, int.Parse(imageInfo.Zoom));
                    TileDownLoader.Projection.Location latlng2 = projections.FromPixelToLatLng(imgXYInVirtualSingleImageEtPoint, int.Parse(imageInfo.Zoom));


                    GroundOverlayType gndOverlay = new GroundOverlayType();

                    gndOverlay.LatLonBox = new LatLonBoxType();

                    gndOverlay.LatLonBox.north = latlng1.Latitude;
                    gndOverlay.LatLonBox.south = latlng2.Latitude;
                    gndOverlay.LatLonBox.east  = latlng2.Longitude;
                    gndOverlay.LatLonBox.west  = latlng1.Longitude;

                    gndOverlay.name      = System.IO.Path.GetFileNameWithoutExtension(fileName);
                    gndOverlay.color     = new byte[] { 0xff, 0xff, 0xff, 0xff };
                    gndOverlay.Icon      = new LinkType();
                    gndOverlay.Icon.href = System.IO.Path.GetFileName(fileName);

                    System.Drawing.Bitmap stichedBmp = new System.Drawing.Bitmap(imageWidth, imageHeight);

                    System.Drawing.Graphics grp = System.Drawing.Graphics.FromImage(stichedBmp);
                    int           rowLoop       = 0;
                    int           columnLoop    = 0;
                    List <string> keysTemp      = new List <string>(tempData.Keys);
                    keysTemp.Sort();
                    foreach (string key in keysTemp)
                    {
                        int x = rowLoop * 256;
                        columnLoop = 0;
                        foreach (ImageInfo imgData in tempData[key])
                        {
                            int y = columnLoop * 256;

                            grp.DrawImage(new System.Drawing.Bitmap(imgData.Path), new System.Drawing.Point(x, y));
                            columnLoop++;
                        }
                        rowLoop++;
                    }

                    grp.Dispose();

                    // EncoderParameter object in the array.

                    System.Drawing.Imaging.ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/jpeg");

                    System.Drawing.Imaging.EncoderParameters myEncoderParameters = new System.Drawing.Imaging.EncoderParameters(1);

                    System.Drawing.Imaging.EncoderParameter myEncoderParameter = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
                    myEncoderParameters.Param[0] = myEncoderParameter;

                    stichedBmp.Save(fileName, myImageCodecInfo, myEncoderParameters);

                    stichedBmp.Dispose();

                    overlays.Add(gndOverlay);

                    index++;
                }

                List <List <GroundOverlayType> > splitedOverlays = new List <List <GroundOverlayType> >();

                for (int localIndex = 0; localIndex < overlays.Count; localIndex += 100)
                {
                    int lastData = (localIndex + 100) < overlays.Count ? localIndex + 100 : overlays.Count;

                    List <GroundOverlayType> tempOverlays = new List <GroundOverlayType>();

                    for (int subIndex = localIndex; subIndex < lastData; subIndex++)
                    {
                        tempOverlays.Add(overlays[subIndex]);
                    }

                    splitedOverlays.Add(tempOverlays);
                }

                int fileCount = 1;

                foreach (List <GroundOverlayType> item in splitedOverlays)
                {
                    GenerateKmlFile(arg, kmlFolder, item, fileCount);
                    fileCount++;
                }
            }

            this.KmlWorker.ReportProgress(100);

            e.Result = true;
        }