Select to view content in your preferred language

ArcGIS Runtime and Python

3176
5
02-19-2014 12:04 AM
Labels (1)
GaryWilkins
Emerging 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.

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.

Code:
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
0 Kudos
5 Replies
BKuiper
Frequent Contributor
Hi Gary, somebody from Esri will be able to confirm, but your current approach won't work. within ArcMap the python execution has access to the variables and objects that are part of ArcMap. This is not the case with Runtime. You have to compare the geoprocessing package as something that run remotely, like on ArcGIS server. It doesn't know anything about your environment, so you will need to pass along the necessary information to the script to let it execute.
0 Kudos
GaryWilkins
Emerging Contributor
Hi Bjorn,

Sadly that makes sense - I thought I was perhaps asking too much of it.

Would you be able to  point me in the right direction on what information I need to pass in about the environment to a script? I'm assuming I need to pass this information as parameters.

Most (but not all) of our data is contained in an MPK file loaded into the application - could I somehow point the script to the same MPK file?

Thanks for your help.

Gary
0 Kudos
AhmedEl-Sisi
Deactivated User
Hi Bjorn,

Sadly that makes sense - I thought I was perhaps asking too much of it.

Would you be able to  point me in the right direction on what information I need to pass in about the environment to a script? I'm assuming I need to pass this information as parameters.

Most (but not all) of our data is contained in an MPK file loaded into the application - could I somehow point the script to the same MPK file?

Thanks for your help.

Gary

you can query your layers through Query Task
You should check Runtime samples installed with runtime SDK as It covers alot of concepts.

If you want to pass features to your GP tool you can use FeatureSet or RecordSet to pass a specific layer/table to your script.
It's similar to Silverlight API.
http://resources.arcgis.com/en/help/runtime-wpf/concepts/index.html#/Geoprocessing_task_parameters/0...
https://developers.arcgis.com/silverlight/sample-code/start.htm#ClipFeatures
0 Kudos
MarcoBoeringa
MVP Alum
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.

Code:
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?


Gary, have you tried to replace "CURRENT" with the full folder path to your *.mxd Map document? Current refers to the "current" document opened in ArcMap. Since you are running the Python script outside ArcMap, it probably has no knowledge what "current" is.
0 Kudos
BKuiper
Frequent Contributor
Ahmed's approach will work. Marco's approach not. You will need to explain a bit more about what you are trying to achieve. Based on that we can advise you what the best approach is. There are all kinds of services available, as Ahmed pointed out, that you can directly use on the layers within your application. In other cases you might need to go to GPKs, and depending on what you want it can be really easy to complex.
0 Kudos