How to get  XMin, YMin, XMax and YMax values from Multipoint features?

6726
2
01-29-2015 10:25 AM
alexbullen
New Contributor II

Is there a way to extract the XMin, YMin, XMax and YMax coordinate values from a feature without using ArcMap's Field Calculator?  Is there a tool or other way to programmatically pull out these particular coordinates?

 


I'm in ArcGIS 10.2, working with multipoint features. I also have an ASCII text file with coordinates of each point, if that helps.

0 Kudos
2 Replies
DanPatterson_Retired
MVP Emeritus

This demo I posted recently, but can't find it now, so I will repost.

It is a demo,

  • you specify the shapefile name (not tested on anything else)
  • an empty array is produced (arr) to store point objects
  • you provide the fields to query, in this case the FID and Shape (SHAPE@ alias) fields in a search cursor
  • for every for record (row) it gets the extent of the Shape field (ie row[1], as indexed by fieldname)
  • two extent points are obtained extents are printed and the points are added to the array
  • When everything is done, the collected points are converted to a multipoint and finally
  • the extent of all the points is determined

hope this gives you some ideas

import arcpy
input_shp = 'c:/your_path_here/your_shapefile.shp'
arr = arcpy.Array()
with arcpy.da.SearchCursor(input_shp,['FID','SHAPE@']) as cur:
  for row in cur:
    ext = row[1].extent
    p0 = ext.lowerLeft; p1 = ext.upperRight
    print('Extent of shape... {}: '.format(row[0]))
    print(' X min/max  {}, {}'.format(ext.XMin,ext.XMax))
    print(' Y min/max  {}, {}'.format(ext.YMin,ext.YMax))
    arr.add(p0)
    arr.add(p1)
mp = arcpy.Multipoint(arr)
print('Extent of all {}'.format(mp.extent)) 
XanderBakker
Esri Esteemed Contributor

If your objective is to create 4 fields with the extent values in it for each feature, you could use this:

import arcpy
fc = r"C:\Path\To\Your\fgdb.gdb\FeatureclassName"

flds = ["XMin", "YMin", "XMax", "YMax"]
# add fields to output featureclass
for fld in flds:
    if len(arcpy.ListFields(fc, wild_card=fld)) == 0:
        arcpy.AddField_management(fc, fld, "DOUBLE")
flds.insert(0, "SHAPE@")

with arcpy.da.UpdateCursor(fc, flds) as curs:
    for row in curs:
        ext = row[0].extent
        for fld in flds[1:]:
            row[flds.index(fld)] = eval("ext.{0}".format(fld))
        curs.updateRow(row)