BLOG
|
That was it! Both LINKID are Long data types. As soon as I defined them both as "int" on lines 18 and 35, it worked! Richard, thank you so much for your time on this. I learned a great deal. Below is my result.
... View more
10-28-2019
08:54 AM
|
0
|
0
|
7736
|
BLOG
|
Thanks Richard! I appreciate your time. No errors, but still getting 0 counts. The related count diagnostics has helped. Both the Feature Class Primary Key (LINKID) and the Related Table Foreign Key (LINKID) match, but I pull back only a subset of the total rows in the Relate Table. However, when I change the following line 12 from: relateFieldsList = ["LINKID", "COMNAME", "FEAT_LABEL"]
to relateFieldsList = ["*"] I get the count of the entire record set, which is what I would expect. Anyways, I'll keep at it. Thanks!
... View more
10-25-2019
02:52 PM
|
0
|
0
|
7736
|
BLOG
|
Hi Richard. Thanks for reply! Please see below. The Geodatabase comes with the relationship class built already, but are they are in a Feature Dataset, so I wonder if this is causing the issue somehow. Here is my working code: # Initialize a global dictionary for a related feature class/table
relateDict = {}
def FindLabel ( [LINKID] ):
# declare the dictionary global so it can be built once and used for all labels
global relateDict
# only populate the dictionary if it has no keys
if len(relateDict) == 0:
# Provide the path to the relate feature class/table
# Provide the path and table name to the relate feature class/table
relateFC = r"D:\delbay_v3_3.gdb\Envr_hab_ls_v3_3_delbay_sp_02"
# create a field list with the relate field first (LINKID),
# followed by sort field(s) (COMNAME), then label field(s) (COMNAME, "FEAT_LABEL")
relateFieldsList = ["LINKID", "COMNAME", "COMNAME", "FEAT_LABEL"]
# process a da search cursor to transfer the data to the dictionary
with arcpy.da.SearchCursor(relateFC, relateFieldsList) as relateRows:
for relateRow in relateRows:
# store the key value in a variable so the relate value
# is only read from the row once, improving speed
relateKey = relateRow[0]
# if the relate key of the current row isn't found
# create the key and make it's value a list of a list of field values
if not relateKey in relateDict:
# [searchRow[1:]] is a list containing
# a list of the field values after the key.
relateDict[relateKey] = [relateRow[1:]]
else:
# if the relate key is already in the dictionary
# append the next list of field values to the
# existing list associated with the key
relateDict[relateKey].append(relateRow[1:])
# delete the cursor, and row to make sure all locks release
del relateRows, relateRow
# store the current label feature's relate key field value
# so that it is only read once, improving speed
labelKey = [LINKID]
# variables to adjust table cell sizes
iMaxLbl1Sz = 0
iMaxLbl2Sz = 0
iSpace = 5
# determine if the label key is in the dictionary
if labelKey in relateDict:
# sort the list of the list of fields
sortedList = sorted(relateDict[labelKey])
# process the sorted list to determine cell spacing
for fieldValues in sortedList:
strLabel1 = fieldValues[1]
strLabel2 = '{:.4f}'.format(fieldValues[0])
if (len(strLabel1) > iMaxLbl1Sz):
iMaxLbl1Sz = len(strLabel1)
if (len(strLabel2) > iMaxLbl2Sz):
iMaxLbl2Sz = len(strLabel2)
# clean up the fieldValues variable once the for loop is done
del fieldValues
# My label has a key value header followed by a record count
expression = labelKey
expression += '\n<UND>Species Count = {}</UND>'.format(len(sortedList)) + '_' * (iMaxLbl1Sz + iMaxLbl2Sz + iSpace + 1 - len('Species = {}'.format(len(sortedList))))
# process the sorted list
for fieldValues in sortedList:
strLabel1 = fieldValues[1]
strLabel2 = '{:.4f}'.format(fieldValues[0])
k1 = (iMaxLbl1Sz - len(strLabel1)) + 2
k2 = iSpace + (iMaxLbl2Sz - len(strLabel2)) - 3
# append related data to the label expression
# my label shows a list of related
# cross streets and measures sorted in driving order
expression += '\n' + strLabel1 + "." * k1
expression += "|"
expression += "." * k2 + strLabel2 + "|"
# clean up all list variables after completing the for loops
del sortedList, fieldValues
else:
# My label has a key value header followed by a record count
expression = labelKey
expression += '\n<UND>Species Count = 0</UND>'
# return the label expression to display
return expression
... View more
10-25-2019
06:20 AM
|
0
|
0
|
7736
|
BLOG
|
Hi Richard. Great work. I managed to get your code running on my feature class without error, but the related part of the label is blank. The counts, in this case species count, for every polygon is 0. I wonder if the reason is a memory limit you mentioned in your write up since the geodatabase is fairly large. But more likely a code modification error on my part. Anyways, I've post a link to the geodatabase I am working with if you have time to take a quick look. Any help/advice is much appreciated. I'll will continue to troubleshoot. Thanks! Landscape 3.3 Data for Delaware Bay Region of New Jersey # Provide the path and table name to the relate feature class/table
relateFC = r"D:\delbay_v3_3.gdb\Envr_hab_ls_v3_3_delbay_sp_02"
# create a field list with the relate field first (LINKID),
# followed by sort field(s) (COMNAME), then label field(s) (COMNAME, "FEAT_LABEL")
relateFieldsList = ["LINKID", "COMNAME", "COMNAME","FEAT_LABEL"] Ultimately, going for a simple label like below. Species Count = 5
_________________________________
Bald Eagle...............Foraging
Bald Eagle...............Nest
Northern Myotis..........Nest
Rapids Clubtail..........Nest
Cliff Swallow............Breeding Sighting
... View more
10-24-2019
02:38 PM
|
0
|
0
|
7736
|
POST
|
Hey Dan. I've learned much today from you and folks on this thread since I originally posted. I can appreciate the intricacies of what I am trying to accomplish and that it is not as straightforward as I hoped. I'll keep at it. I appreciate the help. Thanks!
... View more
03-22-2019
03:05 PM
|
1
|
0
|
799
|
POST
|
Thanks Joshua. This helps. I'll need to formulate my approach around this behavior to be sure my code is repeatable every time.
... View more
03-22-2019
02:59 PM
|
0
|
0
|
799
|
POST
|
Thanks again Dan for your help. My ultimate goal was to get the count replicated for the sequence in order to do further processing, like a definition query (e.g. Count > 2) or perhaps some other analysis on it. If replication can achieved, great!
... View more
03-22-2019
01:55 PM
|
0
|
2
|
799
|
POST
|
Hey Dan. I got study your output a bit more and yes, I think this is what I am after actually. Just need to capture the number in the third column of that array and use it in the cursor so I can write it to the 'Count' field. Any hints on how to do that?
... View more
03-22-2019
12:42 PM
|
0
|
4
|
2541
|
POST
|
Thanks Joshua. Wasn't aware of the insertion order could cause issues. I always assumed it starts at row 0 every time.
... View more
03-22-2019
12:05 PM
|
0
|
2
|
799
|
POST
|
Hi Joshua. Thanks for your time. It comes to me as Feature Class. The feature class geometry and the 'Consecutive' field comes to me ordered based on how the data is collected in the field. Its collected the same each time.
... View more
03-22-2019
09:38 AM
|
0
|
4
|
799
|
POST
|
Hi Dan. I didn't see your explanation at first in your code block when I was reading it. Let me think about it more. It may be what I need.
... View more
03-22-2019
09:22 AM
|
0
|
0
|
2541
|
POST
|
Hi Dan. Thanks for the reply! I like the numpy approach as it simplifies things quite a bit, but don't think the return is precisely what I need. I should clarify, the random_list was something I was playing with to test the def count_consec code. The def returns counts as I would expect from any list with or without consecutive numbers. Now just figuring out how to leverage all or part of that def or potentially numpy into the Update Cursor code.
... View more
03-22-2019
09:18 AM
|
0
|
1
|
2541
|
POST
|
Yep! The field comes in to me in a specific way, so I don't sort the field or count all instances of specific number. I think about it like making subsets for each range of consecutive numbers the code finds going down the column, then counting how many numbers are in the subset. For example; starting at the first row: 1,2,3,4 (first subset has 4 consecutive), the next number in the sequence is 8 (second subset with only 1 consecutive), next numbers are 34,35,36,37,38 (third subset with 5), next numbers may be 1,2,3,4 again (fourth subset has 4 consecutive), and so on down the rows. Then, the 'Count' field would contain the corresponding count of the subset.
... View more
03-22-2019
08:43 AM
|
0
|
15
|
2541
|
POST
|
Hi. Both code blocks work independently of each other. I don't understand how to get my desired result using the return from def count_consec into the update row cursor in order to write the count of consecutive numbers into the Count field. Probably missing something fundamental. My gut is telling me the count code in the update cursor code block needs to be simplified and rewritten with the code from the def. I appreciate the reply.
... View more
03-22-2019
08:12 AM
|
0
|
17
|
2541
|
Title | Kudos | Posted |
---|---|---|
1 | 08-21-2017 11:44 AM | |
1 | 03-22-2019 03:05 PM |
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:23 AM
|