Example #1
0
        public void UpdateAerial()
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;

            if (doc == null)
            {
                return;
            }

            Editor ed = doc.Editor;

            // Select an aerial entity to update
            PromptEntityResult entityResult = ed.GetEntity("Pick an aerial to update : ");

            if (entityResult.Status == PromptStatus.OK)
            {
                // Create a new transaction to contain the update
                Transaction tx = doc.TransactionManager.StartTransaction();

                try
                {
                    // Get the aerial entity object and try to read its extension dictionary
                    RasterImage aerialEnt = tx.GetObject(entityResult.ObjectId, OpenMode.ForRead) as RasterImage;

                    if (aerialEnt.ExtensionDictionary.IsNull)
                    {
                        throw new Exception(ErrorStatus.InvalidInput, "Unrecognized aerial image.");
                    }

                    // Get the extension dictionary object
                    DBDictionary extDict = tx.GetObject(aerialEnt.ExtensionDictionary, OpenMode.ForRead) as DBDictionary;

                    if (!extDict.Contains("ImageData"))
                    {
                        throw new Exception(ErrorStatus.InvalidInput, "Unrecognized aerial image.");
                    }

                    // Get the associated image data
                    ObjectId boundsEntryId = extDict.GetAt("ImageData");
                    Xrecord  boundsRecord  = tx.GetObject(boundsEntryId, OpenMode.ForRead) as Xrecord;

                    TypedValue[] resBuff = boundsRecord.Data.AsArray();

                    GeoPoint center   = new GeoPoint((resBuff[0].Value as double?).Value, (resBuff[1].Value as double?).Value);
                    GeoPoint neCorner = new GeoPoint((resBuff[2].Value as double?).Value, (resBuff[3].Value as double?).Value);
                    GeoPoint swCorner = new GeoPoint((resBuff[4].Value as double?).Value, (resBuff[5].Value as double?).Value);

                    int zoom         = (resBuff[6].Value as int?).Value;
                    int mapTypeInt   = (resBuff[7].Value as int?).Value;
                    int mapSourceInt = (resBuff[8].Value as int?).Value;

                    MapType          mapType = (MapType)mapTypeInt;
                    AerialRepository repository;

                    if (mapSourceInt == 0)
                    {
                        repository = new BingAerialRepository();
                    }
                    else
                    {
                        repository = new GoogleMapsAerialRepository();
                    }

                    // Get the current filename using the aerial's RasterImageDef
                    ObjectId       aerialImgDefId = aerialEnt.ImageDefId;
                    RasterImageDef aerialImgDef   = tx.GetObject(aerialImgDefId, OpenMode.ForRead) as RasterImageDef;

                    string filename = aerialImgDef.SourceFileName;

                    // Get the image width and height

                    int width  = (int)aerialImgDef.Size.X;
                    int height = (int)aerialImgDef.Size.Y;

                    // Get the request details for the image downlaod
                    AerialImageData imageData = new AerialImageData(center, neCorner,
                                                                    swCorner, width, height, zoom, mapType, filename);
                    string requestUrl = repository.BuildImageRequestUrl(imageData, update: true);

                    // Delete the current entity
                    aerialImgDef.UpgradeOpen();
                    aerialImgDef.Unload(true);

                    // Download the new aerial photo to replace the old
                    AerialRepository.DownloadImage(requestUrl, filename);

                    // Reload the image entity
                    aerialImgDef.Load();

                    // Commit the transaction
                    tx.Commit();
                } catch (Exception ex)
                {
                    ed.WriteMessage("Error updating aerial: " + ex.Message + '\n');
                    ed.WriteMessage("Please try again.\n");
                } finally
                {
                    tx.Dispose();
                }
            }
        }