Commit 5a22538e authored by Alice Salsé's avatar Alice Salsé
Browse files

function export troncon CSV

parent 4a7758d7
......@@ -115,6 +115,14 @@ class NetworkTools:
self.moveselected.triggered.connect(self.moveSelected)
self.toolbar.addAction(self.moveselected)
self.orderedexport = QAction(QIcon(":/qgs_plugins/NetworkPlugin/icons/orderedexport.png"),
"Exporter les objets sélectionnés",
self.iface.mainWindow())
self.orderedexport.setObjectName("OrderedExport")
self.orderedexport.setWhatsThis("Exporter les objets sélectionnés")
self.orderedexport.setStatusTip("This is status tip")
self.orderedexport.triggered.connect(self.orderedExport)
self.toolbar.addAction(self.orderedexport)
def unload(self):
del self.toolbar
......@@ -392,3 +400,32 @@ class NetworkTools:
else:
self.iface.messageBar().pushMessage("Sélectionner au moins 1 objet", Qgis.Warning)
return
def orderedExport(self):
print("orderedExport: run called!")
selection = self.activeLayerSelection()
if selection :
lyr_id, l_ids = selection
if len(l_ids) > 0:
layer = self.root.findLayer(lyr_id).layer()
if layer.geometryType() == QgsWkbTypes.LineGeometry \
or "start_node" not in layer.fields().names() :
dir = QFileDialog.getSaveFileName(QFileDialog(), "Définir le fichier de destination", filter="CSV (*.csv)")
if dir :
l_qtablename=layer.dataProvider().uri().quotedTablename ()
column_names, records = executeQuery(layer.dataProvider()
, "select p.id, p.start_node, p.end_node \
,code_insee, diametre, length \
from utils.fnc_order_pipe('"+l_qtablename+"'::regclass, ARRAY["+','.join([str(i) for i in l_ids])+"]) p \
left outer join "+l_qtablename+" x on p.id = x.id;")
with open(dir[0], 'w', newline='') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL)
csvwriter.writerow(column_names)
for r in records :
csvwriter.writerow(r)
else :
self.iface.messageBar().pushMessage("La couche sélectionnée n'est pas une conduite", Qgis.Warning)
return
else:
self.iface.messageBar().pushMessage("Sélectionner au moins 1 conduite", Qgis.Warning)
return
\encoding UTF8
set search_path to utils, public;
CREATE OR REPLACE FUNCTION fnc_order_pipe(
IN i_pipe_regclass regclass,
IN i_ids int[]
)
RETURNS TABLE(id int, start_node int, end_node int, depth int) AS $BODY$
BEGIN
--==== Order selected pipes ====
EXECUTE 'create temp table selection as (
select id, start_node, end_node
from '|| i_pipe_regclass ||'
where id = ANY(ARRAY['|| array_to_string(i_ids, ',') ||'])
);';
RETURN QUERY
EXECUTE '
with recursive toaval(id, start_node, end_node, tids, depth) as (
-- partie non recursive
select t.id, t.start_node, t.end_node, ARRAY[t.id], 1 as a
from selection t
where not exists (select from selection s where s.end_node = t.start_node)
UNION ALL
-- partie recursive
select t.id, t.start_node, t.end_node, o.tids || t.id, o.depth + 1
from selection t, toaval o
where
-- connection entre troncons
o.end_node=t.start_node
-- troncon pas parcouru
and not (t.id = any(tids))
)
, distinctt as (
select distinct on (id) id, start_node, end_node, depth
from toaval
order by id, depth
)
select * from distinctt order by depth, id;';
END;
$BODY$
LANGUAGE plpgsql VOLATILE SECURITY DEFINER
COST 100;
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment