wenn ich die Rechenschritte printen lasse siehts in etwa so aus:
Projected: a.asc
Projected: b.asc
Rastered: b.asc
Clipped: b
Zonal Statistics: b
Ich verstehe nicht warum das Programm nach dem Projezieren nur mit b weitermacht. Weiß jemand Rat?
Besser wäre es wohl wenn das Programm erst alle Rechenschritte mit a.asc rechnet und dann mit b.asc.
Code: Alles auswählen
import shutil as st
import arcpy, os, sys, glob
from arcpy import env
from arcpy.sa import *
arcpy.env.overwriteOutput = True
arcpy.CheckOutExtension("Spatial")
from os.path import basename
def main():
#Dateistamm folgen, Ordner aus Datei erzeugen und Datei hinein kopieren
root_path = 'F:/test/PythonClippingTree' #!!!!!!!!!!Eingabe
extension = '.asc'
for path, _, filenames in os.walk(root_path):
for filename in filenames:
if filename.endswith(extension):
source = os.path.join(path, filename)
target = source[:-len(extension)]
os.mkdir(target)
st.move(source, target)
target2 = target + "_rastered"
target3 = target + "_clipped"
target4 = target + "_zonalstat"
os.mkdir(target2)
os.mkdir(target3)
os.mkdir(target4)
###################################################################
#Projektion
arcpy.env.workspace = (target)
arcpy.env.overwriteOutput = True
# set local variables
coordinateSystem = 'PROJCS["Bessel_1841_Lambert_Conformal_Conic",GEOGCS["GCS_Bessel_1841",DATUM["D_Bessel_1841",SPHEROID["Bessel_1841",6377397.155,299.1528128]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Lambert_Conformal_Conic"],PARAMETER["False_Easting",0.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",9.0],PARAMETER["Standard_Parallel_1",50.0],PARAMETER["Standard_Parallel_2",51.0],PARAMETER["Latitude_Of_Origin",47.0],UNIT["Meter",1.0]]'
arcpy.DefineProjection_management(filename, coordinateSystem)
print 'Projected: ' + os.path.basename(filename)
###################################################################
#.asc in Raster wandeln
files = glob.glob(target + '/*.asc')
for f in files:
inASCII = f
x = os.path.splitext(os.path.basename(f))[0]
outRaster = os.path.relpath(target2) + '/%s'%x
rasterType = ("INTEGER")
arcpy.ASCIIToRaster_conversion(inASCII, outRaster, rasterType)
print 'Rastered: ' + os.path.basename(f)
###################################################################
#Clipping:
# input workspace
raster_ws = target2
shp_ws = (r'F:/test/PythonClipping/CalcPythClipZonal/Schablone') #!!!!!!!!!!Eingabe
arcpy.env.overwriteOutput = True
# output workspace
out_ws = target3
# get list raster
arcpy.env.workspace = raster_ws
rasters = [os.path.join(raster_ws, r) for r in arcpy.ListRasters()]
# get shapefile
arcpy.env.workspace = shp_ws
shapef = (r'F:/test/PythonClipping/CalcPythClipZonal/Schablone/schab.shp') #!!!!!!!!!!Eingabe
# get dictionary {raster1 : shapefile.shp, ...}
clip_dict = dict(zip(rasters, shapef))
# clip all rasters
outputList = [] # container
for raster, shapefile in clip_dict.iteritems():
out_raster = os.path.join(out_ws, os.path.basename(raster))
arcpy.Clip_management(raster, "", out_raster, shapef, "", "ClippingGeometry", "NO_MAINTAIN_EXTENT")
print 'Clipped: ' + os.path.basename(raster)
outputList.append(out_raster) # apend all the output to the container
###################################################################
#Zonal Statistics:
# Check out any necessary licenses
arcpy.CheckOutExtension("spatial")
# Input data source
arcpy.env.workspace = target3
arcpy.env.overwriteOutput = True
ZoneShapefile = ("F:/test/PythonClipping/BL_Grenzen/l_d_bundneu_polygon.shp")
# Output File
OutputFolder = target4
# Loop through a list of files in the workspace
RasterFiles = arcpy.ListRasters()
# Local variables:
for filename in RasterFiles:
print("Zonal Statistics: " + filename)
inRaster = arcpy.env.workspace + "/" + filename
fileroot = filename
outRaster = OutputFolder + "/" + fileroot + ".dbf"
# Process: Zonal Statistics
arcpy.gp.ZonalStatisticsAsTable(ZoneShapefile, "GRID_CODE", inRaster, outRaster, "DATA", "ALL")
###################################################################
#Ordner alle in target verschieben
st.move(target2, target)
st.move(target3, target)
st.move(target4, target)
if __name__ == '__main__':
main()