generating scheduled rasters using python€¦ · generating scheduled rasters using python, 2015...

24
Generating Scheduled Rasters using Python Kevin B. Cary, M.Sc., GISP July 19, 2015

Upload: others

Post on 29-May-2020

32 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Generating Scheduled Rasters using Python€¦ · Generating Scheduled Rasters using Python, 2015 Esri Education GIS Conference—Presentation, 2015 Esri Education GIS Conference,

Generating Scheduled Rasters using PythonKevin B. Cary, M.Sc., GISP

July 19, 2015

Page 2: Generating Scheduled Rasters using Python€¦ · Generating Scheduled Rasters using Python, 2015 Esri Education GIS Conference—Presentation, 2015 Esri Education GIS Conference,

Kevin B. Cary, M.Sc., GISP | WKU — Department of Geography & Geology2/24

Page 3: Generating Scheduled Rasters using Python€¦ · Generating Scheduled Rasters using Python, 2015 Esri Education GIS Conference—Presentation, 2015 Esri Education GIS Conference,

Kevin B. Cary, M.Sc., GISP | WKU — Department of Geography & Geology3/24

Page 4: Generating Scheduled Rasters using Python€¦ · Generating Scheduled Rasters using Python, 2015 Esri Education GIS Conference—Presentation, 2015 Esri Education GIS Conference,

Kevin B. Cary, M.Sc., GISP | WKU — Department of Geography & Geology4/24

Page 5: Generating Scheduled Rasters using Python€¦ · Generating Scheduled Rasters using Python, 2015 Esri Education GIS Conference—Presentation, 2015 Esri Education GIS Conference,

Kevin B. Cary, M.Sc., GISP | WKU — Department of Geography & Geology5/24

Page 6: Generating Scheduled Rasters using Python€¦ · Generating Scheduled Rasters using Python, 2015 Esri Education GIS Conference—Presentation, 2015 Esri Education GIS Conference,

Available Data• CSV (Ky. Mesonet): Station Name, UTC Time, Local Time (CDT

and EDT), Air Temp. (Celsius), Relative Humidity, Dew Point,Wind Speed (MPH), Wind Speed Max, Solar Radiation, and DailyPrecipitation.

• Additional Precipitation Variables

– Precipitation amounts for the past 30 (p30), 60 (p60), 90(p90), 120 (p120), and 180 (p180) days.

– Precipitation normal amounts for the past 30 (n30), 60 (n60),90 (n90), 120 (n120), and 180 (n180) days.

• ArcSDE Database (Center for GIS at WKU)

Kevin B. Cary, M.Sc., GISP | WKU — Department of Geography & Geology6/24

Page 7: Generating Scheduled Rasters using Python€¦ · Generating Scheduled Rasters using Python, 2015 Esri Education GIS Conference—Presentation, 2015 Esri Education GIS Conference,

Kevin B. Cary, M.Sc., GISP | WKU — Department of Geography & Geology7/24

Page 8: Generating Scheduled Rasters using Python€¦ · Generating Scheduled Rasters using Python, 2015 Esri Education GIS Conference—Presentation, 2015 Esri Education GIS Conference,

Kevin B. Cary, M.Sc., GISP | WKU — Department of Geography & Geology8/24

Page 9: Generating Scheduled Rasters using Python€¦ · Generating Scheduled Rasters using Python, 2015 Esri Education GIS Conference—Presentation, 2015 Esri Education GIS Conference,

The Script(s)

• Windows Task Scheduler: Windows Batch File (BAT)

• Python Script 1 using default Python.exe

– Connect to SDE and create shapefile in correct coordinates.

– Expand envelope and use IDW Spatial Analyst Tool for inter-polation

– Create amount and deviation from normal precip rasters.

– Clip to Kentucky.

• Python Script 2 using 32 bit Python.exe(Updating Service on ArcGIS for Server)

Kevin B. Cary, M.Sc., GISP | WKU — Department of Geography & Geology9/24

