Select to view content in your preferred language

Re: Dynamic segmentation with respect to a control factor Additional Requirements

10310
56
05-08-2015 08:06 AM
ChukwuemekaEzeiruaku
Deactivated User

I was able to figure out the error from earlier, but i have one more request.

If i have other fields in the table i want to also show on the out_table for example see table below:

   

DataUniqueIDItem IDBegin EndInput Table
Observation1555100001
555100105
555100217
555100345
555100435
Observation2600200103
600200226
600200335

   

DataUniqueIDBegin EndItem IDOutput Table
Observation1555011001
555131001; 1002
555341001; 1002; 1004
555451001; 1002; 1003; 1004
555571002
Observation2600022001; 2002
600232001; 2002
600352002; 2003
600562003

Is there a way to differentiate between records that are within a certain range(with UniqueID) as shown in the table above, plus have other fields for that range (with same uniqueID) in the output table?

I am really grateful for your help thus far guys, Thanks.

0 Kudos
56 Replies
ChukwuemekaEzeiruaku
Deactivated User

i saw the decimal places too but the ItemIDs should be a Long data type though.

0 Kudos
ChukwuemekaEzeiruaku
Deactivated User

i saw the decimal places too but the ItemIDs should be Integer (Long) data type though.

0 Kudos
ChukwuemekaEzeiruaku
Deactivated User

If its ok with you we can take the dates out of the script, i tried modifying your codes, the run was successful but the output has just one UniqueID and duplicate records of the only uniqueID

0 Kudos
ChukwuemekaEzeiruaku
Deactivated User

i think i know why the last script failed;

i noticed that from the segment, there are overlapping records for which one record/row do have a date and the other didn't... i think that was why we i had the error below(image)Capture.JPG...

0 Kudos
ChukwuemekaEzeiruaku
Deactivated User

is there a line(syntax) i can add to it that'll which will add both the date and the null values for a segment?

0 Kudos
ChukwuemekaEzeiruaku
Deactivated User

thanks Blake for the codes, i was able to get one of the previous script to work(see below)

is there a way to include a loop statement in an attempt to eliminate duplicate records?

  1. def main(): 
  2.     import arcpy 
  3.     import os 
  4.  
  5.     # Local variables 
  6.     sourcegdb = r"N:\TechTemp\BlakeT\Work\TEMP.gdb" 
  7.     table_in = os.path.join(sourcegdb, "SegmentSequence"
  8.  
  9.     # Create output table 
  10.     arcpy.CreateTable_management(sourcegdb, "SegmentSequence_output"
  11.     table_out = os.path.join(sourcegdb, "SegmentSequence_output"
  12.     arcpy.AddField_management(table_out, "UniqueID", "SHORT"
  13.     arcpy.AddField_management(table_out, "Begin", "SHORT"
  14.     arcpy.AddField_management(table_out, "End", "SHORT"
  15.     arcpy.AddField_management(table_out, "SegmentItems", "TEXT", field_length=255
  16.  
  17.     # Identify observation groups 
  18.     sqlprefix = "DISTINCT UniqueID" 
  19.     sqlpostfix = "ORDER BY UniqueID" 
  20.     observations = tuple(uid[0] for uid in arcpy.da.SearchCursor(table_in, ["UniqueID"], sql_clause=(sqlprefix, sqlpostfix))) 
  21.  
  22.     fields_out = ["UniqueID", "Begin", "End", "SegmentItems"
  23.     with arcpy.da.InsertCursor(table_out, fields_out) as i_cursor: 
  24.         for obsv in observations: 
  25.             # Read item and (begin, end) into dictionary 
  26.             fields_in = ["ItemID", "Begin", "End"
  27.             where_clause = "UniqueID = {}".format(obsv) 
  28.             itemsdict = {r[0]:(r[1], r[2]) for r in arcpy.da.SearchCursor(table_in, fields_in, where_clause)} 
  29.  
  30.             # Identify segments 
  31.             allsegments = [i[0] for i in itemsdict.values()] + [i[1] for i in itemsdict.values()] 
  32.             segments = tuple(sorted(set(allsegments))) 
  33.             del allsegments 
  34.  
  35.             # Identify items in each segment 
  36.             for enum, seg in enumerate(segments): 
  37.                 try
  38.                     begin = seg 
  39.                     end = segments[enum + 1
  40.                 except IndexError: 
  41.                     break 
  42.                 seg_items = [k for k, v in itemsdict.items() if v[0] <= begin and v[1] >= end] 
  43.  
  44.                 # Write segment items to output table 
  45.                 row = (obsv, begin, end, str(seg_items).strip('[]')) 
  46.                 i_cursor.insertRow(row) 
  47.  
  48.  
  49. if __name__ == '__main__'
  50.     main()
0 Kudos
BlakeTerhune
MVP Frequent Contributor

I don't get the duplicates when I run the script, so I can't modify the code unless I know what the problem is. Maybe this would be a good time for you to jump into the Python pool and try modifying the code yourself. Use my comments (and the other code posted by Xander) and look up what some of the functions are doing. Empower yourself to solve your own problems and develop new solutions. I don't mean for that to sound harsh, I'm just trying to help.

0 Kudos
ChukwuemekaEzeiruaku
Deactivated User

Thanks a lot Blake for your assistance thus far, i really appreciate every help.

But, I just have one more question/request hope you can help still....

I'm trying to make a script tool for the script for just the input and output parameters and i  came up with:

Capture.JPG

After making the script tool i get the following error:

Capture.JPG

0 Kudos
BlakeTerhune
MVP Frequent Contributor

Looks like it can't find SegmentSequence_output12 in HT_Overlap_Inscope.gdb

Did you verify that feature class is there and that it doesn't have a lock?

0 Kudos
ChukwuemekaEzeiruaku
Deactivated User

Thanks for putting words out there for me not been able to view attachments,and i can now view attachments.

Oh it is supposed to create a GDB table from the user input from user input to also a user output as shown in the script (see picture above)

0 Kudos