Can I use cursors in a geoprocessing service?

1652
6
Jump to solution
08-30-2013 10:53 AM
ionarawilson1
Occasional Contributor III
Can I use cursors (search, insert and update cursors) in a geoprocessing service that is going to be used in a web app? Thanks
1 Solution

Accepted Solutions
ionarawilson1
Occasional Contributor III
A note on the last note: If use arcpy.GeParameterAsText() it works in arcmap but not when running the gp, because apparently a parameter cannot have a feature class as an input (go figure!), so if you use arcpy.GetParameterAsText() and then have that parameter as a featureset (which I needed for my gp) it won't work in the gp. What I did was to create a variable for that parameter and when I run it inside ArcMap I grab the layer for that feature class (in my case the layer is referencing a feature class in a sde geodatabase). Then I publish the gp, then when I run the geoprocessing service I can grab the file directly from the connection instead of the layer. I hope it will help somebody because it took me a while to figure this out.

 Input_Polygons = "Stewardship"    with arcpy.da.UpdateCursor(Input_Polygons, ("DateStart", "PlanID", "FFY")) as rows:      for row in rows:          if not (row[1] or "").strip(): #covers blank, one blank space, or Null             Datestarstr1 = row[0]             Datestarstr2 = str(Datestarstr1)                      yearonly = Datestarstr2[0:4]                               row[2] = yearonly              rows.updateRow(row)    

View solution in original post

0 Kudos
6 Replies
DanielSmith
Occasional Contributor III
Can I use cursors (search, insert and update cursors) in a geoprocessing service that is going to be used in a web app? Thanks


Any answer to this? Currently working with an update cursor in a python script. Script runs fine stand alone and as a geoprocessing tool. Service says it finishes w/o errors but the table is not updated.

many thanks
0 Kudos
ionarawilson1
Occasional Contributor III
Yes, we can use all the cursors, the problem is usually with the data input. Here is something that works for me. For the inputs (the inputpolygons) make sure you create a python toolbox parameter as a feature set. To make the update occur in the geoprocessing service, make sure when you run the tool in ArcMap that you select the inputs from their actual files (their databases), not the layers in your map. It took a while for me to figure out this. Of course, make sure the arcgisserver can access the data before through the data store. Good luck!

# Import arcpy module
import os, sys, arcpy, traceback, arcgisscripting

Input_Polygons = "Stewardship"

with arcpy.da.UpdateCursor(Input_Polygons, ("DateStart", "PlanID", "FFY")) as rows:


      
    for row in rows:

        if not (row[1] or "").strip(): #covers blank, one blank space, or Null
           Datestarstr1 = row[0]

           Datestarstr2 = str(Datestarstr1)
      


           yearonly = Datestarstr2[0:4]

                
           row[2] = yearonly


           rows.updateRow(row)
DanielSmith
Occasional Contributor III
thank you very much. i will test this now. The data will be local to the user so we'll see how this shakes out soon.
0 Kudos
ionarawilson1
Occasional Contributor III
No problem. Make sure you check the logs in the manager also. It can give you a clue about the problem.
0 Kudos
ionarawilson1
Occasional Contributor III
Just a note:

When I started adding more feature class, things stopped working, so I changed this

Input_Polygons = "Stewardship"
Counties = "Counties"

to

Input_Polygons = arcpy.GetParameterAsText(0)
Counties = arcpy.GetParameterAsText(1)

And Everything is working again!
0 Kudos
ionarawilson1
Occasional Contributor III
A note on the last note: If use arcpy.GeParameterAsText() it works in arcmap but not when running the gp, because apparently a parameter cannot have a feature class as an input (go figure!), so if you use arcpy.GetParameterAsText() and then have that parameter as a featureset (which I needed for my gp) it won't work in the gp. What I did was to create a variable for that parameter and when I run it inside ArcMap I grab the layer for that feature class (in my case the layer is referencing a feature class in a sde geodatabase). Then I publish the gp, then when I run the geoprocessing service I can grab the file directly from the connection instead of the layer. I hope it will help somebody because it took me a while to figure this out.

 Input_Polygons = "Stewardship"    with arcpy.da.UpdateCursor(Input_Polygons, ("DateStart", "PlanID", "FFY")) as rows:      for row in rows:          if not (row[1] or "").strip(): #covers blank, one blank space, or Null             Datestarstr1 = row[0]             Datestarstr2 = str(Datestarstr1)                      yearonly = Datestarstr2[0:4]                               row[2] = yearonly              rows.updateRow(row)    
0 Kudos