Page 10: Generating Scheduled Rasters using Python€¦ · Generating Scheduled Rasters using Python, 2015 Esri Education GIS Conference—Presentation, 2015 Esri Education GIS Conference,

Kevin B. Cary, M.Sc., GISP | WKU — Department of Geography & Geology10/24

Page 11: Generating Scheduled Rasters using Python€¦ · Generating Scheduled Rasters using Python, 2015 Esri Education GIS Conference—Presentation, 2015 Esri Education GIS Conference,

Kevin B. Cary, M.Sc., GISP | WKU — Department of Geography & Geology11/24

Page 12: Generating Scheduled Rasters using Python€¦ · Generating Scheduled Rasters using Python, 2015 Esri Education GIS Conference—Presentation, 2015 Esri Education GIS Conference,

Kevin B. Cary, M.Sc., GISP | WKU — Department of Geography & Geology12/24

Page 13: Generating Scheduled Rasters using Python€¦ · Generating Scheduled Rasters using Python, 2015 Esri Education GIS Conference—Presentation, 2015 Esri Education GIS Conference,

Kevin B. Cary, M.Sc., GISP | WKU — Department of Geography & Geology13/24

Page 14: Generating Scheduled Rasters using Python€¦ · Generating Scheduled Rasters using Python, 2015 Esri Education GIS Conference—Presentation, 2015 Esri Education GIS Conference,

Kevin B. Cary, M.Sc., GISP | WKU — Department of Geography & Geology14/24

Page 15: Generating Scheduled Rasters using Python€¦ · Generating Scheduled Rasters using Python, 2015 Esri Education GIS Conference—Presentation, 2015 Esri Education GIS Conference,

Kevin B. Cary, M.Sc., GISP | WKU — Department of Geography & Geology15/24

Page 16: Generating Scheduled Rasters using Python€¦ · Generating Scheduled Rasters using Python, 2015 Esri Education GIS Conference—Presentation, 2015 Esri Education GIS Conference,

Windows Batch File (BAT)D:\ P ath1\ prec i pgen . pyC :\ P ython27\ A rcGIS10 .2\ python . exe D:\ P ath1\ r enewser v i ce . py

Kevin B. Cary, M.Sc., GISP | WKU — Department of Geography & Geology16/24

Page 17: Generating Scheduled Rasters using Python€¦ · Generating Scheduled Rasters using Python, 2015 Esri Education GIS Conference—Presentation, 2015 Esri Education GIS Conference,

Python Script 1# Name: Pr eci pGen . py# D escr i p t i o n : Gener at e r a st e r su r f ac es of p r ec i p i t a t i o n amount s# and d ev i a t i o n f rom nor mal s .# Requi rement s : Sp at i a l A nal yst Ex t ensi on

# Impor t syst em modulesi mpor t ar cpyf rom ar cpy i mpor t envf rom ar cpy . sa i mpor t ∗

# Impor t sh u t i l modulesi mpor t sh u t i lf rom sh u t i l i mpor t r mt r ee

i mpor t os

# D el et e t he ol d r a st e r s i n Rast er s f o l d e r t o avoi d copy over e r r o r s ( i f any ) sh u t i l . r m t r ee ( ’ Rast er s ’ )

# Cr eat e Rast er s f o l d e r agai n f o r s t o r i n gos . mak edi r s ( ’D: \ Pat h1\ Rast er s ’ )

env . wor kspace = r " D at abase Connect i ons\ M esonet L i ve ( AdminMode ) . sde" in Feat u r es = " SDE. St at i on sL i v e "ou t L ocat i on = "D: \ Pat h1 "

Kevin B. Cary, M.Sc., GISP | WKU — Department of Geography & Geology17/24

Page 18: Generating Scheduled Rasters using Python€¦ · Generating Scheduled Rasters using Python, 2015 Esri Education GIS Conference—Presentation, 2015 Esri Education GIS Conference,

