Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

Is it possible to export the extents of all bookmarks in an active map document to a shapefile containing the polygons of the shape and/or to automate the print to pdf of all the bookmark extents.

The code for doing this with extents at How do I insert a DATAFRAME_ELEMENT value into a dynamically generated shape file containing its extent?

share|improve this question

marked as duplicate by PolyGeo Dec 1 '15 at 10:17

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

up vote 4 down vote accepted

Try the following:

IMapBookmarks pMapBookmarks;
ISpatialBookmark pSpatialBookmark;
IEnumSpatialBookmark pBookmarkEnum;
IAOIBookmark pAOIBookmark;

pMapBookmarks = (IMapBookmarks)pMap;
pBookmarkEnum = pMapBookmarks.Bookmarks;

pBookmarkEnum.Reset();

pSpatialBookmark = pBookmarkEnum.Next();
pAOIBookmark = (IAOIBookmark)pSpatialBookmark;

IEnvelope pEnv = new EnvelopeClass();

while (pSpatialBookmark != null)
    {
       pEnv = pAOIBookmark.Location.Envelope; //Get the envelope from the bookmark

        pSpatialBookmark = pBookmarkEnum.Next();
    }

Once you have the envelope, you can create features in the output shapefile.

Here's the complete code in C#

public partial class Form1 : Form
{
    DirectoryInfo StartDirectory;

    List<string> MXDFileList = new List<string>();


    string InitialPath = "C:\\NORMALTEMPLATE";

    DialogResult dr;

    IAoInitialize pESRILicense;
    IMapDocument pMapDoc;
    IMap pMap;
    IEnumLayer pLayers;
    ILayer pLayer;
    IWorkspaceFactory pWSF;
    IFeatureWorkspace pFeatureWS;
    IWorkspace pWS;
    IMapBookmarks pMapBookmarks;
    ISpatialBookmark pSpatialBookmark;
    IEnumSpatialBookmark pBookmarkEnum;
    IAOIBookmark pAOIBookmark;
    IEnvelope pEnv;
    IPointCollection pPointCollection;
    UID pUID = new UID();
    IEditor pEditor;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        LicenseCheckout();

        //parse directories for mxd files
        ParseDirectory(InitialPath);

