Select to view content in your preferred language

GP Service insert cursor error

755
3
12-15-2018 08:41 AM
DEVAPP
by
Occasional Contributor

Hi at all,

I have developed a Script python that read some different rest service, creates an empty feature and table with "in_memory" method and after use the insert cursor to insert data into in memory Feature and Table.

The script works very fine into Desktop and I have published on ArcGIS Server.

When I run the GP by rest folder I receive the error:

Error executing tool. Spatial Job ID: j375ccc4c96d14276854f03877963f6cc : Traceback (most recent call last): File "/opt/esri/arcgis/data/server/arcgissystem/arcgisinput/GP/Script.GPServer/extracted/v101/script/Script.py", line 162, in ioid = InCur.insertRow(row) RuntimeError: Cannot find field 'field name' Failed to execute  

Ther filed name exsist into in_memory table.

This is a part of the script

import arcpy
import sys
import urllib
import urllib2
import datetime
import sys, os


bbox = '\''+arcpy.GetParameterAsText(0)+'\''
starttime = arcpy.GetParameterAsText(1)
endtime = arcpy.GetParameterAsText(2)


service_params = [
    {
        'url': "http://xxxxxxxx/MapServer",
        'layerID': '14',
        'fields': 'ORIG_CODE',
        'name': 'PGRA_H',
        'query':'1=1'
    },
    {
        'url': "http://xxxxxxxxx/MapServer",
        'layerID': '15',
        'fields': 'ORIG_CODE',
        'name': 'PGRA_M',
        'query':'1=1'
    },
    {
        'url': "http://xxxxxxxxxx/MapServer",
        'layerID': '16',
        'fields': 'ORIG_CODE',
        'name': 'PGRA_L',
        'query':'1=1'
    },
    {
        'url': "http://xxxxxxxxxxxxxxxxxxx/MapServer",
        'layerID': '0',
        'fields': 'PREV6_BOLLETTINO, PREV6_TIME, PREV6_ACCUMULO, PREV6_PROB_PREC, PREV6_PK',
        'name': 'meteo',
        'query': 'PREV6_TIME > \''+starttime+'\' AND PREV6_TIME < \''+endtime+'\''

    },
    {
        'url': "http://xxxxxxxxxx/MapServer",
        'layerID': '3',
        'fields': 'OR_ID, DEFINIZIONE, STATO_SISTEMA, STATO_UTENTE, LO_ORIGINE, LO_DESTINAZIONE,KM_INIZIO, KM_FINE,LT_PERFORMANCE',
        'name': 'tratte',
        'query': '1=1'

    },
    {
        'url': "http://xxxxxxxxxxxxxxxxxx/MapServer",
        'layerID': '0',
        'fields': 'COD_PER_IT',
        'name': 'PAIfrane',
        'query':'1=1'
    }
]


def buildUrl(serviceUrl, layerId, fields, bbox, query):
    params = { 'outFields' : fields, 'geometry' : bbox, 'where': query, 'geometryType' : 'esriGeometryEnvelope', 'returnGeometry': 'true', 'spatialRel':'esriSpatialRelIntersects',
               'f': 'pjson', 'inSR': '4326'}
    params_encoded = urllib.urlencode(params)
    url = serviceUrl + "/" + layerId + "/query?" + params_encoded
    arcpy.AddMessage(url)
    return url

def createFeatureSet(serviceUrl, layerId, fields, bbox, query):
    fsURL = buildUrl(serviceUrl, layerId, fields, bbox, query)
    fs = arcpy.FeatureSet()
    fs.load(fsURL)
    return fs


layer = {}

for s in service_params:
    url = s['url']
    layerId = s['layerID']
    fields = s['fields']
    name = s['name']
    query = s['query']
    fs_item = createFeatureSet(url, layerId, fields, bbox, query)
    fs_item.save("in_memory/"+ name)
    layer[name] = "in_memory/"+ name
print layer

