<div dir="ltr"><div dir="ltr">I'm not sure I understood you correctly, but it looks like it works on my postgres-postgis installation. See the attached file.</div><div dir="ltr">"PostgreSQL 11.2 (Ubuntu 11.2-1.pgdg18.04+1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0, 64-bit"<br></div><div dir="ltr">"POSTGIS="2.5.1 r17027" [EXTENSION] PGSQL="110" GEOS="3.6.2-CAPI-1.10.2 4d2925d6" PROJ="Rel. 4.9.3, 15 August 2016" GDAL="GDAL 2.2.3, released 2017/11/20" LIBXML="2.9.4" LIBJSON="0.12.1" LIBPROTOBUF="1.2.1" RASTER"</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">pt., 15 mar 2019 o 16:30 <<a href="mailto:Michael.Haertel@t-systems.com" target="_blank">Michael.Haertel@t-systems.com</a>> napisał(a):<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div lang="DE" link="#0563C1" vlink="#954F72">
<div class="m_-7437438812876749905m_-686965810349276645WordSection1">
<p class="MsoNormal">Hello List,<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal"><span lang="EN-US">I observed a strange behavior when I implemented a trigger function.<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">Given: Table “fcp_4258_kls_addr” with address points, Table “fcp_4258_candidates” with some other points.<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">Objective: find closest point in fcp_4258_kls_addr to a point in fcp_4258_candidates (from all found address points in a radius of 0.01 degrees around the candidate).<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">After playing around with the statements I came up with this:<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-family:"Courier New"">select <a href="http://s.id" target="_blank">s.id</a>, ST_DistanceSphere(ST_SetSRID(ST_Point(6.951468, 50.93651),4258), s.wkb_geometry) as klsdistancetocandidatem FROM kddb.fcp_4258_kls_addr s WHERE ST_DWithin(s.wkb_geometry,
ST_SetSRID(ST_Point(6.951468, 50.93651),4258), 0.01) ORDER BY klsdistancetocandidatem ASC LIMIT 1;<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">I hope that the function does what I want: First select all addresses within 0.01 degrees distance to the point 6.951468, 50.93651 and then calculate the distance to all of them. Order by distance in ascending order and
select the first which should be the closest to the questioned candidate. <u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">This works fine when I run the statement manually in the console:<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-family:"Courier New""> id | geo_kls_dist2candidate_m<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-family:"Courier New"">----------+--------------------------<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-family:"Courier New"">17553866 | 6.85436569<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">My goal was to save the closest address point (id) and the distance to the table as soon as the candidate table is updated. The relevant line in the trigger function for the trigger “BEFORE UPDATE ON candidates FOR EACH
ROW EXECUTE PROCEDURE” looks like this:<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-family:"Courier New"">SELECT <a href="http://s.id" target="_blank">s.id</a>, ST_DistanceSphere(s.wkb_geometry, NEW.wkb_geometry) INTO klsid, klsdistancetocandidatem FROM kddb.fcp_4258_kls_addr s WHERE ST_DWithin(s.wkb_geometry, NEW.wkb_geometry, 0.01)
ORDER BY klsdistancetocandidatem ASC LIMIT 1;<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-family:"Courier New"">NEW.geo_kls_id = klsid;<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-family:"Courier New"">NEW.geo_kls_distance2candidate_m = klsdistancetocandidatem;<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US" style="font-family:"Courier New""><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">I assumed that the trigger does the same as the select statement but I get a different result for a candidate. The address point referenced in the candidates table for the candidate with the coordinates 6.951468, 50.93651
is 497 meters away and not 6.8 meters. <u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">Some findings:<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p>
<ul style="margin-top:0cm" type="disc">
<li class="m_-7437438812876749905m_-686965810349276645MsoListParagraph" style="margin-left:0cm"><span lang="EN-US">The id returned is within 0.01 degree radius for the candidate<u></u><u></u></span></li><li class="m_-7437438812876749905m_-686965810349276645MsoListParagraph" style="margin-left:0cm"><span lang="EN-US">The address point with the given id has correctly referenced distance to the candidate (but is not the closest)<u></u><u></u></span></li><li class="m_-7437438812876749905m_-686965810349276645MsoListParagraph" style="margin-left:0cm"><span lang="EN-US">The address point returned does neither belong to the preceding candidate point nor to the successive candidate (when sorted by primary key)<u></u><u></u></span></li><li class="m_-7437438812876749905m_-686965810349276645MsoListParagraph" style="margin-left:0cm"><span lang="EN-US">The update trigger is fired and the procedure is executed (Tested with various update statements)<u></u><u></u></span></li></ul>
<p class="MsoNormal"><span lang="EN-US" style="font-family:"Courier New""><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">The wkb_geometry doesn’t change with the update. I have no idea why the address points are different.
<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">Can anybody help?<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="color:#00b050">Michael</span><span style="color:#1f497d">
</span><span><u></u><u></u></span></p>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>
_______________________________________________<br>
postgis-users mailing list<br>
<a href="mailto:postgis-users@lists.osgeo.org" target="_blank">postgis-users@lists.osgeo.org</a><br>
<a href="https://lists.osgeo.org/mailman/listinfo/postgis-users" rel="noreferrer" target="_blank">https://lists.osgeo.org/mailman/listinfo/postgis-users</a></blockquote></div></div>