Launch External Script in ArcGIS Desktop Explorer

24 Jan

arcgis_explorer_15001

I have started to toy with the ArcGIS Desktop Explorer API. My first few attempts at functionality required only the use of C# Libraries and not the actual SDK Library – with the exception of getting the button in.

If you have the SDK installed, you can start a new project in Visual Studio Express and select the GIS Button. This first example will allow you to execute a script or program on your machine. Where it tells you to insert your code, enter the following line:

 System.Diagnostics.Process.Start(“myPythonFile.py”);

This will run the python file. You could also use launch system applications like calc or notepad.

My next test was to launch a website from a button.  Same as the above, except enter the URL of the site. This will open the page in your default browser.

System.Diagnostics.Process.Start(“http://google.com”);

One thing I had trouble with was how do I add several buttons in a single Namespace? I copied the button that was created by default and pasted it in to the namespace. Then you need to change the name and the functionality. Lastly, you need to edit the AddIns.xml file. Copy the first button and paste it. Then modify the values to math your new button. I have put the values that MUST change in bold. You should change the captions too. Also, drag any images to the images folder. 32×32 for large and 16×16 for small.

<button
id=”Button1
assembly=”WebLaunchButtons.dll”
className=”WebLaunchButtons.Button1
caption=”Google”
tooltip=”Quick Access to Google.”
helpString=”http://mydomain.com&#8221;
largeImage=”Images\google.png”
smallImage=”Images\google_SmallImage.png” />

<button
id=”Button2
assembly=”WebLaunchButtons.dll”
className=”WebLaunchButtons.Button2
caption=”Yahoo!”
tooltip=”Quick Access to Yahoo.”
helpString=”http://mydomain.com&#8221;
largeImage=”Images\yahoo.png”
smallImage=”Images\yahoo_SmallImage.png” />

Now you will have 2 buttons in a frame with a single group title – you set this in the initial project settings.

My last example will connect to an FTP site, grab a shapefile, and load it. The code is a mashup of code from MSDN and ESRI as well as some modifications on my part.

public override void OnClick()
        {
            m_disp = ESRI.ArcGISExplorer.Application.Application.ActiveMapDisplay;

            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(“ftp://ftp1.freebsd.org/pub/FreeBSD/README.TXT”); //this would be to a shapefile but I dont have an FTP server.
            request.Method = WebRequestMethods.Ftp.DownloadFile;

           
            //request.Credentials = new NetworkCredential(“me”, “mypassword”);

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            System.IO.StreamWriter file = new System.IO.StreamWriter(“c:\\downloaded.txt”); //your shapefile
            file.WriteLine(reader.ReadToEnd());
            
            reader.Close();
            response.Close();
            file.Close();

            FeatureLayer fl = FeatureLayer.OpenShapefile(“C:\\downloaded.shp”); // the file you saved
            fl.Renderer.SetSymbol(CreateSymbol(fl.GeometryType));
            m_disp.Map.ChildItems.Add(fl);
            m_disp.ZoomTo(fl.Extent);

            
        }

        private Symbol CreateSymbol(GeometryType geomType)
        {
            //Create a new symbol for the feature layer data being added based on feature type
            Symbol sym = null;
            switch (geomType)
            {
                case GeometryType.Point:
                    sym = Symbol.Marker.Pushpin.LightBlue;
                    break;
                case GeometryType.Polygon:
                    sym = Symbol.CreateFill(System.Drawing.Color.Aqua, System.Drawing.Color.Black);
                    break;
                case GeometryType.Polyline:
                    sym = Symbol.Line.Solid.Purple;
                    break;
            }

            return sym;
        }

    }

In my next post, I will show how to send an SMS text message from GIS.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: