@@ -115,38 +115,41 @@ def pivot_to_pgr(source, cost_calculation_file_path, database_work: DatabaseMana
115115 index = 0
116116 batchsize = 10000
117117 generator = database_work .execute_select_fetch_multiple (tr_query , show_duration = True , batchsize = batchsize )
118- rows , count = next (generator ,(None , None ))
119- # Insertion petit à petit -> plus performant
120-
121- logger .info ("SQL: Inserting or updating {} values in out db" .format (count ))
122-
123- st_execute = time .time ()
124-
125- while rows :
126- values_str = ""
127- # Tuple des valuers à insérer
128- values_tuple = ()
129- for row in rows :
130- values_str += "(%s, %s, %s),"
131- values_tuple += (index , row ['id_from' ], row ['id_to' ])
132- index += 1
133- values_str = values_str [:- 1 ]
134-
135- set_on_conflict = (
136- "id_from = excluded.id_from,id_to = excluded.id_to"
137- )
118+ try :
119+ rows , count = next (generator ,(None , None ))
120+ # Insertion petit à petit -> plus performant
121+
122+ logger .info ("SQL: Inserting or updating {} values in out db" .format (count ))
123+
124+ st_execute = time .time ()
125+
126+ while rows :
127+ values_str = ""
128+ # Tuple des valuers à insérer
129+ values_tuple = ()
130+ for row in rows :
131+ values_str += "(%s, %s, %s),"
132+ values_tuple += (index , row ['id_from' ], row ['id_to' ])
133+ index += 1
134+ values_str = values_str [:- 1 ]
135+
136+ set_on_conflict = (
137+ "id_from = excluded.id_from,id_to = excluded.id_to"
138+ )
138139
139- sql_insert = """
140- INSERT INTO {}.turn_restrictions (id, id_from, id_to)
141- VALUES {}
142- ON CONFLICT (id) DO UPDATE
143- SET {};
144- """ .format (schema , values_str , set_on_conflict )
145- database_out .execute_update (sql_insert , values_tuple )
140+ sql_insert = """
141+ INSERT INTO {}.turn_restrictions (id, id_from, id_to)
142+ VALUES {}
143+ ON CONFLICT (id) DO UPDATE
144+ SET {};
145+ """ .format (schema , values_str , set_on_conflict )
146+ database_out .execute_update (sql_insert , values_tuple )
146147
147- rows , _ = next (generator ,(None , None ))
148+ rows , _ = next (generator ,(None , None ))
148149
149- et_execute = time .time ()
150+ et_execute = time .time ()
151+ finally :
152+ generator .close () # Ensure the generator is closed to free resources
150153 logger .info ("Writing turn restrinctions Done. Elapsed time : %s seconds." % (et_execute - st_execute ))
151154
152155 # Noeuds ---------------------------------------------------------------------------------------
@@ -171,30 +174,32 @@ def pivot_to_pgr(source, cost_calculation_file_path, database_work: DatabaseMana
171174 index = 0
172175 batchsize = 10000
173176 generator = database_work .execute_select_fetch_multiple (nd_query , show_duration = True , batchsize = batchsize )
174- rows , count = next (generator , (None , None ))
175- while rows :
176- values_str = ""
177- # Tuple des valeurs à insérer
178- values_tuple = ()
179- for row in rows :
180- values_str += "(%s, %s),"
181- values_tuple += (row ['id' ], row ['geom' ])
182- index += 1
183- values_str = values_str [:- 1 ]
184-
185- set_on_conflict = (
186- "the_geom = excluded.the_geom"
187- )
188-
189- sql_insert = """
190- INSERT INTO {}_vertices_pgr (id, the_geom)
191- VALUES {}
192- ON CONFLICT (id) DO UPDATE
193- SET {};
194- """ .format (ways_table_name , values_str , set_on_conflict )
195- database_out .execute_update (sql_insert , values_tuple )
196- rows , _ = next (generator ,(None , None ))
177+ try :
178+ rows , count = next (generator , (None , None ))
179+ while rows :
180+ values_str = ""
181+ # Tuple des valeurs à insérer
182+ values_tuple = ()
183+ for row in rows :
184+ values_str += "(%s, %s),"
185+ values_tuple += (row ['id' ], row ['geom' ])
186+ index += 1
187+ values_str = values_str [:- 1 ]
188+
189+ set_on_conflict = (
190+ "the_geom = excluded.the_geom"
191+ )
197192
193+ sql_insert = """
194+ INSERT INTO {}_vertices_pgr (id, the_geom)
195+ VALUES {}
196+ ON CONFLICT (id) DO UPDATE
197+ SET {};
198+ """ .format (ways_table_name , values_str , set_on_conflict )
199+ database_out .execute_update (sql_insert , values_tuple )
200+ rows , _ = next (generator ,(None , None ))
201+ finally :
202+ generator .close () # Ensure the generator is closed to free resources
198203
199204 et_execute = time .time ()
200205 logger .info ("Writing vertices Done. Elapsed time : %s seconds." % (et_execute - st_execute ))
@@ -266,45 +271,48 @@ def pivot_to_pgr(source, cost_calculation_file_path, database_work: DatabaseMana
266271 # logger.info("SQL: Inserting or updating {} values in out db".format(cursor_in.rowcount))
267272 st_execute = time .time ()
268273 percent = 0
269- rows , count = next (generator , (None , None ))
270- while rows :
271- percent += 1000000 / count
272- # Chaîne permettant l'insertion de valeurs via psycopg
273- values_str = ""
274- # Tuple des valuers à insérer
275- values_tuple = ()
276- for row in rows :
277- values_str += "(" + single_value_str + "),"
278- output_costs = output_costs_from_costs_config (costs , row )
279- values_tuple += tuple (
280- row [ output_columns_name ] for output_columns_name in output_columns_names
281- ) + output_costs
282- values_str = values_str [:- 1 ]
283-
284- output_columns = "("
285- for output_columns_name in output_columns_names :
286- output_columns += output_columns_name + ','
287- output_columns = output_columns [:- 1 ]
288-
289- set_on_conflict = ''
290- for output_columns_name in output_columns_names :
291- set_on_conflict += "{0} = excluded.{0}," .format (output_columns_name )
292- set_on_conflict = set_on_conflict [:- 1 ]
293-
294- for output in costs ["outputs" ]:
295- output_columns += "," + output ["name" ] + ",reverse_" + output ["name" ]
296- set_on_conflict += ",{0} = excluded.{0}" .format (output ["name" ])
297- set_on_conflict += ",{0} = excluded.{0}" .format ("reverse_" + output ["name" ])
298-
299- output_columns += ")"
300- sql_insert = """
301- INSERT INTO {} {}
302- VALUES {}
303- ON CONFLICT (id) DO UPDATE
304- SET {};
305- """ .format (ways_table_name , output_columns , values_str , set_on_conflict )
306- database_out .execute_update (sql_insert , values_tuple )
307- rows , _ = next (generator ,(None , None ))
274+ try :
275+ rows , count = next (generator , (None , None ))
276+ while rows :
277+ percent += 1000000 / count
278+ # Chaîne permettant l'insertion de valeurs via psycopg
279+ values_str = ""
280+ # Tuple des valuers à insérer
281+ values_tuple = ()
282+ for row in rows :
283+ values_str += "(" + single_value_str + "),"
284+ output_costs = output_costs_from_costs_config (costs , row )
285+ values_tuple += tuple (
286+ row [ output_columns_name ] for output_columns_name in output_columns_names
287+ ) + output_costs
288+ values_str = values_str [:- 1 ]
289+
290+ output_columns = "("
291+ for output_columns_name in output_columns_names :
292+ output_columns += output_columns_name + ','
293+ output_columns = output_columns [:- 1 ]
294+
295+ set_on_conflict = ''
296+ for output_columns_name in output_columns_names :
297+ set_on_conflict += "{0} = excluded.{0}," .format (output_columns_name )
298+ set_on_conflict = set_on_conflict [:- 1 ]
299+
300+ for output in costs ["outputs" ]:
301+ output_columns += "," + output ["name" ] + ",reverse_" + output ["name" ]
302+ set_on_conflict += ",{0} = excluded.{0}" .format (output ["name" ])
303+ set_on_conflict += ",{0} = excluded.{0}" .format ("reverse_" + output ["name" ])
304+
305+ output_columns += ")"
306+ sql_insert = """
307+ INSERT INTO {} {}
308+ VALUES {}
309+ ON CONFLICT (id) DO UPDATE
310+ SET {};
311+ """ .format (ways_table_name , output_columns , values_str , set_on_conflict )
312+ database_out .execute_update (sql_insert , values_tuple )
313+ rows , _ = next (generator ,(None , None ))
314+ finally :
315+ generator .close () # Ensure the generator is closed to free resources
308316
309317 et_execute = time .time ()
310318 logger .info ("Writing ways ended. Elapsed time : %s seconds." % (et_execute - st_execute ))
0 commit comments