comparison flys-backend/contrib/shpimporter/shpimporter.py @ 4872:a563e9f58f93

Improve error handling and unify dbconn for all importers
author Andre Heinecke <aheinecke@intevation.de>
date Wed, 23 Jan 2013 15:30:09 +0100
parents 890eaa0a5162
children 63f82bdd45f8
comparison
equal deleted inserted replaced
4871:890eaa0a5162 4872:a563e9f58f93
1 import ogr 1 try:
2 from osgeo import ogr
3 except ImportErrror:
4 import ogr
2 5
3 import utils, optparse 6 import utils, optparse
4 7
5 from uesg import UESG 8 from uesg import UESG
6 from axis import Axis 9 from axis import Axis
33 def ERROR(msg): 36 def ERROR(msg):
34 config = getConfig() 37 config = getConfig()
35 print "ERROR: %s" % msg 38 print "ERROR: %s" % msg
36 39
37 40
38 def getImporters(config): 41 def getImporters(config, dbconn):
39 return [ 42 return [
40 Axis(config), 43 Axis(config, dbconn),
41 KM(config), 44 KM(config, dbconn),
42 CrosssectionTrack(config), 45 CrosssectionTrack(config, dbconn),
43 Line(config), 46 Line(config, dbconn),
44 Fixpoint(config), 47 Fixpoint(config, dbconn),
45 Building(config), 48 Building(config, dbconn),
46 Floodplain(config), 49 Floodplain(config, dbconn),
47 HydrBoundary(config), 50 HydrBoundary(config, dbconn),
48 HydrBoundaryPoly(config), 51 HydrBoundaryPoly(config, dbconn),
49 HWS(config), 52 HWS(config, dbconn),
50 GaugeLocation(config), 53 GaugeLocation(config, dbconn),
51 Catchment(config), 54 Catchment(config, dbconn),
52 UESG(config) 55 UESG(config, dbconn)
53 ] 56 ]
54 57
55 58
56 def getConfig(): 59 def getConfig():
57 parser = optparse.OptionParser() 60 parser = optparse.OptionParser()
127 return True 130 return True
128 131
129 return False 132 return False
130 133
131 134
132 def parse(): 135 def main():
133 config=None 136 config=None
134 try: 137 try:
135 config = getConfig() 138 config = getConfig()
136 except: 139 except:
137 return 140 return -1
138 141
139 if config == None: 142 if config == None:
140 ERROR("Unable to read config from command line!") 143 ERROR("Unable to read config from command line!")
141 return 144 return
142 145
143 if config.dry_run > 0: 146 if config.dry_run > 0:
144 INFO("You enable 'dry_run'. No database transaction will take place!") 147 INFO("You enable 'dry_run'. No database transaction will take place!")
145 148
146 importers = getImporters(config) 149 if config.ogr_connection:
150 dbconn = ogr.Open(config.ogr_connection)
151 else:
152 dbconn = ogr.Open('OCI:%s/%s@%s' % (config.user, config.password, config.host))
153
154 if not dbconn:
155 shpimporter.ERROR("Could not connect to database %s" % self.dbconn)
156 return -1
157
158 importers = getImporters(config, dbconn)
147 types = {} 159 types = {}
148 160
149 for importer in importers: 161 for importer in importers:
150 if skip_importer(config, importer): 162 if skip_importer(config, importer):
151 INFO("Skip import of '%s'" % importer.getName()) 163 INFO("Skip import of '%s'" % importer.getName())
168 for key in types: 180 for key in types:
169 DEBUG("%i x geometry type %s" % (types[key], key)) 181 DEBUG("%i x geometry type %s" % (types[key], key))
170 182
171 183
172 if __name__ == '__main__': 184 if __name__ == '__main__':
173 parse() 185 main()

http://dive4elements.wald.intevation.org