Jump to content

 
Photo

Python scripting in ArcMap (9.3)

- - - - -

  • Please log in to reply
3 replies to this topic

#1
barukaah

barukaah

    Contributor

  • Validated Member
  • PipPip
  • 12 posts
  • Canada

Hi all,

I am just a beginner in python scripting with the ArcGIS geoprocessor and i had written a script that does what i want it to do when the input values are hard coded. However, what i want is the script to be dynamic so i could add it to the ArcToolbox and use it within the ArcMap interface. But I can't get both the "GetParameterAsText()" and "sys.argv[]" to work when creating the temporary/selectable layer with the "GP.MakeFeatureLayer". If anyone has any idea about why this is not working please land me a hand as I have several issues like in this script that i would like to automate.
Here's the script itself and the error i am getting when i assign to the variables "InLayer" and "Infield" the values "GetParameterAsText(0)" and "GetParameterAsText(1)" . That being said, i am sure there should be a more intelligent approach (algorithm) for doing the entire think.

Thank you all.
[codebox] _______________________________THE SCRIPT "FIND STREET DIRECTIONALS.PY"_____________________ # Importing the geoprocessor script import arcgisscripting # Creating the geoprocessor gp = arcgisscripting.create (9.3) # Overwrite the output gp.overwriteoutput = 1 # Set the workspace gp.workspace = r"D:\GISData\Mesilla_Valley_NM\GIS_Data\updt" #inWrksp = gp.GetParameterAsText(0) ---> I know this is not mandatory # Local variables inLayer = "Roads.shp" # My desire is to assign "gp.GetParameterAsText(1)" instead inField = "ROADNAME" # My desire is to assign "gp.GetParameterAsText(2)" instead Op1 = " LIKE " Op2 = " OR " outLayer = "outLyr" Selrecs = "Street_Directionals" fldValue = inField + Op1 + "'E %'" + Op2 + \ inField + Op1 + "'W %'" + Op2 + \ inField + Op1 + "'N %'" + Op2 + \ inField + Op1 + "'S %'" + Op2 + \ inField + Op1 + "'NE %'" + Op2 + \ inField + Op1 + "'NW %'" + Op2 + \ inField + Op1 + "'SE %'" + Op2 + \ inField + Op1 + "'SW %'" + Op2 + \ inField + Op1 + "'% E'" + Op2 + \ inField + Op1 + "'% W'" + Op2 + \ inField + Op1 + "'% N'" + Op2 + \ inField + Op1 + "'% S'" + Op2 + \ inField + Op1 + "'% NE'" + Op2 + \ inField + Op1 + "'% NW'" + Op2 + \ inField + Op1 + "'% SE'" + Op2 + \ inField + Op1 + "'% SW'" + Op2 + \ inField + Op1 + "'EAST %'" + Op2 + \ inField + Op1 + "'WEST %'" + Op2 + \ inField + Op1 + "'NORTH %'" + Op2 + \ inField + Op1 + "'SOUTH %'" + Op2 + \ inField + Op1 + "'% EAST'" + Op2 + \ inField + Op1 + "'% WEST'" + Op2 + \ inField + Op1 + "'% NORTH'" + Op2 + \ inField + Op1 + "'% SOUTH'" Query = fldValue # Make Feature layer (temporary or selectable layer) gp.MakeFeatureLayer_management(inLayer, outLayer) # Select layer by attributes gp.SelectLayerByAttribute_management(outLayer, "NEW_SELECTION", Query) # Count the number of selected records result = gp.GetCount_management(outLayer) numRecs = int(result.getOutput(0))

if numRecs > 0:
gp.CopyFeatures_management(outLayer, Selrecs)

else:
print "No records were selected for this query"

# Free the memory
del gp, inLayer, inField, Op1, Op2, fldValue, Selrecs, outLayer, Query, result #, inWrksp,

