Hi Tim Gattinger , I branched your question to a new thread.
Yes it is possible to adapt the script to read width and height attributes from attributes in a DBF. What was not clear to me is if that DBF is a stand alone table or not. In case it is look at the example below.
This will take a DBF (specified on row 7) with the attributes defined on lines 11 to 14 and create a new output featureclass (defined at line 18) that will hold the polygons of the rectangles by reading XY attributes (is this the case?) and the width and height defined in attributes. Important is to specify a relevant (projected) coordinate system on line 21 in which X, Y, width and height are expressed:
import arcpy
def main():
import os
tbl = r'D:\Folder\SubFolder\NameOfYour.dbf'
fld_oid_in = 'OID@'
fld_X = 'X'
fld_Y = 'Y'
fld_width = 'Width'
fld_height = 'Height'
fld_oid_out = 'OID_DBF'
fc_out = r'D:\Folder\SubFolder\NameOfWorkspace.gdb\rectangles'
sr = arcpy.SpatialReference(3857)
fc_ws, fc_name = os.path.split(fc_out)
arcpy.CreateFeatureclass_management(fc_ws, fc_name, "POLYGON", None, None, None, sr)
AddField(fc_out, fld_oid_out, "LONG", None)
flds_out = ('SHAPE@', fld_oid_out)
with arcpy.da.InsertCursor(fc_out, flds_out) as curs_out:
flds = (fld_oid, fld_X, fld_Y, fld_width, fld_height)
with arcpy.da.SearchCursor(fc, flds) as curs:
for row in curs:
oid = row[0]
x = row[1]
y = row[2]
w = row[3]
h = row[4]
polygon = createRectangle(x, y, w, h, sr)
curs_out.insertRow((polygon, oid, ))
def AddField(fc, fld_name, fld_type, fld_length):
if len(arcpy.ListFields(fc, fld_name)) == 0:
arcpy.AddField_management(fc, fld_name, fld_type, None, None, fld_length)
def createRectangle(x, y, w, h, sr):
xmin = float(x) - float(w) / 2.0
xmax = float(x) + float(w) / 2.0
ymin = float(y) - float(h) / 2.0
ymax = float(y) + float(h) / 2.0
lst_pnts = []
lst_pnts.append(arcpy.Point(xmin, ymin))
lst_pnts.append(arcpy.Point(xmin, ymax))
lst_pnts.append(arcpy.Point(xmax, ymax))
lst_pnts.append(arcpy.Point(xmax, ymin))
lst_pnts.append(lst_pnts[0])
return arcpy.Polygon(arcpy.Array(lst_pnts), sr)
if __name__ == '__main__':
main()