Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am a newbie to both ArcGIS and Python. My requirement is to automate the below MANUAL process:

  1. Creating a layer in ArcGIS for Desktop. To put it in another words, creating an ArcMap document (.mxd).
  2. Publishing the created ArcMap document (in Step 1) as a service to ArcServer.

Currently we are doing this manually. I have come across scripts to automate step 2 using Python. Any guidance to automate step 1 and step 2 would be great.

share|improve this question
    
Is it possible to automate this using Java. Just curious to know... –  user15051 Feb 14 '13 at 19:27
    
Your Java question would be better researched/asked separately rather than tacked on as a Comment. –  PolyGeo Jun 20 at 23:20

2 Answers 2

Sample code to do 1. is on the Online Help for AddLayer (arcpy.mapping).

The steps for 2. are in the Online Help for Publishing a map service with Python.

Note that it is not possible to create an MXD using ArcPy - you need to have an existing MXD to which you can add layers. That design decision is described in the Online Help on Guidelines for arcpy.mapping , but being able to create a new Map Document from nothing in ArcPy is an ArcGIS Idea that I would like to see implemented.

See @Luke answer for an advanced Python and ArcObjects method that I have not tested but may provide you with a workaround for creating an MXD from a Python script that ArcPy can then go on to manipulate.

share|improve this answer
1  
+1 Ditto on wanting to see mxd creation from scratch. –  om_henners Feb 12 '13 at 23:35
    
Thank you PolyGeo... Pouring over the material and will get back... –  user15051 Feb 13 '13 at 18:33

This isn't really a standalone answer, more of an addition to @PolyGeo's answer as it addresses the 'mxd creation from scratch' in python issue.

Note: it's probably a bit advanced for a python 'new bee'...

You can create MXD's from scratch in python if you access ArcObjects. You will need the comtypes package and if using ArcGIS 10.1, you need to make a small change to automation.py. See ArcObjects + comtypes at 10.1

Below is some code to create an MXD from scratch in python:

import comtypes,os

def CreateMXD(path):
    GetModule('esriCarto.olb')
    import comtypes.gen.esriCarto as esriCarto
    pMapDocument = CreateObject(esriCarto.MapDocument, esriCarto.IMapDocument)
    pMapDocument.New(path)
    pMapDocument.Save() #probably not required...

def GetLibPath():
    """ Get the ArcObjects library path

        It would be nice to just load the module directly instead of needing the path,
        they are registered after all... But I just don't know enough about COM to do this

    """
    import _winreg
    #Little kludge for 64bit OS
    if 'PROGRAMFILES(X86)' in os.environ:path='SOFTWARE\\Wow6432Node\\ESRI'
    else:path='SOFTWARE\\ESRI'
    keyESRI = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, path)

    #Little kludge juuust in case ArcGIS 10.1 uses a different registry key,
    #i.e. "Desktop10.1"...
    nkeys=_winreg.QueryInfoKey(keyESRI)[0]
    for i in range(nkeys):
        subkey=_winreg.EnumKey(keyESRI,i)
        if subkey[:7].lower()=='desktop' or subkey.lower()=='arcgis':
            try:
                keyDesktop = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, path+'\\'+subkey)
                compath=os.path.join(_winreg.QueryValueEx(keyDesktop,"InstallDir")[0],'com')
                if os.path.exists(compath):return compath
            except:pass

def GetModule(sModuleName):
    """ Generate (if not already done) wrappers for COM modules
    """
    from comtypes.client import GetModule
    sLibPath = GetLibPath()
    GetModule(os.path.join(sLibPath,sModuleName))

def CreateObject(COMClass, COMInterface):
    """ Creates a new comtypes POINTER object where
        COMClass is the class to be instantiated,
        COMInterface is the interface to be assigned
    """
    ptr = comtypes.client.CreateObject(COMClass, interface=COMInterface)
    return ptr

if __name__=='__main__':
    #testing...
    import arcpy
    arcpy.SetProduct('arcview')
    filepath='c:/temp/testing123.mxd'
    if os.path.exists(filepath):os.unlink(filepath)
    CreateMXD(filepath)
share|improve this answer
    
Thank you Luke for the code... As you sai a bit complex one for a new bee but will get to it..... –  user15051 Feb 13 '13 at 18:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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