Select to view content in your preferred language

Python script with memory for intermediate data: wrong calculations

619
2
06-21-2023 02:05 AM
Labels (1)
IlkaIllers1
Occasional Contributor III

I have a python script that uses the georprocessing tools Copy, Intersect, MultipartToSinglepart, RepairGeometry, CalculateField, GenerateNearTable, Sort, Dissolve, Buffer, AddField, JoinField, and da.UpdateCursor. 

When I write the intermediate data to a Scratch gdb, the model works fine. However, when I write the intermediate data to the /memory, the calculations from the UpdateCursor are not being calculated correctly. The model runs without error messages, but the returned values are wrong. 

Any ideas why this might be happening?

0 Kudos
2 Replies
DanPatterson
MVP Esteemed Contributor

Is that the order of tool use?

Do all the featureclasses reside in the "\memory" workspace (not in_memory) at the time of processing each step (eg Dissolve)

You are have seen the requirements for "\memory" workspaces I presume

Write geoprocessing output to memory—ArcGIS Pro | Documentation

And finally

UpdateCursor—ArcGIS Pro | Documentation

Note:
Using an UpdateCursor on a layer with a joined table is not supported.

Which you seem to have accounted for by using JoinField instead of AddJoin.

Perhaps showing your script would help...although you mention "model towards the end

 


... sort of retired...
0 Kudos
IlkaIllers1
Occasional Contributor III

Hello @DanPatterson 

I probably mentioned model because it used to be a model which took a long time to run, which is why I wanted to change it into a python script with the intermediate data stored in the memory. It is rather a long script... probably too long to share all of it. I shared the part where the calculation goes wrong.

# Add and Calculate "Area_ha_in", "Area_perc_in"
arcpy.management.AddField(in_table=_Name_criteria_diss, field_name="Area_ha_in", field_type="DOUBLE")[0]
arcpy.management.AddField(in_table=_Name_criteria_diss , field_name="Area_perc_in", field_type="SHORT")[0]
arcpy.management.AddField(in_table=_Name_criteria_diss, field_name="FID_location_criteria", field_type="TEXT", field_length=1000)[0]
with arcpy.da.UpdateCursor(_Name_criteria_diss, ["Area_ha_in","Area_perc_in","FID_location_criteria","SUM_Shape_Area","Shape_Area_location","FID_common","criteria_ID"]) as cursor:
        for row in cursor:
          z = row[3]/10000
          a = float(str(z).replace(',', '.'))
          b = round(a,2)
          row[0] = b
          u = (row[3]/row[4]) *100
          v = float(str(u).replace(',', '.'))
          w = int(round(v,0))
          row[1] = w
          row[2] = row[5] + row[6]
          cursor.updateRow(row)
0 Kudos