________________________________THE ERROR I AM GETTING____________________________________
Traceback (most recent call last):
File "C:\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
exec codeObject in __main__.__dict__
File "D:\GISData\Script_Python\Find Street Directionals.py", line 44, in <module>
gp.MakeFeatureLayer_management(inLayer, outLayer)
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000735: Input Features: Value is required
Failed to execute (MakeFeatureLayer).[/codebox]

Edited by Hans van der Maarel, 10 October 2009 - 08:47 AM.
put code in codebox


#2
ceicher

ceicher

    Contributor

  • Validated Member
  • PipPip
  • 44 posts
  • Gender:Male
  • Location:Charlottesville, Virginia
  • United States

Hi

In ArcGIS 9.3.1, the code below runs fine for me. I didn't change anything important, so I'm not sure why you're seeing a problem.

It is possible that something could have changed (for the better) between 9.3 and 9.3.1

I'm no expert, but maybe try a different strategy for setting inLayer and/or outLayer? Set full paths for one or both of these, instead of relying on gp.workspace.

You might get more help by posting your question to the ESRI support forums:

http://forums.esri.com/forums.asp?c=93

Good luck,

-Cory


# Importing the geoprocessor scriptimport arcgisscripting# Creating the geoprocessorgp = arcgisscripting.create (9.3)# Overwrite the outputgp.overwriteoutput = 1# Set the workspacegp.workspace = r"C:\Work\py" #inWrksp = gp.GetParameterAsText(0) ---> I know this is not mandatory# Local variablesinLayer = "world_borders.shp" # My desire is to assign "gp.GetParameterAsText(1)" insteadinField = "ROADNAME" # My desire is to assign "gp.GetParameterAsText(2)" insteadOp1 = " LIKE "Op2 = " OR "outLayer = "outLyr"Selrecs = "big_borders"fldValue = inField + Op1 + "'E %'" + Op2 + \inField + Op1 + "'W %'" + Op2 + \inField + Op1 + "'N %'" + Op2 + \inField + Op1 + "'S %'" + Op2 + \inField + Op1 + "'NE %'" + Op2 + \inField + Op1 + "'NW %'" + Op2 + \inField + Op1 + "'SE %'" + Op2 + \inField + Op1 + "'SW %'" + Op2 + \inField + Op1 + "'% E'" + Op2 + \inField + Op1 + "'% W'" + Op2 + \inField + Op1 + "'% N'" + Op2 + \inField + Op1 + "'% S'" + Op2 + \inField + Op1 + "'% NE'" + Op2 + \inField + Op1 + "'% NW'" + Op2 + \inField + Op1 + "'% SE'" + Op2 + \inField + Op1 + "'% SW'" + Op2 + \inField + Op1 + "'EAST %'" + Op2 + \inField + Op1 + "'WEST %'" + Op2 + \inField + Op1 + "'NORTH %'" + Op2 + \inField + Op1 + "'SOUTH %'" + Op2 + \inField + Op1 + "'% EAST'" + Op2 + \inField + Op1 + "'% WEST'" + Op2 + \inField + Op1 + "'% NORTH'" + Op2 + \inField + Op1 + "'% SOUTH'"Query = fldValue# Make Feature layer (temporary or selectable layer)gp.MakeFeatureLayer_management(inLayer, outLayer)# Select layer by attributes#gp.SelectLayerByAttribute_management(outLayer, "NEW_SELECTION", Query)gp.SelectLayerByAttribute_management(outLayer, "NEW_SELECTION", "POP_CNTRY > 10000000")# Count the number of selected recordsresult = gp.GetCount_management(outLayer)numRecs = int(result.getOutput(0))if numRecs > 0: gp.CopyFeatures_management(outLayer, Selrecs)else: print "No records were selected for this query"# Free the memorydel gp, inLayer, inField, Op1, Op2, fldValue, Selrecs, outLayer, Query, result #, inWrksp,


#3
barukaah

barukaah

    Contributor

  • Validated Member
  • PipPip
  • 12 posts
  • Canada

