Hi Sean Wineland ,
Just tried something out on a single TIFF, but I am a bit disappointed by the result, since a strip of white remains on the left side, probably since the coordinates may not have been transformed correctly:
Find below the code I used:
def main():
import arcpy
tiff_in = r'C:\GeoNet\TopoView\MA_Boston North_350031_1956_24000_geo.tif'
tiff_out = r'C:\GeoNet\TopoView\tiff\MA350031.tif'
fc_out = r'C:\GeoNet\TopoView\test.gdb\polygon_rectangle'
sr_xml = arcpy.SpatialReference(4326)
trans = "NAD_1927_To_WGS_1984_3"
xml_file = tiff_in + ".xml"
prj_file = tiff_in + ".prj"
sr_tif = arcpy.SpatialReference(prj_file)
polygon = getRectanglePolygon(xml_file, sr_xml, sr_tif, trans)
arcpy.CopyFeatures_management([polygon], fc_out)
rectangle_extent = "{0} {1} {2} {3}".format(polygon.extent.XMin, polygon.extent.YMin, polygon.extent.XMax, polygon.extent.YMax)
arcpy.Clip_management(in_raster=tiff_in, rectangle=rectangle_extent,
out_raster=tiff_out, in_template_dataset=fc_out,
nodata_value="256", clipping_geometry="NONE",
maintain_clipping_extent="NO_MAINTAIN_EXTENT")
def getRectanglePolygon(xml_file, sr_in, sr_out, trans):
dct_bounding = getBoundingCoords(xml_file)
polygon = createPolygonFromCoords(dct_bounding, sr_in, sr_out, trans)
return polygon
def getBoundingCoords(xml_file):
lst_tags = ["westbc", "eastbc", "northbc", "southbc"]
dct = {}
with open(xml_file, 'r') as f:
for line in f:
line = line.replace("\n", "")
for tag in lst_tags:
if tag in line:
val = getValue(line, tag)
dct[tag] = val
return dct
def createPolygonFromCoords(dct, sr_in, sr_out, trans):
xmin = dct["westbc"]
xmax = dct["eastbc"]
ymin = dct["southbc"]
ymax = dct["northbc"]
lst_pnt_in = [arcpy.Point(xmin, ymin), arcpy.Point(xmin, ymax),
arcpy.Point(xmax, ymax), arcpy.Point(xmax, ymin),
arcpy.Point(xmin, ymin)]
lst_pntg_in = [arcpy.PointGeometry(pnt, sr_in) for pnt in lst_pnt_in]
lst_pntg_out = [pntg.projectAs(sr_out, trans) for pntg in lst_pntg_in]
lst_pnt_out = [pntg.firstPoint for pntg in lst_pntg_out]
return arcpy.Polygon(arcpy.Array(lst_pnt_out), sr_out)
def getValue(line, tag):
start_tag = "<{0}>".format(tag)
end_tag = "</{0}>".format(tag)
pos1 = line.find(start_tag)
pos2 = line.find(end_tag)
txt = line[pos1 + len(start_tag):pos2]
return float(txt)
if __name__ == '__main__':
main()
This could be place in a loop to run through the TIFF files you have, but first the possible shift in coordinates should be examined and corrected.