Trying to create a GP script in python without joining tools together in a model....

1727
6
03-06-2017 09:57 AM
grahamcooke
Occasional Contributor

hi,

I recently posted this thread in the geoprocessing forum with not much response.

I have tried to soldier on but keep hitting issues. Everything I know about Geoprocessing and Python I have hacked together from google searches.

Would someone please be able to take a look at my script here and see where it is going wrong? In short I am trying to test my theory that I can pass in an XY and a list of id's as two parameters. then create a point based on the xy and a select on my feature class where the id's are equal to the id's passed in and then use the result of the above 2 operations as inputs for a call to the "generatenear" tool to get the distances from my XY to all the selected features. I'm sure this should be pretty straight forward but my python skills aren't up to the challenge.

I have tried to use the in_memory workspace and this seems to be the cause of most of my problems! I have hardcoded the values for the origin point and some feature id's but in the real version these would need to be passed in parameters from a call to the python script. The intent is to publish the script as a GP tool so it can be consumed via REST by one of our company websites. Some pointers, links to relevant tutorials info etc.. would be greatly appreciated:

I appreciate you guys aren't going to write the code for me, but I am struggling a fair bit. Am I far off? I don't get any errors but the output in the IDLE debugger window is:

working
all done
None

How do I check if there is something in myOutput and how would I expose this so it can be consumed by a REST get operation?

0 Kudos
6 Replies
NeilAyres
MVP Alum

GP services can be quite tricky....

Just had a very brief look at your code. I do not see where it is returning anything? Where is the output to go back to the client. And if this is a table, how is the client supposed to use it?

And BTW I don't think you need coords specified down to the sub-atomic level.

xy = (414038.9985242901,130874.00962267019)

0 Kudos
grahamcooke
Occasional Contributor

HI Neil,

thanks for your comment. The co-ords are as returned by another REST call to the findaddresscandidates service that we already have published. The end user inputs a postcode the client calls to the service returns the XY and this would then be used as the first passed in parameter for my script

0 Kudos
FilipKrál
Occasional Contributor III

Hi Graham,

Nice to see the good old British National Grid but boy this is a loaded question. Almost all the advanced topics in one. I suggest some changes in the attachment but you still have a long way to go.

Some key points are:

  • Result of a geoprocessing tool run is not the output variable but a result object from which you need to pluck out the appropriate variable (hence the .getOutput(0)).
  • In order to return a value from a geoprocessing python script you need to setParameter. 
  • I would stay away from in_memory workspaces until you get everything working and then you can experiment with it. ArcGIS Server should handle the creation of new workspace and scratch workspace for you and then get rid of it after a few weeks or whatever the settings are.
  • The way it is now, result of the script will be a path to the output near table, but I suspect that will not return the content of the table as a result of the REST service. I thing you would need to read the table with a search cursor into something like a RecordSet and return that or dump it to json, then to text, set the text as an output parameter of type Text and then parse this as JSON on the client side.

Well... not to discourage you. I did this kind of thing before and it kinda worked, but we never used it in production. It was like scratching your left ear with your right hand behind your head.

You may want to consider publishing both the data and the Generate Near Table tool as services and using these in the client fully over the RESTful interface.

Best wishes,

Filip.

grahamcooke
Occasional Contributor

Hey Filip,

thanks for the feedback. Guess this was simple in explanation only, the execution not so simple huh! I did consider publishing the generatetool as a service  as - as you point out if I already have a rest end point to accept the postcode and get an XY and I publish the featureset as a service which could be queried with a select using the passed in ID's - the inputs for the generatenear can all be gotten in REST and then it would be a 3rd call to do the distance calculations. the issue I hit pretty quickly was generatenear needs its inputs as features but a simple XY is not a feature.

I figured if I had to do more geoprocessing to take an XY and make it a point in a feature class that I might as well go the whole hog and do the lot in a script.

If you (or anyone else!) have any tips on the easiest way to publish the generatenear as a REST service so I can leave everything up to the client and just make a bunch of resources available over http then I'd love to hear them.

thanks!

0 Kudos
JonathanQuinn
Esri Notable Contributor

A few things I notice; do you want to return the results of the table to the client?  In which case you'll need to use arcpy.SetParameter, assign it the next parameter, and then set what you want returned, arcpy.SetParameter(2,myOutput).  You don't need to use your own try: except block unless you're going to catch the exceptions properly and return the messages.  I would get rid of the try: except block and let the GP framework return the error for you.  Also, print statements won't work in a tool.  You'll need to use arcpy.AddMessage().  Finally, what problem are you running into with the tool?

grahamcooke
Occasional Contributor

Hi Jonathan,

Thanks very much for your reply. I was running the script above just as a script in IDLE initially so as to check if it works before wrapping it in a tool and publishing it. Yours and Filips input has been very helpful.

The issue I seem to be hitting is getting the generatenear tool to actually work. It doesn't seem to like the inputs.

I have just tried publishing the generatenear on it's own, with inputs coming from dummy features I created in a gdb just to see if I can get it to work and then view the resultant python script.

This is where I hit my newest problem  - which is that we are on Server standard here and I just found out that to publish advanced geoprocessing tools you need Server advanced or at a minimum the spatial analyst server extension. Hopefully this is useful to anyone else stumbling across this thread. I will update this thread as we get nearer to a solution

Thanks to everyone who has offered input and advice so far.

0 Kudos