# Execut e ( SDE) Feat u r e Cl ass To Sh ap ef i l ear cpy . Feat u r eCl assT oSh ap ef i l e_ con ver si on ( i nFeat u r es , ou t L ocat i on )env . wor kspace = "D: \ Pat h1 "env . ex t en t = Ext en t ( − 89.7 , 36. 4 , − 82, 39. 2)

# Conver t sh a p e f i l e t o Ky . St at e Pl ane Sout h US Feetar cpy . Proj ect _ management ( " St a t i on sL i v e . shp " , " SDE_ St at ionsLiveK YSPS . shp " , "PCSKySPS . p r j " )

# Set envi r onment se t t i n g s agai nenv . wor kspace = "D: \ Pat h1 "env . ex t en t = Ext en t ( 500000 , 1650000 , 2740000 , 2700000)

# Set l o c a l v a r i a b l e si n P o i n t F eat u r es = " SDE_ St at ionsLiveK YSPS . shp "c el l S i z e = 2500.0power = 2sear chRad i u s = Rad i u sV ar i ab l e ( 8 , 500000)

# Check out an ArcGIS Sp at i a l A nal yst ex t en si on l i c en sear cpy . Check Out Ext ensi on ( " Sp at i a l " )

# Execut e IDWout IDW p30 = Idw ( i nPo i n t Feat u r es , " p30 " , c e l l S i z e , power , sear chRad i us )out IDW p60 = Idw ( i nPo i n t Feat u r es , " p60 " , c e l l S i z e , power , sear chRad i us )out IDW p90 = Idw ( i nPo i n t Feat u r es , " p90 " , c e l l S i z e , power , sear chRad i us )

Kevin B. Cary, M.Sc., GISP | WKU — Department of Geography & Geology18/24

Page 19: Generating Scheduled Rasters using Python€¦ · Generating Scheduled Rasters using Python, 2015 Esri Education GIS Conference—Presentation, 2015 Esri Education GIS Conference,

out IDW p120 = Idw ( i nP oi n t Feat u r es , " p120 " , c e l l S i z e , power , sear chRad i us )out IDW p180 = Idw ( i nP oi n t Feat u r es , " p180 " , c e l l S i z e , power , sear chRad i us )out IDW pn30 = Idw ( i nP oi n t Feat u r es , " n30 " , c e l l Si z e , power , sear ch Rad i us )out IDW pn60 = Idw ( i nP oi n t Feat u r es , " n60 " , c e l l Si z e , power , sear ch Rad i us )out IDW pn90 = Idw ( i nP oi n t Feat u r es , " n90 " , c e l l Si z e , power , sear ch Rad i us )out IDW pn120 = Idw ( i nPo i n t Feat u r es , " n120 " , c e l l S i z e , power , sear chRad i us )out IDW pn180 = Idw ( i nPo i n t Feat u r es , " n180 " , c e l l S i z e , power , sear chRad i us )

# Save t he out putout IDW p30 . save ( " D: \ Pat h1\ Rast er s\ pt 30 . img " )out IDW p60 . save ( " D: \ Pat h1\ Rast er s\ pt 60 . img " )out IDW p90 . save ( " D: \ Pat h1\ Rast er s\ pt 90 . img " )out IDW p120 . save ( " D: \ Pat h1\ Rast er s\ pt 120 . img " )out IDW p180 . save ( " D: \ Pat h1\ Rast er s\ pt 180 . img " )out IDW pn30 . save ( " D: \ Pat h1\ Rast er s\ pt n30 . img " )out IDW pn60 . save ( " D: \ Pat h1\ Rast er s\ pt n60 . img " )out IDW pn90 . save ( " D: \ Pat h1\ Rast er s\ pt n90 . img " )out IDW pn120 . save ( " D: \ Pat h1\ Rast er s\ pt n120 . img " )out IDW pn180 . save ( " D: \ Pat h1\ Rast er s\ pt n180 . img " )

