# Generating sequence of numbers based on existing attribute and spatial configuration of polygons using ArcGIS Desktop?

I have come across a very specific problem and I'm sure there must be a way to get around it with Python in ArcGIS, but can't figure it out on my own.

The situation is as follows:

• The initial data consisted of a Fishnet set of equal sized 150x150m polygons stack side-by-side and covering a total area of 10.2x6.9km (that is 68x46 rectangular polygons = 3128 total polygons). Each polygon received a unique ID (labelled TID) based on a quasi-random pattern.
• The next step required the creation of a Fishnet of equally sized 25x25m polygons for the given area, which resulted to the production of a fishnet of 408x276 polygons. Using the Spatial Join command, the TID was transferred to the new smaller rectangular polygons and the resulting table has 112608 rows.
• In the resulting table, there is a column titled TID, which is of type Long and carries the numbers from the underlying layer. More specifically the numbers run from 1 to 3128, and as each polygon was dissected to smaller ones there are now 36 rows associated with each number (therefore 3128*36= 112608 rows).
• In the same table, there is also a column titled GID and is of type Text, as it is meant to carry the ID of each specific polygon, ideally using some form of concatenation per the following formula: TID + "-" + Sequential numbering of the specific rectangle. This column is currently empty.

What I want to do is the following (the result was created manually editing properties): For this to work it needs the following:

• Assign sequential number based on specific pattern (South to North for each column) for each TID.
• Concatenate the TID with the sequential number.

Is this possible with Python and, if so, would it be possible for someone to guide me through?

Currently, I'm a novice learner of Python, hence most concepts are beyond my grasp.

• Not only is it possible, it's quite easy. Unfortunately, in order to prevent GIS SE from devolving into a "code this for me" haven, we have the expectation that coding questions provide a best effort attempt to code a solution. A two dimensional binning algorithm is not beyond anyone's grasp. Apr 16 '18 at 20:27
• That is assuming one understands what a two dimensional binning algorithm is. :) I suspect it's something that will make use of ArcPy loops and spatial data cursors. I'll try to cook up something more advanced, because my early efforts are trying to work around via Field Calculator. Knowing that it can be done is encouraging. Thank you. Apr 16 '18 at 20:57
• I would skip the fishnet tool and draw the polygons in arcpy, that way you get control of extents and would know which row/column you're up to.. another really neat trick is to create an Esri ASCII raster with the cell size of your grids and just pop the numbers into it; ASCII rasters are just text - read about one resources.esri.com/help/9.3/arcgisdesktop/com/gp_toolref/… in the example you can see how easy it would be to write one as text then raster to polygon for both and spatial join - easy as! How does that sound to you? Apr 17 '18 at 0:54

## 1 Answer

Input shows small cells labelled by their physical order:

Solution below mostly uses standard tools:

``````arcpy.AddGeometryAttributes_management("small_net", "CENTROID")
arcpy.Sort_management("small_net", "./sorted.shp", "CENTROID_X ASCENDING;CENTROID_Y ASCENDING")
arcpy.SpatialJoin_analysis("sorted", "Big_net", "./sj.shp", "JOIN_ONE_TO_ONE", .. "HAVE_THEIR_CENTER_IN")
# Replace a layer/table view name with a path to a dataset (which can be a layer file) or create the layer/table view within the script
# The following inputs are layers or table views: "sj"
arcpy.AddField_management("sj", "GROUP_NO", "SHORT")
``````

Use field calculator expression from here to populate new field with sequential number in their group.

Output shows small cells labeled by values from new field:

Using sort on 3 fields and % it can be done with fewer steps.