Python and ArcGIS Runtime

509
3
02-19-2014 12:03 AM
GaryWilkins
New Contributor
Hi,

I'm new to using python and ArcGIS together and really struggling with something which I'm not entirely sure is possible. I'm really hoping somebody out there has managed to do similar or could please point me to some useful reference material.

We are looking to allow users to run python scripts within our application built upon the ArcGIS Runtime.

Having trawled around I found an article which says that (theoretically!) it is possible to run .py scripts in the ArcGIS Runtime by essentially calling them from a geoprocessing package containing the following code.

filename = arcpy.GetParameterAsText(0)
execfile(filename, {'__file__': filename})


This works perfectly for very simple python scripts but my problem comes that anything that attempts to operate on the map (such as adding or querying a layer or even querying the map ) simply fails.

I can't seem to access anything map related!

The same scripts work perfectly in the Python window in ArcMap (an example below), but run as a py script = nothing.

import arcpy
import ctypes

mxd = arcpy.mapping.MapDocument("CURRENT")
nam = mxd.activeDataFrame.name

ctypes.windll.user32.MessageBoxA(0, nam, "Dialog Title", 1)


Are there any limitations or approaches I should be aware of?

We really need the ability to run python scripts in our application 😞

Apologies if this is not the right forum to post on, but could someone please help!

Much appreciated,

Gary
Tags (2)
0 Kudos
3 Replies
KevinHibma
Esri Regular Contributor
ArcGIS Runtime (GPKs) and Geoprocessing Services are very similar in nature. Neither of them support the notion of "CURRENT" with arcpy.Mapping. The CURRENT keyword, for use inside ArcMap does exactly what it sounds like, leverages the current, open, map document. When something is running as service (runtime/gp services), there is no "current" map document open, available to be acted upon.
So, I guess to answer your question, yes this is a limitation.

I can't find any help-doc on it, but my personal feeling is more arcpy.mapping is not support, than it is supported. I'm sorry I can't provide you a list of what does and does not work.
0 Kudos
SachinKanaujia
Occasional Contributor III
ArcGIS Runtime is conceptually more like a local ArcGIS Server so there isnt a concept of MXD within the ArcGIS Runtime environment I suspect it wont be supported within Runtime environment. Accessing and manipulating MXD's might work outside the Runtime environment.

You may need to create a MPK (map package) from your MXD and then publish it as a service. After that you should be able to access Layers and other properties. What exactly is the requirement that you are trying to accomplish?
0 Kudos
GaryWilkins
New Contributor
We are trying to provide functionality to allow users to run their own python scripts at runtime to perform some relatively simple ad-hoc spatial analysis, rather than having to either develop the functionality as .net code in our application or having to create gpk files in ArcMap.

Our idea being to compile the simple 'execfile' python script into a GPK and then call that from our application passing in the name of the python script to run.

Typically we don't know what analysis the user(s) wish to perform as it tends to be quite specialised (although simple). We have a number of different clients, each wishing to perform slightly different analysis, so we can't really go down the route of coding the functionality for them in the ArcGISRuntime.

I'll try referencing the MPK file we have rather than "CURRENT" to see if that helps with my sample code.

Thanks for the suggestions so far.
0 Kudos