How to generate grid with offset and assign values to the cells of a the grid?

2996
28
Jump to solution
06-23-2018 08:04 PM
HushamMohamed
Occasional Contributor

I have to generate thousands of cells as a grid, and then I have to assign  values to the cells, so far I know the a fishnet tool can generate the grid but how can I manage to create the offset.  May some one have better Idea.


The details of the grid are as illustrated in the attached diagram.
I have 5 Rows and 6 column,    after the 3rd column  the grid should start with B, other numbering should be the same as A.

0 Kudos
28 Replies
XanderBakker
Esri Esteemed Contributor

Could you provide an example of what it should look like? I notice that you have configured 7 blocks horizontally and vertically (49 blocks in total). Each block consists of 4 columns and 8 rows (?). How does this affect the numbering? What about the character "A", "B" and perhaps "C"? Should the character change every 3 blocks?

0 Kudos
HushamMohamed
Occasional Contributor

Thank you,

Yes I have 49 Blocks now, that mean its 196 Rectangle in total.

"Should the character change every 3 blocks?" No

Each block consists of 4 columns and 8 rows (?) Yes

For this new case I have only one Section A.

Need the number to goes like this from lower left Row>

A183-A196

-------

A15 - A28

A1  To A14

using same logic you used before for the three part- (A-13-1)

The Problem is I have Many cases with different h_block, v_block no. 

I am trying to understand the script logic.

Thanks

0 Kudos
XanderBakker
Esri Esteemed Contributor

Is this what you are looking for? See Attachment.

0 Kudos
XanderBakker
Esri Esteemed Contributor

The code used:

def main():
    import arcpy
    import os

    # settings
    fc_out = r'C:\HH\ce\cemeterypy.gdb\result05'
    # fc_out = r'C:\GeoNet\RectangleBlocks\data.gdb\recblcks04'
    fld_lbl = r'RectangleID'
    sr = arcpy.SpatialReference(2276)  # NAD_1983_StatePlane_Texas_North_Central_FIPS_4202_Feet?

    x_start = 2523853.5  # lower left
    y_start = 7124354.1  # lower left
    rect_width = 10.0
    rect_height = 5.0
    num_blocks_h = 7
    num_blocks_v = 7
    num_rect_in_block_h = 4
    num_rect_in_block_v = 8
    spacing_h = 2.0
    spacing_v = 2.0

    # create output featureclass
    arcpy.env.overwriteOutput = True
    fc_ws, fc_name = os.path.split(fc_out)
    arcpy.CreateFeatureclass_management(fc_ws, fc_name, "POLYGON", None, None, None, sr)

    # add field
    arcpy.AddField_management(fc_out, fld_lbl, "TEXT", None, None, 10)

    # insert cursor
    flds = ("SHAPE@", fld_lbl)
    cnt = 0
    with arcpy.da.InsertCursor(fc_out, flds) as curs:

        # loop through blocks
        for block_h in range(num_blocks_h):
            for block_v in range(num_blocks_v):
                # loop through rectangles
                for rect_h in range(num_rect_in_block_h):
                    for rect_v in range(num_rect_in_block_v):
                        x_min = x_start + block_h * spacing_h + block_h * num_rect_in_block_h * rect_width + rect_h * rect_width
                        y_min = y_start + block_v * spacing_v + block_v * num_rect_in_block_v * rect_height + rect_v * rect_height
                        x_max = x_min + rect_width
                        y_max = y_min + rect_height
                        rectangle = CreateRactangle(x_min, y_min, x_max, y_max, sr)
                        label = GetLabel(block_h, block_v, rect_h, rect_v)
                        cnt += 1
                        if cnt % 50 == 0:
                            print("Processing rectangle: {}".format(cnt))
                        curs.insertRow((rectangle, label, ))

    print("Finished processing {} rectangles".format(cnt))


def CreateRactangle(x_min, y_min, x_max, y_max, sr):
    coords = [[x_min, y_min], [x_min, y_max], [x_max, y_max],
              [x_max, y_min], [x_min, y_min]]
    return arcpy.Polygon(arcpy.Array([arcpy.Point(*coord) for coord in coords]), sr)

def GetLabel(block_h, block_v, rect_h, rect_v):
    # 4 parameters necessary:
    # - block_h = block number horizontally
    # - block_v = block number vertically
    # - rect_h  = rectangle number horizontally
    # - rect_v  = rectangle number vertically

    # determine if the rectangle number horizontally is even or uneven
    even = rect_h % 2 == 0

    # devide the block vertically in two halves
    if rect_v < 4:
        # lower 4 rectangle lines inside a block
        if even:
            # determine the 3rd part of the label
            # values 0 + 1 to 3 + 1 -> (1 - 4)
            part3 = rect_v + 1
        else:
              # values 0 + 5 to 3 + 5 -> (5 - 8)
            part3 = rect_v + 7

        # determine the quadrant of the block (in rows)
        # for instance the second block (=1 when starting to count at 0) vertically
        # will result in 1 + 12 * 1 = 13 (which is where the lower left starts
        # see "A-13-1"
        quad_row = 1 + 28 * block_v

        # determine the quadrant of the block (in cols)
        if rect_h <2:
            # left part of block
            quad_col = block_h * 2
        else:
            # right part of block
            quad_col = 1 + block_h * 2
    else:
        # do the same for the upper 4 rectangle lines inside a block
        if even:
            part3 = rect_v - 3
        else:
            part3 = rect_v + 1
        quad_row = 15 + 28 * block_v
        if rect_h <2:
            quad_col = block_h * 2
        else:
            # lower right
            quad_col = 1 + block_h * 2

    # part 2 of the label is the result of adding quad_row and quad_col
    part2 = quad_row +  quad_col

    # for first 3 blocks horizontally, assign an "A"
    part1 = "A"

    # construct the label combing the 3 parts
    return "{}-{}-{}".format(part1, part2, part3)
if __name__ == '__main__':
    main()
HushamMohamed
Occasional Contributor

Thank you for the great help,  Yes this what I want,  I was confused about these 2 line of code

 quad_row = 1 + 28 * block_v

and 

 quad_row = 15 + 28 * block_v

Thanks again  every thing is clear now.

0 Kudos
XanderBakker
Esri Esteemed Contributor

The 28 refers to 7 blocks times 4 rows for the value to change when passing to a new block (vertically) or to a new quadrant inside a block.

0 Kudos
HushamMohamed
Occasional Contributor

Thanks,  Yeah it took me a while to figure it.

Perfect.

0 Kudos
DanPatterson_Retired
MVP Emeritus

No it doesn't, but you could replicate at the new location, but you would have to use the field calculator to replace your coding to represent the new block values.

Beyond that, it would be a coding effort.  

HushamMohamed
Occasional Contributor

Thank you, do you have any code for replicating new location using the field calculator

0 Kudos