yuanhung yeh
2020-10-27 5b98e9857d5081c30fcef0206c38f384dc542514
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
import datetime
 
from pub.extactJSON import ParserMOI as moiParser
from pub.queryMoi import QueryElevation as moiEvApi
from pub.postgis import Dbconnect 
 
from parser import Updateev0
from parser import GetMoiEvBy84
 
class Db():
 
    def __init__(self,dbConf):
        self.dbConf=dbConf
        self.updateid=0
        self.dtStart=datetime.datetime.now()
        self.dtNext=datetime.datetime.now()
 
    def resetTabelEv(self,tablename,dbSRS='3826',srcgeom='geom',desgeom='geom'):
        #print (self.dbConf)
        self.gis3d= Dbconnect() 
        print('setEv') 
        self.gis3d.tryConn(self.dbConf)
 
        print('db ok')
        poi=  Updateev0 (tablename,self.updDB,dbSRS=dbSRS,srcgeom=srcgeom,desgeom=desgeom) #self.writeFile)
        
        #other connection
        self.gis3d.fetchTable( self.dbConf,   
         poi.sql_sel() ,
         fnProc=poi
        )
        self.gis3d.destroyConn()
 
    def getTableEv(self,tablename,limit=100,dbSRS='3826',srcgeom='geom',desgeom='geom'):
        
        self.gis3d= Dbconnect()
        print('getEv')
 
        self.gis3d.tryConn(self.dbConf)
 
        print('db ok,limit={},srs={}'.format(limit,dbSRS))
        
        poi=  GetMoiEvBy84 (tablename,self.updDB,limit=limit,fnErr=self.writeFile,dbSRS=dbSRS,srcgeom=srcgeom,desgeom=desgeom) #self.writeFile) #self.updDB) #self.writeFile)
        #poi=  GetMoiEvBy84 (tablename,self.writeFile,limit=limit,fnErr=self.writeFile,dbSRS=dbSRS,srcgeom=srcgeom,desgeom=desgeom) #self.writeFile) #self.updDB) #self.writeFile)
 
        self.dtStart=datetime.datetime.now()
        #other connection
        self.gis3d.fetchTable( self.dbConf,
         poi.sql_sel() ,
         fnProc=poi
        )
        print ('total {}'.format(datetime.datetime.now()-self.dtStart))
        self.gis3d.destroyConn()
    
    
    def updDB(self,sql_upd,wkt,id,table):
        
        #print('update id',id)
        #self.updateid=self.updateid+1
        if(self.updateid % 100 == 0):
           dtnow= datetime.datetime.now()
           print('count:',self.updateid,'{}({})({} @ {})'.format(table,id,dtnow-self.dtNext, dtnow-self.dtStart))
           self.dtNext=datetime.datetime.now()
        
        self.updateid=self.updateid+1   
        #print ('update!{}'.format(id,wkt))
        self.gis3d.executeWithConn(sql_upd.format(wkt,id))
#       #  print('nothing')
 
    def writeFile(self,sql_upd,wkt,id,table):
        #print('writefile',id)
        print(id)
        fname='logs/{}.{}.log'.format(table,self.dtStart.strftime('%Y%m%d_%H%M%S') )
        print ('error@({})-->{}'.format(id,fname ) )
        fp = open(fname, "a")
        lines =[sql_upd.format(wkt,id),";\n"]
        # ["One\n", "Two\n", "Three\n", "Four\n", "Five\n"]
        fp.writelines(lines)
        fp.close()