# Migrating script from tool to geoprocessing service?

I have a script I've written and converted to a tool in ArcMap 10.4.1. The script goes out to a web service, pulls in content, and then loads results into a point feature class within a file geodatabase.

I'd like to publish this tool as a geoprocessing service on my organization's ArcGIS Server (also 10.4). However I would like the output to be geojson not a feature class so anyone could use it with or without ArcMap.

I've looked around but most of the use cases for publishing geoprocessing services assume that consumers will be working within an ArcMap client. The users of this script would not be, and I want them to be able to parse results in whatever way they want (create a shapefile, KML, csv, pipe it into other services, whatever).

I can easily publish the service through the ArcGIS Server publishing wizard, but afterwards, no matter what payload I give it in the web-based parameters screen, the json output is empty. Clearly I'm missing some modifications to make this work.

Given the non-availability of intermediate/advanced arcpy books, I need either a nudge in the right direction or some advice on how I can structure my parameters within the script, in a generic way, to configure them to output a geojson string instead of writing a file.

Output features from a geoprocessing service can be read and used across Desktop (ArcMap) and Web clients (JavaScript API) equally. Of course the web clients require a bit of coding if you're building from scratch. You could always use the Web App Builder to construct a web app that consumes the GP Service that has outputted features (GPFeatureRecordSet if you want to be technical), and the app will display the output.

If you're set on GeoJson (and I emphasis GeoJSON as the output from the GP Service or arcpy commands is Esri JSON) you'll need to do some modifications within your script tool. This forum post talks about some methods of converting features into GeoJSON. Specially it points to this code which I'd use as a starting point. With that you'd be getting back a "string" of geojson. Your output parameter in the tool then needs to be a string. After this its up to whatever client you're using (a web app?) to handle and display or present the geojson to the user. I still question exactly why you want to output geojson, in my opinion it doesnt seem to have a lot of use as straight output. I guess you're probably doing something else with it after this conversion. If thats the case, I'd consider whatever app you're building to consume the GP Service just as important as the GP Service itself. The two are going to have to work together to perform the whole workflow.

You also note a problem with empty output. I'd need to see the input you're giving it. I'm guessing you're passing in some feature set which your tool parsing doesnt like? Best way to debug this would be to do some AddMessages() in your script and turn the GP Service level to INFO.

Also note, when consuming a service from the REST end point (services directory), you can specify f=json and get back the JSON string of features from a gpfeaturerecordset (this is essentially what the javascript api does). Coming at 10.5 (targeted to be released Q4 2016 or Q1 2017) the Features to JSON tool will output true GeoJSON and the f=geojson support is added to GP Service output

One more note, I forgot there is a way to get arcpy to spit out some GeoJSON (its noted in the previously mentioned forum post). Just make sure to do some spot checks with it to ensure its giving you the expected results.

geos = arcpy.CopyFeatures_management(r'C:\data\test.shp', arcpy.Geometry())
print(geos)
>>>[<Polygon object at 0x14a65250[0x14a65100]>, <Polygon object at 0x14a65290[0x14a650c0]>, <Polygon object at 0x14a652d0[0x14a65020]>]

for g in geos:
print(g.__geo_interface__)
>>>{'type': 'Polygon', 'coordinates': [[(-120.3638928065078, 55.853314527843224), (-98.0225669957702, 54.736248236766755), (-97.46403385068163, 24.57545839209115), (-120.3638928065078, 25.133991537179725), (-120.3638928065078, 55.853314527843224)]]}

• Thanks! This is helpful. I am just providing a service to other 2nd order consumers who can/will ingest the content for further use. I find geojson to be a very common and easy to use format for doing this. Though there isn't any particular reason it couldn't be xml of course. – auslander Oct 4 '16 at 15:03
• Ahh. Gotcha. You're basically making a "conversion" service and its up to the user to provide good inputs/understand the outputs. That makes your life a bit easier – KHibma Oct 4 '16 at 15:07

After running your tool succesfully, open the results window, right click your tool, and publish the results from that as a gp service to whatever server to prefer. If you go to the arcgis/rest/services directory for that server, and find your service. You can run it, and choose json as output