TitlePages

Showing posts with label Tkinter. Show all posts
Showing posts with label Tkinter. Show all posts

Wednesday, 24 December 2014

ArcGIS Python Mapping Module


Arc Python to interact with Arc-map

What is ArcPy Mapping and Why is it actually used for :

Arcpy.mapping was built for the professional GIS analyst (as well as for developers). Traditionally, the scenarios listed above had to be done using ArcObjects and it often proved to be a very difficult programming environment to learn for the average GIS professional. Arcpy.mapping is a courser-grained object model, meaning that the functions are designed in a way that a single arcpy.mapping function can replace many lines of ArcObjects code. The following is a very simple example of how arcpy.mapping can be used to reference an existing map document and export it to a PDF document with only two lines of code.

To explore the Overall Arcpy mapping Module use the following PDF:

Arcpy Python Mapping Module


Code for Getting the reference to the MXD :

YourMXD= arcpy.mapping.MapDocument("path to the MXD")

To export the above MXD to PDF using arcpy:

arcpy.mapping.ExportToPdf(YourMXD,"your pdf file")



Get Reference to the Current Arcmap
Pointer to a MXD present on the Disk


Map Layout Document in the Arc map
Layout View of the Map Document

Arcpy.mapping is not a replacement for ArcObjects but rather an alternative for the different scenarios it supports. ArcObjects is still necessary for finer-grain development and application customization, whereas arcpy.mapping is intended for automating the contents of existing map documents and layer files.



Friday, 12 December 2014

How to create Graphical User Interface in Arcpy

To Show GUI in your Script tool of your ArcMap



Python consist of  huge number of GUI frameworks (or toolkits) available for it, from TkInter to a number of other cross-platform solutions.

In your case we going to use the default TkInter Module to access the windows form from the python script tool.
TkInter is being proposed as the most interactive and easy to program module in Python GUI framework

1. Create a new Script tool in the Arcmap's ArcCatalog window in your desired location
2. Copy the below codes in to a notepad file and save it as python .py extension
3.Go to the script tool and Right Click > Properties > Parameter
4.Browse to your newly created python code as the source file to execute
5.All is well.Start GUI python from TkInter. Happy Programming




from Tkinter import *
import datetime
from array import *



root = Tk()
logo = PhotoImage(file="Image.Gif")
w2 = Label(root,
           image=logo).grid(row=0,column=1)
explanation = """Team Geo Zoner"""
w2 = Label(root, 
          compound = CENTER,
           fg="Red",
           bg="black",
           font="Times 55 bold",
          text=explanation)
w2.grid(row=0,column=0)

p=str(datetime.datetime.now().isoformat())
texter="Your Heading Goes Here"+str(p);
w3 = Label(root, 
          compound = CENTER,
           fg="Green",
           font="Times 20 bold",
          text=texter)
w3.grid(row=1,column=0)

Parameters = ['Number of Inputs','Number of Ouputs','Number of Processing Parameters','Number of Intermediate data']

r = 2
i=1
for c in Parameters:
    Label(text=c, relief=RIDGE,width=25).grid(row=r,column=0)
    Entry(text="Value", relief=SUNKEN,width=10).grid(row=r,column=1)
    r = r + 1
    i = i + 1

root.title("GISSTUDY")
root.mainloop()

Saturday, 6 December 2014

Addin Setting flow direction in Geometric Network

Addin source code for setup flow direction   



1.Create an c# object with establish flow as a function

2.Call this function from the addin on click method by passing the application reference and the type of flow direction you wish to set to your network

To know more about geometric network setting flow direction setting flow direction


source code for setup flow direction
Setting Flow Direction through ArcObjects Code