# T ot al minus Normalou t RasD i f f 30 = out IDW p30 − out IDW pn30out RasD i f f 30 . save ( " D: \ Pat h1\ Rast er s\ p t n 3 0d i f f . img " )ou t RasD i f f 60 = out IDW p60 − out IDW pn60out RasD i f f 60 . save ( " D: \ Pat h1\ Rast er s\ p t n 6 0d i f f . img " )ou t RasD i f f 90 = out IDW p90 − out IDW pn90out RasD i f f 90 . save ( " D: \ Pat h1\ Rast er s\ p t n 9 0d i f f . img " )ou t RasD i f f 120 = out IDW p120 − out IDW pn120

Kevin B. Cary, M.Sc., GISP | WKU — Department of Geography & Geology19/24

Page 20: Generating Scheduled Rasters using Python€¦ · Generating Scheduled Rasters using Python, 2015 Esri Education GIS Conference—Presentation, 2015 Esri Education GIS Conference,

out RasD i f f 120 . save ( " D: \ Pat h1\ Rast er s\ p t n 1 20d i f f . img " )ou t RasD i f f 180 = out IDW p180 − out IDW pn180out RasD i f f 180 . save ( " D: \ Pat h1\ Rast er s\ p t n 1 80d i f f . img " )

# Cl i p Rast erRast er T o BeCl i ppedp 30 = "D: \ Pat h 1 \ Rast er s\ pt 30 . img "Rast er T o BeCl i ppedp 60 = "D: \ Pat h 1 \ Rast er s\ pt 60 . img "Rast er T oBeCl i ppedp90 = "D: \ Pat h1\ Rast er s\ pt 90 . img "Rast er T oBeCl i ppedp120 = "D: \ Pat h1\ Rast er s\ pt 120 . img "Rast er T oBeCl i ppedp180 = "D: \ Pat h1\ Rast er s\ pt 180 . img "Rast er T o BeCl i ppedpn 30 = "D: \ Pat h 1 \ Rast er s\ p t n 3 0 d i f f . img "Rast er T o BeCl i ppedpn 60 = "D: \ Pat h 1 \ Rast er s\ p t n 6 0 d i f f . img "Rast er T oBeCl i ppedpn90 = "D: \ Pat h1\ Rast er s\ p t n 9 0 d i f f . img "Rast er T oBeCl i ppedpn120 = "D: \ Pat h1\ Rast er s\ p t n 120d i f f . img "Rast er T oBeCl i ppedpn180 = "D: \ Pat h1\ Rast er s\ p t n 180d i f f . img "

Cl i p L ayer = " ky . shp "

Cl i p p ed R ast er L ay er p 3 0 = "D: \ Pat h 1 \ Rast er s\ p30 . img"Cl i p p ed R ast er L ay er p 6 0 = "D: \ Pat h 1 \ Rast er s\ p60 . img"Cl i ppedRast er L ay er p90 = "D: \ Pat h1\ Rast er s\ p90 . img "Cl i p p ed Rast er L ay er p 1 2 0 = "D: \ Pat h 1 \ Rast er s\ p120 . img"Cl i p p ed Rast er L ay er p 1 8 0 = "D: \ Pat h 1 \ Rast er s\ p180 . img"Cl i p p ed Rast er L ay er p n 3 0 = "D: \ Pat h 1\ Rast er s\ pn30 . img"Cl i p p ed Rast er L ay er p n 6 0 = "D: \ Pat h 1\ Rast er s\ pn60 . img"Cl i ppedRast er L ayer pn90 = "D: \ Pat h1\ Rast er s\ pn90 . img "Cl i ppedRast er L ayer pn120 = "D: \ Pat h1\ Rast er s\ pn120 . img "

Kevin B. Cary, M.Sc., GISP | WKU — Department of Geography & Geology20/24

Page 21: Generating Scheduled Rasters using Python€¦ · Generating Scheduled Rasters using Python, 2015 Esri Education GIS Conference—Presentation, 2015 Esri Education GIS Conference,

Cl i ppedRast er L ayer pn180 = "D: \ Pat h1\ Rast er s\ pn180 . img "

