Writing Geometry to Records with Empty Geometry Based on Matching IDs

305
16
Jump to solution
02-21-2020 07:38 AM
deleted-user-PUOLzzPmi8Uz
New Contributor

I have a polyline feature class with a related table attached. Is there a good way to write the feature class line geometry to the records in the related table? Goal is to create a feature class with lines with duplicate geometry, e.g. stacked. Any py code out that can accomplish this?

Thanks!

0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Regular Contributor

import arcpy

inFC = #path toExample.gdb\Lines
inFields = ['ID', 'SHAPE@', "Shape_Length"]
valueDict = {r[0]:r[1] if int(r[2])!= 0 for r in arcpy.da.SearchCursor(inFC, inFields)}

print(valueDict)

with arcpy.da.UpdateCursor(inFC, inFields) as cursor:
for row in cursor:

row[1] = valueDict[row[0]]
print(row[1])
row.updateRow(row)

my dictionary comprehension might be wrong, I always get confused doing them.

the rest miiiight work..

View solution in original post

16 Replies
DavidPike
MVP Regular Contributor

I dont quite get what you want to achieve, can you give more detail? Geometry objects can be accessed with a search cursor and SHAPE@ 

0 Kudos
deleted-user-PUOLzzPmi8Uz
New Contributor

Hi David. Thanks for the quick reply. Essentially, I have a polyline feature class that contains records with geometry and without geometry. Records with geometry are related to those without geometry (Shape_Length = 0) by an ID. Thought it might be easier to break those no geometry records out to a related table, but now I am thinking it would be easier and more straightforward not to do that and just have a cursor write geometry to no geometry records based on matching IDs it encounters in the attribute table. Please to see below table.

0 Kudos
DavidPike
MVP Regular Contributor

Yep I'd recommend that. Let me know if you need a hand with the script.

I thought you meant a related table 'joins and relates'.

0 Kudos
deleted-user-PUOLzzPmi8Uz
New Contributor

Thanks David. Yeah, was able to cobble together so py code and create a data dictionary. From there, not sure how to write the SHAPE@ to the records with NULL Geometry based on the matching ID that has Geometry. Any help/guidance is much appreciated. Thanks!

import arcpy

inFC = #path toExample.gdb\Lines
inFields = ['ID', 'SHAPE@']
valueDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(inFC, inFields)}

print(valueDict)

with arcpy.da.UpdateCursor(inFC, inFields) as updateRows:
for updateRow in updateRows:
keyValue = updateRow[0]
print(keyValue)
if keyValue in valueDict:
updateRow[1] = valueDict[keyValue][0]
print(valueDict[keyValue][0])
updateRows.updateRow(updateRow)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
DavidPike
MVP Regular Contributor

import arcpy

inFC = #path toExample.gdb\Lines
inFields = ['ID', 'SHAPE@', "Shape_Length"]
valueDict = {r[0]:r[1] if int(r[2])!= 0 for r in arcpy.da.SearchCursor(inFC, inFields)}

print(valueDict)

with arcpy.da.UpdateCursor(inFC, inFields) as cursor:
for row in cursor:

row[1] = valueDict[row[0]]
print(row[1])
row.updateRow(row)

my dictionary comprehension might be wrong, I always get confused doing them.

the rest miiiight work..

View solution in original post

deleted-user-PUOLzzPmi8Uz
New Contributor

Thanks David. I got snippet to update the geometry, but unfortunately it only takes the first geometry and applies it to the rest of the non-geometry records. Thinking there needs to be a where clause that says if ID = ID and has geometry, apply geometry to non-geometry records with same ID. Anyways thanks a look. I appreciate the help. I'll keep at it! 

import arcpy

inFC = #".\Example.gdb\Lines"
fields = ['SHAPE@','Unique_ID']

#Update cursor
with arcpy.da.UpdateCursor(inFC, fields) as cursor:
#Set up dictionary to store previous non-null non-empty values
lastNonNullVals={}
for field in fields:
lastNonNullVals[field]="" #set a starting value for each field
for row in cursor:
for field in fields:
#Check if the current cell is null or empty
if row[fields.index(field)] in [None,"", " "]:
#Check if the previous value for the field exists
if lastNonNullVals[field] not in [None,"", " "]:
#Set the cell to the previous non-null non-empty value
row[fields.index(field)]=lastNonNullVals[field]
else:
#if there is a value for the cell, set it so it can be used in the next row(s)
lastNonNullVals[field]=row[fields.index(field)]
cursor.updateRow(row)
0 Kudos
DavidPike
MVP Regular Contributor

I can't see why it's doing that. Could you show the prints or resend the feature as it wouldnt open in arc

0 Kudos
deleted-user-PUOLzzPmi8Uz
New Contributor

Here is the feature class.

0 Kudos
DavidPike
MVP Regular Contributor

Wifi down so back to phone.

The last line in my previous should be cursor.updateRow(row)