From ff5dcb2a5c3f645519911c8fd25876b9e32080b7 Mon Sep 17 00:00:00 2001 From: Colas Geier Date: Mon, 26 Feb 2024 15:33:48 +0100 Subject: [PATCH] insertion cadastre bd_cen v3 --- 0_FONCIER/foncier_insert_cadastre_V3.py | 109 ++++++++++++++++++++++-- 1 file changed, 100 insertions(+), 9 deletions(-) diff --git a/0_FONCIER/foncier_insert_cadastre_V3.py b/0_FONCIER/foncier_insert_cadastre_V3.py index d506c1a..fee0463 100644 --- a/0_FONCIER/foncier_insert_cadastre_V3.py +++ b/0_FONCIER/foncier_insert_cadastre_V3.py @@ -219,6 +219,24 @@ def _get_voie2(schema='38_202207',list_parid=None): return _where_parcelle(sql0,schema,list_parid) +def _get_lots_natcult1(schema='38_202207',list_parid=None): + sql0 = ''' + SELECT + ccodep||ccocom||replace(ccopre, ' ', '0')||replace(ccosec, ' ', '0')||dnupla||btrim(dnulot) as lot_id, -- Identifiant du lot (c'est bon, quand il n'y a pas de lot, c'est la même façon de noter l'identifiant) + btrim(dsgrpf) as dsgrpf , -- Sous-groupe de nature de culture + btrim(cnatsp) as cnatsp , -- Code nature de culture spéciale + CASE WHEN trim(dclssf) = '' OR dclssf = '00' + THEN NULL::integer + ELSE ltrim(dclssf,'0')::integer + END AS dclssf, -- Classe dans le groupe et la série tarif + btrim(ccosub) as ccosub , -- Lettres indicatives de la suf + dcntsf -- Contenance de la suf + FROM "{sch}"."{t1}" + '''.format( + sch=schema, + t1='suf') + + return _where_parcelle(sql0,schema,list_parid) def _get_lots_natcult2(schema='38_202207',list_parid=None): sql0 = ''' @@ -242,31 +260,104 @@ def _get_lots_natcult2(schema='38_202207',list_parid=None): return _where_parcelle(sql0,schema,list_parid) +def _get_lots01(schema='38_202207'): + sql0 = ''' + SELECT DISTINCT + ccodep||ccocom||replace(ccopre, ' ', '0')||replace(ccosec, ' ', '0')||dnupla||dnulot as lot_id, -- Identifiant du lot character varying(21) + ccodep||ccocom||replace(ccopre, ' ', '0')||replace(ccosec, ' ', '0')||dnupla as par_id, -- Identifiant de la parcelle + dnulot, -- Numéro du lot + max(dcntlo) as dcntlo -- Contenance cadastrale (m²) on prend contenance maxi quand même lot_id mais dcntlo différentes + FROM "{sch}".{t1} + JOIN "{sch}".geo_parcelle ON lots.parcelle = geo_parcelle.geo_parcelle -- on ne garde que les lots sur des parcelles dont on a la géométrie + WHERE + ccodep||ccocom||replace(ccopre, ' ', '0')||replace(ccosec, ' ', '0')||dnupla||dnulot IN + ( -- on sélectionne uniquement les lots qui supportent une subdivision fiscale pour exclure les lots batis + SELECT ccodep||ccocom||replace(ccopre, ' ', '0')||replace(ccosec, ' ', '0')||dnupla||dnulot FROM "{sch}".suf WHERE btrim(dnulot) <> '') + GROUP BY ccodep, ccocom, ccopre, ccosec, dnupla, dnulot + '''.format( + sch=schema, + t1='lots') + + return _where_parcelle(sql0,schema,None) + +def _get_lots02(schema='38_202207'): + sql0 = ''' + SELECT + ccodep||ccocom||replace(ccopre, ' ', '0')||replace(ccosec, ' ', '0')||dnupla||dnulot as lot_id, -- Identifiant du lot character varying(21) + ccodep||ccocom||replace(ccopre, ' ', '0')||replace(ccosec, ' ', '0')||dnupla as par_id, -- Identifiant de la parcelle + dnulot, -- Numéro du lot + sum(dcntsf) as dcntlo -- Contenance cadastrale (m²) = On additionne les contenances des différentes suf qui composent le lot + FROM "{sch}".suf + JOIN "{sch}".geo_parcelle ON suf.parcelle = geo_parcelle.geo_parcelle -- on ne garde que les sufs sur des parcelles dont on a la géométrie + WHERE + btrim(dnulot) <> '' AND ccodep||ccocom||replace(ccopre, ' ', '0')||replace(ccosec, ' ', '0')||dnupla||dnulot NOT IN ( + SELECT DISTINCT ccodep||ccocom||replace(ccopre, ' ', '0')||replace(ccosec, ' ', '0')||dnupla||dnulot FROM "{sch}".lots WHERE btrim(dnulot)<>'') + GROUP BY ccodep, ccocom, ccopre, ccosec, dnupla, dnulot + '''.format( + sch=schema, + t1='lots') + return _where_parcelle(sql0,schema,None) + + +def _get_lots03(schema='38_202207'): + sql0 = ''' + -- on sélectionne les parcelles qui supportent une subdivision fiscale (unique ou multiple) même si divisées en lots car dans ce cas ce sont surement des copropriétés (lots batis/jardins...) + SELECT DISTINCT + ccodep||ccocom||replace(ccopre, ' ', '0')||replace(ccosec, ' ', '0')||dnupla as lot_id, -- Identifiant du lot fictif(=id parcelle) + ccodep||ccocom||replace(ccopre, ' ', '0')||replace(ccosec, ' ', '0')||dnupla as par_id, -- Identifiant de la parcelle + null as dnulot, -- Numéro du lot + dcntpa as dcntlo-- Contenance cadastrale (m²) + FROM "{sch}".parcelle + JOIN "{sch}".geo_parcelle ON parcelle.parcelle = geo_parcelle.geo_parcelle -- on ne garde que les les parcelles dont on a la géométrie + WHERE + ccodep||ccocom||replace(ccopre, ' ', '0')||replace(ccosec, ' ', '0')||dnupla IN + (SELECT ccodep||ccocom||replace(ccopre, ' ', '0')||replace(ccosec, ' ', '0')||dnupla||trim(dnulot) FROM "{sch}".suf); -- toutes les parcelles dont dnulot est NULL + '''.format( + sch=schema, + t1='lots') + return _where_parcelle(sql0,schema,None) + +def _get_lots1(schema='38_202207'): + l1 = _get_lots01(schema) + l2 = _get_lots02(schema) + l3 = _get_lots03(schema) + return pd.concat([*l1,*l2,*l3]) + def _get_lots2(schema='38_202207',list_parid=None): sql0 = ''' SELECT DISTINCT - CASE WHEN TRIM(t.dnulot) = '' - THEN substring(t.parcelle from 1 for 2)||substring(t.parcelle from 4 for 12)||'0000000' + CASE WHEN TRIM(t.dnulot) = '' OR TRIM(t.dnulot) IS NULL + THEN substring(t.parcelle from 1 for 2)||substring(t.parcelle from 4 for 12)||TRIM(t.ccosub) ELSE substring(t.parcelle from 1 for 2)||substring(t.parcelle from 4 for 12)||TRIM(t.dnulot) END lot_id, t.parcelle, substring(t.parcelle from 1 for 2)||substring(t.parcelle from 4 for 12) par_id, CASE WHEN TRIM(t.dnulot) = '' OR TRIM(t.dnulot) IS NULL - THEN TRIM(l.dnulot) + THEN TRIM(t.ccosub) ELSE TRIM(t.dnulot) END dnulot, - CASE WHEN l.dcntlo IS NULL - THEN 0 - ELSE l.dcntlo + CASE WHEN t.dcntsf IS NULL + THEN l.dcntlo + ELSE t.dcntsf END dcntlo, - geo_sub.geom + geo_sub1.creat_date, + CASE WHEN geo_sub1.geom IS NULL + THEN geo_sub2.geom + ELSE geo_sub1.geom + END geom FROM "{sch}"."{t1}" t JOIN "{sch}".parcelle p USING (parcelle) + LEFT JOIN "{sch}".geo_parcelle geo_p ON geo_p.geo_parcelle = p.parcelle LEFT JOIN "{sch}".lots l USING (parcelle) - LEFT JOIN "{sch}".lotslocaux ll USING (lots) + --LEFT JOIN "{sch}".lotslocaux ll USING (lots) LEFT JOIN "{sch}".geo_subdfisc_parcelle geo_sub_p ON p.parcelle = geo_sub_p.geo_parcelle - LEFT JOIN "{sch}".geo_subdfisc geo_sub USING (geo_subdfisc) + --LEFT JOIN "{sch}".geo_subdfisc geo_sub USING (geo_subdfisc) + LEFT JOIN "{sch}".geo_subdfisc geo_sub1 + ON (geo_sub1.geo_subdfisc = geo_sub_p.geo_subdfisc AND LOWER(geo_sub1.tex) = LOWER(t.ccosub)) + LEFT JOIN "{sch}".geo_subdfisc geo_sub2 + ON (geo_sub2.geo_subdfisc = geo_sub_p.geo_subdfisc AND TRIM(LOWER(geo_sub2.tex)) = '') + WHERE ST_INTERSECTS(geo_sub1.geom,geo_p.geom) OR ST_INTERSECTS(geo_sub2.geom,geo_p.geom) '''.format( sch=schema, t1='suf')