Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
BMG PUBLIC RESOURCES
SIG-TOPO-BMG
Commits
5a22538e
Commit
5a22538e
authored
Jan 31, 2021
by
Alice Salsé
Browse files
❇
function export troncon CSV
parent
4a7758d7
Changes
2
Hide whitespace changes
Inline
Side-by-side
python/qgs_plugins/NetworkPlugin/mainPlugin.py
View file @
5a22538e
...
...
@@ -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
sql/network/function_order_pipe.sql
0 → 100644
View file @
5a22538e
\
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
;
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment