<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>OpenText Analytics Database 26.2.x – Spatial joins with ST_Intersects and STV_Intersect</title>
    <link>/en/data-analysis/geospatial-analytics/working-with-spatial-objects-tables/spatial-joins-with-st-intersects-and-stv-intersect/</link>
    <description>Recent content in Spatial joins with ST_Intersects and STV_Intersect on OpenText Analytics Database 26.2.x</description>
    <generator>Hugo -- gohugo.io</generator>
    
	  <atom:link href="/en/data-analysis/geospatial-analytics/working-with-spatial-objects-tables/spatial-joins-with-st-intersects-and-stv-intersect/index.xml" rel="self" type="application/rss+xml" />
    
    
      
        
      
    
    
    <item>
      <title>Data-Analysis: Best practices for spatial joins</title>
      <link>/en/data-analysis/geospatial-analytics/working-with-spatial-objects-tables/spatial-joins-with-st-intersects-and-stv-intersect/best-practices-spatial-joins/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/data-analysis/geospatial-analytics/working-with-spatial-objects-tables/spatial-joins-with-st-intersects-and-stv-intersect/best-practices-spatial-joins/</guid>
      <description>
        
        
        &lt;p&gt;Use these best practices to improve overall performance and optimize your spatial queries.&lt;/p&gt;
