From c1cba8f300b83b712a39d6fd3cc790dfa7562dec Mon Sep 17 00:00:00 2001 From: Colas Geier Date: Mon, 6 Oct 2025 15:27:26 +0200 Subject: [PATCH] outils d'aide au calculs de l'indicateur PAQ --- 10_PAQ/1C-2-1.py | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 10_PAQ/1C-2-1.py diff --git a/10_PAQ/1C-2-1.py b/10_PAQ/1C-2-1.py new file mode 100644 index 0000000..23f3ddb --- /dev/null +++ b/10_PAQ/1C-2-1.py @@ -0,0 +1,74 @@ +#!/usr/bin/env python3 +# -*- coding: UTF-8 -*- + +import geopandas as gpd + +def get_sites_cen(con_foncier, annee): + sql_f = ''' + SELECT vscs.id_site_cen FROM _tdbfcen.vm_sites_cen_{}_shp AS vscs + '''.format(annee) + f = gpd.pd.read_sql(sql_f,con_foncier) + + sql_s = ''' + SELECT * FROM sites.sites WHERE site_id ilike any(array['{}']) + '''.format("%%','".join(f.id_site_cen.tolist())+'%%') + s = gpd.read_postgis(sql_s,con_foncier) + return s + +def get_esp_lr(sites:gpd.GeoDataFrame,con_espece): + sql = """ + WITH t_date AS ( + --SELECT ((date_part('year'::text, CURRENT_DATE) - 1::double precision) || '-12-31'::text)::date AS old_date --état 2024 + SELECT '2023-12-31'::date AS old_date --état 0 + ) + SELECT + '1C-2-1' AS indice, + 'Dpt38'::text AS echelle, + array_agg(distinct synthese.cd_nom) AS val + FROM t_date, + gn_synthese.synthese + --JOIN _cen.sites_cen_savoie_2025 tsite ON (st_intersects(tsite.geom, synthese.the_geom_local)) --état 2024 + --JOIN _cen.sites_cen_savoie_2024 tsite ON (st_intersects('SRID=2154;{geom}', synthese.the_geom_local)) --état 0 + JOIN utilisateurs.t_roles ON t_roles.id_role = synthese.id_digitiser + JOIN utilisateurs.bib_organismes USING (id_organisme) + WHERE + st_intersects('SRID=2154;{geom}', synthese.the_geom_local) AND + synthese.date_min <= t_date.old_date + AND synthese.id_nomenclature_observation_status <> 83 + AND bib_organismes.nom_organisme::text = 'Conservatoire d’espaces naturels Isère'::text + AND synthese.cd_nom::integer IN (100024, 137826, 133886, 100114, 137821, 100273, 100275, 100278, 100288, 100338, 100356, 100576, 100584, 128330, 105002, 100710, 612593, 100825, 100827, 100983, 101101, 89852, 108739, 101220, 101223, 101315, 138817, 143985, 93456, 101777, 4765, 4767, 434118, 434543, 96163, 107802, 112319, 102013, 102674, 4247, 66145, 102794, 102845, 102870, 102990, 103012, 103019, 103170, 103027, 103056, 103120, 103142, 103260, 103272, 103324, 103383, 136794, 103415, 103433, 103469, 103501, 103536, 103596, 103598, 103631, 103648, 103651, 4724, 4725, 4726, 610886, 103777, 103841, 103843, 103862, 103987, 103995, 104014, 104115, 104123, 104148, 104196, 104302, 104329, 104340, 104349, 137017, 104770, 104877, 10499, 105010, 105086, 105159, 105177, 105204, 105239, 105241, 105280, 105409, 105407, 105441, 105490, 105548, 105631, 105641, 105687, 105827, 105841, 105908, 106026, 106128, 106148, 106150, 106201, 106226, 106257, 106260, 106280, 106306, 106313, 116870, 106346, 106347, 106353, 106419, 106435, 106446, 106517, 106545, 106550, 95269, 125940, 95281, 106766, 106807, 106825, 106993, 107085, 107086, 107106, 107123, 107347, 107407, 107509, 107967, 121074, 108612, 108613, 108686, 108755, 108869, 108948, 109095, 109126, 109135, 109139, 109151, 109215, 109252, 109499, 109534, 109594, 109735, 109769, 10979, 10983, 109838, 109869, 109893, 109898, 10999, 100487, 100515, 110108, 110110, 110211, 110221, 110226, 110290, 110296, 110306, 138278, 110480, 110425, 110445, 110473, 110735, 11088, 11090, 110920, 110945, 110961, 110987, 111297, 111452, 111464, 111474, 111494, 111539, 111556, 111560, 111561, 111605, 111649, 111686, 111815, 111976, 111996, 107519, 112391, 112413, 112447, 112453, 112590, 112592, 112604, 122112, 11285, 112868, 11288, 11301, 113090, 610875, 113098, 113099, 113100, 11311, 113178, 113213, 11330, 113485, 101667, 101885, 113547, 11356, 113620, 138815, 113626, 113683, 11370, 113778, 113804, 113905, 11395, 113957, 114035, 114080, 11411, 138955, 114209, 114226, 114247, 11435, 114480, 114519, 114520, 114539, 114545, 114554, 114569, 114579, 114660, 114942, 115061, 115226, 115228, 115233, 115256, 115258, 115270, 115282, 115286, 115296, 115302, 115305, 115326, 115395, 115449, 115458, 115563, 115669, 115883, 92573, 11616, 116225, 116405, 116460, 116531, 116547, 117090, 117096, 117123, 117139, 117146, 117151, 117155, 117156, 117521, 117528, 117681, 117712, 117731, 117732, 11775, 11777, 117820, 118192, 119509, 119533, 119556, 119587, 108623, 119812, 119827, 119860, 119959, 120029, 120052, 120057, 12014, 120594, 120619, 12062, 120732, 120819, 120951, 120962, 120973, 121047, 121076, 121124, 121132, 121144, 140682, 140683, 121184, 121205, 121329, 86189, 121500, 121555, 121556, 121570, 121581, 12159, 121860, 121995, 122070, 122136, 12218, 12220, 122281, 122305, 122810, 122837, 122988, 122998, 12300, 123082, 123293, 12336, 12337, 123438, 12348, 123485, 123507, 123562, 123597, 123679, 123785, 117935, 123841, 123864, 123872, 123899, 123960, 124139, 124410, 124572, 124583, 12465, 124699, 124775, 124842, 125025, 125142, 125173, 125319, 125519, 125842, 125894, 125895, 126008, 126027, 126034, 126070, 126124, 126213, 126248, 126276, 126474, 126478, 126615, 126806, 126812, 126861, 126925, 127002, 127081, 127117, 127191, 127195, 127237, 127269, 127387, 127416, 127419, 127457, 127458, 127469, 127496, 127546, 127547, 127865, 127915, 127921, 127928, 127943, 127945, 127950, 127956, 127988, 128084, 128291, 128311, 128315, 128343, 128347, 128391, 128469, 128546, 128782, 128789, 128871, 128970, 129007, 129033, 129127, 129137, 129157, 129195, 129233, 129260, 129275, 129481, 129520, 129534, 129557, 129596, 129602, 129646, 129660, 129643, 129910, 129999, 130022, 130046, 130124, 130128, 130492, 130520, 130599, 130728, 82879, 131848, 131849, 131858, 131929, 131930, 132135, 132357, 132840, 94063, 134095, 134096, 159904, 109589, 138126, 110382, 138536, 138844, 138865, 138907, 138908, 139, 139818, 140615, 140750, 140804, 141033, 141543, 141545, 141546, 141689, 142006, 143482, 117203, 155051, 159441, 159445, 87290, 159853, 127379, 161784, 162132, 1958, 1970, 1984, 1991, 199294, 199374, 1995, 199685, 1998, 199909, 199958, 200336, 200406, 212, 219757, 219766, 219771, 219787, 53524, 219808, 221939, 222022, 222046, 222053, 222065, 222075, 222085, 222088, 222110, 222111, 222113, 222116, 222120, 222128, 222146, 222149, 222186, 222951, 223065, 223067, 223085, 223109, 223110, 223112, 223118, 223119, 223120, 223129, 223134, 223143, 223149, 223158, 223167, 223170, 223172, 223177, 223178, 223183, 223184, 223185, 223186, 223191, 223192, 223197, 223239, 223242, 223569, 223573, 223576, 223584, 223594, 223964, 223972, 223974, 223975, 223980, 223981, 223985, 223986, 223989, 223991, 223992, 224032, 224033, 224040, 224047, 224064, 224065, 224071, 224076, 224099, 224101, 224105, 224150, 224162, 224165, 224168, 224170, 224191, 224192, 234608, 234673, 234817, 234837, 234838, 234840, 234845, 234850, 234853, 234857, 234858, 234859, 234863, 235, 235005, 235006, 235103, 235190, 235193, 235406, 235408, 235422, 235425, 240285, 535850, 240345, 240347, 240349, 240356, 240362, 240410, 240413, 240426, 240431, 240432, 240439, 240451, 240452, 240453, 240456, 240482, 240484, 240500, 244583, 244611, 244636, 244663, 244664, 244665, 244668, 244676, 244682, 244684, 244685, 244686, 247038, 247040, 247041, 247043, 247049, 247053, 247054, 247068, 247071, 247073, 2477, 2481, 2489, 2497, 2508, 2514, 2517, 252, 2543, 2559, 2576, 2616, 2654, 2676, 2679, 272854, 273019, 273399, 2734, 2741, 279135, 281, 2844, 2873, 2881, 2887, 2891, 2938, 3036, 3039, 305984, 306611, 3120, 3136, 3187, 333015, 3343, 3350, 339978, 340019, 340040, 340110, 3439, 3482, 3489, 3493, 3507, 3511, 3525, 3533, 3561, 3571, 3582, 3590, 3595, 3601, 3780, 3814, 3844, 3845, 3851, 3856, 3859, 433977, 3884, 3984, 4049, 4084, 4112, 4127, 4167, 416813, 416833, 416834, 416838, 416839, 416843, 4198, 4254, 4272, 4289, 4319, 432556, 4330, 433966, 433975, 433999, 434000, 434011, 434017, 434021, 434055, 434090, 434128, 434157, 434178, 434191, 434198, 434203, 434208, 434209, 434213, 434214, 434245, 434249, 434251, 434328, 434342, 434343, 434377, 434378, 434384, 434405, 434406, 434441, 434445, 434453, 434458, 434468, 434476, 434521, 434530, 434531, 434540, 434558, 434571, 434587, 434600, 434666, 5637, 434207, 434217, 5065, 5754, 436093, 5795, 5158, 5159, 434418, 5170, 6029, 6054, 436534, 436599, 436631, 436682, 4375, 4376, 4388, 4393, 4411, 4413, 4414, 4421, 4423, 4429, 4434, 4441, 444431, 445267, 4460, 447951, 4480, 4510, 4532, 4540, 4571, 4574, 458701, 4595, 4657, 4663, 4665, 4669, 4696, 4700, 4746, 4747, 4750, 4761, 4775, 4779, 4786, 4788, 4797, 4819, 4820, 4826, 4830, 4835, 4837, 4848, 4852, 4859, 4862, 4868, 4879, 4881, 4882, 4889, 4893, 4898, 4900, 4903, 4909, 4920, 4924, 4927, 4936, 4941, 4944, 4950, 4951, 4962, 4966, 4979, 4981, 4988, 4990, 4994, 5000, 5008, 5016, 5019, 5029, 5031, 5034, 5035, 5036, 5038, 5046, 5047, 5050, 5056, 5063, 5075, 5120, 5136, 5149, 5151, 5153, 5180, 5182, 5205, 5210, 5214, 5216, 521628, 521629, 5219, 5223, 526279, 5273, 5278, 5281, 5283, 5289, 5298, 5300, 530157, 5311, 5314, 5322, 53251, 5328, 53356, 53358, 53370, 53378, 53425, 5343, 5347, 534752, 53520, 5353, 53542, 5356, 5362, 53621, 53631, 5364, 53640, 53651, 53676, 53707, 53733, 53765, 5379, 5385, 5390, 53911, 53926, 5395, 53976, 54031, 5407, 5415, 5425, 54309, 5431, 54362, 54433, 54472, 54496, 5455, 5491, 5516, 5520, 5525, 5526, 5529, 5530, 5533, 5534, 5537, 5540, 5546, 5547, 5552, 5557, 5577, 5583, 5586, 5590, 5613, 5620, 5621, 5639, 5642, 5643, 5660, 5665, 5670, 5680, 5689, 5697, 5744, 5745, 5749, 5766, 5782, 5796, 5802, 5816, 5818, 5870, 5872, 5881, 5898, 5910, 5916, 5919, 5924, 5926, 593262, 5933, 5951, 5965, 5971, 5973, 5986, 5990, 6011, 6018, 6020, 60295, 60330, 60345, 60400, 60418, 60427, 60439, 60490, 60537, 60557, 608263, 608264, 608286, 6091, 609224, 6096, 6097, 609941, 6105, 610643, 610645, 610647, 610681, 610859, 6109, 610907, 610910, 610911, 611038, 611300, 611455, 611481, 6118, 82846, 612499, 612597, 613479, 6141, 6155, 6157, 80009, 618717, 82380, 94271, 97700, 6204, 124739, 6210, 6211, 154932, 160135, 6212, 6213, 6216, 6218, 6219, 6220, 6226, 6227, 6232, 6235, 6237, 6254, 6257, 6258, 626169, 6282, 6285, 6286, 6289, 6294, 6296, 6305, 54083, 54085, 631135, 6319, 6365, 6392, 6405, 641669, 6417, 6418, 6421, 6422, 6423, 6429, 6434, 6441, 6453, 6454, 6455, 6460, 6463, 6466, 6471, 6488, 6489, 6494, 6502, 6508, 6510, 6513, 65136, 65139, 651399, 65145, 651457, 65147, 65151, 65161, 6517, 6518, 65182, 6519, 6520, 6523, 65231, 65236, 6528, 65308, 65318, 6532, 6533, 65352, 65356, 6536, 6537, 65387, 6539, 65395, 65435, 65496, 6550, 6557, 6558, 6560, 6563, 65635, 65641, 6568, 6572, 65771, 65878, 6598, 6600, 66029, 66035, 6605, 66057, 66067, 66068, 66075, 66080, 66086, 66087, 6609, 6610, 66101, 6613, 66178, 66186, 66217, 6628, 66315, 6635, 6654, 6658, 6667, 6668, 6679, 66832, 6691, 66964, 66967, 670711, 6711, 6712, 67179, 67606, 6765, 6768, 67759, 6776, 67765, 67772, 67837, 6809, 714417, 786517, 6830, 6831, 68336, 69016, 69378, 699479, 699690, 701819, 131319, 92639, 123905, 121011, 115252, 133579, 133017, 136656, 6694, 134602, 136092, 718798, 718835, 718836, 718838, 718837, 107000, 6279, 721499, 6377, 6578, 6587, 770790, 5721, 771005, 771006, 77381, 779733, 78141, 781708, 6698, 784260, 6588, 6313, 786414, 6442, 6438, 5753, 6585, 6378, 6300, 786441, 6331, 6359, 6346, 6354, 786459, 4877, 5772, 5734, 5724, 5713, 5254, 786481, 788216, 6318, 6316, 6308, 786509, 786516, 434616, 786540, 788433, 786514, 6187, 788390, 102715, 79273, 79301, 79302, 79305, 794360, 794577, 794580, 794625, 795235, 795236, 79865, 79887, 7990, 79914, 79939, 80211, 80212, 80224, 80302, 86894, 80864, 80988, 81035, 81142, 81166, 81260, 81287, 81316, 81347, 81355, 81369, 81376, 81439, 81358, 81499, 81501, 81507, 81620, 81624, 81658, 81837, 81897, 81943, 82130, 82282, 82283, 82286, 82287, 82420, 82493, 82498, 82503, 82509, 82516, 82535, 82783, 82909, 82916, 82943, 83152, 83422, 83488, 83535, 131608, 83617, 1956, 1975, 83638, 94638, 535769, 837913, 5013, 434291, 83934, 83945, 131712, 83958, 84100, 84229, 84297, 84328, 84330, 84476, 84497, 84511, 84514, 84540, 84626, 5901, 84738, 84846, 84853, 84859, 84869, 84904, 84920, 84937, 85152, 85486, 85628, 85760, 85798, 85897, 85978, 86017, 112763, 86085, 90727, 86124, 86131, 86182, 86186, 86199, 86262, 86394, 86492, 86879, 87027, 87053, 87068, 87085, 87095, 87102, 87106, 87136, 87218, 87275, 87417, 87420, 87476, 87478, 87577, 87583, 87613, 87620, 87716, 87897, 87905, 87942, 87963, 132569, 88077, 88344, 88359, 88360, 88380, 88387, 112725, 88391, 88404, 88426, 88453, 88465, 88467, 88468, 88472, 88482, 88493, 88499, 88508, 88509, 88561, 88562, 88576, 88578, 88582, 88608, 88614, 5625, 88632, 88655, 88662, 88669, 88675, 88691, 88693, 88732, 88734, 88762, 88774, 88804, 93973, 88386, 88893, 4586, 4242, 88932, 88952, 89149, 89195, 89235, 89316, 91949, 89858, 89881, 89940, 90046, 90080, 133165, 90222, 90319, 90344, 90389, 6085, 91116, 91132, 91199, 91322, 91346, 91422, 91684, 91715, 91727, 91910, 92054, 145654, 92146, 92254, 92331, 84487, 92421, 92497, 92515, 92536, 92567, 92615, 92630, 92651, 92806, 92807, 5188, 6056, 93112, 93122, 93140, 932869, 93295, 93307, 93309, 93449, 112403, 93828, 93829, 93830, 93929, 93967, 94079, 94111, 94123, 94252, 94270, 94257, 94259, 94388, 94398, 94449, 945114, 945115, 92270, 94578, 94579, 94580, 92269, 94633, 94698, 94803, 94820, 94830, 94833, 94923, 95055, 95056, 95058, 95059, 95060, 95141, 95289, 95362, 95340, 95398, 95436, 95438, 95461, 95550, 95561, 95581, 95589, 95601, 95666, 5771, 5738, 5768, 608241, 53294, 95741, 95847, 95858, 95860, 146323, 134044, 95877, 95916, 95919, 90593, 95927, 95933, 434469, 96069, 96106, 96110, 96218, 5793, 5797, 5742, 434320, 5773, 5778, 96432, 96443, 96454, 96456, 96465, 96469, 96476, 96499, 965, 6565, 96691, 96695, 96698, 96736, 5751, 96851, 96852, 96876, 96919, 611983, 128512, 434483, 129118, 97133, 80955, 97222, 122118, 97454, 97533, 97544, 124314, 124329, 97567, 97601, 121554, 121552, 97680, 977, 97804, 97904, 98045, 98060, 110068, 137365, 98910, 98977, 134736, 99139, 99165, 99182, 99193, 99194, 134906, 99513, 99521, 99566, 99581, 794359, 99741, 99754, 95723, 99881, 99951, 99988, 99983, 1000311, 1000842, 1001991, 100694, 100702, 100824, 1011337, 1012157, 1014561, 1015002, 1015901, 1018857, 1018858, 1018864, 1018985, 1019304, 1019388, 1019750, 1027359, 1027360, 103020, 103406, 1036867, 1036878, 1036884, 103768, 104558, 105406, 106258, 106330, 106677, 106685, 106708, 108484, 110062, 110063, 110341, 110394, 111647, 112293, 112811, 113093, 113508, 113515, 114124, 115894, 119756, 121174, 121461, 123789, 124412, 125024, 129698, 131432, 133622, 134101, 138067, 138282, 150460, 159574, 160269, 1972, 219802, 240287, 2651, 3352, 3870, 435566, 435838, 435867, 435952, 436028, 436115, 436151, 436152, 436153, 436157, 436226, 436230, 4576, 4597, 612395, 618651, 618756, 619231, 619438, 620862, 621123, 621133, 631131, 631133, 65234, 66187, 6713, 6819, 6820, 717075, 717152, 717179, 717309, 717572, 717745, 717746, 717776, 718190, 718375, 718390, 719157, 719571, 770367, 770423, 770437, 770935, 783398, 784919, 786410, 786415, 786418, 786419, 786425, 786432, 786436, 786443, 786444, 786445, 786452, 786465, 786466, 786472, 786473, 786476, 786478, 786482, 786484, 786485, 786486, 786525, 786543, 786995, 787264, 788791, 80317, 81463, 83607, 836203, 836222, 83756, 839011, 839015, 83951, 846822, 85759, 86082, 86122, 87971, 883891, 886176, 888128, 88871, 889056, 889079, 89531, 90180, 904305, 92124, 923640, 930633, 931038, 93826, 94572, 94599, 95323, 956668, 956735, 956740, 956888, 956889, 95864, 959234, 960102, 963515, 963521, 963836, 963845, 963854, 963858, 966763, 967395, 969492, 970234, 970669, 97085, 971406, 973446, 975508, 975567, 976153, 976160, 983577, 987988, 98988, 99414, 995814, 99763, 99994) + ; + """.format(geom=sites.unary_union) + df = gpd.pd.read_sql(sql,con_gn) + return df + +if __name__ == "__main__": + + from pycen import con_fon,con_gn + from sqlalchemy import create_engine + from sqlalchemy.engine import URL + + fon_user = 'admin_ra' + fon_pwd = 'adminRAsig' + fon_adr = '94.23.100.197' + fon_base = 'tdb_fcen' + fon_url = URL.create("postgresql+psycopg2", username=fon_user, password=fon_pwd, host=fon_adr, database=fon_base) + con_fcen = create_engine(fon_url) + + an = '2023' + + sql_f = ''' + SELECT vscs.id_site_cen,geom FROM "fcen_2023"."sites_cen_2023" AS vscs + WHERE id_site_fcen ilike 'CENISE%%' + '''.format(an) + f = gpd.pd.read_sql(sql_f,con_fcen) + f = gpd.read_postgis(sql_f,con_fcen) + + sql_s = ''' + SELECT * FROM sites.sites WHERE site_id ilike any(array['{}']) + '''.format("%%','".join(f.id_site_cen.tolist())+'%%') + s = gpd.read_postgis(sql_s,con_fon) + + sites_cen = get_sites_cen(con_fon,an) + indic = get_esp_lr(sites_cen,con_gn) + indic = get_esp_lr(f,con_gn)