Seite 1 von 1

ArcGIS & Python: .ASCII Files mittels 1Polygon(.shp) clippen

Verfasst: Montag 2. Juni 2014, 14:42
von MarcNAV
Hey brauche mal wieder eure Hilfe. Ich habe einen In-Ordner mit ASCII-Files (Testgebiet.asc, Testgebiet2.asc....) aus denen ich jeweils mit einer Polygon-shapefile (Schablone.shp) einen Teil herauschneiden und in einen out-Ordner speichern will. Das ganze mittels Python automatisiert. Ich habe folgenden Code im Internet gefunden und versucht auf meine Daten anzupassen.

Fehler ist folgender: -Zeile19: rasters: non types not iterable)

Außerdem scheint er die ASCII Files nich einlesen zu können und das Programm muss abgeändert werden auf eine Shapefile als clip für alle ASCII´s.

Weiß jemand Hilfe oder nen besseren weg. Vielen Dank hab schon 1000 versch. Möglichkeiten probiert und dreh langsam durch:D

Code: Alles auswählen

import arcpy, os, sys
from arcpy import env
from arcpy.sa import *
arcpy.env.overwriteOutput = True
arcpy.CheckOutExtension("Spatial")


def main():

    # input workspace
    raster_ws = r'F:\2014_05\Python Clipping\in'
    shp_ws = r'F:\2014_05\Python Clipping\Schablone'

    # output workspace
    out_ws = r'F:\2014_05\Python Clipping\out'

    # get lists
    arcpy.env.workspace = raster_ws
    rasters = [os.path.join(raster_ws, r) for r in arcpy.ListRasters()]

    arcpy.env.workspace = shp_ws
    shapes = [os.path.join(shp_ws, s) for s in arcpy.ListFeatureClasses()]

    # get dictionary {raster1 : shapefile1.shp, ...}
    clip_dict = dict(zip(rasters, shapes))

    # 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, shapefile, "", "ClippingGeometry", "NO_MAINTAIN_EXTENT")
        print 'Clipped {0}'.format(os.path.basename(raster))
        outputList.append(out_raster) # aopend all the output to the container

    # compute the sum of all rasters with ignore no data option
    sumRaster = CellStatistics(outputList, "SUM", "DATA")
    sumRaster.save(r'F:\2014_05\Python Clipping\out\Statistic\ofsumrasters.tif') # remove .tif if GRID format is desired
    print "Created Sum Raster"

if __name__ == '__main__':
    main()

Re: ArcGIS & Python: .ASCII Files mittels 1Polygon(.shp) cli

Verfasst: Montag 2. Juni 2014, 14:56
von BlackJack
@MarcNAV: Das ist eine eigenartige Fehlermeldung weil die Dokumentation zu `ListRasters()` behauptet der Rückgabewert ist eine Liste: http://help.arcgis.com/en/arcgisdesktop ... 000000.htm

Da steht auch welche Rasterformate verstanden werden. Welches davon wären denn Deine ``*.asc``-Dateien? Und was ist da überhaupt drin? Der Begriff ASCII-Datei sagt ja eigentlich erst einmal nicht mehr aus als das die Bytes dort drin sich auf den ASCII-Wertebereich beschränken.

Re: ArcGIS & Python: .ASCII Files mittels 1Polygon(.shp) cli

Verfasst: Dienstag 3. Juni 2014, 10:35
von MarcNAV
Meine .asc Files sind Rasterkarten. In den Dateien stehen nur Zahlenwerte von 0-9999 durch Leerzeichen getrennt.

In der raster_type Spalte sind im Hilfe Forum keine .asc Files aufgelistet. Hab auch irgendwo gelesen, dass die .asc Files nur readable sind und nicht writeable. Vielleicht muss ich die auch in ein anderes Format umwandeln, also .tiff oder so.

Re: ArcGIS & Python: .ASCII Files mittels 1Polygon(.shp) cli

Verfasst: Mittwoch 4. Juni 2014, 14:12
von MarcNAV
Okay ich habe das Problem gelöst. Ich muss die .asc Files in Rasterdaten erst umwandeln dann funtioniert es. Ein Problem habe ich noch. Mein Programm geht von einer Liste von Rasterdaten und einer Liste von Shapefiles aus mit denen geclipped wird. Ich habe aber nur eine Shapefile mit der ich alle Rasterdaten clippe. Weiß jemand wie ich mein Programm abändern muss. Sorry bin Neuling.

in Ordner: testgebiet.asc, testgebiet2.asc........
out Ordner: gerasterte .asc Files
outfinal Ordner: geclippte files
Schablone: schab.shp (Shapefile die als Ausschneideschablone fürs clipping der Rasterdaten genommen wird)

Code: Alles auswählen

def main():


    #  .asc in Raster wandeln
    files = glob.glob("F:/PythonClipping/in/*.asc")
    for f in files:

        x = "new" + os.path.splitext(os.path.basename(f))[0]
        inASCII = f
        outRaster = ("F:/PythonClipping/out/%s" %x)
        rasterType = ("INTEGER")
        arcpy.ASCIIToRaster_conversion(inASCII, outRaster, rasterType)


        # input workspace
        raster_ws = (r'F:\PythonClipping\out')
        shp_ws = (r'F:\PythonClipping\Schablone')


        # output workspace
        out_ws = (r'F:\PythonClipping\outfinal')


        # get lists
        arcpy.env.workspace = raster_ws
        rasters = [os.path.join(raster_ws, r) for r in arcpy.ListRasters()]

        arcpy.env.workspace = shp_ws
        shapes = [os.path.join(shp_ws, s) for s in arcpy.ListFeatureClasses()]


        # get dictionary {raster1 : shapefile1.shp, ...}
        clip_dict = dict(zip(rasters, shapes))


        # 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, shapefile, "", "ClippingGeometry", 



if __name__ == '__main__':
    main()