Get extent of raster or shp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# -*- coding: utf-8 -*-
import arcpy
import os
from arcpy import env
arcpy.CheckOutExtension("Spatial")
env.overwriteOutput=1

def getExtentOfRasterOrShp(filePath):
folder_path, file_name = os.path.split(filePath)
arcpy.env.workspace =folder_path
ext=file_name[-4:]
if ext==".tif":
extent=arcpy.Raster(filePath).extent
return str(extent.XMin)+" "+str(extent.YMin)+" "+str(extent.XMax)+" "+str(extent.YMax)
elif ext==".shp":
xmin=[]
xmax=[]
ymin=[]
ymax=[]
with arcpy.da.SearchCursor(filePath, 'SHAPE@') as cursor:
for row in cursor:
result = row[0].extent
xmin.append(result.XMin)
xmax.append(result.XMax)
ymin.append( result.YMin)
ymax.append(result.YMax)
xmin.sort()
ymin.sort()
xmax.sort(reverse=True)
ymax.sort(reverse=True)
return str(xmin[0])+" "+str(ymin[0])+" "+str(xmax[0])+" "+str(ymax[0])