Mercurial > gemma
comparison pkg/imports/isr.go @ 4571:a413b4a89bcc iso-areas
Update areas if config has changed.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Sun, 06 Oct 2019 11:41:31 +0200 |
parents | 671441357db0 |
children | c657dec6b0fa |
comparison
equal
deleted
inserted
replaced
4570:4b3a298b94f8 | 4571:a413b4a89bcc |
---|---|
42 func (isrJobCreator) StageDone(context.Context, *sql.Tx, int64) error { | 42 func (isrJobCreator) StageDone(context.Context, *sql.Tx, int64) error { |
43 return nil | 43 return nil |
44 } | 44 } |
45 | 45 |
46 func (isrJobCreator) Depends() [2][]string { | 46 func (isrJobCreator) Depends() [2][]string { |
47 return [2][]string{ | 47 return srJobCreator{}.Depends() |
48 {"sounding_results", "sounding_results_contour_lines"}, | |
49 {}, | |
50 } | |
51 } | 48 } |
52 | 49 |
53 const ( | 50 const ( |
54 fetchSoundingResultsIDsSQL = ` | 51 fetchSoundingResultsIDsSQL = ` |
55 SELECT bottleneck_id, id | 52 SELECT bottleneck_id, id |
56 FROM waterway.sounding_results | 53 FROM waterway.sounding_results |
57 ORDER BY bottleneck_id | 54 ORDER BY bottleneck_id |
58 ` | 55 ` |
59 deleteContourLinesSQL = ` | 56 deleteContourLinesSQL = ` |
60 DELETE FROM waterway.sounding_results_contour_lines | 57 DELETE FROM waterway.sounding_results_contour_lines |
58 WHERE sounding_result_id = $1 | |
59 ` | |
60 deleteIsoAreasSQL = ` | |
61 DELETE FROM waterway.sounding_results_iso_areas | |
61 WHERE sounding_result_id = $1 | 62 WHERE sounding_result_id = $1 |
62 ` | 63 ` |
63 ) | 64 ) |
64 | 65 |
65 func (isr *IsoRefresh) CleanUp() error { return nil } | 66 func (isr *IsoRefresh) CleanUp() error { return nil } |
164 if err != nil { | 165 if err != nil { |
165 return nil | 166 return nil |
166 } | 167 } |
167 defer tx.Rollback() | 168 defer tx.Rollback() |
168 | 169 |
169 insertStmt, err := tx.Prepare(insertContourSQL) | 170 insertContoursStmt, err := tx.Prepare(insertContourSQL) |
170 if err != nil { | 171 if err != nil { |
171 return err | 172 return err |
172 } | 173 } |
174 defer insertContoursStmt.Close() | |
175 | |
176 insertAreasStmt, err := tx.Prepare(insertIsoAreasSQL) | |
177 if err != nil { | |
178 return err | |
179 } | |
180 defer insertAreasStmt.Close() | |
173 | 181 |
174 // For all sounding results in bottleneck. | 182 // For all sounding results in bottleneck. |
175 for _, sr := range bn.srs { | 183 for _, sr := range bn.srs { |
176 tree, err := octree.FetchOctreeDirectly(ctx, tx, sr) | 184 tree, err := octree.FetchOctreeDirectly(ctx, tx, sr) |
177 if err != nil { | 185 if err != nil { |
183 // Delete the old contour lines. | 191 // Delete the old contour lines. |
184 if _, err := tx.ExecContext(ctx, deleteContourLinesSQL, sr); err != nil { | 192 if _, err := tx.ExecContext(ctx, deleteContourLinesSQL, sr); err != nil { |
185 return err | 193 return err |
186 } | 194 } |
187 | 195 |
196 // Delete the old iso areas. | |
197 if _, err := tx.ExecContext(ctx, deleteIsoAreasSQL, sr); err != nil { | |
198 return err | |
199 } | |
200 | |
188 octree.DoContours(tree, hs, func(res *octree.ContourResult) { | 201 octree.DoContours(tree, hs, func(res *octree.ContourResult) { |
189 if err == nil && len(res.Lines) > 0 { | 202 if err == nil && len(res.Lines) > 0 { |
190 _, err = insertStmt.ExecContext( | 203 _, err = insertContoursStmt.ExecContext( |
191 ctx, | 204 ctx, |
192 sr, res.Height, tree.EPSG, | 205 sr, res.Height, tree.EPSG, |
193 res.Lines.AsWKB2D(), | 206 res.Lines.AsWKB2D(), |
194 contourTolerance) | 207 contourTolerance) |
195 } | 208 } |
196 }) | 209 }) |
197 if err != nil { | 210 if err != nil { |
198 return err | 211 return err |
199 } | 212 } |
213 | |
214 // Calculate and store the iso areas. | |
215 areas := tree.TraceAreas(hs, isoCellSize) | |
216 for i, a := range areas { | |
217 if len(a) == 0 { | |
218 continue | |
219 } | |
220 if _, err := insertAreasStmt.ExecContext( | |
221 ctx, | |
222 sr, hs[i], tree.EPSG, | |
223 a.AsWKB(), | |
224 contourTolerance, | |
225 ); err != nil { | |
226 return err | |
227 } | |
228 } | |
200 } | 229 } |
201 | 230 |
202 return tx.Commit() | 231 return tx.Commit() |
203 } | 232 } |