public static void EstablishFlow(Globals.GNFlowDirection flowDirection, IApplication app)
        {
            IProgressDialog2 progressDialogs= default(IProgressDialog2);
            IProgressDialogFactory progressDialogFactorys = null;
            IEditor editing = null;
         
            IMouseCursor appCursor = null;
            INetworkAnalysisExt netExt = null;
            UID pUID = null;
         
            List<IGeometricNetwork> geonetworklist= null;
            ITrackCancel trackCancel = null;
            Int32 int32_hWnd;
            IStepProgressor stepProgressorobj= null;
            IMxDocument mxdoc = null;
   IEditLayers eLayers = null;
   IMap pMap = null;
            try
            {




                int calcCount = 0;

                //Get editing

                editing = Globals.geteditor(ref app);

                if (editing.EditState != esriEditState.esriStateEditing)
                {
                    MessageBox.Show(Teamgeofunctions.Localizer.GetString("MustBEditg"), Teamgeofunctions.Localizer.GetString("GeoNetToolsLbl_2"));
                    return;
                }

                eLayers = editing as IEditLayers;

                //Change mouse cursor to wait - automatically changes back (ArcGIS Desktop only)
                appCursor = new MouseCursorClass();
                appCursor.SetCursor(2);

                ESRI.ArcGIS.esriSystem.IStatusBar statusBar = app.StatusBar;
                statusBar.set_Message(0, Teamgeofunctions.Localizer.GetString("GeoNetToolsWait_1"));

                //Get NA Extension in order to update the current network with the first visible network
                pUID = new UIDClass();
                pUID.Value = "esriEditorExt.UtilityNetworkAnalysisExt";
                netExt = app.FindExtensionByCLSID(pUID) as INetworkAnalysisExt;

                //Get Visible geometric networks
                pMap = editing.Map;
                geonetworklist= Globals.GetGeometricNetworksCurrentlyVisible(ref pMap);


                if (gnList.Count > 0)
                {


                    //ProgressBar
                    progressDialogFactorys= new ProgressDialogFactoryClass();

                    // Create a CancelTracker
                    trackCancel = new CancelTrackerClass();

                    // Set the properties of the Step Progressor
                    int32_hWnd = app.hWnd;
                    stepProgressorobj= progressDialogFactory.Create(trackCancel, int32_hWnd);
                    stepProgressor.MinRange = 0;
                    stepProgressor.MaxRange = gnList.Count;
                    stepProgressor.StepValue = 1;
                    stepProgressor.Message = "";
                    stepProgressor.Hide();

                    // Create the ProgressDialog. This automatically displays the dialog
                    progressDialogs= (ESRI.ArcGIS.Framework.IProgressDialog2)stepProgressor; // Explict Cast

                    // Set the properties of the ProgressDialog
                    progressDialog.CancelEnabled = false;
                    progressDialog.Description = Teamgeofunctions.Localizer.GetString("GeoNetToolsDesc_1");
                    progressDialog.Title = Teamgeofunctions.Localizer.GetString("GeoNetToolsTitle_1");
                    progressDialog.Animation = ESRI.ArcGIS.Framework.esriProgressAnimationTypes.esriProgressSpiral;

                    bool editStarted = false;
                    try
                    {// Create an edit operation enabling undo/redo
                        editing.StartOperation();
                        editStarted = true;
                    }
                    catch
                    {
                        editStarted = false;
                    }
                    IGeometricNetwork gn = null;
                    IEnumFeatureClass enumFC = null;
                    INetwork net = null;
                    IUtilityNetworkGEN unet = null;
                    IEnumNetEID edgeEIDs = null;
                    //IFeatureLayer fLayer = null;
                    try
                    {

                        for (int i = 0; i < gnList.Count; i++)
                        {

                            gn = gnList[i] as IGeometricNetwork;
                         
                            stepProgressor.Message = Teamgeofunctions.Localizer.GetString("GeoNetToolsProc_1");// +fLayer.Name;
                            //Establish flow using AncillaryRole values


                            if (flowDirection == Globals.GNFlowDirection.AncillaryRole)
                            {
                                enumFC = gn.get_ClassesByNetworkAncillaryRole(esriNetworkClassAncillaryRole.esriNCARSourceSink);
                                if (enumFC.Next() == null)
                                    MessageBox.Show(Teamgeofunctions.Localizer.GetString("GeoNetToolsError_1a") + gn.FeatureDataset.Name + Teamgeofunctions.Localizer.GetString("GeoNetToolsError_1b") + Environment.NewLine +
                                                    Teamgeofunctions.Localizer.GetString("GeoNetToolsError_1c"), Teamgeofunctions.Localizer.GetString("GeoNetToolsProc_2"));
                                else
                                {
                                    gn.EstablishFlowDirection();
                                    calcCount += 1;
                                }
                            }

                            //Establish flow direction based on digitized direction.
                            else
                            {
                                net = gn.Network;
                                unet = net as IUtilityNetworkGEN;
                                edgeEIDs = net.CreateNetBrowser(esriElementType.esriETEdge);
                                edgeEIDs.Reset(); int edgeEID;
                                for (long j = 0; j < edgeEIDs.Count; j++)
                                {
                                    edgeEID = edgeEIDs.Next();
                                    unet.SetFlowDirection(edgeEID, esriFlowDirection.esriFDWithFlow);
                                }
                                calcCount += 1;
                            }
                            stepProgressor.Step();

                        }
                    }

                    catch (Exception ex)
                    {
                        editing.AbortOperation();
                        MessageBox.Show(Teamgeofunctions.Localizer.GetString("GeoNetToolsProc_2") + "\n" + ex.Message, ex.Source);
                    }
                    finally
                    {
                        if (enumFC != null)
                            Marshal.ReleaseComObject(enumFC);

                        gn = null;
                        enumFC = null;
                        net = null;
                        unet = null;
                        edgeEIDs = null;
                        //fLayer = null;
                    }
                    if (editStarted)
                    {   // Stop the edit operation
                        if (flowDirection == Globals.GNFlowDirection.AncillaryRole)
                            editing.StopOperation(Teamgeofunctions.Localizer.GetString("GeoNetToolsProc_2"));
                        else
                            editing.StopOperation(Teamgeofunctions.Localizer.GetString("GeoNetToolsProc_3"));
                    }
                    object Missing = Type.Missing;
                    mxdoc = app.Document as IMxDocument;
                    mxdoc.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, Missing, mxdoc.ActiveView.Extent);

           
            }
            catch (Exception ex)
            {
       
                return;


            }
            finally
            {
                // delete the references to cleanup the memory
                if (progressDialogs!= null)
                    progressDialog.HideDialog();
                progressDialogs= null;
                progressDialogFactorys= null;
                editing = null;
                eLayers = null;
                appCursor = null;
                netExt = null;
                pUID = null;
                pMap = null;
                geonetworklist= null;
                trackCancel = null;

                stepProgressorobj= null;
                mxdoc = null;
            }