0

I'm trying to extend lines grouped by same value inside column.

Sample

So what i want to do is extend lines for each line with same WireID only. So for example lines with WireID 2 should all be extened to each other but not extended to lines with WireID 20.

I am trying to do it with grouping script

import itertools, arcpy


def getGrouper(row):
    """Key function to retrieve Field value. Change depending on how you group"""
    return row[0]
def getDate(row):
    """Key function to retrieve Date value. Used in mapping function below"""
    return row[1]
def mostRecentDate(group):
    """Mapping function for retrieving row in each group with most recent date"""
    return max(group, key=getDate)

# I define up here to avoid hard coding into body of script
fc = "YourFeatureClass"
# Column order is important, so define the field names outside cursor definitions
fields = ["Field","Date","Flagged_Field"]
#also putting the index of the flagged field up here, instead of hardcoding
flagindex = 2

# groups is used to store the grouped records for further operations
groups = [] 

# Set search cursor
with arcpy.da.SearchCursor(fc, fields) as cursor:
    # use itertools.groupby to group by Field and then append the results to groups
    for k, g in itertools.groupby(cursor, getGrouper):
        #I do nothing with the key, k, since you only care about the groups, not what they grouped on
        groups.append(list(g))

# At this point you have your groups
# The rest is to do your example of flagging the most recent date

# map groups to the most recent record in each group
flaggedrecords = map(mostRecentDate, groups)
# update records
with arcpy.da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        # Very important to tuple the update row, or else you will not have a match
        if tuple(row) in flaggedrecords:
            # If it is in the flagged records, set the flag
            row[flagindex] = 'Y'
        else:
            # Else, unset the flag
            row[flagindex] = ''
        cursor.updateRow(row)

I came up with idea that if i calculate bearing and then add 180 to it i can use it to create extended lines. Using bearing distance to line with bearing field and new distance field (with for example 2m distance) on End_X, End_Y column will create correct extended lines. If i do the same think using 'bearing+180' field, new distance on Start_X, Start_Y column i will get other extended lines.

My logic is if i group by WireID i can start a loop that use bearing distance to line, creates intersection point and then recreate my lines using new intersection points for each WireID separately. But first i don't know how to code it and secondly is it even possible?

I am working on ArcGIS Pro Basic Licence version 1.3. Can you help me?

  • What does your code so far look like? – PolyGeo Jan 24 '17 at 18:43
  • 2
    operator.itemgetter is useful for itertools and sorting. In answer to your question, yes it is possible with some simple geometry. If you want a quick way, extend your pair of lines some arbitrarily large distance and find where they intersect. Then use that as the new end point for each line. – Paul Jan 25 '17 at 22:13
  • Yep that's what i want to do but i'm not that experience in python to do it :( – Losbaltica Jan 26 '17 at 8:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.