153GIS2 / 10. cvičení: Porovnání verzí
Řádek 21: | Řádek 21: | ||
import arcgisscripting #import modulu | import arcgisscripting #import modulu | ||
gp = arcgisscripting.create() #vytvoreni objektu geoprocessingu | gp = arcgisscripting.create() #vytvoreni objektu geoprocessingu | ||
gp.Overwriteoutput = | gp.Overwriteoutput = True #nastaveno prepisovani | ||
gp.workspace = "d:/jirka/skola/vyuka/gis/model/" #nastaveni workspace - pracovni adresar nebo geodatabaze | gp.workspace = "d:/jirka/skola/vyuka/gis/model/" #nastaveni workspace - pracovni adresar nebo geodatabaze | ||
Řádek 39: | Řádek 39: | ||
<source lang="python"> | <source lang="python"> | ||
import arcpy #import modulu | import arcpy #import modulu | ||
arcpy.env.overwiteOutput = | arcpy.env.overwiteOutput = True | ||
arcpy.env.workspace = "d:/jirka/skola/vyuka/gis/model/" #nastaveni workspace - pracovni adresar nebo geodatabaze | arcpy.env.workspace = "d:/jirka/skola/vyuka/gis/model/" #nastaveni workspace - pracovni adresar nebo geodatabaze | ||
Verze z 21. 4. 2011, 14:55
Hlavní stránka předmětu • Předcházející cvičení • Následující cvičení
Skriptování v Python - úvod
- What is Python?
- What is Python Window?
- Quick tour of Python
- Writing Python scripts
- Getting started with Python in ArcGIS 10
- What is ArcPy?
Ukázka skriptů
1 (verze ArcGIS 9)
import arcgisscripting #import modulu
gp = arcgisscripting.create() #vytvoreni objektu geoprocessingu
gp.Overwriteoutput = True #nastaveno prepisovani
gp.workspace = "d:/jirka/skola/vyuka/gis/model/" #nastaveni workspace - pracovni adresar nebo geodatabaze
try:
data = gp.Describe("obce.shp")
fields = data.Fields
field = fields.next()
while field:
print field.Name
field = fields.next()
except:
print gp.GetMessages()
1 (verze ArcGIS 10)
import arcpy #import modulu
arcpy.env.overwiteOutput = True
arcpy.env.workspace = "d:/jirka/skola/vyuka/gis/model/" #nastaveni workspace - pracovni adresar nebo geodatabaze
try:
data = arcpy.Describe("obce.shp")
i = 0
while (data.fields[i].name):
print data.fields[i].name
i = i+1
except:
print arcpy.GetMessages()
2 (verze ArcGIS 9)
import arcgisscripting, sys #import modulu
gp = arcgisscripting.create() #vytvoreni objektu geoprocessingu
gp.Overwriteoutput = 1 #nastaveno prepisovani
gp.workspace = "d:/jirka/skola/vyuka/gis/model/" #nastaveni workspace - pracovni adresar nebo geodatabaze
try:
input = "Liberec" #String - nazev obce
rows = gp.SearchCursor("obce.shp") #vytvori cursor
row = rows.next() #promena row obsahuje jeden zaznam ze shapefilu
while row:
if row.GetValue("NAZEV") == input:
pocet = row.GetValue("OB01") #pocet obyvatel zadane obce
row = rows.next()
radky = gp.SearchCursor("obce.shp")
rad = radky.next()
while rad:
if rad.GetValue("OB01") > pocet:
print rad.GetValue("NAZEV"), rad.GetValue("OB01")
rad = radky.next()
except:
print gp.GetMessages()
2 (verze ArcGIS 10)
import arcpy, sys #import modulu
arcpy.env.overwiteOutput = 1
arcpy.env.workspace = "d:/jirka/skola/vyuka/gis/model/" #nastaveni workspace - pracovni adresar nebo geodatabaze
try:
rows = arcpy.SearchCursor("obce.shp", '"NAZEV" = \'Liberec\'') #vytvori cursor
for row in rows:
pocet = row.OB01 #pocet obyvatel zadane obce
radky = arcpy.SearchCursor("obce.shp")
for radek in radky:
if (radek.OB01 > pocet):
print "%s - %i" %(radek.NAZEV, radek.OB01)
except:
print arcpy.GetMessages()
3
import arcgisscripting, sys #import modulu
gp = arcgisscripting.create() #vytvoreni objektu geoprocessingu
gp.Overwriteoutput = 1 #nastaveno prepisovani
gp.workspace = "d:/jirka/skola/vyuka/gis/model/" #nastaveni workspace - pracovni adresar nebo geodatabaze
try:
input = sys.argv[1] #String - nazev obce
output = sys.argv[2] #cesta k textovemu souboru
f = open(output, 'w') #otevru file
rows = gp.SearchCursor("obce.shp") #vytvori cursor
row = rows.next() #promena row obsahuje jeden zaznam ze shapefilu
while row:
if row.GetValue("NAZEV") == input:
pocet = row.GetValue("OB01") #pocet obyvatel zadane obce
row = rows.next()
radky = gp.SearchCursor("obce.shp")
rad = radky.next()
while rad:
if rad.GetValue("OB01") > pocet:
f.write(rad.GetValue("NAZEV")+" "+str(rad.GetValue("OB01"))+"\n") #do souboru pisu obce ktere maji vic obyvatel nez zadana
rad = radky.next()
f.close()
except:
print gp.GetMessages()
4 (verze ArcGIS 9)
import sys, string, os, arcgisscripting
gp = arcgisscripting.create()
gp.workspace = "d:/jirka/skola/vyuka/gis/"
kraje = "D:\\jirka\\skola\\vyuka\\gis\\kraje.shp"
rows = gp.SearchCursor("kraje.shp") #vytvori cursor
row = rows.next() #promena row obsahuje jeden zaznam ze shapefilu
while row:
nazev = row.GetValue("NAZEV_ENG")
kraje_Select_shp = "D:\\jirka\\skola\\vyuka\\gis\\"+nazev+".shp"
gp.Select_analysis(kraje, kraje_Select_shp, "\"NAZEV_ENG\" = '"+nazev+"'")
row = rows.next()
4 (verze ArcGIS 10)
import arcpy, sys #import modulu
arcpy.env.overwiteOutput = 1
arcpy.env.workspace = "d:/jirka/skola/vyuka/gis/model/" #nastaveni workspace - pracovni adresar nebo geodatabaze
kraje = "D:\\jirka\\skola\\vyuka\\gis\\model\\kraje.shp"
rows = arcpy.SearchCursor("kraje.shp") #vytvori cursor
for row in rows:
nazev = row.KRAJ
kraje_Select_shp = "D:\\jirka\\skola\\vyuka\\gis\\model\\"+nazev+".shp"
arcpy.Select_analysis(kraje, kraje_Select_shp, "\"KRAJ\" = '"+nazev+"'")
5
import arcgisscripting, sys
gp = arcgisscripting.create()
gp.Overwriteoutput = 1
gp.workspace = "i:/pracovni/cajthaml/"
try:
input = sys.argv[1] # vstup
mezi = sys.argv[2] # mezivysledek po dissolve
dis = sys.argv[3] # nazev atributu pro dissolve
gp.Dissolve_management (input, mezi, dis)
radky = gp.SearchCursor(mezi)
rad = radky.next()
while rad:
nazev = rad.GetValue(dis)
vystup = nazev+".shp"
gp.Select_analysis (mezi, vystup, "\""+dis+"\" = '"+nazev+"'") # vytvori samostane shapefily pro dissolvovane prvky
rad = radky.next()
except:
print gp.GetMessages()
6 (kód v rámci Field Calculatoru)
def pocet(jmeno):
delka=jmeno.count(' ')+1
return delka
slov=pocet(!NAZEV!)
7 (kód v rámci Field Calculatoru)
def posun(geometrie):
point = geometrie.getPart(0)
point.X = point.X - 2000
return point
Shape=posun(!Shape!)