1

I had two layers (datasets). I have run the tabulate intersection to get a table of intersected features.

This is the tableenter image description here

Now I want to populate field with values based on "BROJ_UP" field, like this, and do that for every field. For example, value from "BROJ_UP" is 1.5, and values from NAM1 are S, Z, Z1, etc. I want to populate field NAM with text from NAM1 field, but based on value from "BROJ_UP" field.

enter image description here

Even better, it could be great to delete duplicate fields, so I after calculations I want to have just one field. And for every different value in "BROJ_UP" field.

enter image description here

4
  • 3
    There are a LOT of ArcGIS Field Calculator with ArcPy Q&As on this site so I think you should review our self-assembling FAQ on them.
    – PolyGeo
    Commented Jun 26, 2017 at 10:12
  • You can achieve this concatenation with a bit of code or if your processing allows you to process the data as such the spatial join and Merge rule. Read this blog from 2015 discussed here.
    – Hornbydd
    Commented Jun 26, 2017 at 10:50
  • 1
    A couple hints: put an arcpy.da.SearchCursor() on your data, using a SQL query for your values in "BROJ_UP" and iterate through them using an arcpy.da.UpdateCursor() to populate your field. If you want to use python for it, you cannot expect people to write the code for you. You should at least provide something to start with.
    – dru87
    Commented Jun 26, 2017 at 11:24
  • 1
    for start, thank for some useful hints. will try to use them. @dru87, didn't expect from you to write the whole code, just to provide some hints. this is a good start. i will provide some code along the way.
    – Dean7
    Commented Jun 26, 2017 at 11:36

2 Answers 2

1

Dean,

The code below should work for you... you'll change the values in the first 4 lines as needed... (Note though that this script assumes that all the fields specified are text fields, and does not do error checking for things like making sure the "toField" is big enough to hold the concatenated value...)

import arcpy

# Change these values as needed
layerOfIntersectedFeatures = "E:/Temporary Files/My_Layer.shp"
evalField = "BROJ_UP"
fromField = "NAM1"
toField = "NAM"
separator = ", "

print("Make a FeatureLayer for the Intersected Features")
arcpy.MakeFeatureLayer_management(layerOfIntersectedFeatures, "lyr_IntersectedFeatures")

print("Create a list and store all the unique values from the {0} field".format(evalField))
listOfEvaluatedValues= []
rows = arcpy.SearchCursor("lyr_IntersectedFeatures")
for row in rows:
    if row.getValue(evalField) not in listOfEvaluatedValues:
        listOfEvaluatedValues.append(row.getValue(evalField))

print("Loop through each value, and choose records that match that value")
for myValue in listOfEvaluatedValues:
    arcpy.MakeFeatureLayer_management("lyr_IntersectedFeatures", "lyr_" + myValue, "\"{0}\" = '{1}'".format(evalField, myValue))
    listOfRowValues = []
    rows = arcpy.SearchCursor("lyr_" + myValue)
    for row in rows:
        listOfRowValues.append(row.getValue(fromField))   # use this to get all values, even duplicate ones
        #if row.getValue(fromField) not in listOfRowValues: listOfRowValues.append(row.getValue(fromField))   # use this instead if you only want unique values

    print("Build the concatenated string and put it in the {0} field".format(toField))
    concateRowValues = separator.join(listOfRowValues)
    arcpy.CalculateField_management("lyr_" + myValue, toField, "'{0}'".format(concateRowValues), "PYTHON_9.3", "")


print("Delete Identical Rows")
arcpy.DeleteIdentical_management("lyr_IntersectedFeatures", evalField, "", "0")
0

After a thorough search as @PolyGeo pointed out, I managed to find some good solutions, and workarounds. I definitely wanted to do my own script, but I am short on schedule. For now, these solutions will provide results. Maybe these links will be useful for someone else.

This is simple code on how to Concatenate values, worked good for one variables.

These are some solutions and workarounds for my problem. Includes some data manipulations and conversions, and out-of-the-box solutions. That worked good.

And third one, and the best, this article which included the tool worked perfectly. The tool basically does what I wanted, to Concatenate Row Values, based on another row.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.