ar cpy . Cl ip_ management ( Rast er T oBeCl i ppedp30 , " # " , Cl i ppedRast er L ayer p30 , Cl i pL ayer , "− 2147483648" , " Cl i ppi ngGeomet r y " , " MAINTAIN_EXTENT " )

ar cpy . Cl ip_ management ( Rast er T oBeCl i ppedp60 , " # " , Cl i ppedRast er L ayer p60 , Cl i pL ayer , "− 2147483648" , " Cl i ppi ngGeomet r y " , " MAINTAIN_EXTENT " )

ar cpy . Cl ip_ management ( Rast er T oBeCl i ppedp90 , " # " , Cl i ppedRast er L ayer p90 , Cl i pL ayer , "− 2147483648" , " Cl i ppi ngGeomet r y " , " MAINTAIN_EXTENT " )

ar cpy . Cl ip_ management ( Rast er T oBeCl i ppedp120 , " # " , Cl i ppedRast er L ayer p120 , Cl i pL ayer , "− 2147483648" , " Cl i ppi ngGeomet r y " , " MAINTAIN_EXTENT " )

ar cpy . Cl ip_ management ( Rast er T oBeCl i ppedp180 , " # " , Cl i ppedRast er L ayer p180 , Cl i pL ayer , "− 2147483648" , " Cl i ppi ngGeomet r y " , " MAINTAIN_EXTENT " )

ar cpy . Cl ip_ management ( Rast er T oBeCl i ppedpn30 , " # " , Cl i ppedRast er L ayer pn30 , Cl i pL ayer , "− 2147483648" , " Cl i ppi ngGeomet r y " , " MAINTAIN_EXTENT " )

ar cpy . Cl ip_ management ( Rast er T oBeCl i ppedpn60 , " # " , Cl i ppedRast er L ayer pn60 , Cl i pL ayer , "− 2147483648" , " Cl i ppi ngGeomet r y " , " MAINTAIN_EXTENT " )

ar cpy . Cl ip_ management ( Rast er T oBeCl i ppedpn90 , " # " , Cl i ppedRast er L ayer pn90 , Cl i pL ayer , "− 2147483648" , " Cl i ppi ngGeomet r y " , " MAINTAIN_EXTENT " )

ar cpy . Cl ip_ management ( Rast er T oBeCl i ppedpn120 , " # " , Cl i ppedRast er L ayer pn120 , Cl i pL ayer , "− 2147483648" , " Cl i ppi ngGeomet r y " , " MAINTAIN_EXTENT " )

ar cpy . Cl ip_ management ( Rast er T oBeCl i ppedpn180 , " # " , Cl i ppedRast er L ayer pn180 , Cl i pL ayer , "− 2147483648" , " Cl i ppi ngGeomet r y " , " MAINTAIN_EXTENT " )

# D el et e ex i st i n g SD E_ St at i onsL i ve . shp as a r esu l t of r unn i ng t h i s# sc r i p t p r ev i o u s l y i n an ef f o r t t o keep at t r i b u t e s updat edar cpy . Delet e_ management ( " SDE_ St at ionsLiveK YSPS . shp " )

Kevin B. Cary, M.Sc., GISP | WKU — Department of Geography & Geology21/24

Page 22: Generating Scheduled Rasters using Python€¦ · Generating Scheduled Rasters using Python, 2015 Esri Education GIS Conference—Presentation, 2015 Esri Education GIS Conference,

ar cpy . Delet e_ management ( " St at i o n sL i v e . shp " )

# D el et e r a st e r s t hat begi n wi t h t he l e t t e r s " pt " ( t h i s i s i n t er m ed i at e dat a ) dat aset s = ar cpy . L i s t Ras t er s ( " pt ∗ " )f o r d at aset i n d at aset s :

ar cpy . Delet e_ management ( dat aset )

# D el et e t he i n f o f o l d e r as a r esu l t f rom i n t er m ed i at e dat a p r ocessi n g sh u t i l . r m t r ee ( ’ i n f o ’ )

