Mercurial > dive4elements > river
comparison flys-backend/contrib/shpimporter/shpimporter.py @ 3654:59ca5dab2782
Shape importer: use python's OptionParse to read user specific configuration from command line.
flys-backend/trunk@5231 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Wed, 22 Aug 2012 11:55:55 +0000 |
parents | 998f1a7dcfde |
children | 8654e4480fc6 |
comparison
equal
deleted
inserted
replaced
3653:20d249e1589f | 3654:59ca5dab2782 |
---|---|
1 import ogr | 1 import ogr |
2 | 2 |
3 import utils | 3 import utils, optparse |
4 | |
4 from uesg import UESG | 5 from uesg import UESG |
5 from axis import Axis | 6 from axis import Axis |
6 from km import KM | 7 from km import KM |
7 from lines import Line | 8 from lines import Line |
8 from fixpoints import Fixpoint | 9 from fixpoints import Fixpoint |
12 from boundaries import HydrBoundary, HydrBoundaryPoly | 13 from boundaries import HydrBoundary, HydrBoundaryPoly |
13 from hws import HWS | 14 from hws import HWS |
14 from gauges import GaugeLocation | 15 from gauges import GaugeLocation |
15 from catchments import Catchment | 16 from catchments import Catchment |
16 | 17 |
17 DBCONN='OCI:user/pass@host' | 18 |
18 PATH='/path/to/Gewaesser/Elbe' | 19 VERBOSE_DEBUG=2 |
19 RIVER_ID=the_river_id | 20 VERBOSE_INFO=1 |
20 DEST_SRS=31467 | |
21 | 21 |
22 | 22 |
23 def getImporters(): | 23 def DEBUG(msg): |
24 config = getConfig() | |
25 if config.verbose >= VERBOSE_DEBUG: | |
26 print "DEBUG: %s" % msg | |
27 | |
28 def INFO(msg): | |
29 config = getConfig() | |
30 if config.verbose >= VERBOSE_INFO: | |
31 print "INFO: %s" % msg | |
32 | |
33 def ERROR(msg): | |
34 config = getConfig() | |
35 print "ERROR: %s" % msg | |
36 | |
37 | |
38 def getImporters(config): | |
24 return [ | 39 return [ |
25 Axis(DBCONN, RIVER_ID, DEST_SRS), | 40 Axis(config), |
26 KM(DBCONN, RIVER_ID, DEST_SRS), | 41 KM(config), |
27 CrosssectionTrack(DBCONN, RIVER_ID, DEST_SRS), | 42 CrosssectionTrack(config), |
28 Line(DBCONN, RIVER_ID, DEST_SRS), | 43 Line(config), |
29 Fixpoint(DBCONN, RIVER_ID, DEST_SRS), | 44 Fixpoint(config), |
30 Building(DBCONN, RIVER_ID, DEST_SRS), | 45 Building(config), |
31 Floodplain(DBCONN, RIVER_ID, DEST_SRS), | 46 Floodplain(config), |
32 HydrBoundary(DBCONN, RIVER_ID, DEST_SRS), | 47 HydrBoundary(config), |
33 HydrBoundaryPoly(DBCONN, RIVER_ID, DEST_SRS), | 48 HydrBoundaryPoly(config), |
34 HWS(DBCONN, RIVER_ID, DEST_SRS), | 49 HWS(config), |
35 GaugeLocation(DBCONN, RIVER_ID, DEST_SRS), | 50 GaugeLocation(config), |
36 Catchment(DBCONN, RIVER_ID, DEST_SRS), | 51 Catchment(config), |
37 UESG(DBCONN, RIVER_ID, DEST_SRS) | 52 UESG(config) |
38 ] | 53 ] |
39 | 54 |
40 | 55 |
41 if __name__ == '__main__': | 56 def getConfig(): |
42 importers = getImporters() | 57 parser = optparse.OptionParser() |
58 parser.add_option("--directory", type="string") | |
59 parser.add_option("--target_srs", type="int") | |
60 parser.add_option("--host", type="string") | |
61 parser.add_option("--user", type="string") | |
62 parser.add_option("--password", type="string") | |
63 parser.add_option("--river_id", type="int") | |
64 parser.add_option("--verbose", type="int", default=1) | |
65 parser.add_option("--skip_axis", type="int") | |
66 parser.add_option("--skip_hydr_boundaries", type="int") | |
67 parser.add_option("--skip_buildings", type="int") | |
68 parser.add_option("--skip_crosssections", type="int") | |
69 parser.add_option("--skip_lines", type="int") | |
70 parser.add_option("--skip_fixpoints", type="int") | |
71 parser.add_option("--skip_floodplains", type="int") | |
72 parser.add_option("--skip_hws", type="int") | |
73 parser.add_option("--skip_gauge_locations", type="int") | |
74 parser.add_option("--skip_catchments", type="int") | |
75 parser.add_option("--skip_kms", type="int") | |
76 parser.add_option("--skip_uesgs", type="int") | |
77 (config, args) = parser.parse_args() | |
43 | 78 |
79 if config.directory == None: | |
80 ERROR("No river directory specified!") | |
81 raise Exception("Invalid config") | |
82 elif config.host == None: | |
83 ERROR("No database host specified!") | |
84 raise Exception("Invalid config") | |
85 elif config.user == None: | |
86 ERROR("No databaser user specified!") | |
87 raise Exception("Invalid config") | |
88 elif config.password == None: | |
89 ERROR("No password specified!") | |
90 raise Exception("Invalid config") | |
91 elif config.river_id == None: | |
92 ERROR("No river id specified!") | |
93 raise Exception("Invalid config") | |
94 | |
95 return config | |
96 | |
97 | |
98 def skip_importer(config, importer): | |
99 if config.skip_axis == 1 and isinstance(importer, Axis): | |
100 return True | |
101 elif config.skip_hydr_boundaries == 1 and isinstance(importer, HydrBoundary): | |
102 return True | |
103 elif config.skip_hydr_boundaries == 1 and isinstance(importer, HydrBoundaryPoly): | |
104 return True | |
105 elif config.skip_buildings == 1 and isinstance(importer, Building): | |
106 return True | |
107 elif config.skip_crosssections == 1 and isinstance(importer, CrosssectionTrack): | |
108 return True | |
109 elif config.skip_lines == 1 and isinstance(importer, Line): | |
110 return True | |
111 elif config.skip_fixpoints == 1 and isinstance(importer, Fixpoint): | |
112 return True | |
113 elif config.skip_floodplains == 1 and isinstance(importer, Floodplain): | |
114 return True | |
115 elif config.skip_hws == 1 and isinstance(importer, HWS): | |
116 return True | |
117 elif config.skip_gauge_locations == 1 and isinstance(importer, GaugeLocation): | |
118 return True | |
119 elif config.skip_catchments == 1 and isinstance(importer, Catchment): | |
120 return True | |
121 elif config.skip_kms == 1 and isinstance(importer, KM): | |
122 return True | |
123 elif config.skip_uesgs == 1 and isinstance(importer, UESG): | |
124 return True | |
125 | |
126 return False | |
127 | |
128 | |
129 def parse(): | |
130 config=None | |
131 try: | |
132 config = getConfig() | |
133 except: | |
134 return | |
135 | |
136 if config == None: | |
137 print "No Config" | |
138 ERROR("Unable to read config from command line!") | |
139 return | |
140 | |
141 importers = getImporters(config) | |
44 types = {} | 142 types = {} |
45 | 143 |
46 for importer in importers: | 144 for importer in importers: |
47 shapes = utils.findShapefiles(importer.getPath(PATH)) | 145 if skip_importer(config, importer): |
48 print "Found %i Shapefiles" % len(shapes) | 146 INFO("Skip import of '%s'" % importer.getName()) |
147 continue | |
148 | |
149 INFO("Start import of '%s'" % importer.getName()) | |
150 | |
151 shapes = utils.findShapefiles(importer.getPath(config.directory)) | |
152 INFO("Found %i Shapefiles" % len(shapes)) | |
49 | 153 |
50 for shpTuple in shapes: | 154 for shpTuple in shapes: |
51 geomType = importer.walkOverShapes(shpTuple) | 155 geomType = importer.walkOverShapes(shpTuple) |
52 try: | 156 try: |
53 num = types[geomType] | 157 num = types[geomType] |
54 types[geomType] = num+1 | 158 types[geomType] = num+1 |
55 except: | 159 except: |
56 types[geomType] = 1 | 160 types[geomType] = 1 |
57 | 161 |
58 for key in types: | 162 for key in types: |
59 print "%i x geometry type %s" % (types[key], key) | 163 DEBUG("%i x geometry type %s" % (types[key], key)) |
60 | 164 |
165 | |
166 if __name__ == '__main__': | |
167 parse() |