# repeat for the level lines
i=levels.index(level)forseginsegs:line.append('<Placemark><name>level:'+str(level)+'</name><styleUrl>#level'+str(i)+head2)leng=-9999forjinrange(len(seg[:,0])):line.append(str(seg[j,0])+','+str(seg[j,1])+',0 ')ifj>0:leng=max(leng,np.sqrt((seg[j,0]-seg[j-1,0])**2+(seg[j,1]-seg[j-1,1])**2))leng0=np.sqrt((seg[j,0]-seg[0,0])**2+(seg[j,1]-seg[0,1])**2)
邊界線之閉合
多邊形碰到東西南北其中1邊、碰到2個邊、等等情況,逐一處理
ewsn=np.zeros(shape=(4,2))j=-1# end points not closed, add coner point(s) to close the polygons.
ifleng0>lengandleng0/leng>5:ifabs(seg[j,0]-e)<tol:ewsn[0,1]=1ifabs(seg[0,0]-e)<tol:ewsn[0,0]=1ifabs(seg[j,0]-w)<tol:ewsn[1,1]=1ifabs(seg[0,0]-w)<tol:ewsn[1,0]=1ifabs(seg[j,1]-s)<tol:ewsn[2,1]=1ifabs(seg[0,1]-s)<tol:ewsn[2,0]=1ifabs(seg[j,1]-n)<tol:ewsn[3,1]=1ifabs(seg[0,1]-n)<tol:ewsn[3,0]=1ifsum(ewsn[1,:]+ewsn[2,:])==2:line.append(str(np.min(lon))+','+str(np.min(lat))+',0 ')ifsum(ewsn[1,:]+ewsn[3,:])==2:line.append(str(np.min(lon))+','+str(np.max(lat))+',0 ')ifsum(ewsn[0,:]+ewsn[3,:])==2:line.append(str(np.max(lon))+','+str(np.max(lat))+',0 ')ifsum(ewsn[0,:]+ewsn[2,:])==2:line.append(str(np.max(lon))+','+str(np.min(lat))+',0 ')# TODO: when contour pass half of the domain,must add two edge points.
line.append(tail2)