        //parse each mxd file
        ParseMXDForBookmarks();

    }

    private void LicenseCheckout()
    {
        try
        {
            int ESRILicenseCheckoutReturnCode;
            bool ReturnResult;

            //checkout ESRI license: ArcEditor
            pESRILicense = new AoInitializeClass();
            ESRILicenseCheckoutReturnCode = (int)pESRILicense.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcEditor);

            ReturnResult = false;

            switch (ESRILicenseCheckoutReturnCode)
            {
                case 10:
                    //esriLicenseAvailable
                    ReturnResult = true;
                    break;

                case 20:
                    //esriLicenseNotLicensed
                    MessageBox.Show("You are not licensed to use the product!", "License Checkout Failure");
                    ReturnResult = false;
                    break;

                case 30:
                    //esriLicenseUnavailable
                    MessageBox.Show("You are not licensed to use the product!", "License Checkout Failure");
                    ReturnResult = false;
                    break;

                case 40:
                    //esriLicenseFailure
                    MessageBox.Show("There has been a licensing Failure!", "License Checkout Failure");
                    ReturnResult = false;
                    break;

                case 50:
                    //esriLicenseAlreadyInitialized
                    MessageBox.Show("The Product license has already been initialized." +
                    "Initialization can only be performed once!", "License Checkout Failure");
                    ReturnResult = true;
                    break;

                case 60:
                    //esriLicenseNotInitialized
                    MessageBox.Show("License not Initialized!");
                    ReturnResult = false;
                    break;

                case 70:
                    //esriLicenseCheckedOut
                    ReturnResult = true;
                    break;

                case 80:
                    //esriLicenseCheckedIn
                    MessageBox.Show("License ChekedIn successfully!");
                    ReturnResult = true;
                    break;
            }

            //return ReturnResult;
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            //return false;
        }
    }

    private void ParseDirectory(string StartPath)
    {
        StartDirectory = new DirectoryInfo(StartPath);

        //get files in current directory
        foreach (FileInfo files in StartDirectory.GetFiles("*.mxd"))
        {
            MXDFileList.Add(files.FullName);
        }

        //check for nested directories
        if (StartDirectory.GetDirectories().Length > 0)
        {
            foreach (DirectoryInfo directories in StartDirectory.GetDirectories())
            {
                //recursion
                ParseDirectory(directories.FullName);
            }
        }
    }

    private void ParseMXDForBookmarks()
    {
        try
        {
            IPolygon pPolygon;
            IFeature pFeature;
            IFeatureClass pFeatClass;

            //initialize workspace for shapefile
            pWSF = new ShapefileWorkspaceFactoryClass();
            pWS = pWSF.OpenFromFile("C:\\NORMALTEMPLATE", (Int32)0);

            pFeatureWS = (IFeatureWorkspace)pWS;
            pFeatClass = pFeatureWS.OpenFeatureClass("test");

            System.Object obj = new System.Object();
            obj = Type.Missing;

            for (int ListIndex = 0; ListIndex < MXDFileList.Count - 1; ListIndex++)
            {
                pMapDoc = new MapDocumentClass();
                pMapDoc.Open(MXDFileList[ListIndex], null);

                pMap = pMapDoc.get_Map(0);

                pMapBookmarks = (IMapBookmarks)pMap;

                pEnv = new EnvelopeClass();

                pBookmarkEnum = pMapBookmarks.Bookmarks;
                pBookmarkEnum.Reset();

                pSpatialBookmark = pBookmarkEnum.Next();
                pAOIBookmark = (IAOIBookmark)pSpatialBookmark;

                while (pSpatialBookmark != null)
                {
                    pEnv = pAOIBookmark.Location.Envelope; //Get the envelope from the bookmark

                    //create polygon out of this envelope
                    pPointCollection = new PolygonClass();


                    pPointCollection.AddPoint(pEnv.LowerLeft, ref obj, ref obj);
                    pPointCollection.AddPoint(pEnv.LowerRight, ref obj, ref obj);
                    pPointCollection.AddPoint(pEnv.UpperRight, ref obj, ref obj);
                    pPointCollection.AddPoint(pEnv.UpperLeft, ref obj, ref obj);

                    //close polygon
                    pPointCollection.AddPoint(pEnv.LowerLeft, ref obj, ref obj);

                    pPolygon = (IPolygon)pPointCollection;
                    pPolygon.Close();

                    pFeature = pFeatClass.CreateFeature();
                    pFeature.Shape = pPolygon;
                    pFeature.Store();

                    pSpatialBookmark = pBookmarkEnum.Next();

                    pEnv = null;
                    pPointCollection = null;
                    pPolygon = null;
                    pFeature = null;
                }


            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            MessageBox.Show("Done!");
        }
   }  
share|improve this answer
    
thanks but is this VB code? I was looking for python script to run in arc gis 10. Thanks. – GeorgeC May 18 '11 at 22:59
1  
This is C# code. I am afraid I could not see the requirement for a python script anywhere in the question. – ujjwalesri May 19 '11 at 4:33
    
sorry about that. Can it be done in python? – GeorgeC May 20 '11 at 2:19
    
I am sure it would be achievable...but I have no exposure to Python, so wont be of much help here. – ujjwalesri May 20 '11 at 4:15
1  
cool...thanks. Would you help with a compiled exe that I can run in a directory to create a shp file with each of the extents of the bookmarks in all the mxd's in a directory and subdirectories of it -then I can use python to do the rest of the work. – GeorgeC Jun 9 '11 at 1:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.