Select to view content in your preferred language

Moving geoprocessing script to ArcGIS Server GP service

537
4
02-09-2011 10:37 AM
TimothyMichael
Frequent Contributor
Hello all,

For a current project I created a script tool that exports attributes of selected point features to a comma-separated .txt file.  The tool then removes certain columns from the .txt file, saves it to a new location, and sends it via FTP.

The next thing I need to do is publish this as a GP service in ArcGIS Server 10.  The part that I am having trouble with is the selection of points by the user - in Desktop the script will export any features that are selected, but I'm unsure how to make the same thing work on the web (this will be in a Flex application).

So, how should I set up the service to pass the selection to the service?  I have included the full script below in case it helps with answers.

Thanks, Tim.


import arcpy, csv, ftplib, os

Input_Feature_Class = " "
Value_Field = " "
Delimiter = " "
OutputDirectory = " "
FTPDirectory = " "
Output_ASCII_File_Name = arcpy.GetParameterAsText(0)
Output_ASCII_File = OutputDirectory + Output_ASCII_File_Name + ".txt"
Add_Field_Names_to_Output = "ADD_FIELD_NAMES"

# Run ExportXYv_stats to export .txt file of records
arcpy.ExportXYv_stats (Input_Feature_Class, Value_Field, Delimiter, Output_ASCII_File, Add_Field_Names_to_Output)

#REMOVE UNNEEDED COLUMNS AND CREATE NEW FILE IN FTP/LOG FOLDER

input = Output_ASCII_File
outputFile = FTPDirectory + Output_ASCII_File_Name + ".txt"
output = open(outputFile, 'w')
readFile = csv.reader(open(input, 'r'))

for row in readFile:
    print >>output, row[2]
output.close()

#FTP FILE TO SERVER

inputFTP = outputFile
fileName = Output_ASCII_File_Name + ".txt"
server = " "
username = " "
password = " "

sftp = ftplib.FTP(server, username, password)

fp = open(inputFTP, 'rt')
sftp.storbinary('STOR '+ fileName, fp)

fp.close()
sftp.quit()
0 Kudos
4 Replies
KevinHibma
Esri Regular Contributor
Tim,

In looking at your script, and reading what you want to do - I think either its missing a part, or maybe thats just the part you need to add in.
What I dont see is the points layer and the selection on it. Perhaps you're doing the selection within ArcMap, and the tool you're running is against the layer inside ArcMap, thus the selection is honored? (yes no?)

If thats the case you'll probably want to add another input to your script - a feature set (assuming you want your end user to draw an area of interest to select features). Otherwise a textbox to pass an expression in for the Select Layer by Attributes

Some untested code (as written directly in this forum textbox)

originalPointsFC = r'c:\data\myPoints.shp'
SelectionSet = arcpy.GetParameterAsText(0)

arcpy.MakeFeatureLayer(originalPointsFC, inputLayer)
selectedLayer = arcpy.SelectLayerByLocation(inputLayer, SelectionSet, "NEW_Selection")

#Your code
Output_ASCII_File_Name = arcpy.GetParameterAsText(1)
Output_ASCII_File = OutputDirectory + Output_ASCII_File_Name + ".txt"
Add_Field_Names_to_Output = "ADD_FIELD_NAMES"

arcpy.ExportXYv_stats (selectedLayer, Value_Field, Delimiter, Output_ASCII_File, Add_Field_Names_to_Output)

# and so on....


Then in flex when you call this service, you'd have 2 inputs to pass in: the feature set (to be used as a selection) and the output ASCII file name. Though, I'd make sure it works when using the Service in ArcMap before diving into the Flex application.
0 Kudos
TimothyMichael
Frequent Contributor
Kevin,

You are absolutely correct, I am using this script as a tool directly from ArcMap.  I do have a variable in the script for the input feature (Input_Feature_Class), but as you say, the selection is being done in ArcMap and is then honored in the script.

I will need to add a feature set, since the user will be drawing an area of interest that they want the points exported from - an attribute query will not work in this particular case.  From your code example, I will want to use the FeatureLayer, not the SelectionSet then?

I'll be able to dedicate some time to this tomorrow.  Thanks for the help so far.

-Tim
0 Kudos
KevinHibma
Esri Regular Contributor
Well it all depends how you are publishing.
If you're publishing a standalone tool you're working with featureclasses (something on disk).
If you're publishing as a tool layer (with an MXD), you're working with layers.

The Select Layer by Location tool only works with layers. So if you're publishing a toolbox, you need to convert your input points into a layer (Make FeatureLayer), then you can use a feature set to make a query against it.

In setting up your script tool, create a new input parameter. Make it of type "Feature Set" and make sure to set the schema to the path of a polygon featureclass. You then take this input and use it as the selection with Select Layer by Attribute.

There is absolutely no problem in scripting this - this however is one of those workflows which becomes real easy when done in ModelBuilder.

Maybe you want 2 tools? The ModelBuilder portion to get the data you need, then a script which just takes those points as input and does the FTP (or whatever else you may need). Just a thought.
0 Kudos
TimothyMichael
Frequent Contributor
Kevin,

I will be publishing as a tool layer in an .mxd.  I tried to integrate your suggestions and am getting an error when attempting to run in ArcMap (just the script tool, not yet published as a service).  Here is what I have:

import arcpy, csv, ftplib, os

Input_Feature_Class = "C:/arcgisserver/Sites/Site/Geodatabase/Site.gdb/Data/AddressPoints"
SelectionSet = arcpy.GetParameterAsText(1)
Value_Field = "PHONE_NUMB"
Delimiter = "COMMA"
OutputDirectory = "C:/arcgisserver/TempOutput/"
FTPDirectory = "C:/inetpub/wwwroot/FTP_Files/Site/"
Output_ASCII_File_Name = arcpy.GetParameterAsText(0)
Output_ASCII_File = OutputDirectory + Output_ASCII_File_Name + ".txt"
Add_Field_Names_to_Output = "ADD_FIELD_NAMES"

arcpy.MakeFeatureLayer_management(Input_Feature_Class, "inputLayer")

selectedLayer = arcpy.SelectLayerByLocation_management(inputLayer, "WITHIN", SelectionSet, "", "NEW_SELECTION")

arcpy.ExportXYv_stats (SelectionSet, Value_Field, Delimiter, Output_ASCII_File, Add_Field_Names_to_Output)


When I set up the script tool in ArcMap I have two inputs: File Name (Data Type is Any Value) and Selection (Data Type is Feature Set, schema is pointed to polygon shapefile).  When I run the tool I get the following error:

Start Time: Tue Feb 15 12:56:24 2011
Running script Script...
<type 'exceptions.NameError'>: name 'inputLayer' is not defined
Failed to execute (Script).

It seems like the tool is not recognizing the FeatureLayer inputLayer.  Any thoughts on this?

Thanks,
Tim
0 Kudos