&lt;p&gt;Best practices for using spatial joins in OpenText™ Analytics Database include the following:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Table segmentation to speed up index creation&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Adequately sizing a geometry column to store point data&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Loading Well-Known Text (WKT) directly into a Geometry column, using STV_GeometryPoint in a COPY statement&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Using OVER (PARTITION BEST) with STV_Intersect transform queries&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;best-practices-example&#34;&gt;Best practices example&lt;/h2&gt;
&lt;p&gt;Before performing the steps in the following example, download &lt;code&gt;place_output.csv.zip&lt;/code&gt; from the Geospacial GitHub repository (&lt;a href=&#34;https://github.com/vertica/Vertica-Geospatial&#34;&gt;https://github.com/vertica/Vertica-Geospatial&lt;/a&gt;). You need to use the data set from this repository.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Create the table for the polygons. Use a GEOMETRY column width that fits your data without being excessively large. A good column-width fit improves performance. In addition, segmenting the table by HASH provides the advantages of parallel computation.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE TABLE artworks (gid int, g GEOMETRY(700)) SEGMENTED BY HASH(gid) ALL NODES;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Use a copy statement with ST_Buffer to create and load the polygons on which to run the intersect. By using ST_Buffer in your copy statement, you can use that function to create the polygons.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; COPY artworks(gid, gx FILLER LONG VARCHAR, g AS ST_Buffer(ST_GeomFromText(gx),8)) FROM STDIN DELIMITER &amp;#39;,&amp;#39;;
&amp;gt;&amp;gt; 1, POINT(10 45)
&amp;gt;&amp;gt; 2, POINT(25 45)
&amp;gt;&amp;gt; 3, POINT(35 45)
&amp;gt;&amp;gt; 4, POINT(35 15)
&amp;gt;&amp;gt; 5, POINT(30 5)
&amp;gt;&amp;gt; 6, POINT(15 5)
&amp;gt;&amp;gt; \.
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Create a table for the location data, represented by points. You can store point data in a GEOMETRY column of 100 bytes. Avoid over-fitting your GEOMETRY column. Doing so can significantly degrade spatial intersection performance. Also, segment this table by HASH, to take advantage of parallel computation.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE TABLE usr_data (gid identity, usr_id int, date_time timestamp, g GEOMETRY(100))
     SEGMENTED BY HASH(gid) ALL NODES;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;During the copy statement, transform the raw location data to GEOMETRY data. You must perform this transformation because your location data needs to use the GEOMETRY data type. Use the function STV_GeometryPoint to transform the &lt;em&gt;x&lt;/em&gt; and &lt;em&gt;y&lt;/em&gt; columns of the source table.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; COPY usr_data (usr_id, date_time, x FILLER LONG VARCHAR,
                  y FILLER LONG VARCHAR, g AS STV_GeometryPoint(x, y))
   FROM LOCAL &amp;#39;place_output.csv&amp;#39; DELIMITER &amp;#39;,&amp;#39; ENCLOSED BY &amp;#39;&amp;#39;;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Create the spatial index for the polygons. This index helps you speed up intersection calculations.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT STV_Create_Index(gid, g USING PARAMETERS index=&amp;#39;art_index&amp;#39;, overwrite=true) OVER() FROM artworks;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Write an analytic query that returns the number of intersections per polygon. Specify that the database ignores any &lt;code&gt;usr_id&lt;/code&gt; that intersects less than 20 times with a given polygon.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT pol_gid,
       COUNT(DISTINCT(usr_id)) AS count_user_visit
   FROM
     (SELECT pol_gid,
       usr_id,
       COUNT(usr_id) AS user_points_in
    FROM
       (SELECT STV_Intersect(usr_id, g USING PARAMETERS INDEX=&amp;#39;art_index&amp;#39;) OVER(PARTITION BEST) AS (usr_id,
                                                        pol_gid)
    FROM usr_data
      WHERE date_time BETWEEN &amp;#39;2014-07-02 09:30:20&amp;#39; AND &amp;#39;2014-07-02 17:05:00&amp;#39;) AS c
    GROUP BY pol_gid,
    usr_id HAVING COUNT(usr_id) &amp;gt; 20) AS real_visits
    GROUP BY pol_gid
    ORDER BY count_user_visit DESC;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;optimizations-in-the-example-query&#34;&gt;Optimizations in the example query&lt;/h2&gt;
&lt;p&gt;This query has the following optimizations:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The time predicated appears in the subquery.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Using the location data table avoids the need for an expensive join.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The query uses OVER (PARTITION BEST), to improve performance by partitioning the data.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The &lt;code&gt;user_points_in&lt;/code&gt; provides an estimate of the combined time spent intersecting with the artwork by all visitors.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

      </description>
    </item>
    
    <item>
      <title>Data-Analysis: Ensuring polygon validity before creating or refreshing an index</title>
      <link>/en/data-analysis/geospatial-analytics/working-with-spatial-objects-tables/spatial-joins-with-st-intersects-and-stv-intersect/ensuring-polygon-validity-before-creating-or-refreshing-an-index/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/data-analysis/geospatial-analytics/working-with-spatial-objects-tables/spatial-joins-with-st-intersects-and-stv-intersect/ensuring-polygon-validity-before-creating-or-refreshing-an-index/</guid>
      <description>
        
        
        &lt;p&gt;When OpenText™ Analytics Database creates or updates a spatial index it does not check polygon validity. To prevent getting invalid results when you query your spatial index, you should check the validity of your polygons prior to creating or updating your spatial index.&lt;/p&gt;
&lt;p&gt;The following example shows you how to check the validity of polygons.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Create a table and load spatial data.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; CREATE TABLE polygon_validity_test (gid INT, geom GEOMETRY);
CREATE TABLE
=&amp;gt; COPY polygon_validity_test (gid, gx FILLER LONG VARCHAR, geom AS St_GeomFromText(gx)) FROM STDIN;
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
&amp;gt;&amp;gt; 2|POLYGON((-31 74,8 70,8 50,-36 53,-31 74))
&amp;gt;&amp;gt; 3|POLYGON((-38 50,4 13,11 45,0 65,-38 50))
&amp;gt;&amp;gt; 4|POLYGON((-12 42,-12 42,27 48,14 26,-12 42))
&amp;gt;&amp;gt; 5|POLYGON((0 0,1 1,0 0,2 1,1 1,0 0))
&amp;gt;&amp;gt; 6|POLYGON((3 3,2 2,2 1,2 3,3 3))
&amp;gt;&amp;gt; \.
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Use ST_IsValid and STV_IsValidReason to find any invalid polygons.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;=&amp;gt; SELECT gid, ST_IsValid(geom), STV_IsValidReason(geom) FROM polygon_validity_test;
 gid | ST_IsValid |            STV_IsValidReason
-----+------------+------------------------------------------
   4 | t          |
   6 | f          | Self-intersection at or near POINT (2 1)
   2 | t          |
   3 | t          |
   5 | f          | Self-intersection at or near POINT (0 0)
(5 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Now that we have identifed the invalid polygons in our table, there are a couple different ways you can handle the invalid polygons when creating or refreshing a spatial index.&lt;/p&gt;
&lt;h2 id=&#34;filtering-invalid-polygons-using-a-where-clause&#34;&gt;Filtering invalid polygons using a WHERE clause&lt;/h2&gt;
&lt;p&gt;This method is slower than filtering before creating an index because it checks the validity of each polygon at execution time.&lt;/p&gt;
&lt;p&gt;The following example shows you how to exclude invalid polygons using a WHERE clause.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;```
=&amp;gt; SELECT STV_Create_Index(gid, geom USING PARAMETERS index = &#39;valid_polygons&#39;) OVER()
   FROM polygon_validity_test
   WHERE ST_IsValid(geom) = &#39;t&#39;;
```
&lt;/code&gt;&lt;/pre&gt;
&lt;h2 id=&#34;filtering-invalid-polygons-before-creating-or-refreshing-an-index&#34;&gt;Filtering invalid polygons before creating or refreshing an index&lt;/h2&gt;
&lt;p&gt;This method is faster than filtering using a WHERE clause because you incur the performance cost prior to building the index.&lt;/p&gt;
&lt;p&gt;The following example shows you how to exclude invalid polygons by creating a new table excluding invalid polygons.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;```
=&amp;gt; CREATE TABLE polygon_validity_clean AS
   SELECT *
   FROM polygon_validity_test
   WHERE ST_IsValid(geom) = &#39;t&#39;;
CREATE TABLE
=&amp;gt; SELECT STV_Create_Index(gid, geom USING PARAMETERS index = &#39;valid_polygons&#39;) OVER()
   FROM polygon_validity_clean;
```
&lt;/code&gt;&lt;/pre&gt;

      </description>
    </item>
    
    <item>
      <title>Data-Analysis: STV_Intersect: scalar function vs. transform function</title>
      <link>/en/data-analysis/geospatial-analytics/working-with-spatial-objects-tables/spatial-joins-with-st-intersects-and-stv-intersect/stv-intersect-scalar-function-vs-transform-function/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/data-analysis/geospatial-analytics/working-with-spatial-objects-tables/spatial-joins-with-st-intersects-and-stv-intersect/stv-intersect-scalar-function-vs-transform-function/</guid>
      <description>
        
        
        &lt;p&gt;The &lt;code&gt;STV_Intersect&lt;/code&gt; functions are similar in purpose, but you use them differently.

&lt;table class=&#34;table table-bordered&#34; &gt;



&lt;tr&gt; 

&lt;th &gt;
STV_Intersect&lt;br /&gt;Function Type&lt;/th&gt; 

&lt;th &gt;
Description&lt;/th&gt; 

&lt;th &gt;
Performance&lt;/th&gt;&lt;/tr&gt;

&lt;tr&gt; 

&lt;td &gt;


Scalar&lt;/td&gt; 

&lt;td &gt;


Matches a point to a polygon. If several polygons contain the point, this function returns a &lt;code&gt;gid&lt;/code&gt; value. The result is a polygon &lt;code&gt;gid&lt;/code&gt; or, if no polygon contains the point, the result is NULL.&lt;/td&gt; 

&lt;td &gt;


Eliminates points that do not intersect with any indexed polygons, avoiding unnecessary comparisons.&lt;/td&gt;&lt;/tr&gt;

&lt;tr&gt; 

&lt;td &gt;


Transform&lt;/td&gt; 

&lt;td &gt;


Matches a point to all the polygons that contain it. When a point does not intersect with any polygon in the index, the function returns no rows.&lt;/td&gt; 

&lt;td &gt;


Processes all input points regardless of whether or not they intersect with the indexed polygons.&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/p&gt;
&lt;p&gt;In the following example, the &lt;code&gt;STV_Intersect&lt;/code&gt; scalar function compares the points in the &lt;code&gt;points&lt;/code&gt; table to the polygons in a spatial index named &lt;code&gt;my_polygons&lt;/code&gt;. &lt;code&gt;STV_Intersect&lt;/code&gt; returns all points and polygons that match exactly:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;
=&amp;gt; SELECT gid AS pt_gid
   STV_Intersect(geom USING PARAMETERS index=&amp;#39;my_polygons&amp;#39;) AS pol_gid
   FROM points ORDER BY pt_gid;
 pt_gid | pol_gid
--------+---------
    100 |       2
    101 |
    102 |       2
    103 |
    104 |
    105 |       3
    106 |
    107 |
 (8 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The following example shows how to use the &lt;code&gt;STV_Intersect&lt;/code&gt; transform function to return information about the three point-polygon pairs that match and each of the polygons they match:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;
=&amp;gt; SELECT STV_Intersect(gid, geom
   USING PARAMETERS index=&amp;#39;my_polygons&amp;#39;)
   OVER (PARTITION BEST) AS (pt_gid, pol_id)
   FROM points;
 pt_gid | pol_id
--------+--------
    100 |      1
    100 |      2
    100 |      3
    102 |      2
    105 |      3
(3 rows)
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;see-also&#34;&gt;See also&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/sql-reference/functions/geospatial-functions/stv-intersect-scalar-function/#&#34;&gt;STV_Intersect scalar function&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href=&#34;../../../../../en/sql-reference/functions/geospatial-functions/stv-intersect-transform-function/#&#34;&gt;STV_Intersect transform function&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

      </description>
    </item>
    
    <item>
      <title>Data-Analysis: Performing spatial joins with STV_Intersect functions</title>
      <link>/en/data-analysis/geospatial-analytics/working-with-spatial-objects-tables/spatial-joins-with-st-intersects-and-stv-intersect/performing-spatial-joins-with-stv-intersect-functions/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/data-analysis/geospatial-analytics/working-with-spatial-objects-tables/spatial-joins-with-st-intersects-and-stv-intersect/performing-spatial-joins-with-stv-intersect-functions/</guid>
      <description>
        
        
        &lt;p&gt;Suppose you want to process a medium-to-large spatial data set and determine which points intersect with which polygons. In that case, first create a spatial index using &lt;code&gt;STV_Create_Index&lt;/code&gt;. A spatial index provides efficient access to the set of polygons.&lt;/p&gt;
&lt;p&gt;Then, use the &lt;code&gt;STV_Intersect&lt;/code&gt; scalar or transform function to identify which point-polygon pairs match.&lt;/p&gt;

      </description>
    </item>
    
    <item>
      <title>Data-Analysis: When to use ST_Intersects vs. STV_Intersect</title>
      <link>/en/data-analysis/geospatial-analytics/working-with-spatial-objects-tables/spatial-joins-with-st-intersects-and-stv-intersect/when-to-use-st-intersects-vs-stv-intersect/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/data-analysis/geospatial-analytics/working-with-spatial-objects-tables/spatial-joins-with-st-intersects-and-stv-intersect/when-to-use-st-intersects-vs-stv-intersect/</guid>
      <description>
        
        
        &lt;p&gt;OpenText™ Analytics Database provides two capabilities to identify whether a set of points intersect with a set of polygons. Depending on the size of your data set, choose the approach that gives the best performance:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;When comparing a set of geometries to a single geometry to see if they intersect, use the &lt;a href=&#34;../../../../../en/sql-reference/functions/geospatial-functions/st-intersects/#&#34;&gt;ST_Intersects&lt;/a&gt; function.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;To determine if a set of points intersects with a set of polygons in a medium-to-large data set, first create a spatial index using &lt;code&gt;STV_Create_Index&lt;/code&gt;. Then, use one of the &lt;code&gt;STV_Intersect&lt;/code&gt; functions to return the set of pairs that intersect.

&lt;div class=&#34;alert admonition note&#34; role=&#34;alert&#34;&gt;
&lt;h4 class=&#34;admonition-head&#34;&gt;Note&lt;/h4&gt;

You can only perform spatial joins on GEOMETRY data.

&lt;/div&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;

      </description>
    </item>
    
  </channel>
</rss>
