init depot renew
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
CREATE OR REPLACE VIEW gn_exports.v_synthese_sinp_acquisition_flore_with_metadata_for_gn2pg AS
|
||||
WITH af_actors AS
|
||||
(SELECT cafa.*
|
||||
FROM gn_meta.cor_acquisition_framework_actor cafa
|
||||
LEFT JOIN utilisateurs.bib_organismes borg ON cafa.id_organism = borg.id_organisme
|
||||
WHERE borg.uuid_organisme = '5a433bd0-2070-25d9-e053-2614a8c026f8'::uuid
|
||||
--OR borg.uuid_organisme = 'a3c44538-f22a-4134-9e21-28646a73986b'::uuid
|
||||
), act AS
|
||||
(SELECT *
|
||||
FROM crosstab( 'SELECT id_acquisition_framework, id_nomenclature_actor_role, id_organism
|
||||
FROM gn_meta.cor_acquisition_framework_actor
|
||||
WHERE id_acquisition_framework = 1 OR id_acquisition_framework = 6
|
||||
ORDER BY 1,2' , $$VALUES ('359'::text), ('361')$$ ) AS ct ("id_acquisition_framework" int, "Contact principal" int, "Producteur du jeu de données" int))
|
||||
SELECT taf.id_acquisition_framework AS id_ca,
|
||||
taf.unique_acquisition_framework_id AS id_sinp_ca,
|
||||
taf.acquisition_framework_name AS libelle,
|
||||
NULL::text AS cadre_acquisition_parent,
|
||||
taf.acquisition_framework_desc AS description,
|
||||
taf.keywords AS mots_cles,
|
||||
ref1.label_default as niveau_territorial,
|
||||
taf.territory_desc AS description_territoire,
|
||||
ref2.label_default AS objectifs_cadre_acquisition,
|
||||
ref4.label_default AS volet_SINP,
|
||||
ref3.label_default AS type_financement,
|
||||
taf.target_description AS description_cible,
|
||||
to_char(taf.meta_create_date, 'DD-MM-YYYY')::date AS date_debut,
|
||||
NULL::date AS date_fin,
|
||||
ref5.nom_organisme AS acteur1_organisme,
|
||||
NULL::text AS acteur1_personne_groupe,
|
||||
'Contact principal' AS acteur1_type_role,
|
||||
ref6.nom_organisme AS acteur2_organisme,
|
||||
NULL::text AS acteur2_personne_groupe,
|
||||
CASE
|
||||
WHEN ref6.nom_organisme IS NOT NULL THEN 'Producteur du jeu de données'
|
||||
ELSE NULL
|
||||
END AS acteur2_type_role,
|
||||
NULL::text AS acteur3_organisme,
|
||||
NULL::text AS acteur3_personne_groupe,
|
||||
NULL::text AS acteur3_type_role
|
||||
FROM gn_meta.t_acquisition_frameworks taf
|
||||
JOIN af_actors ON af_actors.id_acquisition_framework = taf.id_acquisition_framework
|
||||
JOIN gn_meta.cor_acquisition_framework_objectif obj ON obj.id_acquisition_framework = taf.id_acquisition_framework
|
||||
JOIN ref_nomenclatures.t_nomenclatures ref1 ON ref1.id_nomenclature = taf.id_nomenclature_territorial_level
|
||||
JOIN ref_nomenclatures.t_nomenclatures ref2 ON ref2.id_nomenclature = obj.id_nomenclature_objectif
|
||||
JOIN ref_nomenclatures.t_nomenclatures ref3 ON ref3.id_nomenclature = taf.id_nomenclature_financing_type
|
||||
JOIN gn_meta.cor_acquisition_framework_voletsinp sinp ON sinp.id_acquisition_framework = taf.id_acquisition_framework
|
||||
JOIN ref_nomenclatures.t_nomenclatures ref4 ON ref4.id_nomenclature = sinp.id_nomenclature_voletsinp
|
||||
JOIN act ON act.id_acquisition_framework = taf.id_acquisition_framework
|
||||
LEFT JOIN utilisateurs.bib_organismes ref5 ON ref5.id_organisme = act."Contact principal"
|
||||
LEFT JOIN utilisateurs.bib_organismes ref6 ON ref6.id_organisme = act."Producteur du jeu de données"
|
||||
GROUP BY taf.id_acquisition_framework,
|
||||
taf.unique_acquisition_framework_id,
|
||||
taf.acquisition_framework_name,
|
||||
taf.acquisition_framework_desc,
|
||||
taf.keywords,
|
||||
ref1.label_default,
|
||||
taf.territory_desc,
|
||||
ref2.label_default,
|
||||
ref4.label_default,
|
||||
ref3.label_default,
|
||||
taf.target_description,
|
||||
to_char(taf.meta_create_date, 'DD-MM-YYYY')::date,
|
||||
ref5.nom_organisme,
|
||||
ref6.nom_organisme
|
||||
@@ -0,0 +1,77 @@
|
||||
CREATE OR REPLACE VIEW gn_exports.v_synthese_sinp_jeu_donnees_flore_with_metadata_for_gn2pg AS
|
||||
WITH ds_actors AS (
|
||||
SELECT cda.id_dataset,
|
||||
borg.nom_organisme,
|
||||
cda.id_role,
|
||||
borg.uuid_organisme,
|
||||
tro.nom_role,
|
||||
tro.prenom_role,
|
||||
borg.email_organisme,
|
||||
tro.email
|
||||
FROM gn_meta.cor_dataset_actor cda
|
||||
LEFT JOIN utilisateurs.bib_organismes borg ON cda.id_organism = borg.id_organisme
|
||||
LEFT JOIN utilisateurs.t_roles tro ON cda.id_role = tro.id_role
|
||||
JOIN ref_nomenclatures.t_nomenclatures tn ON cda.id_nomenclature_actor_role = tn.id_nomenclature
|
||||
-- WHERE borg.uuid_organisme = '5a433bd0-2070-25d9-e053-2614a8c026f8'::uuid
|
||||
-- OR borg.uuid_organisme = 'a3c44538-f22a-4134-9e21-28646a73986b'::uuid
|
||||
),
|
||||
actor AS (
|
||||
SELECT *
|
||||
FROM crosstab( 'SELECT id_dataset, id_nomenclature_actor_role, id_organism
|
||||
FROM gn_meta.cor_dataset_actor
|
||||
ORDER BY 1,2' ,
|
||||
$$VALUES ('359'), ('364'), ('365'), ('395')$$ ) AS ct ("id_dataset" int, "Contact principal" int, "Producteur du jeu de données" int, "Point de contact base de données de production" int, "Point de contact pour les métadonnées" int)
|
||||
),
|
||||
ds AS (
|
||||
SELECT tds.id_dataset AS id_jdd,
|
||||
tds.unique_dataset_id AS id_sinp_jdd,
|
||||
acq.acquisition_framework_name AS cadre_acquisiton,
|
||||
tds.dataset_name AS nom_jeu_donnees,
|
||||
tds.dataset_shortname AS nom_court,
|
||||
tds.dataset_desc AS description,
|
||||
ndso.label_default AS objectif,
|
||||
ncm.label_default AS methode_collecte,
|
||||
ndo.label_default AS origine_donnees,
|
||||
nss.label_default AS statut_source,
|
||||
tds.keywords AS mots_cles,
|
||||
act1.nom_organisme AS acteur1_organisme,
|
||||
NULL::text AS acteur1_personne_groupe,
|
||||
'Contact principal' AS acteur1_type_role,
|
||||
act2.nom_organisme AS acteur2_organisme,
|
||||
NULL::text AS acteur2_personne_groupe,
|
||||
CASE
|
||||
WHEN act2.nom_organisme IS NOT NULL THEN 'Producteur du jeu de données'
|
||||
ELSE NULL
|
||||
END AS acteur2_type_role,
|
||||
act3.nom_organisme AS acteur3_organisme,
|
||||
NULL::text AS acteur3_personne_groupe,
|
||||
CASE
|
||||
WHEN act3.nom_organisme IS NOT NULL THEN 'Point de contact base de données de production'
|
||||
ELSE NULL
|
||||
END AS acteur3_type_role,
|
||||
act4.nom_organisme AS acteur4_organisme,
|
||||
NULL::text AS acteur4_personne_groupe,
|
||||
CASE
|
||||
WHEN act4.nom_organisme IS NOT NULL THEN 'Point de contact pour les métadonnées'
|
||||
ELSE NULL
|
||||
END AS acteur4_type_role
|
||||
FROM gn_meta.t_datasets tds
|
||||
JOIN ds_actors ON ds_actors.id_dataset = tds.id_dataset
|
||||
JOIN actor ON actor.id_dataset = tds.id_dataset
|
||||
JOIN gn_meta.t_acquisition_frameworks acq ON tds.id_acquisition_framework = acq.id_acquisition_framework
|
||||
LEFT JOIN gn_meta.cor_dataset_territory cdt ON cdt.id_dataset = tds.id_dataset
|
||||
LEFT JOIN ref_nomenclatures.t_nomenclatures ndt ON tds.id_nomenclature_data_type = ndt.id_nomenclature
|
||||
LEFT JOIN ref_nomenclatures.t_nomenclatures ncm ON tds.id_nomenclature_collecting_method = ncm.id_nomenclature
|
||||
LEFT JOIN ref_nomenclatures.t_nomenclatures ndo ON tds.id_nomenclature_data_origin = ndo.id_nomenclature
|
||||
LEFT JOIN ref_nomenclatures.t_nomenclatures ndso ON tds.id_nomenclature_dataset_objectif = ndso.id_nomenclature
|
||||
LEFT JOIN ref_nomenclatures.t_nomenclatures nrt ON tds.id_nomenclature_resource_type = nrt.id_nomenclature
|
||||
LEFT JOIN ref_nomenclatures.t_nomenclatures nss ON tds.id_nomenclature_source_status = nss.id_nomenclature
|
||||
LEFT JOIN utilisateurs.bib_organismes act1 ON actor."Contact principal" = act1.id_organisme
|
||||
LEFT JOIN utilisateurs.bib_organismes act2 ON actor."Producteur du jeu de données" = act2.id_organisme
|
||||
LEFT JOIN utilisateurs.bib_organismes act3 ON actor."Point de contact base de données de production" = act3.id_organisme
|
||||
LEFT JOIN utilisateurs.bib_organismes act4 ON actor."Point de contact pour les métadonnées" = act4.id_organisme
|
||||
GROUP BY tds.id_dataset, acq.acquisition_framework_name, tds.unique_dataset_id, tds.dataset_name, tds.dataset_shortname, tds.dataset_desc, ndso.label_default, ncm.label_default, ndo.label_default, nss.label_default, ds_actors.nom_organisme, act1.nom_organisme, act2.nom_organisme, act3.nom_organisme, act4.nom_organisme
|
||||
)
|
||||
SELECT * FROM ds
|
||||
--WHERE acteur1_organisme IN ('Conservatoire d’espaces naturels Isère')
|
||||
;
|
||||
@@ -0,0 +1,107 @@
|
||||
from pycen import con
|
||||
|
||||
|
||||
url = 'https://wxs.ign.fr/agriculture/geoportail/wfs?SERVICE=WFS&VERSION=2.0.0&REQUEST=GetCapabilities'
|
||||
layer = 'RPG.2020:parcelles_graphiques.title'
|
||||
wpr_name = 'rpgwfs'
|
||||
tab_name = 'rpg2020_parcellaire_graphique'
|
||||
|
||||
drop_fgn = 'DROP SERVER IF EXISTS %s CASCADE;'%wpr_name
|
||||
fgn_data_wrapper = '''
|
||||
CREATE SERVER IF NOT EXISTS %s
|
||||
FOREIGN DATA WRAPPER ogr_fdw
|
||||
OPTIONS (datasource 'WFS:%s', format 'WFS')
|
||||
'''%(wpr_name,url)
|
||||
|
||||
table = '''
|
||||
DROP FOREIGN TABLE IF EXISTS {table};
|
||||
CREATE FOREIGN TABLE IF NOT EXISTS flux_geo.{table} (
|
||||
id_parcel varchar,
|
||||
surf_parcel float,
|
||||
code_cultu varchar,
|
||||
code_group varchar,
|
||||
culture_d1 varchar,
|
||||
culture_d2 varchar,
|
||||
geom public.geometry(multipolygon, 4326)
|
||||
)
|
||||
SERVER {srv}
|
||||
OPTIONS (layer '{layer}');
|
||||
'''.format(table=tab_name,srv=wpr_name,layer=layer)
|
||||
|
||||
with con.begin() as cnx:
|
||||
cnx.execute(drop_fgn)
|
||||
with con.begin() as cnx:
|
||||
cnx.execute(fgn_data_wrapper)
|
||||
|
||||
with con.begin() as cnx:
|
||||
cnx.execute(table)
|
||||
|
||||
#######
|
||||
#######
|
||||
from pycen import con_anm as con
|
||||
from sqlalchemy import text
|
||||
db = 'bd_cen'
|
||||
host = '91.134.194.221'
|
||||
port = '5432'
|
||||
srv_name = 'fdw_bd_cen'
|
||||
tab_name = 'communes'
|
||||
sch_name = 'territoire'
|
||||
view_name = 'v_'+tab_name
|
||||
mview_name = 'vm_'+tab_name
|
||||
user = 'cen_admin'
|
||||
pwd = '#CEN38@venir'
|
||||
|
||||
|
||||
create_ext = 'CREATE EXTENSION IF NOT EXISTS postgres_fdw;'
|
||||
drop_fgn = 'DROP SERVER IF EXISTS %s CASCADE;'%srv_name
|
||||
fgn_data_wrapper = '''
|
||||
CREATE SERVER IF NOT EXISTS %s
|
||||
FOREIGN DATA WRAPPER postgres_fdw
|
||||
OPTIONS (dbname '%s', host '%s', port '%s')
|
||||
'''%(srv_name,db,host,port)
|
||||
|
||||
fgn_user_wrapper = '''
|
||||
CREATE USER MAPPING IF NOT EXISTS FOR %s
|
||||
SERVER %s
|
||||
OPTIONS (user '%s', password '%s');
|
||||
'''%(user,srv_name,user,pwd)
|
||||
|
||||
table = '''
|
||||
CREATE FOREIGN TABLE IF NOT EXISTS {sch_name}.{tab_name} (
|
||||
code_insee varchar(5),
|
||||
id varchar(24),
|
||||
prec_plani float8,
|
||||
nom varchar(45),
|
||||
statut varchar(20),
|
||||
canton varchar(45),
|
||||
arrondisst varchar(45),
|
||||
depart varchar(30),
|
||||
region varchar(30),
|
||||
popul int4,
|
||||
multican varchar(3),
|
||||
actif bool,
|
||||
epfl varchar(10),
|
||||
geom geometry(geometry,2154)
|
||||
)
|
||||
SERVER {fgn_server}
|
||||
OPTIONS (schema_name 'administratif', table_name 'communes');
|
||||
'''.format(
|
||||
sch_name=sch_name, tab_name=tab_name, fgn_server=srv_name
|
||||
)
|
||||
|
||||
vm = '''
|
||||
CREATE MATERIALIZED VIEW {sch_name}.{mview_name} AS
|
||||
SELECT * FROM {sch_name}.{tab_name};
|
||||
|
||||
GRANT SELECT ON {sch_name}.{mview_name} TO grp_consult;
|
||||
GRANT SELECT ON {sch_name}.{mview_name} TO cen_user;
|
||||
'''.format(sch_name=sch_name, mview_name=mview_name, tab_name=tab_name)
|
||||
with con.begin() as cnx:
|
||||
# cnx.execute(create_ext)
|
||||
# cnx.execute(drop_fgn)
|
||||
# cnx.execute(fgn_data_wrapper)
|
||||
# cnx.execute(fgn_user_wrapper)
|
||||
cnx.execute(text(table))
|
||||
|
||||
with con.begin() as cnx:
|
||||
cnx.execute(text(vm))
|
||||
@@ -0,0 +1,107 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: UTF-8 -*-
|
||||
|
||||
from sqlalchemy.engine import URL
|
||||
from sqlalchemy import create_engine,text
|
||||
import pandas as pd
|
||||
|
||||
# Parametres bdd
|
||||
|
||||
|
||||
usr = {
|
||||
'usr_siege': ['abavarot','aguedou','apagano','cgeier','dlopez-pinot','dmichallet','jlgrossi','mjuton','msimean','nbiron'],
|
||||
'usr_glps': ['lquay','gmaillet','jlucas'],
|
||||
'usr_plt' : ['bpont','yprat-mairet','clebreton','cfregat','mbounous']
|
||||
}
|
||||
|
||||
|
||||
def revoke_group(con,user,group):
|
||||
alter = 'ALTER GROUP "{grp}" DROP USER "{usr}";'
|
||||
with con.begin() as cnx:
|
||||
cnx.execute(alter.format(usr=user,grp=group))
|
||||
|
||||
|
||||
def add_group(con,user,group):
|
||||
alter = 'ALTER GROUP "{grp}" ADD USER "{usr}";'
|
||||
with con.begin() as cnx:
|
||||
cnx.execute(alter.format(usr=user,grp=group))
|
||||
|
||||
|
||||
def deactivate_user(con,user):
|
||||
alter = 'ALTER USER "{usr}" WITH NOLOGIN;'
|
||||
with con.begin() as cnx:
|
||||
cnx.execute(alter.format(usr=user))
|
||||
|
||||
|
||||
def add_limit_user(con,user,date_limit):
|
||||
alter = """ALTER USER "{usr}" VALID UNTIL '{date} 00:00:00';"""
|
||||
with con.begin() as cnx:
|
||||
cnx.execute(alter.format(usr=user,date=date_limit))
|
||||
|
||||
|
||||
def change_password(con,user,pwd):
|
||||
alter = """ALTER USER "{usr}" WITH PASSWORD '{password}';"""
|
||||
with con.begin() as cnx:
|
||||
cnx.execute(alter.format(usr=user,password=pwd))
|
||||
|
||||
|
||||
def create_grp(con,grp):
|
||||
sql = """CREATE ROLE {grp} WITH
|
||||
NOSUPERUSER
|
||||
NOCREATEDB
|
||||
NOCREATEROLE
|
||||
INHERIT
|
||||
NOLOGIN
|
||||
NOREPLICATION
|
||||
NOBYPASSRLS
|
||||
CONNECTION LIMIT -1;""".format(grp=grp)
|
||||
with con.begin() as cnx:
|
||||
cnx.execute(sql)
|
||||
|
||||
def create_usr(con,usr,pwd):
|
||||
sql = """CREATE USER "{usr}" WITH
|
||||
NOSUPERUSER
|
||||
NOCREATEDB
|
||||
NOCREATEROLE
|
||||
INHERIT
|
||||
LOGIN
|
||||
NOREPLICATION
|
||||
NOBYPASSRLS
|
||||
PASSWORD '{pwd}'
|
||||
CONNECTION LIMIT -1;""".format(usr=usr,pwd=pwd)
|
||||
with con.begin() as cnx:
|
||||
cnx.execute(sql)
|
||||
# sql = "select * from pg_catalog.pg_user" # where tableowner = 'gpasquier'"
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from pycen import con_bdcen,con_fon,con
|
||||
|
||||
for grp in usr.keys():
|
||||
# create grp_role
|
||||
create_grp(con_fon,grp)
|
||||
|
||||
# create usr
|
||||
for user in usr[grp]:
|
||||
sql = """
|
||||
DO
|
||||
$do$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT FROM pg_catalog.pg_roles
|
||||
WHERE rolname = '{usr}') THEN
|
||||
|
||||
CREATE ROLE "{usr}" LOGIN PASSWORD '{usr}';
|
||||
END IF;
|
||||
GRANT {grp} TO "{usr}" ;
|
||||
END $do$""".format(usr=user,grp=grp)
|
||||
with con.begin() as cnx:
|
||||
cnx.execute(text(sql))
|
||||
|
||||
|
||||
# grant grp_role
|
||||
for grp in usr.keys():
|
||||
sql = """GRANT grp_consult TO {usr} ;""".format(usr=grp)
|
||||
with con.begin() as cnx:
|
||||
cnx.execute(sql)
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
|
||||
sql = '''
|
||||
CREATE TABLE IF NOT EXISTS sites.hist_id_site (
|
||||
id serial4 NOT NULL,
|
||||
id_site_old varchar(10) NOT NULL,
|
||||
id_site_new varchar(10) NOT NULL,
|
||||
"date" timestamp NOT NULL DEFAULT NOW(),
|
||||
CONSTRAINT hist_id_site_pkey PRIMARY KEY (id),
|
||||
CONSTRAINT hist_id_site_un UNIQUE (id_site_old, id_site_new)
|
||||
);'''
|
||||
|
||||
with con.begin() as cnx:
|
||||
cnx.execute(sql)
|
||||
@@ -0,0 +1,50 @@
|
||||
|
||||
from pycen import con
|
||||
# import pycen
|
||||
|
||||
s1 = '''create trigger get_communnes_intersects after
|
||||
insert
|
||||
or
|
||||
delete
|
||||
or
|
||||
update
|
||||
on
|
||||
sites.r_sites_geom for each row execute function sites.refresh_site_comm()'''
|
||||
|
||||
s2 = '''CREATE OR REPLACE FUNCTION sites.refresh_site_comm()
|
||||
RETURNS trigger
|
||||
LANGUAGE plpgsql
|
||||
AS $function$
|
||||
BEGIN
|
||||
with t1 as (
|
||||
SELECT
|
||||
id,
|
||||
nom_com,
|
||||
insee_com,
|
||||
geom
|
||||
FROM flux_geo.vm_communes_isere
|
||||
),
|
||||
t2 as (SELECT
|
||||
s.id_site,
|
||||
json_object_agg(t1.insee_com,t1.nom_com) all_comm,
|
||||
max(s.date) date,
|
||||
s.geom
|
||||
-- FROM s1 s, t1
|
||||
FROM sites.r_sites_geom s, t1
|
||||
WHERE st_intersects(s.geom,t1.geom)
|
||||
and s.id_site = NEW.id_site
|
||||
GROUP BY s.id_site,s.geom,s.date
|
||||
ORDER BY s.id_site, s.date DESC)
|
||||
UPDATE sites.sites SET nom_com_json = t2.all_comm
|
||||
FROM t2
|
||||
WHERE id = t2.id_site;
|
||||
IF NOT FOUND THEN
|
||||
UPDATE sites.sites SET nom_com = null WHERE id = t2.id_site;
|
||||
END IF;
|
||||
RETURN NEW;
|
||||
END;
|
||||
$function$
|
||||
;'''
|
||||
with con.begin() as cnx:
|
||||
cnx.execute(s1)
|
||||
cnx.execute(s2)
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
from pycen import con
|
||||
|
||||
tab = 'rpg2020_ilots_anonymes_reg'
|
||||
vm_tab = 'vm_'+tab[:-3]+'isere'
|
||||
|
||||
|
||||
vm = '''
|
||||
CREATE MATERIALIZED VIEW ref_territoire.{vm_name}
|
||||
TABLESPACE pg_default
|
||||
AS SELECT s.*
|
||||
FROM ref_territoire.{table} s, ref_territoire.dept_isere reg
|
||||
WHERE ST_INTERSECTS(s.geom,reg.geom)
|
||||
WITH DATA;
|
||||
|
||||
-- View indexes:
|
||||
CREATE UNIQUE INDEX {vm_name}_idx ON ref_territoire.{vm_name} USING btree (id_ilot);
|
||||
CREATE INDEX {vm_name}_geom ON ref_territoire.{vm_name} USING gist (geom);
|
||||
'''.format(vm_name=vm_tab,table=tab)
|
||||
|
||||
with con.begin() as cnx:
|
||||
cnx.execute(vm)
|
||||
|
||||
Reference in New Issue
Block a user