Kevin B. Cary, M.Sc., GISP | WKU — Department of Geography & Geology22/24

Page 23: Generating Scheduled Rasters using Python€¦ · Generating Scheduled Rasters using Python, 2015 Esri Education GIS Conference—Presentation, 2015 Esri Education GIS Conference,

Python Script 2# D escr i p t i o n : Cr eat es a se r v i c e d ef i n i t i o n t hat can be used t o ov er w r i t e an##

e x i st i n g se r v i c e . When t h i s se r v i c e d e f i n i t i o n i s p ub l i sh ed i t w i l l o ver w r i t e t he e x i st i n g se r v i c e .

# Requi rement s : Connect i on t o an ArcGIS Ser ver or My Host ed Ser v i c es

# Impor t syst em modulesi mpor t ar cpyf rom ar cpy i mpor t envi mpor t xml . dom . minidom as DOM

i mpor t osi mpor t sh u t i l

sh u t i l . c o p y f i l e ( ’D: \ Pat h1\ Exper i ment al D r ought _ Or i g i nal . sdd r af t ’ , ’D : \ Pat h1\ Exper i ment alD r ough t . sddr af t ’ )

# Set envi r onment se t t i n g senv . wor kspace = r "D: \ Pat h1 "

# Set l o c a l v a r i a b l e si n Se r v i c e D e f i n i t i o n D r a f t = r " Exper iment al D r ought . sd d r af t "

# . sd d r a f t i s cr eat ed by e x i t i n g out of Ser v i ce Ed i t o r r i g h t bef or e t he op t i on# t o ac t u a l l y p u b l i sh .o u t Ser v i c eD ef i n i t i o n = r "D: \ Pat h1\ SD Fi l e\ Exper i ment alD r ough t . sd "

Kevin B. Cary, M.Sc., GISP | WKU — Department of Geography & Geology23/24

Page 24: Generating Scheduled Rasters using Python€¦ · Generating Scheduled Rasters using Python, 2015 Esri Education GIS Conference—Presentation, 2015 Esri Education GIS Conference,

newT ype = ’ esr i Ser v i ceD ef i n i t i onT y pe_ Rep l acem ent ’xml = env . wor kspace + " / " + i n Se r v i c e D e f i n i t i o n D r a f t doc = DOM. par se ( xml )d e sc r i p t i o n s = doc . get Element sByT agName ( ’ T ype ’ )f o r desc i n d e sc r i p t i o n s :

i f desc . parent Node . t agName == ’ SVCM ani f est ’ : if desc . hasChi l dN odes ( ) :

desc . f i r st C h i l d . dat a = newT ypeout Xml = xmlf = open ( out Xml , ’ w ’ )doc . w r i t exml ( f )f . c l o se ( )

# Execut e St ageSer v i ce . Not e : you may get Er r o r 001269 Compr essi ng t he se r v i c e# d e f i n i t i o n f a i l ed i f accessi n g o t her f i l e s i n same f o l d e r as . sd f i l e . ar cpy . St ageSer v i ce_ ser ver ( i n Ser v i c eD ef i n i t i o n D r a f t , o u t Ser v i c eD ef i n i t i o n )

# Set l o c a l v a r i a b l e si n Sd F i l e = o u t Ser v i c eD ef i n i t i o ni n Ser v er = r " C: \ User s\ A d m i n i st r at o r \ AppDat a\ Roaming\ ESRI \ Desk t op10 . 2\ A r cCat al og \ AGS_ Machine ( p u b l i s h er ) . ags"

# Execut e U p l o ad Ser v i ceD ef i n i t i o nar cpy . U p l o ad Ser v i ceD ef i n i t i on _ ser v er ( i n Sd F i l e , i n Ser v er )

os . remove ( ’D: \ Pat h1\ SD Fi l e\ Exper i ment al D r ough t . sd ’ )

Kevin B. Cary, M.Sc., GISP | WKU — Department of Geography & Geology24/24