#Segmentazione tratte

arcpy.Merge_management("in_memory/PGRA_H;in_memory/PGRA_M;in_memory/PGRA_L", "in_memory/PGRA")
arcpy.Dissolve_management(layer['meteo'], "in_memory/meteopoint_dissolve", "PREV6_PK")
arcpy.SplitLineAtPoint_management(layer['tratte'],'in_memory/meteopoint_dissolve','in_memory/tratte_split_meteopoints',search_radius='10 Meters')
arcpy.FeatureToPoint_management('in_memory/tratte_split_meteopoints','in_memory/tratte_mid_points','INSIDE')
arcpy.SplitLineAtPoint_management('in_memory/tratte_split_meteopoints','in_memory/tratte_mid_points','in_memory/tratte_split_midpoints',search_radius='10 Meters')
arcpy.SpatialJoin_analysis('in_memory/tratte_split_midpoints','in_memory/meteopoint_dissolve','in_memory/tratte_join_meteopoint','JOIN_ONE_TO_ONE',match_option='INTERSECT',search_radius='10 Meters')
arcpy.Dissolve_management('in_memory/tratte_join_meteopoint', 'in_memory/tratte_segmentate',"PREV6_PK", statistics_fields="OR_ID FIRST;DEFINIZIONE FIRST;STATO_SISTEMA FIRST;STATO_UTENTE FIRST;LO_ORIGINE FIRST;LO_DESTINAZIONE FIRST;KM_INIZIO FIRST;KM_FINE FIRST;LT_PERFORMANCE FIRST", multi_part="SINGLE_PART", unsplit_lines="DISSOLVE_LINES")
print arcpy.GetCount_management('in_memory/tratte_segmentate')

arcpy.AddMessage('Eseguita segmentazione tratte')

#Intersezione con pericolo idrogeologico (PGRA e PAI)

arcpy.Identity_analysis('in_memory/tratte_segmentate','in_memory/PGRA','in_memory/identity_PGRA')
arcpy.Identity_analysis('in_memory/identity_PGRA',layer['PAIfrane'],'in_memory/identity_PGRA_PAI')
print arcpy.GetCount_management('in_memory/identity_PGRA_PAI')
arcpy.AddMessage('Eseguite intersezioni con criticita idrogeologica')

#Ciclo punti meteo e creazione output tratte

arcpy.CreateTable_management('in_memory','tableoutput','in_memory/identity_PGRA_PAI')
arcpy.JoinField_management("in_memory/tableoutput",'PREV6_PK',layer['meteo'],'PREV6_PK',['PREV6_BOLLETTINO','PREV6_TIME','PREV6_ACCUMULO','PREV6_PROB_PREC'])
arcpy.AddMessage('Iniziato ciclo punti meteo per associazione informazioni\n\n')

infoarray = []

fields = arcpy.ListFields('in_memory/tableoutput')
for f in fields:
    print(f.name)

