AnsweredAssumed Answered

Grid inside polygon

Question asked by p.racz on Aug 30, 2017
Latest reply on Sep 1, 2017 by p.racz

I am trying to create a grid inside polygon. I applied a fishnet solution. Nevertheless, when I run the code the polygon is expanding (see attached photos). Could you point me what I am doing wrong? I am using the code that I have found in one of discussions. I have changed many things, but maybe I have omitted something important. Many thanks for help!

class NetButton(object):
    """Implementation for NetButton.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        in_polys= ExcavationZone #established earlier as global
        out_loc= "\\\\abc\\home\\xxx\\Documents\\moje dokumenty\\xyz\\arcgis" #final grid
        cell_x= 0
        cell_y= 0
        n_rows= 30
        n_cols= 30
   

        # Create file gdb to store data
        gdb = str(arcpy.CreateFileGDB_management(out_loc, 'Fishnets.gdb').getOutput(0))

        # spatial reference
        arcpy.env.outputCoordinateSystem = arcpy.Describe(in_polys).spatialReference
   
        # Loop thru rows of input polygons
        with arcpy.da.SearchCursor(in_polys, ['SHAPE@', 'OID@']) as rows:
            for row in rows:
                ext = row[0].extent
                st = '%f %f' %(ext.XMin, ext.YMin)
                orien = '%f %f' %(ext.XMin, ext.YMax)

                # Create fishnet
                out = os.path.join(gdb, 'fish_{0}'.format(row[1]))
                arcpy.CreateFishnet_management(out, st, orien, cell_x,
                                           cell_y, n_rows, n_cols,
                                           labels='NO_LABELS',
                                           geometry_type='POLYGON')

        # set workspace to new gdb
        arcpy.env.workspace = gdb
        fishnets = arcpy.ListFeatureClasses()
        targ = fishnets[0]
        for i, fish in enumerate(fishnets):
       
            # Add field for original polygon ID
            fid = fish.split('_')[1]
            arcpy.AddField_management(fish, 'POLY_ID', 'LONG')
            with arcpy.da.UpdateCursor(fish, ['POLY_ID']) as rows:
                for row in rows:
                    row[0] = fid
                    rows.updateRow(row)

            # append fishnets into one feature class
            if i > 0:
                arcpy.Append_management([fish], targ, 'NO_TEST')
                arcpy.Delete_management(fish)
                print 'Appended: {0}'.format(fish)

        print 'Done'
        return

        if __name__ == '__main__':

            polys = "\\\\abc\\home\\xxx\\Documents\\moje dokumenty\\abc\\arcgis\\PolygonGrid" 
            loc = "\\\\abc\\home\\xxx\\Documents\\moje dokumenty\\abc\\arcgis" 
   
        CreateFishnetsForFeats(polys, loc, 100, 100, 22, 22)

Attachments

Outcomes