AnsweredAssumed Answered

Looking for resources for programming directly against REST Service

Question asked by cwholmes on Nov 8, 2018

Hello,

I'm really not sure if my question captures properly what it is I need to do but here's the problem. I have a mxd that looks like this:

You select a parcel and then run a script tool. If the application type is 'Land Use Amendment' a copy of the parcel will be added to both the Boundary and Site layers and a point will be added to the Location layer, other application types will only add a copy of parcel to the Site layer.

Script tool looks like:

And the script looks like:

import arcpy

arcpy.env.overwriteOutput = True

#Get parameters from user
applicationType = arcpy.GetParameterAsText(0).upper()
itemNo = arcpy.GetParameterAsText(1).upper()
proposedLuCode = arcpy.GetParameterAsText(5).upper() #WAS 2

mxd = arcpy.mapping.MapDocument("CURRENT")
df=arcpy.mapping.ListDataFrames(mxd,"Layers")[0]

#CREATE SITE POLYGON
#Variable to reference in_memory feature class
tempSiteFc = 'in_memory/tempSiteFc'
#Create in_memory feature class
arcpy.CreateFeatureclass_management('in_memory','tempSiteFc','POLYGON')
#Find OPF_PARCEL layer and save the selected record to the new in_memory feature class
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.name == 'Parcel':
arcpy.CopyFeatures_management('Parcel',tempSiteFc)

#Add fields to in_memory fc
arcpy.AddField_management(tempSiteFc,'ITEM','TEXT')
arcpy.AddField_management(tempSiteFc,'APPLICATION_TYPE','TEXT')
arcpy.AddField_management(tempSiteFc,'IS_VALID','TEXT')

#Use update cursor to add user data to fields in in_memory fc
with arcpy.da.UpdateCursor(tempSiteFc,['ITEM','APPLICATION_TYPE','IS_VALID']) as uSiteCursor:
for row in uSiteCursor:
row[0]= itemNo
row[1] = applicationType
row[2] = 'N'
uSiteCursor.updateRow(row)

#Find the Site layer and append the in_memory fc to it
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.name == 'Site':
arcpy.Append_management(tempSiteFc,'Site','NO_TEST')

del tempSiteFc

#CREATE BOUNDARY POLYGON (ONLY IF APPLICATION_TYPE == 'LAND USE AMENDMENT'
if applicationType == 'LAND USE AMENDMENT':
#Variable to reference in_memory feature class
tempBoundaryFc = 'in_memory/tempBoundaryFc'
tempPointFc = 'in_memory/tempPointFc'
#Create in_memory feature class
arcpy.CreateFeatureclass_management('in_memory','tempBoundaryFc','POLYGON')
arcpy.CreateFeatureclass_management('in_memory','tempPointFc','POINT')
arcpy.AddField_management(tempPointFc,'ITEM', 'TEXT')
arcpy.AddField_management(tempPointFc,'ORIG_ID', 'LONG')

#Find OPF_PARCEL layer and save the selected record to the new in_memory feature class
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.name == 'Parcel':
arcpy.CopyFeatures_management('Parcel',tempBoundaryFc)

#Add fields to in_memory fc
arcpy.AddField_management(tempBoundaryFc,'ITEM','TEXT')
arcpy.AddField_management(tempBoundaryFc,'STATUS','TEXT')
arcpy.AddField_management(tempBoundaryFc,'LU_CODE','TEXT')
arcpy.AddField_management(tempBoundaryFc,'IS_VALID','TEXT')

#Use update cursor to add user data to fields in in_memory fc
with arcpy.da.UpdateCursor(tempBoundaryFc,['ITEM','STATUS','LU_CODE','IS_VALID']) as uBoundaryCursor:
for row in uBoundaryCursor:
row[0]= itemNo
row[1] = 'Pending'
row[2] = proposedLuCode
row[3] = 'N'
uBoundaryCursor.updateRow(row)

#Find the Boundary layer and append the in_memory fc to it
for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.name == 'Boundary':
arcpy.Append_management(tempBoundaryFc,'Boundary','NO_TEST')

#del tempBoundaryFc

# create an InsertCursor containing all the fields in ourCentroids, which are all the fields in polyFC plus "ORIG_ID"
cursor = arcpy.da.InsertCursor(tempPointFc, ['SHAPE@XY','ITEM'])

count = 0
# read all features in polyFC
for row in arcpy.da.SearchCursor(tempBoundaryFc, ["SHAPE@",'*','OID@']):
count += 1
arcpy.AddMessage('count: ' + str(count))
#arcpy.AddMessage('row: ' + str(row))
#arcpy.AddMessage('row centroid: ' + str(row[0].centroid))
# create an array to hold a new, modified row
rowArray = []
#arcpy.AddMessage('len row:' + str(len(row)))
for fieldnum in range(1,len(row)):
#arcpy.AddMessage('fieldnum: ' +str(fieldnum))
# if this is the second field [i.e. SHAPE], replace it with the centroid coordinates
if fieldnum == 2:
rowArray.append(row[0].centroid)
elif fieldnum == 15:
rowArray.append(row[fieldnum])

#arcpy.AddMessage('row[fieldnum]: ' + str(row[fieldnum]))
arcpy.AddMessage('rowArray: ' + str(rowArray))
arcpy.AddMessage('rowArray[0]: ' + str(rowArray[0]))
arcpy.AddMessage('rowArray[1]: ' + str(rowArray[1]))
# write the new row to the cursor
cursor.insertRow(rowArray)
del row

for lyr in arcpy.mapping.ListLayers(mxd):
if lyr.name == 'Location':
arcpy.Append_management(tempPointFc,'Location','NO_TEST')

del tempBoundaryFc

It works fine in the arcmap/mxd/script tool world .I'm sure it could be improved upon to be more efficient but the end goal for this work is to have it operating in webappbuilder. So the mxd will be published as a map service and I need to figure out how to turn the script tool into a geoprocessing service, which I believe means having the script work directly with the rest endpoint for the map service.

I've spend some time searching and have looked through the ArcGIS rest service api documentation and am having a difficult time finding documentation and examples, mainly around insert which is what the script does.

Any pointers, direction, links appreciated. Thanks,

Chris

Outcomes