Mercurial > dive4elements > river
comparison flys-backend/contrib/shpimporter/shpimporter.py @ 3689:c938e568c4a2 2.9
merged flys-backend/2.9
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:44 +0200 |
parents | 8654e4480fc6 |
children | 890eaa0a5162 |
comparison
equal
deleted
inserted
replaced
3651:06a65baae494 | 3689:c938e568c4a2 |
---|---|
1 import ogr | |
2 | |
3 import utils, optparse | |
4 | |
5 from uesg import UESG | |
6 from axis import Axis | |
7 from km import KM | |
8 from lines import Line | |
9 from fixpoints import Fixpoint | |
10 from buildings import Building | |
11 from crosssectiontracks import CrosssectionTrack | |
12 from floodplains import Floodplain | |
13 from boundaries import HydrBoundary, HydrBoundaryPoly | |
14 from hws import HWS | |
15 from gauges import GaugeLocation | |
16 from catchments import Catchment | |
17 | |
18 | |
19 VERBOSE_DEBUG=2 | |
20 VERBOSE_INFO=1 | |
21 | |
22 | |
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): | |
39 return [ | |
40 Axis(config), | |
41 KM(config), | |
42 CrosssectionTrack(config), | |
43 Line(config), | |
44 Fixpoint(config), | |
45 Building(config), | |
46 Floodplain(config), | |
47 HydrBoundary(config), | |
48 HydrBoundaryPoly(config), | |
49 HWS(config), | |
50 GaugeLocation(config), | |
51 Catchment(config), | |
52 UESG(config) | |
53 ] | |
54 | |
55 | |
56 def getConfig(): | |
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("--dry_run", type="int", default=0) | |
66 parser.add_option("--skip_axis", type="int") | |
67 parser.add_option("--skip_hydr_boundaries", type="int") | |
68 parser.add_option("--skip_buildings", type="int") | |
69 parser.add_option("--skip_crosssections", type="int") | |
70 parser.add_option("--skip_lines", type="int") | |
71 parser.add_option("--skip_fixpoints", type="int") | |
72 parser.add_option("--skip_floodplains", type="int") | |
73 parser.add_option("--skip_hws", type="int") | |
74 parser.add_option("--skip_gauge_locations", type="int") | |
75 parser.add_option("--skip_catchments", type="int") | |
76 parser.add_option("--skip_kms", type="int") | |
77 parser.add_option("--skip_uesgs", type="int") | |
78 (config, args) = parser.parse_args() | |
79 | |
80 if config.directory == None: | |
81 ERROR("No river directory specified!") | |
82 raise Exception("Invalid config") | |
83 elif config.host == None: | |
84 ERROR("No database host specified!") | |
85 raise Exception("Invalid config") | |
86 elif config.user == None: | |
87 ERROR("No databaser user specified!") | |
88 raise Exception("Invalid config") | |
89 elif config.password == None: | |
90 ERROR("No password specified!") | |
91 raise Exception("Invalid config") | |
92 elif config.river_id == None: | |
93 ERROR("No river id specified!") | |
94 raise Exception("Invalid config") | |
95 | |
96 return config | |
97 | |
98 | |
99 def skip_importer(config, importer): | |
100 if config.skip_axis == 1 and isinstance(importer, Axis): | |
101 return True | |
102 elif config.skip_hydr_boundaries == 1 and isinstance(importer, HydrBoundary): | |
103 return True | |
104 elif config.skip_hydr_boundaries == 1 and isinstance(importer, HydrBoundaryPoly): | |
105 return True | |
106 elif config.skip_buildings == 1 and isinstance(importer, Building): | |
107 return True | |
108 elif config.skip_crosssections == 1 and isinstance(importer, CrosssectionTrack): | |
109 return True | |
110 elif config.skip_lines == 1 and isinstance(importer, Line): | |
111 return True | |
112 elif config.skip_fixpoints == 1 and isinstance(importer, Fixpoint): | |
113 return True | |
114 elif config.skip_floodplains == 1 and isinstance(importer, Floodplain): | |
115 return True | |
116 elif config.skip_hws == 1 and isinstance(importer, HWS): | |
117 return True | |
118 elif config.skip_gauge_locations == 1 and isinstance(importer, GaugeLocation): | |
119 return True | |
120 elif config.skip_catchments == 1 and isinstance(importer, Catchment): | |
121 return True | |
122 elif config.skip_kms == 1 and isinstance(importer, KM): | |
123 return True | |
124 elif config.skip_uesgs == 1 and isinstance(importer, UESG): | |
125 return True | |
126 | |
127 return False | |
128 | |
129 | |
130 def parse(): | |
131 config=None | |
132 try: | |
133 config = getConfig() | |
134 except: | |
135 return | |
136 | |
137 if config == None: | |
138 ERROR("Unable to read config from command line!") | |
139 return | |
140 | |
141 if config.dry_run > 0: | |
142 INFO("You enable 'dry_run'. No database transaction will take place!") | |
143 | |
144 importers = getImporters(config) | |
145 types = {} | |
146 | |
147 for importer in importers: | |
148 if skip_importer(config, importer): | |
149 INFO("Skip import of '%s'" % importer.getName()) | |
150 continue | |
151 | |
152 INFO("Start import of '%s'" % importer.getName()) | |
153 | |
154 shapes = utils.findShapefiles(importer.getPath(config.directory)) | |
155 DEBUG("Found %i Shapefiles" % len(shapes)) | |
156 | |
157 for shpTuple in shapes: | |
158 geomType = importer.walkOverShapes(shpTuple) | |
159 try: | |
160 if geomType is not None: | |
161 num = types[geomType] | |
162 types[geomType] = num+1 | |
163 except: | |
164 types[geomType] = 1 | |
165 | |
166 for key in types: | |
167 DEBUG("%i x geometry type %s" % (types[key], key)) | |
168 | |
169 | |
170 if __name__ == '__main__': | |
171 parse() |