TransWikia.com

Inputing minimum X, maximum X, minimum Y, and maximum Y to create bounding box feature using ArcPy

Geographic Information Systems Asked by John Cortenbach on December 5, 2020

How do I create a bounding box of Hawaii.shp using the minimum X, maximum X, minimum Y, and maximum Y and store it in a polygon shapefile?

xmin: 372236.642923 
xmax: 941120.123667 
ymin: 2094769.12138 
ymax: 2458884.21017
import arcpy
arcpy.env.workspace = r"F:LabsOriginalLab8Lab08"

fc = "newpolygon.shp"
arcpy.CreateFeatureclass_management(arcpy.env.workspace,fc,"Polygon")
array = arcpy.Array()
coordinates = 
for x,y in coordinates:
    point = arcpy.Point()
    array.add(point)
polygon=arcpy.Polygon(array)
cursor = arcpy.da.InsertCursor(fc,["SHAPE@"])
cursor.insertRow([polygon])

print "Done"
del cursor

One Answer

This page has a bunch of relevant examples: https://pro.arcgis.com/en/pro-app/arcpy/get-started/writing-geometries.htm. I modified the final example on the page to match your coords. Though I've no idea what projection (coordinate system) those values are in, so I just picked 26904 out of a hat (you'll need to correct that)

I didn't test it, but I think it's close.

import arcpy
import os

# Create a feature class with a spatial reference of NAD83 / UTM zone 4N
result = arcpy.management.CreateFeatureclass(arcpy.env.scratchGDB, 
                  "esri_square", "POLYGON", spatial_reference=26904)[0]

# hawaii extent
extent = {'xmin': 372236.642923,
          'xmax': 941120.123667,
          'ymin': 2094769.12138,
          'ymax': 2458884.21017,}

# verts walk around counterclockwise, and 
#  repeat the first/last point to close to poly
coords = [(coords["minx", coords["miny"]),
          (coords["maxx", coords["miny"]),
          (coords["maxx", coords["maxy"]),
          (coords["minx", coords["maxy"]),
          (coords["minx", coords["miny"])]

# Write feature to new feature class
with arcpy.da.InsertCursor(feature_class, ['SHAPE@']) as cursor:
    cursor.insertRow([coords])

Answered by gotchula on December 5, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP