forked from zhlin/WRF-GoogleEarth
-
Notifications
You must be signed in to change notification settings - Fork 2
/
lfn2kml.py
168 lines (147 loc) · 3.56 KB
/
lfn2kml.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env python
import matplotlib
try:
matplotlib.use('Agg')
except:
pass
from netCDF4 import Dataset
from pylab import contour
import sys
kmlstr= \
'''<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<name>fire_perimeter.kml</name>
<Style id="redLine">
<LineStyle>
<color>ff0000ff</color>
<width>3</width>
</LineStyle>
<PolyStyle>
<color>ff0000ff</color>
<fill>0</fill>
</PolyStyle>
</Style>
<open>0</open>
%s
</Document>
</kml>'''
placestr= \
'''<Placemark>
<name>Fire perimeter at %(time)s</name>
<styleUrl>#redLine</styleUrl>
%(timestr)s
<MultiGeometry>
%(poly)s
</MultiGeometry>
</Placemark>'''
polystr= \
'''<Polygon>
<tessellate>1</tessellate>
<altitudeMode>clampToGround</altitudeMode>
<outerBoundaryIs>
<LinearRing>
<coordinates>
%s
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>'''
# time interval specification for animated output
timestr=\
'''<TimeSpan>
%(begin)s
%(end)s
</TimeSpan>'''
beginstr='<begin>%s</begin>'
endstr='<end>%s</end>'
wrftimestr='%Y-%m-%d_%H:%M:%S'
def getpts(file,nstep=-1):
f=Dataset(file,'r')
if f.variables['LFN'].shape[0] == 1:
lfn=f.variables['LFN'][0,:,:]
else:
lfn=f.variables['LFN'][nstep,:,:]
(fny,fnx)=lfn.shape
nx=len(f.dimensions['west_east'])+1
ny=len(f.dimensions['south_north'])+1
(srx,sry)=(fnx/nx,fny/ny)
lfn=lfn[:-sry,:-srx]
if (lfn > 0).all():
return None
x=f.variables['FXLONG'][0,:-sry,:-srx]
y=f.variables['FXLAT'][0,:-sry,:-srx]
c=contour(x,y,lfn,[0]).collections[0]
p=c.get_paths()
poly=[]
for i in p:
poly.extend(i.to_polygons())
return poly
def gettime(file,nstep=-1):
f=Dataset(file,'r')
if nstep >= len(f.variables['Times']):
return ''
if len(f.variables['Times']) == 1:
t=f.variables['Times'][0].tostring()
else:
t=f.variables['Times'][nstep].tostring()
return t.replace('_','T')
def createkml(poly,time,tstr):
l=[]
for p in poly:
l.append(createpoly(p,tstr))
s='\n'.join(l)
s=placestr % {'time':time, 'poly':s, 'timestr':tstr}
return s #kmlstr % s
def createpoly(poly,time=''):
l=[]
for i in xrange(poly.shape[0]):
l.append('%f,%f' % (poly[i,0],poly[i,1]))
s='\n'.join(l)
return polystr % s
def main(file,nstep=None):
last=False
only=True
s=[]
if nstep is None:
nstep=0
only=False
while not last:
time=gettime(file,nstep)
stime=time
etime=gettime(file,nstep+1)
if etime=='':
etime=stime
last=True
sstime=beginstr % stime
setime=endstr % etime
tstr=timestr % {'begin':sstime,'end':setime}
poly=getpts(file,nstep)
if poly is not None:
s.append(createkml(poly,time,tstr))
nstep=nstep+1
if only:
last=True
s='\n'.join(s)
s=kmlstr % s
f=open('fire_perimeter.kml','w')
f.write(s)
'''
def main(argv):
import shapefile
if len(sys.argv[1:]) > 1:
n=int(sys.argv[2])
else:
n=-1
poly=getpts(argv[1],n)
w=shapefile.Writer(shapeType=shapefile.POLYGON)
w.poly(parts=poly)
w.save('fire.shp')
sys.exit(0)
'''
if __name__ == '__main__':
if len(sys.argv[1:]) > 1:
n=int(sys.argv[2])
else:
n=None
main(sys.argv[1],n)