Thanks a lot Cory. I may not have mentioned it clearly but i had already tested the code with full paths for both input variables and it works fine. Only issue comes when i replace the full path with something dynamic, i.e. asking the user for these input values (like whatever feature layer or field they want to use as input).
-----------------------------

Hi

In ArcGIS 9.3.1, the code below runs fine for me. I didn't change anything important, so I'm not sure why you're seeing a problem.

It is possible that something could have changed (for the better) between 9.3 and 9.3.1

I'm no expert, but maybe try a different strategy for setting inLayer and/or outLayer? Set full paths for one or both of these, instead of relying on gp.workspace.

You might get more help by posting your question to the ESRI support forums:

http://forums.esri.com/forums.asp?c=93

Good luck,

-Cory


# Importing the geoprocessor scriptimport arcgisscripting# Creating the geoprocessorgp = arcgisscripting.create (9.3)# Overwrite the outputgp.overwriteoutput = 1# Set the workspacegp.workspace = r"C:\Work\py" #inWrksp = gp.GetParameterAsText(0) ---> I know this is not mandatory# Local variablesinLayer = "world_borders.shp" # My desire is to assign "gp.GetParameterAsText(1)" insteadinField = "ROADNAME" # My desire is to assign "gp.GetParameterAsText(2)" insteadOp1 = " LIKE "Op2 = " OR "outLayer = "outLyr"Selrecs = "big_borders"fldValue = inField + Op1 + "'E %'" + Op2 + \inField + Op1 + "'W %'" + Op2 + \inField + Op1 + "'N %'" + Op2 + \inField + Op1 + "'S %'" + Op2 + \inField + Op1 + "'NE %'" + Op2 + \inField + Op1 + "'NW %'" + Op2 + \inField + Op1 + "'SE %'" + Op2 + \inField + Op1 + "'SW %'" + Op2 + \inField + Op1 + "'% E'" + Op2 + \inField + Op1 + "'% W'" + Op2 + \inField + Op1 + "'% N'" + Op2 + \inField + Op1 + "'% S'" + Op2 + \inField + Op1 + "'% NE'" + Op2 + \inField + Op1 + "'% NW'" + Op2 + \inField + Op1 + "'% SE'" + Op2 + \inField + Op1 + "'% SW'" + Op2 + \inField + Op1 + "'EAST %'" + Op2 + \inField + Op1 + "'WEST %'" + Op2 + \inField + Op1 + "'NORTH %'" + Op2 + \inField + Op1 + "'SOUTH %'" + Op2 + \inField + Op1 + "'% EAST'" + Op2 + \inField + Op1 + "'% WEST'" + Op2 + \inField + Op1 + "'% NORTH'" + Op2 + \inField + Op1 + "'% SOUTH'"Query = fldValue# Make Feature layer (temporary or selectable layer)gp.MakeFeatureLayer_management(inLayer, outLayer)# Select layer by attributes#gp.SelectLayerByAttribute_management(outLayer, "NEW_SELECTION", Query)gp.SelectLayerByAttribute_management(outLayer, "NEW_SELECTION", "POP_CNTRY > 10000000")# Count the number of selected recordsresult = gp.GetCount_management(outLayer)numRecs = int(result.getOutput(0))if numRecs > 0: gp.CopyFeatures_management(outLayer, Selrecs)else: print "No records were selected for this query"# Free the memorydel gp, inLayer, inField, Op1, Op2, fldValue, Selrecs, outLayer, Query, result #, inWrksp,



#4
frax

frax

    Hall of Fame

  • Associate Admin
  • PipPipPipPipPipPipPip
  • 2,310 posts
  • Gender:Male
  • Location:Stockholm, Sweden
  • Interests:music, hiking, friends, nature, photography, traveling. and maps!
  • Sweden

I haven't worked much with these things, but what if you create a simple model in the GUI, export that as python, and then examine the code - wouldn't you be able to figure out things from that... ?
Hugo Ahlenius
Nordpil - custom maps and GIS
http://nordpil.com/
Twitter




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

-->