http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//007000000028000000 arcpy.env.workspace = "Z:/work/ortho/Python_scripting/Test.gdb" # This procedure draw one rectangle in respect with the start corner point given, the orientation given and the given width and length. The rectangle is drawn in the clockwise direction def createRectangleAtPoint(x, y, length, width, orientation, layer): from math import * # retrieve the characteristics of "layer" (used later on) desc = arcpy.Describe(layer) if orientation >= 0: corner_1 = (x, y) corner_2 = (x + length * math.cos(math.radians(orientation)), y + length * math.sin(math.radians(orientation))) corner_3 = (x + length * math.cos(math.radians(orientation)) + width * math.sin(math.radians(orientation)), y + length * math.sin(math.radians(orientation)) - width * math.cos(math.radians(orientation))) corner_4 = (x + width * math.sin(math.radians(orientation)), y - width * math.cos(math.radians(orientation))) array = arcpy.Array([arcpy.Point(*coords) for coords in [corner_1, corner_2, corner_3, corner_4, corner_1]]) rect = arcpy.Polygon(array, desc.spatialReference) with arcpy.da.InsertCursor(layer, "SHAPE@") as cursor: cursor.insertRow((rect,)) else: if orientation < 0: corner_1 = (x, y) corner_2 = (x - length * math.cos((-1) * math.radians(orientation)), y + length * math.sin((-1) * math.radians(orientation))) corner_3 = (x - length * math.cos((-1) * math.radians(orientation)) - width * math.sin((-1) * math.radians(orientation)), y + length * math.sin((-1) * math.radians(orientation)) - width * math.cos((-1) * math.radians(orientation))) corner_4 = (x - width * math.sin((-1) * math.radians(orientation)), y - width * math.cos((-1) * math.radians(orientation))) array = arcpy.Array([arcpy.Point(*coords) for coords in [corner_1, corner_2, corner_3, corner_4, corner_1]]) rect = arcpy.Polygon(array, desc.spatialReference) with arcpy.da.InsertCursor(layer, "SHAPE@") as cursor: cursor.insertRow((rect,)) import arcpy def Procedure_Make_Grid(input_feature_class, length, width, ): fc = input_feature_class list_x = []; list_y = []; from arcpy import env # Set workspace env.workspace = "Z:/Work/ORTHO/Python_scripting" # Set local variables out_path = "Z:/Work/ORTHO/Python_scripting" geometry_type = "POLYGON" template = "DISABLED" has_m = "DISABLED" has_z = "DISABLED" # Use Describe to get a SpatialReference object spatial_reference = arcpy.Describe("Z:/Work/ORTHO/Python_scripting/Contaminated_area_allCopy.shp").spatialReference # Execute CreateFeatureclass Plan = arcpy.CreateFeatureclass_management(out_path, "Plan", geometry_type, "Contaminated_area_allCopy", has_m, has_z, spatial_reference) Work_layer = arcpy.CreateFeatureclass_management(out_path, "Work_layer", geometry_type, "Contaminated_area_allCopy", has_m, has_z, spatial_reference) # Enter for loop for each feature arcpy.AddField_management("Contaminated_area_allCopy", "poly_angle", "SHORT", 9, "", "", "refcode", "NULLABLE", "REQUIRED") arcpy.CalculatePolygonMainAngle_cartography("Contaminated_area_allCopy", "poly_angle") for row in arcpy.da.SearchCursor(fc, ["SHAPE@", "poly_angle", "FID"]): partnum = 0 # Step through each part of the feature for part in row[0]: # Step through each vertex in the feature for pnt in part: list_x.append(pnt.X); list_y.append(pnt.Y); partnum += 1 Xmin = min(list_x) Xmax = max(list_x) Ymin = min(list_y) Ymax = max(list_y) orientation = row[1] FID = row[2] margin = width * math.cos(math.radians(orientation)) from math import * if orientation >= 0: cursor_for_origin_x = Xmin cursor_for_origin_y = Ymin while (cursor_for_origin_y <= Ymax) and (cursor_for_origin_x >= (Xmin - (Ymax - Ymin) * math.cos(math.radians(orientation)) * math.sin(math.radians(orientation)))): backup_cursor_position_previous_line_x = cursor_for_origin_x backup_cursor_position_previous_line_y = cursor_for_origin_y while (cursor_for_origin_x <= Xmax and cursor_for_origin_y <= (Ymax + margin)): createRectangleAtPoint (cursor_for_origin_x, cursor_for_origin_y, length, width, orientation, "Work_layer") cursor_for_origin_x = cursor_for_origin_x + length * math.cos(math.radians(orientation)) cursor_for_origin_y = cursor_for_origin_y + length * math.sin(math.radians(orientation)) cursor_for_origin_x = backup_cursor_position_previous_line_x - (width * math.sin(math.radians(orientation))) cursor_for_origin_y = backup_cursor_position_previous_line_y + (width * math.cos(math.radians(orientation))) cursor_for_origin_x = Xmin + (width * math.sin(math.radians(orientation))) cursor_for_origin_y = Ymin - (width * math.cos(math.radians(orientation))) while (cursor_for_origin_x <= Xmax) and (cursor_for_origin_y >= (Ymin - (Xmax - Xmin) * math.cos(math.radians(orientation)) * math.sin(math.radians(orientation)))): backup_cursor_position_previous_line_x = cursor_for_origin_x backup_cursor_position_previous_line_y = cursor_for_origin_y while (cursor_for_origin_x <= Xmax): createRectangleAtPoint (cursor_for_origin_x, cursor_for_origin_y, length, width, orientation, "Work_layer") cursor_for_origin_x = cursor_for_origin_x + length * math.cos(math.radians(orientation)) cursor_for_origin_y = cursor_for_origin_y + length * math.sin(math.radians(orientation)) cursor_for_origin_x = backup_cursor_position_previous_line_x + (width * math.sin(math.radians(orientation))) cursor_for_origin_y = backup_cursor_position_previous_line_y - (width * math.cos(math.radians(orientation))) else: cursor_for_origin_x = Xmax cursor_for_origin_y = Ymin while (cursor_for_origin_y <= Ymax) and (cursor_for_origin_x <= (Xmax - (Ymax - Ymin) * math.cos(math.radians(orientation)) * math.sin(math.radians(orientation)))): backup_cursor_position_previous_line_x = cursor_for_origin_x backup_cursor_position_previous_line_y = cursor_for_origin_y while (cursor_for_origin_x >= Xmin) and (cursor_for_origin_y <= (Ymax + margin)): createRectangleAtPoint (cursor_for_origin_x, cursor_for_origin_y, length, width, orientation, "Work_layer") cursor_for_origin_x = cursor_for_origin_x - length * math.cos(math.radians(orientation)) cursor_for_origin_y = cursor_for_origin_y - length * math.sin(math.radians(orientation)) cursor_for_origin_x = backup_cursor_position_previous_line_x - (width * math.sin(math.radians(orientation))) cursor_for_origin_y = backup_cursor_position_previous_line_y + (width * math.cos(math.radians(orientation))) cursor_for_origin_x = Xmax + (width * math.sin(math.radians(orientation))) cursor_for_origin_y = Ymin - (width * math.cos(math.radians(orientation))) while (cursor_for_origin_x >= Xmin) and (cursor_for_origin_y >= (Ymin + (Xmax - Xmin) * math.cos(math.radians(orientation)) * math.sin(math.radians(orientation)))): backup_cursor_position_previous_line_x = cursor_for_origin_x backup_cursor_position_previous_line_y = cursor_for_origin_y while (cursor_for_origin_x >= Xmin): createRectangleAtPoint (cursor_for_origin_x, cursor_for_origin_y, length, width, orientation, "Work_layer") cursor_for_origin_x = cursor_for_origin_x - length * math.cos(math.radians(orientation)) cursor_for_origin_y = cursor_for_origin_y - length * math.sin(math.radians(orientation)) cursor_for_origin_x = backup_cursor_position_previous_line_x + (width * math.sin(math.radians(orientation))) cursor_for_origin_y = backup_cursor_position_previous_line_y - (width * math.cos(math.radians(orientation))) print FID arcpy.SelectLayerByAttribute_management ("Contaminated_area_allCopy", "CLEAR_SELECTION") arcpy.SelectLayerByAttribute_management ("Contaminated_area_allCopy", "NEW_SELECTION", '\"FID\" = {}'.format(FID)) # Then add a selection to the layer based on location to features in another feature class arcpy.SelectLayerByLocation_management ("Work_layer", "intersect", "Contaminated_area_allCopy") # append selected features into the Plan layer arcpy.Append_management("Work_layer", "Plan") arcpy.DeleteFeatures_management("Z:/Work/ORTHO/Python_scripting/Work_layer.shp") list_x = []; list_y = []; arcpy.Delete_management(Work_layer) Procedure_Make_Grid("Z:\Work\ORTHO\Python_scripting\Contaminated_area_allCopy.shp", 30, 3)