Select to view content in your preferred language

Arcpy - ID generator using a Grid format

1280
9
01-17-2018 12:27 PM
SeanPhayakapong2
Emerging Contributor

Would it be possible to create an ID generator with a grid system overlayed. For example, I have a point and I want the ID to be concatenated from the grid's row/column and a unique ID. For example if the point lands on grid the grid 30(column) and 23 (row) with it being the second point created in that box then the ID should look like this: 302302 being CCRRID. I am using 10.2.2 ArcMap.

0 Kudos
9 Replies
DanPatterson_Retired
MVP Emeritus

how many rows and columns?

how many times do you have to do this?

wouldn't joining the grid to the points suffice for the first part, then you could modify/calculate a supplemental key after?

What have you tried so far? This would take some coding and/or geoprocessing steps but what you have tried would go a long way

XanderBakker
Esri Esteemed Contributor

In addition to the questions that Dan asked, if you have 30 * 23 grid cells, you will have 690 possibilities. If the ID must be unique I guess you would have to use 3 positions for the ID. If the points already have information about the grid cells (row and column) you can concatenate and create the CCRRID. If you have polygons to represent the grid cells with info on row and column, you can create the CCRRID there and convert the centre points to a point featureclass. To clear up all the doubts, it would be best to attach the data too.

SeanPhayakapong2
Emerging Contributor

This is primarily used for manholes, but here is the basic schematic that my employers want. 

Example:

Example

The highlighted point is the tenth manhole (fc = 60) that was added in the 8th column (08) and the 22nd row (22) giving an ID by our standard of 600822010.

There will be about a 35x35 grid. For how many times would be as many times new data is added. So far I just coded an ID generator without any new relationship to the grid.

0 Kudos
DanPatterson_Retired
MVP Emeritus

assigning row and columns to the 'fishnet' shouldn't be a problem and the number of the points if done in sequential order, isn't a problem either, if they are added out of order then that will require a manual entry of that value.  Concatenation isn't a problem either. 

SeanPhayakapong2
Emerging Contributor

Hi Dan,

Could you look over my python script? I am having trouble for the continuation of the sequential order for a particular tile in the grid portion. I don't know what I should have for my "ID" variable.

#Setting Parameters for tool
inputLayer = arcpy.GetParameter(0) #Input layer
idField = arcpy.GetParameter(1) #Facility ID Field
i = arcpy.GetParameter(2) # Feature Class Code
grid = r"R:\GISPROJECTS\Dept_WaterUtilities\Tools\OceansideToolbar\Shapefiles\Grids.shp"
arcpy.MakeFeatureLayer_management(grid, 'grid_lyr')
grid_lyr = 'grid_lyr'
field_names = arcpy.UpdateCursor(grid_lyr)
cursor = arcpy.UpdateCursor(inputLayer)

arcpy.AddMessage("Generating ID's...\n")

   for row in cursor:

      #Select by NULL attribute
      arcpy.SelectLayerByAttribute_management(inputLayer, 'NEW_SELECTION', '"FACILITYID" is NULL')

   for field in field_names:
   C = field.getValue('COL')
   R = field.getValue('ROW')

   Fc = str(i).zfill(2)
   Col = str(C).zfill(2)
   Row = str(R).zfill(2)
   Fno = str(ID).zfill(3)
   Code = Fc + Col + Row + Fno

row.setValue(idField, Code)
cursor.updateRow(row)

ID += 1

arcpy.Delete_management('grid_lyr')
arcpy.AddMessage("\n\nFinished...")
arcpy.AddMessage("Last ID created: " + Code)

0 Kudos
DanPatterson_Retired
MVP Emeritus

How did the script run if ID isn't defined?..  Code formatting... would help too

0 Kudos
SeanPhayakapong2
Emerging Contributor
     #Setting Parameters for tool
     inputLayer = arcpy.GetParameter(0) #Input layer
     idField = arcpy.GetParameter(1)     #Facility ID Field
     i = arcpy.GetParameter(2) # Feature Class Code
     grid = r"R:\GISPROJECTS\Dept_WaterUtilities\Tools\OceansideToolbar\Shapefiles\Grids.shp"
     arcpy.MakeFeatureLayer_management(grid, 'grid_lyr')
     grid_lyr = 'grid_lyr'
     field_names = arcpy.UpdateCursor(grid_lyr)
     cursor = arcpy.UpdateCursor(inputLayer)
     
     for field in field_names:
          C = field.getValue('COL')
          R = field.getValue('ROW') 
     
     arcpy.AddMessage("Generating ID's...\n")
     
     for row in cursor:
     
          #Select by NULL attribute
          selection = arcpy.SelectLayerByAttribute_management(inputLayer, 'NEW_SELECTION', '"FACILITYID" is NULL')
          
          arcpy.MakeFeatureLayer_management(selection, 'selected_lyr')
          selected_grid = arcpy.SelectLayerByLocation_management (grid_lyr, 'intersect', 'selected_lyr')
          arcpy.MakeFeatureLayer_management(selected_grid, 'selected_grid')
          ID = arcpy.GetCount_management(arcpy.SelectLayerByLocation_management (inputLayer, 'intersect', selected_grid)
          
                FC = str(i).zfill(2)
          Col = str(C).zfill(2)
          Row = str(R).zfill(2)
          Fno = str(ID).zfill(3)
          Code = FC + Col + Row + Fno
        
          row.setValue(idField, Code)
          cursor.updateRow(row)
            
          ID += 1
        
     arcpy.Delete_management('grid_lyr')
     arcpy.Delete_management('selected_lyr')
     arcpy.Delete_management('selected_grid')
     arcpy.AddMessage("\n\nFinished...")
     arcpy.AddMessage("Last ID created: " + Code)
0 Kudos
SeanPhayakapong2
Emerging Contributor

Great, thank you, I will look more into the fishnet. I think at that point it wont matter if its in order or not as long as its all within each cell. 

0 Kudos
DanPatterson_Retired
MVP Emeritus

line 27, indent by 4 spaces

Code = FC + Col + Row + Fno  

If the above aren't all text or numbers you would be advised to 

Code = "{}{}{}{}".format(FC, Col, Row, Fno)

and Code will obviously going into a text field.

0 Kudos