PointDict = dict([(r[0], (r[1],r[2],r[3],r[4],r[5])) for r in arcpy.da.SearchCursor(layer['meteo'], ['OID@','PREV6_PK','PREV6_BOLLETTINO','PREV6_TIME','PREV6_ACCUMULO','PREV6_PROB_PREC'])])
for k, v in PointDict.items():
    arcpy.AddMessage('Dictionary key: {}, valori: {}'.format(k,v))
    with arcpy.da.SearchCursor('in_memory/identity_PGRA_PAI', ['PREV6_PK','FIRST_OR_ID','FIRST_DEFINIZIONE','FIRST_STATO_SISTEMA','FIRST_STATO_UTENTE','FIRST_LO_ORIGINE','FIRST_LO_DESTINAZIONE','FIRST_KM_INIZIO','FIRST_KM_FINE','FIRST_LT_PERFORMANCE','ORIG_CODE','COD_PER_IT','OID@']) as TratteCursor:
        with arcpy.da.InsertCursor('in_memory/tableoutput',['PREV6_PK','FIRST_OR_ID','FIRST_DEFINIZIONE','FIRST_STATO_SISTEMA','FIRST_STATO_UTENTE','FIRST_LO_ORIGINE','FIRST_LO_DESTINAZIONE','FIRST_KM_INIZIO','FIRST_KM_FINE','FIRST_LT_PERFORMANCE','ORIG_CODE','COD_PER_IT','OID@']) as InCur:
            for row in TratteCursor:
                if row[0] == v[0]:
                    ioid = InCur.insertRow(row)
                    infoarray.append(ioid)

    with arcpy.da.UpdateCursor('in_memory/tableoutput',['OID@','PREV6_BOLLETTINO','PREV6_TIME','PREV6_ACCUMULO','PREV6_PROB_PREC']) as UpCur:
        for uprow in UpCur:
            if uprow[0] in infoarray[:]:
                uprow[1] = v[1]
                uprow[2] = v[2]
                uprow[3] = v[3]
                uprow[4] = v[4]
                UpCur.updateRow(uprow)

    del infoarray[:]


#Export frane e alluvioni

arcpy.SpatialJoin_analysis('in_memory/PGRA',layer['tratte'],'in_memory/Alluvioni_interessate','JOIN_ONE_TO_ONE','KEEP_COMMON','','INTERSECT')
arcpy.SpatialJoin_analysis(layer['PAIfrane'],layer['tratte'],'in_memory/Frane_interessate','JOIN_ONE_TO_ONE','KEEP_COMMON','','INTERSECT')

arcpy.AddMessage('Eseguita estrazione Frane e Alluvioni')


#Calcolo livello di criticita' idrogeologica

arcpy.AddField_management('in_memory/tableoutput','CR_IDROGEO','TEXT','','',2,'CRITICITA\' IDROGEOLOGICA')
arcpy.AddField_management('in_memory/tableoutput','PRECIPITAZ','LONG','','','','PRECIPITAZIONI')
arcpy.CalculateField_management('in_memory/tableoutput','PRECIPITAZ','!PREV6_ACCUMULO!',"PYTHON_9.3")
arcpy.AddField_management('in_memory/tableoutput','DATAORA','DATE')
arcpy.CalculateField_management('in_memory/tableoutput','DATAORA',"datetime.datetime.strptime(!PREV6_TIME!, '%Y-%m-%dT%H:%M:%S')","PYTHON_9.3")

def AlterAlias(fc):
    AliasDict= dict({'PREV6_PK':['PREV6_PK','ID PUNTO METEO'],'FIRST_OR_ID':['OR_ID','SEDE TECNICA'],'FIRST_DEFINIZIONE':['DEFINIZIONE','DEFINIZIONE'],'FIRST_STATO_SISTEMA':['STATO_SIS','STATO SISTEMA'],'FIRST_STATO_UTENTE':['STATO_UT','STATO UTENTE'],'FIRST_LO_ORIGINE':['LO_ORIG','LOCALITA\' DI ORIGINE',],'FIRST_LO_DESTINAZIONE':['LO_DEST','LOCALITA\' DI DESTINAZIONE'],'FIRST_KM_INIZIO':['KM_INIZIO','KM INIZIO'],'FIRST_KM_FINE':['KM_FINE','KM FINE'],'FIRST_LT_PERFORMANCE':['LINEA','LINEA'],'ORIG_CODE':['PGRA_CODE','PERICOLO ALLUVIONE'],'COD_PER_IT':['PAI_F_CODE','PERICOLO FRANA'],'PREV6_BOLLETTINO':['PREV6_BOLLETTINO','BOLLETTINO METEO'],'PREV6_TIME':['PREV6_TIME','ORARIO PREVISIONE'],'PREV6_ACCUMULO':['PREV6_ACCUMULO','PRECIPITAZIONI'],'PREV6_PROB_PREC':['PREV6_PROB_PREC','PROBABILITA\' DI PRECIPITAZIONI']})
    for k, v in AliasDict.items():
        arcpy.AlterField_management(fc,k,v[0],v[1])

