arcmap and python

Upload: miguel-angel

Post on 08-Jul-2018

232 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/19/2019 Arcmap and Python

    1/29

     ArcMap and Python:

    Closing the VBA GapMark Cederholm

    UniSource Energy Services

    Esri Developer Summit 2012

  • 8/19/2019 Arcmap and Python

    2/29

    2:23:00 PM

    http://www.pierssen.com/arcgis/misc.htm

    Using ArcObjects in Python

    Esri Developer Summit 2010

    http://www.pierssen.com/arcgis10/python.htm

    ArcMap and Python: Closing the VBA Gap

    Tuesday 4:30 Mesquite C

    Extend Python Using C++ and ArcObjects

    Wednesday 11:15 Mesquite B

    Download presentations and code:

  • 8/19/2019 Arcmap and Python

    3/29

    Why Python?

    ArcGIS VBA support ends after 10.0

    Python is integrated with ArcMap

    Geoprocessing tasks: arcpy

    ArcObjects manipulation: comtypes Custom forms: wxPython

    At 10.1, the ArcGIS add-in framework

    supports Python However, debugging can be difficult in

    ArcMap

    2:23:00 PM

  • 8/19/2019 Arcmap and Python

    4/29

    The comtypes site package:

    http://starship.python.net/crew/theller/comtypes/

    2:23:00 PM

    The wxPython site package:http://wxpython.org/

  • 8/19/2019 Arcmap and Python

    5/29

    2:23:00 PM

    Modifying comtypes for 10.1:

    Delete automation.pyc, automation.pyo,safearray.pyc, safearray.pyo

    Edit automation.py

    Add the following entry to the _ctype_to_vartype dictionary (line 794):

    POINTER(BSTR): VT_BYREF|VT_BSTR,

  • 8/19/2019 Arcmap and Python

    6/29

    Loading common ArcMap modules

    2:23:00 PM

    At the Python prompt:>>> from comtypes.client import GetModule

    >>> GetModule("c:/programfiles/arcgis/desktop10.1/com/esriArcMapUI.olb")

    [TIP: If loading one or modules fails, delete all files in thecomtypes/gen folder before trying again.]

    See Snippets.py for examples of using comtypes

  • 8/19/2019 Arcmap and Python

    7/29

    2:23:00 PM

    Workaround: Array object

    >>> import comtypes.gen.esriSystem asesriSystem

    >>> from comtypes.client import CreateObject>>> pArray = CreateObject("{8F2B6061-AB00-11D2-87F4-0000F8751720}",interface=esriSystem.IArray)

  • 8/19/2019 Arcmap and Python

    8/29

    2:23:00 PM

    wxPython and ArcMap

    Will not work out-of-the-box in the ArcMapPython window

    Use PySimpleApp in a dedicated extension

    for the application MainLoop Destroy does not work: use Show instead

    The “print” command will not output tothe Python window from a form

  • 8/19/2019 Arcmap and Python

    9/29

    2:23:00 PM

    Customizing ArcMap, Method 1:

    Creating a Framework component

    (9.x and 10.0)

    [Sample Code: ArcMap_Python\DemoCOM]

  • 8/19/2019 Arcmap and Python

    10/29

    2:23:00 PM

    Requires MIDL.EXE

    Obtain by downloading and installing

    Windows SDK 7.1:

    http://www.microsoft.com/download/en/details.aspx?id=8279

  • 8/19/2019 Arcmap and Python

    11/29

    2:23:00 PM

    Edit DemoExtension.idl and DemoTool.idlto set the correct ESRI paths: 

  • 8/19/2019 Arcmap and Python

    12/29

    2:23:00 PM

    Open Windows SDK 7.1 CommandPrompt: 

  • 8/19/2019 Arcmap and Python

    13/29

    2:23:00 PM

    Use MIDL.EXE to produceDemoExtension.tlb and DemoTool.tlb:

    midl DemoExtension.idlmidl DemoTool.idl

    Use Python to register the COM objects*:python DemoExtension.py -regserver

    python DemoTool.py -regserver

    *WARNING: The file/module name is case sensitive!

  • 8/19/2019 Arcmap and Python

    14/29

    2:23:00 PM

    In ArcMap, add the

    tool in CustomizeMode: 

    Activate the tool,

    select a quote, anddraw a line: 

  • 8/19/2019 Arcmap and Python

    15/29

    2:23:01 PM

    Use Python to unregister the COM objects:

    python DemoExtension.py -unregserver

    python DemoTool.py -unregserver

  • 8/19/2019 Arcmap and Python

    16/29

    2:23:01 PM

    Customizing ArcMap, Method 2:

    Creating an Add-in (10.1)

    [Sample Code: ArcMap_Python\DemoAddin]

  • 8/19/2019 Arcmap and Python

    17/29

    2:23:01 PM

    Python Add-In Wizard — see “Obtaining

    the Python Add-In Wizard” in the ArcGIS10.1 for Desktop Help:

  • 8/19/2019 Arcmap and Python

    18/29

    2:23:01 PM

    Select or create a working folder and setother properties as desired:

  • 8/19/2019 Arcmap and Python

    19/29

    2:23:01 PM

    Add an extension (right-click on “EXTENSIONS”):

  • 8/19/2019 Arcmap and Python

    20/29

    2:23:01 PM

    Add a toolbar:

    Add a tool:

  • 8/19/2019 Arcmap and Python

    21/29

    2:23:01 PM

    Click “Save”, navigate to folder, and add

    files from sample code:

    Double-click makeaddin.py to create add-in:

  • 8/19/2019 Arcmap and Python

    22/29

    2:23:03 PM

    Double-click DemoAddin.esriaddin toinstall add-in:

  • 8/19/2019 Arcmap and Python

    23/29

    2:23:03 PM

    Toolbar automatically appears:

    In ArcMap, make sure extension is checked:

  • 8/19/2019 Arcmap and Python

    24/29

    2:23:03 PM

    TIP: Add script path to sys.path to enable

    local imports

    import osimport sys

    sMyPath = os.path.dirname(__file__)sys.path.insert(0, sMyPath)

  • 8/19/2019 Arcmap and Python

    25/29

    2:23:03 PM

    class DemoExtension(object):_wxApp = Nonedef __init__(self):

    self.enabled = Truedef startup(self):

    try:from wx import PySimpleApp

    self._wxApp = PySimpleApp()self._wxApp.MainLoop()except:

    sMsg = "Error starting extension:\n" + \traceback.format_exc()

    pythonaddins.MessageBox(sMsg, "DemoAddIn")

  • 8/19/2019 Arcmap and Python

    26/29

    2:23:03 PM

    class DemoTool(object):_pApp = None_geometry = None_sQuote = None

    _dlg = Nonedef __init__(self):

    self.enabled = Trueself.cursor = 3self.shape = "Line" # Can set to . . .

    def onClick(self):

    if self._dlg is None:from QuoteDialog import QuoteDialogself._dlg = QuoteDialog(sMyPath, self)

    self._dlg.Show(True)def deactivate(self):

    if self._dlg is None:

    returnself._dlg.Show(False)

    def onLine(self, line_geometry):self._geometry = line_geometryself.DoIt()

    Not stubbed outby default:

  • 8/19/2019 Arcmap and Python

    27/29

    2:22:58 PM

    COM Interop: relative speed

    Benchmark = 500+K ShapeCopy operations

    0 10 20 30 40 50 60 70 80 90 100

    C++

    VBA

    .NET

    Python

    IronPython

    Java

    100

    92

    48

    32

    24

    16

  • 8/19/2019 Arcmap and Python

    28/29

    Some final tips:

    When in doubt, check the wrapper code:Python../Lib/site-packages/comtypes/gen

    Avoid intensive use of fine-grained ArcObjects inPython

    For best performance, use C++ to create coarse-grained objects

    Use geoprocessing objects and tools to simplifysupported tasks – watch for performance, though

    Read the desktop help to check out availablefunctionality in arcgisscripting (and arcpy at 10.x)

    2:23:03 PM

  • 8/19/2019 Arcmap and Python

    29/29

    Questions?

    Mark Cederholm

    [email protected] 

    This presentation and sample codemay be downloaded at: 

    http://www.pierssen.com/arcgis/misc.htm

    2:23:03 PM

    http://www.pierssen.com/arcgis10/python.htm

    For 9.x examples, see:

    mailto:[email protected]:[email protected]