Creating rectangles on centroid points in ArcPy? [closed]

I would like to create rectangles that placed on given centroid points.

1. There are hundreds of points.
2. Certain rectangle shape(not a square) file is given.
3. Create rectangles that place the centroid of it on the hundreds of points each.

I've tried to copy and paste manually but I need an automated process.

Any idea? • please provide more details. Provide an example of the file that contains rectangles information? Meaning, how rectangles can be related to points? Feb 16 '15 at 22:03

Here's a very simplified version of @crmackey's script (no offense intended crmackey, it just doesn't have to be that complicated):

import arcpy
w = 100
h = 50
polygons = []
sr = arcpy.Describe("points").spatialReference
with arcpy.da.SearchCursor("points","SHAPE@",spatial_reference=sr) as cursor:
for row in cursor:
polygon = arcpy.Polygon(arcpy.Array([arcpy.Point(row.centroid.X - w/2,row.centroid.Y + h/2),arcpy.Point(row.centroid.X - w/2, row.centroid.Y - h/2),arcpy.Point(row.centroid.X + w/2,row.centroid.Y - h/2),arcpy.Point(row.centroid.X + w/2,row.centroid.Y + h/2)]),sr)
polygons.append(polygon)

arcpy.CopyFeatures_management(polygons,r'in_memory\rectangles')
• I realize this could have been written in less lines, but that doesn't necessarily mean "better". What you call "complicated", I call more dynamic. My code allows the values to be set either as constants, or by values from fields. I would also argue that the code I posted is more portable and can easily be turned into a script tool. And finally, the code above assumes that no attributes are needed for the output geometry. I included them. No offense intended here either, just explaining why I did what I did :) Feb 17 '15 at 14:46

This worked for me, you can use constant values for your width (X) and height(Y) or the values can be defined by fields:

import arcpy
import os
arcpy.env.overwriteOutput = True

def getPoly(pt, w, h):
x, y = pt
ll = (x - (w * 0.5), y - (h * 0.5))
ul = (x - (w * 0.5), y + (h * 0.5))
ur = (x + (w * 0.5), y + (h * 0.5))
lr = (x + (w * 0.5), y - (h * 0.5))
return arcpy.Polygon(arcpy.Array([arcpy.Point(*coords) for coords in [ll,ul,ur,lr,ll]]))

def pointsToPoly(points, output, w=500, h=500, from_field=False, w_field='', h_field=''):
"""Creates polygons from centroids by user defined width and height

Required:
points -- centroid points
output -- output polygons

Optional:
w -- width (constant, in X direction)
h -- height (constant, in Y direction)
from_field -- if true, will set width and height from fields. Default is False.
w_field -- width field.  Ignored if from_field is set to False.
h_field -- height field.  Ignored if from_field is set to False.
"""

# copy schema
desc = arcpy.Describe(points)
sr = desc.spatialReference
path, name = os.path.split(output)
arcpy.management.CreateFeatureclass(path, name, 'POLYGON', points, spatial_reference=sr)

# create geometry
fields = [f.name for f in desc.fields if not f.required]
if from_field:
w_ind, h_ind = fields.index(w_field), fields.index(h_field)
with arcpy.da.SearchCursor(points, ['SHAPE@XY'] + fields) as rows:
with arcpy.da.InsertCursor(output, ['SHAPE@'] + fields) as irows:
for row in rows:
irows.insertRow((getPoly(row, row[w_ind+1], row[h_ind+1]),) + row[1:])

else:
with arcpy.da.SearchCursor(points, ['SHAPE@XY'] + fields) as rows:
with arcpy.da.InsertCursor(output, ['SHAPE@'] + fields) as irows:
for row in rows:
irows.insertRow((getPoly(row, w, h),) + row[1:])

if __name__ == '__main__':

pts = r'C:\TEMP\Utility\Utilities.gdb\San_Ls'
out = r'C:\TEMP\Utility\Utilities.gdb\polygons2'

# test with constants
pointsToPoly(pts, out, 750, 1000)

# test with fields
out2 = r'C:\TEMP\Utility\Utilities.gdb\polygons3'
pointsToPoly(pts, out2, from_field=1, w_field='Width', h_field='Height')
print 'done'

Output from fields: You are going to need to write some python scripting for this. You would cycle through your point file and compute the coordinates of the 4 four corners of each rectangle and use those to construct the polygon, then write that to your output Feature Class.

Look here for advice and sample code on creating geometries and writing them out.

Thank you, everyone. Really appreciated! As a beginner in scripting and python, I tryed to find out finalized menu.

Here's the way to make rectangle.... https://www.gisnuts.com/terra/site-code-RectangularBufferTool

Also I'm trying to learn the scripting. 😊