Su consulta es bastante grande y debido a la falta de sangría y un violín que funciona, es difícil saber dónde está el problema. Para más preguntas, considere crear un minimal reproducible example
. También noté que en una sola consulta estás transformando geometrías 18 veces (!) de un lado a otro entre 31467
y 25832
. ¿Es realmente necesario? Tenga en cuenta que las transformaciones pueden llevar tiempo y ciertamente ralentizarán sus consultas. Realice las transformaciones necesarias, si es posible, en el origen o en el final de una cadena de funciones. Por ejemplo, en lugar de
WITH j (point) AS(
VALUES ('POINT(1 2)'::geometry)
)
SELECT st_astext(
ST_MakeEnvelope(
ST_X(ST_Transform(ST_SetSRID(point,31467),25832)),
ST_Y(ST_Transform(ST_SetSRID(point,31467),25832)),
ST_X(ST_Transform(ST_SetSRID(point,31467),25832))+10,
ST_Y(ST_Transform(ST_SetSRID(point,31467),25832))+10),2)
FROM j;
.. considera esto:
WITH j (point) AS(
VALUES (ST_SetSRID('POINT(1 2)'::geometry,31467))
)
SELECT
ST_Transform(
ST_MakeEnvelope(
ST_X(point),ST_Y(point),ST_X(point)+10,ST_Y(point)+10,31467),
25832)
FROM j
Los resultados pueden ser ligeramente diferentes, pero dependiendo de su caso de uso, no importará mucho. Pruebe ambos enfoques y, si es posible, mantenga su código limpio.
Dicho esto, podría usar ST_NumGeometries
para contar el número de geometrías en una columna:
SELECT
ST_NumGeometries('MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),((20 35, 10 30, 10 10, 30 5, 45 20, 20 35)))'::geometry),
ST_NumGeometries('POLYGON((40 40, 20 45, 45 30, 40 40))'::geometry);
st_numgeometries | st_numgeometries
------------------+------------------
2 | 1
Entonces su consulta debería verse así:
WITH j AS (
SELECT DISTINCT
ST_MakeEnvelope(
ST_X(ST_Transform(ST_SetSRID(point,31467),25832)),
ST_Y(ST_Transform(ST_SetSRID(point,31467),25832)),
ST_X(ST_Transform(ST_SetSRID(point,31467),25832))+10,
ST_Y(ST_Transform(ST_SetSRID(point,31467),25832))+10) AS geom_int1,
ST_GeomFromText('POLYGON((6.69308799941667 51.1325687572078,6.69314216094032 51.1325286743429,6.6931829229236 51.1324827378523,6.69320873406544 51.1324326960125,6.69321861208315 51.1323804533377,6.69321218108977 51.1323279980963,6.69318968589172 51.1322773266396,6.69315198266368 51.1322303674254,6.69310050635699 51.1321889076246,6.69303721608294 51.1321545251075,6.69296452055024 51.1321285283945,6.69288518639497 51.1321119068596,6.6928022328913 51.132105293078,6.69271881704983 51.1321089387537,6.69263811347528 51.1321227051402,6.69256319355485 51.1321460683214,6.69167022350287 51.1324966230214,6.691601844655 51.1325299362666,6.69154548586769 51.1325712077134,6.6915034414949 51.1326187572348,6.69147742317882 51.1326706491231,6.69146849016453 51.1327247708897,6.69147700617074 51.1327789192613,6.69150262457336 51.1328308898743,6.69154430250688 51.1328785670135,6.6918617087031 51.1331680405352,6.69191278478678 51.1332064363834,6.69197426178939 51.1332382298226,6.69204402058778 51.1332623249167,6.69211965656854 51.1332778910942,6.69219856251863 51.133284391779,6.6922780185011 51.1332816028878,6.69235528561775 51.1332696205548,6.69281658666282 51.13316870319,6.69288972627184 51.1331476779976,6.6929553372192 51.1331184277034,6.6930111102981 51.1330819817912,6.69305508256535 51.1330396229968,6.69308570642734 51.1329928421602,6.69310190410533 51.1329432857531,6.69310310556331 51.1328926979291,6.69307771111191 51.1326908636516,6.69306446700159 51.1326424034468,6.69303745832752 51.1325962617897,6.69303600327017 51.1325947186521,6.69308799941667 51.1325687572078),(6.69243035603289 51.1325209327628,6.69265042517401 51.1327121450621,6.6926758177672 51.132913979408,6.69221451899067 51.13301489625,6.69189711157563 51.1327254236646,6.69279008443662 51.1323748672786,6.69243035603289 51.1325209327628))') AS geom_int2
FROM slope inner join (select
st_setsrid(st_transform(st_setsrid(ST_Buffer(ST_Transform(ST_SetSRID(ST_GeomFromText('POLYGON((6.69308799941667 51.1325687572078,6.69314216094032 51.1325286743429,6.6931829229236 51.1324827378523,6.69320873406544 51.1324326960125,6.69321861208315 51.1323804533377,6.69321218108977 51.1323279980963,6.69318968589172 51.1322773266396,6.69315198266368 51.1322303674254,6.69310050635699 51.1321889076246,6.69303721608294 51.1321545251075,6.69296452055024 51.1321285283945,6.69288518639497 51.1321119068596,6.6928022328913 51.132105293078,6.69271881704983 51.1321089387537,6.69263811347528 51.1321227051402,6.69256319355485 51.1321460683214,6.69167022350287 51.1324966230214,6.691601844655 51.1325299362666,6.69154548586769 51.1325712077134,6.6915034414949 51.1326187572348,6.69147742317882 51.1326706491231,6.69146849016453 51.1327247708897,6.69147700617074 51.1327789192613,6.69150262457336 51.1328308898743,6.69154430250688 51.1328785670135,6.6918617087031 51.1331680405352,6.69191278478678 51.1332064363834,6.69197426178939 51.1332382298226,6.69204402058778 51.1332623249167,6.69211965656854 51.1332778910942,6.69219856251863 51.133284391779,6.6922780185011 51.1332816028878,6.69235528561775 51.1332696205548,6.69281658666282 51.13316870319,6.69288972627184 51.1331476779976,6.6929553372192 51.1331184277034,6.6930111102981 51.1330819817912,6.69305508256535 51.1330396229968,6.69308570642734 51.1329928421602,6.69310190410533 51.1329432857531,6.69310310556331 51.1328926979291,6.69307771111191 51.1326908636516,6.69306446700159 51.1326424034468,6.69303745832752 51.1325962617897,6.69303600327017 51.1325947186521,6.69308799941667 51.1325687572078),(6.69243035603289 51.1325209327628,6.69265042517401 51.1327121450621,6.6926758177672 51.132913979408,6.69221451899067 51.13301489625,6.69189711157563 51.1327254236646,6.69279008443662 51.1323748672786,6.69243035603289 51.1325209327628))'),4326),25832), 30),25832),31467),25832) as geom
) a on point && a.geom
JOIN (
WITH data AS (
SELECT '{"type": "FeatureCollection", "features": [{"type": "Feature", "properties": {"ID": "1"}, "geometry": {"type": "Polygon", "coordinates": [[[6.69253798665417, 51.1321564140328], [6.69251962171524, 51.1321635163992], [6.69249846231729, 51.1321750605202], [6.69249925136169, 51.1321793032012], [6.69251085641084, 51.1321902349559], [6.6925202281672, 51.1321886211859], [6.6925350953045, 51.13217713273], [6.69253798665417, 51.1321564140328]]]}}, {"type": "Feature", "properties": {"ID": "2"}, "geometry": {"type": "Polygon", "coordinates": [[[6.69269313266862, 51.1321811516334], [6.69269456345932, 51.1321667897304], [6.69269648476695, 51.1321311221074], [6.69269663315549, 51.1321252790401], [6.69269334643976, 51.132113791988], [6.69268514848039, 51.1321147993719], [6.69266813655301, 51.1321177013629], [6.69265269992705, 51.1321205445076], [6.69260400160369, 51.1321339733957], [6.69259342024285, 51.1321369123716], [6.69257235999851, 51.1321436017891], [6.6925532535978, 51.1321770414777], [6.69254076871528, 51.1322152886675], [6.69253704911869, 51.1322268172979], [6.69253585685355, 51.1322306611143], [6.69253560204644, 51.1322329045446], [6.69253742373699, 51.132250838247], [6.69255296008214, 51.1322631966762], [6.69261518281268, 51.1322824125944], [6.69263790187194, 51.1322825912308], [6.69264662627284, 51.1322824936254], [6.69267258657931, 51.132229132945], [6.69269313266862, 51.1321811516334]]]}}, {"type": "Feature", "properties": {"ID": "3"}, "geometry": {"type": "Polygon", "coordinates": [[[6.69251672749726, 51.1322215594779], [6.69250460947264, 51.1322094483985], [6.6924921739216, 51.1322008386475], [6.69248182942694, 51.1322018935827], [6.69246834157673, 51.132211520553], [6.69244929502299, 51.1322351581201], [6.69245199419083, 51.1322612935409], [6.69246425167291, 51.1322677412591], [6.69248454251289, 51.1322707500203], [6.69250080856706, 51.1322713409299], [6.69251741124046, 51.1322393807793], [6.69251672749726, 51.1322215594779]]]}}, {"type": "Feature", "properties": {"ID": "4"}, "geometry": {"type": "Polygon", "coordinates": [[[6.6922825274399, 51.1323259414957], [6.69227206035333, 51.1323151221225], [6.69224485573949, 51.1322959677944], [6.69223824707324, 51.1322937687407], [6.69222068683045, 51.1322933321209], [6.69221819197358, 51.1322946319424], [6.69218894528929, 51.1323106931418], [6.69217475443148, 51.1323315485006], [6.69217341036882, 51.1323355691933], [6.69217231313262, 51.1323547044118], [6.69217324495241, 51.1323589499138], [6.69217569889977, 51.1323613367604], [6.69218142835096, 51.1323639681514], [6.6922037172841, 51.1323727724673], [6.69223780236341, 51.1323743447748], [6.69226136483905, 51.1323748099503], [6.69226799202122, 51.1323737715855], [6.69228555129034, 51.1323570299429], [6.69228519515222, 51.1323527058685], [6.6922825274399, 51.1323259414957]]]}}, {"type": "Feature", "properties": {"ID": "5"}, "geometry": {"type": "Polygon", "coordinates": [[[6.69270036788524, 51.1326380915836], [6.69270044963208, 51.1326307182485], [6.69269979440667, 51.1326266580875], [6.69265572460697, 51.1326103188106], [6.69258556546035, 51.1325983212077], [6.69257773814301, 51.1325976270713], [6.69257545328535, 51.1325975819696], [6.69254710479826, 51.1326070954851], [6.69254041373701, 51.1326151478002], [6.69256693935704, 51.1326393252039], [6.69259572746481, 51.1326639969531], [6.69261808158644, 51.1326829655137], [6.69263440827555, 51.1326938105779], [6.69266740157087, 51.1326886158279], [6.69268164386179, 51.1326781942805], [6.6926927410912, 51.1326620445339], [6.69269711646734, 51.1326516980425], [6.69270036788524, 51.1326380915836]]]}}, {"type": "Feature", "properties": {"ID": "6"}, "geometry": {"type": "Polygon", "coordinates": [[[6.69175796394166, 51.1325888173132], [6.69180793429611, 51.1324725243485], [6.69180892473724, 51.1324526675166], [6.69180413787601, 51.1324454678607], [6.6917982202076, 51.1324466101479], [6.69178087259056, 51.1324533727339], [6.69171031275604, 51.1324809395917], [6.69167093571617, 51.1324966207435], [6.69162081516962, 51.132521353381], [6.69161040721569, 51.1325265441535], [6.69161011264506, 51.1325267182132], [6.69159802180106, 51.1325341242118], [6.69157656681147, 51.1325487202367], [6.69155258043593, 51.1325682128804], [6.69153973458902, 51.1325792914381], [6.69150389109257, 51.1326186961186], [6.69147684379341, 51.1326766219394], [6.69147001176052, 51.1327161498552], [6.6914699304478, 51.1327206451701], [6.69147004831695, 51.1327326093068], [6.69147598409746, 51.1327712201722], [6.69149221971477, 51.1328096746915], [6.69150093658731, 51.1328240571066], [6.69151196908048, 51.1328407337113], [6.69152518846839, 51.1328565541182], [6.69154021053508, 51.1328734893842], [6.69155054373049, 51.1328841263015], [6.69158606889102, 51.132914147773], [6.69159074495821, 51.1329178376514], [6.69159617152892, 51.132917944814], [6.69160772711436, 51.132909808738], [6.69163093798561, 51.132865747583], [6.69164647462266, 51.1328351155779], [6.69171060598049, 51.1326957183096], [6.69175796394166, 51.1325888173132]]]}}, {"type": "Feature", "properties": {"ID": "7"}, "geometry": {"type": "Polygon", "coordinates": [[[6.69280944347454, 51.1327325212954], [6.69278198469405, 51.1327098544827], [6.69276787507997, 51.1327032803041], [6.69275702253061, 51.1327001880702], [6.6927110654878, 51.1327274316875], [6.69270049184791, 51.1327359470067], [6.69269795107157, 51.1327439013734], [6.69269675432507, 51.1327478350411], [6.69269555757836, 51.1327517687087], [6.69270108943557, 51.1327612314915], [6.69272374606894, 51.1327856023059], [6.69273137237766, 51.132787461664], [6.69274887457694, 51.1327890662591], [6.69278274605442, 51.1327891951749], [6.69279197959956, 51.1327874887162], [6.69280546252786, 51.1327808295785], [6.69281911568176, 51.13277075614], [6.692818514201, 51.1327656177799], [6.69280944347454, 51.1327325212954]]]}}, {"type": "Feature", "properties": {"ID": "8"}, "geometry": {"type": "Polygon", "coordinates": [[[6.69300203804217, 51.1328749175082], [6.69298489450558, 51.1328403126402], [6.69297071368933, 51.1328322980672], [6.69294514042643, 51.1328205510521], [6.69289902806839, 51.1328193711454], [6.69286658297828, 51.1328279045017], [6.69286227033698, 51.1328369930997], [6.6928613985622, 51.1328688140847], [6.69286281220306, 51.132897802148], [6.69287913508867, 51.1329058590115], [6.6928817901407, 51.1329070806127], [6.69290110745025, 51.1329152865115], [6.69291370059255, 51.1329178734543], [6.69292154588787, 51.1329182081691], [6.69294384634694, 51.1329153205768], [6.69297262282259, 51.13290869344], [6.69298582911113, 51.132901848945], [6.69300203804217, 51.1328749175082]]]}}, {"type": "Feature", "properties": {"ID": "9"}, "geometry": {"type": "Polygon", "coordinates": [[[6.69232916938592, 51.133110495585], [6.69233421078188, 51.1330953955202], [6.69235075084229, 51.1330217027418], [6.69235005039201, 51.1329984848074], [6.69233579852696, 51.1329890297401], [6.69215882485022, 51.1329777112594], [6.69215447738489, 51.1329817625941], [6.69209733560649, 51.1330555531216], [6.69209320085406, 51.1330668037259], [6.6920424322757, 51.1332449586609], [6.69204425051294, 51.1332572261821], [6.69204991732763, 51.1332611154794], [6.69208256036642, 51.1332686852289], [6.69214233141891, 51.1332797585278], [6.69218590448418, 51.1332831370578], [6.69223609649279, 51.133282778893], [6.69232916938592, 51.133110495585]]]}}, {"type": "Feature", "properties": {"ID": "10"}, "geometry": {"type": "Polygon", "coordinates": [[[6.69309575564949, 51.1329620286457], [6.69309963642392, 51.1329472653982], [6.69309881754873, 51.132935017617], [6.69307203062263, 51.1329218076648], [6.69305328289965, 51.1329222471207], [6.6930360925896, 51.1329229871251], [6.69303076850343, 51.1329236914975], [6.69298880311273, 51.1329396817633], [6.69297592220055, 51.1329486012633], [6.69296986419304, 51.1329840973053], [6.69297042430155, 51.1329958003503], [6.69297292705951, 51.1330000768489], [6.69301308177035, 51.1330261420197], [6.69301918206967, 51.133027071858], [6.69303825522546, 51.1330287074127], [6.69306059103336, 51.1330279790168], [6.69307287545165, 51.1330109532808], [6.69307979887065, 51.1330011067537], [6.69308523205943, 51.1329896119255], [6.69309575564949, 51.1329620286457]]]}}, {"type": "Feature", "properties": {"ID": "11"}, "geometry": {"type": "Polygon", "coordinates": [[[6.69264305397923, 51.1322939352281], [6.69264424175731, 51.1322901812605], [6.69264543849516, 51.132286247593], [6.69263785259199, 51.1322835795805], [6.69252746167717, 51.1322814904708], [6.6925183127842, 51.1322843677784], [6.69250842691038, 51.1322905582583], [6.69250503044308, 51.1322927396721], [6.69250412882457, 51.1322936212581], [6.69250834622358, 51.1323151098501], [6.6925161404358, 51.1323250669937], [6.69254901931236, 51.1323359689943], [6.69251034598528, 51.1323352055896], [6.69249005454883, 51.1323350748503], [6.69245693248874, 51.1324059220424], [6.69245559292849, 51.1324098528887], [6.6924544006514, 51.132413696704], [6.69247323550652, 51.1324229724131], [6.69251602483015, 51.132428196049], [6.69246937307172, 51.1324344982216], [6.69244175489256, 51.1324523004608], [6.69244041532956, 51.1324562313069], [6.6924378700452, 51.1324642755178], [6.69243748586974, 51.1324662465788], [6.69244000419296, 51.132481675759], [6.69244043552284, 51.1324828388153], [6.69242276418403, 51.1324862820424], [6.69242216608837, 51.1324868098659], [6.69240793104538, 51.132505685735], [6.69240683952442, 51.1325189750712], [6.69240749472285, 51.1325230352335], [6.69245266197307, 51.1325402956537], [6.69246074152444, 51.1325404449095], [6.69245904780327, 51.1325412311583], [6.69246089414942, 51.1325472035405], [6.69248390393314, 51.1325673542701], [6.69249665598157, 51.1325782187286], [6.692498177537, 51.1325792380866], [6.69250207298905, 51.1325813835662], [6.69250992213802, 51.1325845064756], [6.69251385399889, 51.1325830551374], [6.69253035818111, 51.1325760059787], [6.69253149503538, 51.1325589401237], [6.69253220875509, 51.1325413559783], [6.69254382431863, 51.1325410159433], [6.69256431636541, 51.1324969908775], [6.69256499007752, 51.1324748793294], [6.69255810773198, 51.132470462385], [6.69257536992313, 51.1324530493135], [6.69257749741779, 51.1324361828893], [6.69257444491701, 51.132435038046], [6.69258994872926, 51.1324330110108], [6.69259606294197, 51.1324307933024], [6.69262641162471, 51.132346760322], [6.69262231830842, 51.1323440804244], [6.69262697891405, 51.1323439834292], [6.69264305397923, 51.1322939352281]]]}}, {"type": "Feature", "properties": {"ID": "12"}, "geometry": {"type": "Polygon", "coordinates": [[[6.69242704496132, 51.132323038405], [6.69242269090442, 51.1323157573803], [6.692417553916, 51.1323184440628], [6.69241644391112, 51.1323349708171], [6.69242163295421, 51.1323398399883], [6.69242452621548, 51.1323414919739], [6.6923997028096, 51.1323668382825], [6.69237476252063, 51.1324054691417], [6.69237385584298, 51.1324093185943], [6.6923741412481, 51.1324098621282], [6.69236742132112, 51.1324064934171], [6.69230798969311, 51.1323771694288], [6.69230617358135, 51.13237632413], [6.69228802417196, 51.132376235643], [6.69228559204211, 51.1323762775666], [6.69224488018429, 51.1323957999097], [6.6922422099899, 51.1324034818971], [6.69224101769533, 51.13240732571], [6.69224077631505, 51.1324092995895], [6.69223922596153, 51.1324403876708], [6.69234968554863, 51.1325471667089], [6.69237904767688, 51.1325345254075], [6.69238858528683, 51.1325295872009], [6.69241875730355, 51.1324835047915], [6.692423113175, 51.1324592174721], [6.69242348057106, 51.1324518497756], [6.69242282089158, 51.1324478794633], [6.69242216121221, 51.132443909151], [6.69242027569293, 51.1324329893825], [6.69241230485538, 51.1324289935948], [6.69243827735106, 51.1324131086063], [6.69245242722499, 51.132395939882], [6.69245495010085, 51.1323883449204], [6.69247131545421, 51.1323382125338], [6.69247250772775, 51.1323343687182], [6.69246979340118, 51.1323171369011], [6.69246938291431, 51.1323167690444], [6.69244774801696, 51.1323177809765], [6.69242811022654, 51.132337832463], [6.69242940760109, 51.1323358562987], [6.69242910521451, 51.1323304540251], [6.69242704496132, 51.132323038405]]]}}]}'::json AS featuresCollection
)
SELECT
LIDARDataPolygonsAsGeometry
FROM (
SELECT
ST_Transform(ST_SetSRID(ST_GeomFromGeoJSON(feature->>'geometry'),4326),25832) AS LIDARDataPolygonsAsGeometry
FROM (SELECT json_array_elements(featuresCollection->'features') AS feature
FROM data) AS f) j
GROUP BY
LIDARDataPolygonsAsGeometry
) j ON
NOT ST_Contains('0103000020E864000001000000070000002CC1936439AA1441C2E17F35419E554194D77D6744A91441B8177F714B9E554159A47F43A1A914411A88C850539E5541FCEF81F020AA1441326CDC41509E5541EBFA8E0417AA14415F982DA94A9E554157E297C4D6A91441686B6D77459E55412CC1936439AA1441C2E17F35419E5541',ST_Centroid(ST_SetSRID(ST_MakeEnvelope(
ST_X(ST_Transform(ST_SetSRID(point,31467),25832)),
ST_Y(ST_Transform(ST_SetSRID(point,31467),25832)),
ST_X(ST_Transform(ST_SetSRID(point,31467),25832))+10,
ST_Y(ST_Transform(ST_SetSRID(point,31467),25832))+10),25832)))
AND
ST_Contains(ST_Transform(ST_SetSRID(ST_GeomFromText('POLYGON((6.69308799941667 51.1325687572078,6.69314216094032 51.1325286743429,6.6931829229236 51.1324827378523,6.69320873406544 51.1324326960125,6.69321861208315 51.1323804533377,6.69321218108977 51.1323279980963,6.69318968589172 51.1322773266396,6.69315198266368 51.1322303674254,6.69310050635699 51.1321889076246,6.69303721608294 51.1321545251075,6.69296452055024 51.1321285283945,6.69288518639497 51.1321119068596,6.6928022328913 51.132105293078,6.69271881704983 51.1321089387537,6.69263811347528 51.1321227051402,6.69256319355485 51.1321460683214,6.69167022350287 51.1324966230214,6.691601844655 51.1325299362666,6.69154548586769 51.1325712077134,6.6915034414949 51.1326187572348,6.69147742317882 51.1326706491231,6.69146849016453 51.1327247708897,6.69147700617074 51.1327789192613,6.69150262457336 51.1328308898743,6.69154430250688 51.1328785670135,6.6918617087031 51.1331680405352,6.69191278478678 51.1332064363834,6.69197426178939 51.1332382298226,6.69204402058778 51.1332623249167,6.69211965656854 51.1332778910942,6.69219856251863 51.133284391779,6.6922780185011 51.1332816028878,6.69235528561775 51.1332696205548,6.69281658666282 51.13316870319,6.69288972627184 51.1331476779976,6.6929553372192 51.1331184277034,6.6930111102981 51.1330819817912,6.69305508256535 51.1330396229968,6.69308570642734 51.1329928421602,6.69310190410533 51.1329432857531,6.69310310556331 51.1328926979291,6.69307771111191 51.1326908636516,6.69306446700159 51.1326424034468,6.69303745832752 51.1325962617897,6.69303600327017 51.1325947186521,6.69308799941667 51.1325687572078),(6.69243035603289 51.1325209327628,6.69265042517401 51.1327121450621,6.6926758177672 51.132913979408,6.69221451899067 51.13301489625,6.69189711157563 51.1327254236646,6.69279008443662 51.1323748672786,6.69243035603289 51.1325209327628))'),4326),25832), ST_Centroid(ST_SetSRID(ST_MakeEnvelope(
ST_X(ST_Transform(ST_SetSRID(point,31467),25832)),
ST_Y(ST_Transform(ST_SetSRID(point,31467),25832)),
ST_X(ST_Transform(ST_SetSRID(point,31467),25832))+10,
ST_Y(ST_Transform(ST_SetSRID(point,31467),25832))+10),25832)))
)
SELECT *
FROM j
WHERE
ST_NumGeometries(
ST_Intersection(geom_int1,geom_int2)) > 1