# coding: utf8 # Seminár 05 -- arcpy uvod """ 0: Interakcia Python -- ArcMap -- Príkazový riadok (Python window) -- Použitie kódu vo Field Calculator (atribútová tabuľka) -- Použitie kódu v Model Builderi (toolbox) -- Načítanie vlastného skriptu (toolbox) Dokumentácia arcpy: http://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-functions/describe-object-properties.htm """ """ 1: Python Window Nasledujúce príklady operujú s vrstvami "Zajmova_plocha", "body" atd. """ # prezeranie vrstiev a ich vlastností arcpy.GetCount_management("body") points = arcpy.Describe("body") >>> points.dataType u'FeatureLayer' >>> points.name u'body.shp' >>> points.file u'body.shp' >>> points.baseName u'body' >>> points.children # jednoduché funkcie -- nápoveda... arcpy.Buffer_analysis("Zajmova_plocha", "buff", 9) # vstup na disku arcpy.Buffer_analysis("c:/data/Portland.gdb/streets", "c:/data/Portland.gdb/steets_buffer", "500 METERS") # nastavenie workspace arcpy.env.workspace = "c:/data/Portland.gdb" arcpy.Buffer_analysis("streets", "streetBuf", "500 METERS") # súbory sú uložené vo workspace # test existencie print(arcpy.Exists("c:/data/Portland.gdb/streets")) # aký súradnicový systém sr = arcpy.Describe("c:/data/Portland.gdb/streets").spatialReference print(sr.name) # konverzia arcpy.FeatureClassToFeatureClass_conversion("c:/data/Portland.gdb/streets", "Database Connections/MySDE.sde/PortlandDataset", "streets") """ užitočné podmoduly arcpy.mapping arcpy.da -- data analyst arcpy.sa -- spatial analyst arcpy.na -- network analyst ... """ # nazvy stlpcov fields = arcpy.ListFields("name") >>> for i in fields: ... print i.name # atributova tabulka fc = "feature class" arcpy.CalculateField_management(fc, 'Flag', '1') arcpy.AddField_management("body", "nf", "FLOAT") if arcpy.Exists(fc): arcpy.Clip_analysis(fc, "urban_area", "urban_roads") """ 2: Iterácia cez objekty (layer, feature class) -- kurzory """ # SEARCH CURSOR: # parameter reset >>> sc = arcpy.da.SearchCursor("body", "*", """Level = 1""") >>> for i in sc: ... print i[3] sc.reset() # spat na start cursor = arcpy.da.SearchCursor(fc, ['fieldA', 'fieldB']) for row in cursor: print(row) # INSERT CURSOR # parameter insertRow cursor = arcpy.da.InsertCursor("c:/base/data.gdb/roads_lut", ["roadID", "distance"]) for i in range(0,25): cursor.insertRow([i, 100]) # UPDATE CURSOR # parametre: # deleteRow, updateRow, reset # spustenie v bloku with zaisti ukoncenie kurzoru with arcpy.da.UpdateCursor("c:/base/data.gdb/roads", ["roadtype"]) as cursor: for row in cursor: if row[0] == 4: cursor.deleteRow() with arcpy.da.UpdateCursor("c:/base/data.gdb/roads", ["roadtype", "distance"]) as cursor: for row in cursor: # Update the values in the distance field by multiplying # the roadtype by 100. Road type is either 1, 2, 3 or 4. # row[1] = row[0] * 100 cursor.updateRow(row) # geometry tokens # https://pro.arcgis.com/en/pro-app/arcpy/get-started/reading-geometries.htm for row in arcpy.da.SearchCursor(infc, ["SHAPE@XY"]): # Print x,y coordinates of each point feature # x, y = row[0] print("{}, {}".format(x, y)) # viac prace s geometriami geometries = arcpy.CopyFeatures_management('c:/temp/outlines.shp', arcpy.Geometry()) length = sum([g.length for g in geometries]) print('Total length: {}'.format(length)) # spatial selection: arcpy.SelectLayerByLocation_management("AllStatesLayer","BOUNDARY_TOUCHES","SelectionStateLayer") """ 3: Python v calculate field http://pro.arcgis.com/en/pro-app/tool-reference/data-management/calculate-field-examples.htm K dispozícii sú vstavané funkcie a možnosť písať vlastné. !!! -- pri volaní označíme premenné z atr. tabuľky výkričníkmi: !WELL_YIELD! """