<html>
  <head>

    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>Hi all,</p>
    <p>I am working with fishing vessels positions. I have millions of
      points, and when doing some analysis I need to do some
      intersections with polygons.</p>
    <p>All the database is on EPSG 4326 (Lat Long)<br>
    </p>
    <p>I've noticed some problems when the points are on the edges of
      the polygons. When I use ST_Within I lose the points on the edges,
      while when using ST_Intersects those points get counted twice.</p>
    <p>To fix that I wrote a function:</p>
    <p>CREATE OR REPLACE FUNCTION "monitoreo"."dentro" (punto geometry,
      rect geometry)  RETURNS boolean<br>
        VOLATILE<br>
      AS $body$<br>
      begin<br>
      IF (st_within(punto, rect) OR <br>
      ( <br>
          st_x(punto)=st_xmax(rect) AND st_y(punto) BETWEEN
      st_ymin(rect) AND st_ymax(rect) <br>
      ) <br>
      OR<br>
      ( <br>
          st_y(punto)=st_ymax(rect) AND st_x(punto) BETWEEN
      st_xmin(rect) AND st_xmax(rect) <br>
      ) <br>
      ) THEN<br>
      RETURN TRUE;<br>
      ELSE<br>
      RETURN FALSE;<br>
      END IF;<br>
      end<br>
      $body$ LANGUAGE plpgsql</p>
    <p><br>
    </p>
    <p>I have two questions:</p>
    <p>1- that function works but it is slow. Do you have suggestions to
      optimize it?</p>
    <p>2- the function works for rectangles, do you have an example of
      how to handle irregular polygons?</p>
    <p>Many thanks in advance.</p>
    <p><br>
    </p>
    <div class="moz-signature">-- <br>
      <p><font face="monospace"><font style="font-size: 14pt" size="4"><b>Gustavo
              Martínez Puljak</b></font></font></p>
      <p style="margin-bottom: 0cm"><font style="font-size: 14pt"
          size="4"><b><br>
          </b></font><font face="monospace"><font style="font-size:
            10pt" size="2">Linkedin:<a
              href="https://www.linkedin.com/in/gustavomartinezpuljak">www.linkedin.com/in/gustavomartinezpuljak</a></font></font></p>
      <p><b><br>
        </b><font face="monospace"><font style="font-size: 10pt"
            size="2"><i>Que
              la tierra se vaya haciendo camino ante tus pasos,</i></font></font><font
          style="font-size: 10pt" size="2"><i><br>
          </i></font><font face="monospace"><font style="font-size:
            10pt" size="2"><i>que
              el viento sople siempre a tus espaldas,</i></font></font><font
          style="font-size: 10pt" size="2"><i><br>
          </i></font><font face="monospace"><font style="font-size:
            10pt" size="2"><i>que
              el sol brille cálido sobre tu cara,</i></font></font><font
          style="font-size: 10pt" size="2"><i><br>
          </i></font><font face="monospace"><font style="font-size:
            10pt" size="2"><i>que
              la lluvia caiga suavemente sobre tus campos y,</i></font></font><font
          style="font-size: 10pt" size="2"><i><br>
          </i></font><font face="monospace"><font style="font-size:
            10pt" size="2"><i>hasta
              tanto volvamos a encontrarnos,</i></font></font><font
          style="font-size: 10pt" size="2"><i><br>
          </i></font><font face="monospace"><font style="font-size:
            10pt" size="2"><i>que
              Dios te lleve en la palma de su mano.</i></font></font></p>
      <p><br>
        <br>
      </p>
    </div>
  </body>
</html>