AlterAlias('in_memory/tableoutput')

#I range attuali vanno sostituiti con quelli presenti nel documento dell'applicativo METEO

PGRADict = {xrange(10, 20):{'P1':'B','P2':'B','P3':'M'},xrange(20,30):{'P1':'B','P2':'M','P3':'A'},xrange(30,9999):{'P1':'M','P2':'A','P3':'A'}}

PAIDict = {xrange(10, 20):{1:'B',2:'B',3:'M',4:'M'},xrange(20,30):{1:'B',2:'M',3:'M',4:'A'},xrange(30,9999):{1:'B',2:'M',3:'A',4:'A'}}

with arcpy.da.UpdateCursor('in_memory/tableoutput',['PGRA_CODE','PAI_F_CODE','PRECIPITAZ','CR_IDROGEO','OID@']) as UpCur:
    for row in UpCur:
        arcpy.AddMessage('\n \n Precipitzioni = {}, PGRA = {}, PAI = {}, OID = {}'.format(row[2],row[0],row[1],row[4]))
        for LAll, LAllinfo in PGRADict.items():
            arcpy.AddMessage('   Alluvioni range: {}, valori: {}'.format(LAll,LAllinfo))
            if row[2] in LAll:
                arcpy.AddMessage('      Le precipitazioni rientrano nel range {} per il pericolo alluvione'.format(LAll))
                for Allkey in LAllinfo:
                    arcpy.AddMessage('         Valore chiave: {}'.format(Allkey))
                    if row[0] == Allkey:
                        arcpy.AddMessage('            Valore associato: {}'.format(LAllinfo[Allkey]))
                        row[3] = str(row[3]).replace('None','')+str(LAllinfo[Allkey])
                        UpCur.updateRow(row)
        for LFra, LFrainfo in PAIDict.items():
            arcpy.AddMessage('   Frane range: {}, valori: {}'.format(LFra,LFrainfo))
            if row[2] in LFra:
                arcpy.AddMessage('      Le precipitazioni rientrano nel range {} per il pericolo frana'.format(LFra))
                for Frakey in LFrainfo:
                    arcpy.AddMessage('         Valore chiave: {}'.format(Frakey))
                    if row[1] == Frakey:
                        arcpy.AddMessage('            Valore associato: {}'.format(LFrainfo[Frakey]))
                        row[3] = str(row[3]).replace('None','')+str(LFrainfo[Frakey])
                        UpCur.updateRow(row)

arcpy.AddMessage('Terminato calcolo livello di criticita idrogeologica')


arcpy.SetParameter(3, arcpy.MakeFeatureLayer_management("in_memory/Alluvioni_interessate","Alluvioni_interessate"))

arcpy.SetParameter(4, arcpy.MakeFeatureLayer_management("in_memory/Frane_interessate","Frane_interessate"))

arcpy.SetParameter(5, arcpy.MakeTableView_management("in_memory/tableoutput","tableoutput"))
arcpy.SetParameter(6, arcpy.MakeFeatureLayer_management("in_memory/identity_PGRA_PAI","identity_PGRA_PAI"))

Any idea why the server gives the error on insert cursor into in_memory?

Thanks

0 Kudos
3 Replies
DanPatterson_Retired
MVP Emeritus

where is 'infoarray'

the code is hard to read without formatting

/blogs/dan_patterson/2016/08/14/script-formatting 

DEVAPP
by
Occasional Contributor

Hi Dan,

i have updated my code using a correct format and i have added all my code.

I hope that you could help me with this issues

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

The error message implies the cursor is looking for a field named 'field name', but you aren't passing the cursor anything remotely like that name.  I see you are working with field aliases with in_memory/tableoutput, maybe try not using field aliases?  Honestly, I am not sure why you would be getting that error.