blob: fd8e37ee230c17885b4b1698da40bec84b3558bb [file] [log] [blame]
Ian Maxon858061a2022-05-12 19:11:28 -07001<!DOCTYPE html>
2<!--
3 | Generated by Apache Maven Doxia Site Renderer 1.8.1 from src/site/markdown/geo/quickstart.md at 2022-05-12
4 | Rendered using Apache Maven Fluido Skin 1.7
5-->
6<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
7 <head>
8 <meta charset="UTF-8" />
9 <meta name="viewport" content="width=device-width, initial-scale=1.0" />
10 <meta name="Date-Revision-yyyymmdd" content="20220512" />
11 <meta http-equiv="Content-Language" content="en" />
12 <title>AsterixDB &#x2013; Getting Started with GIS in AsterixDB (DRAFT)</title>
13 <link rel="stylesheet" href="../css/apache-maven-fluido-1.7.min.css" />
14 <link rel="stylesheet" href="../css/site.css" />
15 <link rel="stylesheet" href="../css/print.css" media="print" />
16 <script type="text/javascript" src="../js/apache-maven-fluido-1.7.min.js"></script>
17
18 </head>
19 <body class="topBarDisabled">
20 <div class="container-fluid">
21 <div id="banner">
22 <div class="pull-left"><a href=".././" id="bannerLeft"><img src="../images/asterixlogo.png" alt="AsterixDB"/></a></div>
23 <div class="pull-right"></div>
24 <div class="clear"><hr/></div>
25 </div>
26
27 <div id="breadcrumbs">
28 <ul class="breadcrumb">
29 <li id="publishDate">Last Published: 2022-05-12</li>
30 <li id="projectVersion" class="pull-right">Version: 0.9.8</li>
31 <li class="pull-right"><a href="../index.html" title="Documentation Home">Documentation Home</a></li>
32 </ul>
33 </div>
34 <div class="row-fluid">
35 <div id="leftColumn" class="span2">
36 <div class="well sidebar-nav">
37 <ul class="nav nav-list">
38 <li class="nav-header">Get Started - Installation</li>
39 <li><a href="../ncservice.html" title="Option 1: using NCService"><span class="none"></span>Option 1: using NCService</a></li>
40 <li><a href="../ansible.html" title="Option 2: using Ansible"><span class="none"></span>Option 2: using Ansible</a></li>
41 <li><a href="../aws.html" title="Option 3: using Amazon Web Services"><span class="none"></span>Option 3: using Amazon Web Services</a></li>
42 <li class="nav-header">AsterixDB Primer</li>
43 <li><a href="../sqlpp/primer-sqlpp.html" title="Using SQL++"><span class="none"></span>Using SQL++</a></li>
44 <li class="nav-header">Data Model</li>
45 <li><a href="../datamodel.html" title="The Asterix Data Model"><span class="none"></span>The Asterix Data Model</a></li>
46 <li class="nav-header">Queries</li>
47 <li><a href="../sqlpp/manual.html" title="The SQL++ Query Language"><span class="none"></span>The SQL++ Query Language</a></li>
48 <li><a href="../SQLPP.html" title="Raw SQL++ Grammar"><span class="none"></span>Raw SQL++ Grammar</a></li>
49 <li><a href="../sqlpp/builtins.html" title="Builtin Functions"><span class="none"></span>Builtin Functions</a></li>
50 <li class="nav-header">API/SDK</li>
51 <li><a href="../api.html" title="HTTP API"><span class="none"></span>HTTP API</a></li>
52 <li><a href="../csv.html" title="CSV Output"><span class="none"></span>CSV Output</a></li>
53 <li class="nav-header">Advanced Features</li>
54 <li><a href="../aql/externaldata.html" title="Accessing External Data"><span class="none"></span>Accessing External Data</a></li>
55 <li><a href="../feeds.html" title="Data Ingestion with Feeds"><span class="none"></span>Data Ingestion with Feeds</a></li>
56 <li><a href="../udf.html" title="User Defined Functions"><span class="none"></span>User Defined Functions</a></li>
57 <li><a href="../sqlpp/filters.html" title="Filter-Based LSM Index Acceleration"><span class="none"></span>Filter-Based LSM Index Acceleration</a></li>
58 <li><a href="../sqlpp/fulltext.html" title="Support of Full-text Queries"><span class="none"></span>Support of Full-text Queries</a></li>
59 <li><a href="../sqlpp/similarity.html" title="Support of Similarity Queries"><span class="none"></span>Support of Similarity Queries</a></li>
60 <li class="active"><a href="#"><span class="none"></span>GIS Support Overview</a></li>
61 <li><a href="../geo/functions.html" title="GIS Functions"><span class="none"></span>GIS Functions</a></li>
62 <li><a href="../interval_join.html" title="Support of Interval Joins"><span class="none"></span>Support of Interval Joins</a></li>
63 <li><a href="../spatial_join.html" title="Support of Spatial Joins"><span class="none"></span>Support of Spatial Joins</a></li>
64 <li><a href="../sqlpp/arrayindex.html" title="Support of Array Indexes"><span class="none"></span>Support of Array Indexes</a></li>
65 <li class="nav-header">Deprecated</li>
66 <li><a href="../aql/primer.html" title="AsterixDB Primer: Using AQL"><span class="none"></span>AsterixDB Primer: Using AQL</a></li>
67 <li><a href="../aql/manual.html" title="Queries: The Asterix Query Language (AQL)"><span class="none"></span>Queries: The Asterix Query Language (AQL)</a></li>
68 <li><a href="../aql/builtins.html" title="Queries: Builtin Functions (AQL)"><span class="none"></span>Queries: Builtin Functions (AQL)</a></li>
69</ul>
70 <hr />
71 <div id="poweredBy">
72 <div class="clear"></div>
73 <div class="clear"></div>
74 <div class="clear"></div>
75 <div class="clear"></div>
76<a href=".././" title="AsterixDB" class="builtBy"><img class="builtBy" alt="AsterixDB" src="../images/asterixlogo.png" /></a>
77 </div>
78 </div>
79 </div>
80 <div id="bodyColumn" class="span10" >
81<!--
82 ! Licensed to the Apache Software Foundation (ASF) under one
83 ! or more contributor license agreements. See the NOTICE file
84 ! distributed with this work for additional information
85 ! regarding copyright ownership. The ASF licenses this file
86 ! to you under the Apache License, Version 2.0 (the
87 ! "License"); you may not use this file except in compliance
88 ! with the License. You may obtain a copy of the License at
89 !
90 ! http://www.apache.org/licenses/LICENSE-2.0
91 !
92 ! Unless required by applicable law or agreed to in writing,
93 ! software distributed under the License is distributed on an
94 ! "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
95 ! KIND, either express or implied. See the License for the
96 ! specific language governing permissions and limitations
97 ! under the License.
98 !-->
99<h1>Getting Started with GIS in AsterixDB (DRAFT)</h1>
100<div class="section">
101<h2><a name="Table_of_Contents"></a><a name="toc" id="toc">Table of Contents</a></h2>
102<ul>
103
104<li><a href="#Introduction">Introduction</a></li>
105<li><a href="#create">Create a GIS data type</a></li>
106<li><a href="#update">Insert geometry data</a></li>
107<li><a href="#query">Query geometries</a></li>
108<li><a href="#query2">Spatial analysis function</a></li>
109<li><a href="#aggre">Spatial aggregate example</a></li>
110<li><a href="#range">Range query</a></li>
111<li><a href="#knn">K Nearest Neighbor (KNN) query</a></li>
112<li><a href="#joint">Spatial join query</a></li>
113</ul></div>
114<div class="section">
115<h2><a name="Introduction" id="Introduction">Introduction</a></h2>
116<p>This page provides a simple guide to the OGC-compliant geometry functionality in AsterixDB. Internally, AsterixDB relies on the open source library <a class="externalLink" href="https://github.com/Esri/geometry-api-java">Esri/geometry-api-java</a> that provides OGC-geometry feature processing. Currently, the AsterixDB geometry library supports <a class="externalLink" href="https://tools.ietf.org/html/rfc7946">GeoJSON</a>, <a class="externalLink" href="http://docs.opengeospatial.org/is/12-063r5/12-063r5.html">Well known Text</a> and <a class="externalLink" href="http://portal.opengeospatial.org/files/?artifact_id=25354">Well known Binary formats</a>. For a complete list of all the functions, please check the <a href="functions.md">AsterixDB GIS functions page</a>. Here are some detailed examples.</p></div>
117<div class="section">
118<h2><a name="Create_a_GIS_data_type"></a><a name="create" id="create">Create a GIS data type</a></h2>
119
120<div>
121<div>
122<pre class="source">DROP DATAVERSE GISTest IF EXISTS;
123CREATE DATAVERSE GISTest;
124USE GISTest;
125
126CREATE TYPE GeometryType AS{
127 id : int,
128 myGeometry : geometry
129};
130
131CREATE DATASET Geometries (GeometryType) PRIMARY KEY id;
132</pre></div></div>
133</div>
134<div class="section">
135<h2><a name="Insert_geometry_data"></a><a name="update" id="update">Insert geometry data</a></h2>
136
137<div>
138<div>
139<pre class="source">USE GISTest;
140
141INSERT INTO Geometries ([
142{&quot;id&quot;: 123, &quot;myGeometry&quot;: st_geom_from_geojson({&quot;type&quot;:&quot;Point&quot;,&quot;coordinates&quot;:[-118.4,33.93]})},
143{&quot;id&quot;: 124, &quot;myGeometry&quot;: st_geom_from_geojson({&quot;type&quot;:&quot;Polygon&quot;,&quot;coordinates&quot;:[[[8.7599721,49.7103028],[8.759997,49.7102752],[8.7600145,49.7102818],[8.7600762,49.7102133],[8.760178,49.7102516],[8.7600914,49.7103478],[8.7599721,49.7103028]]]})},
144{&quot;id&quot;: 126, &quot;myGeometry&quot;: st_geom_from_geojson({&quot;type&quot;:&quot;LineString&quot;,&quot;coordinates&quot;:[[-69.1991349,-12.6006222],[-69.199136,-12.599842],[-69.1982979,-12.5998268],[-69.1982598,-12.599869],[-69.1982188,-12.5998698],[-69.19817,-12.5998707],[-69.198125,-12.5998218],[-69.1973024,-12.5998133],[-69.1972972,-12.6003109],[-69.197394,-12.6003514],[-69.1973906,-12.6009231],[-69.1975115,-12.601026],[-69.1975081,-12.6010968]]})},
145{&quot;id&quot;: 127, &quot;myGeometry&quot;: st_geom_from_geojson({&quot;type&quot;: &quot;MultiPoint&quot;,&quot;coordinates&quot;: [[10, 40], [40, 30], [20, 20], [30, 10]]})},
146{&quot;id&quot;: 128, &quot;myGeometry&quot;: st_geom_from_geojson({&quot;type&quot;: &quot;MultiLineString&quot;,&quot;coordinates&quot;: [[[10, 10], [20, 20], [10, 40]],[[40, 40], [30, 30], [40, 20], [30, 10]]]})},
147{&quot;id&quot;: 129, &quot;myGeometry&quot;: st_geom_from_geojson({&quot;type&quot;: &quot;MultiPolygon&quot;,&quot;coordinates&quot;: [[[[40, 40], [20, 45], [45, 30], [40, 40]]],[[[20, 35], [10, 30], [10, 10], [30, 5], [45, 20], [20, 35]],[[30, 20], [20, 15], [20, 25], [30, 20]]]]})},
148{&quot;id&quot;: 130, &quot;myGeometry&quot;: st_make_point(-71.1043443253471, 42.3150676015829)},
149{&quot;id&quot;: 131, &quot;myGeometry&quot;: st_make_point(1.0,2.0,3.0)},
150{&quot;id&quot;: 132, &quot;myGeometry&quot;: st_make_point(1.0,2.0,3.0,4.0)},
151{&quot;id&quot;: 133, &quot;myGeometry&quot;: st_geom_from_text('POLYGON((743238 2967416,743238 2967450,743265 2967450,743265.625 2967416,743238 2967416))')},
152{&quot;id&quot;: 134, &quot;myGeometry&quot;: st_geom_from_wkb(hex(&quot;0102000000020000001F85EB51B87E5CC0D34D621058994340105839B4C87E5CC0295C8FC2F5984340&quot;))},
153{&quot;id&quot;: 135, &quot;myGeometry&quot;: st_line_from_multipoint(st_geom_from_text('MULTIPOINT(1 2 , 4 5 , 7 8 )'))},
154{&quot;id&quot;: 136, &quot;myGeometry&quot;: st_make_envelope(10, 10, 11, 11, 4326)},
155{&quot;id&quot;: 137, &quot;myGeometry&quot;: st_geom_from_text(&quot;POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10),(20 30, 35 35, 30 20, 20 30))&quot;)}
156]);
157</pre></div></div>
158</div>
159<div class="section">
160<h2><a name="Query_geometries"></a><a name="query" id="query">Query geometries</a></h2>
161
162<div>
163<div>
164<pre class="source">USE GISTest;
165
166FROM Geometries SELECT *;
167</pre></div></div>
168
169<p>result:</p>
170
171<div>
172<div>
173<pre class="source">{ &quot;Geometries&quot;: { &quot;id&quot;: 124, &quot;myGeometry&quot;: {&quot;type&quot;:&quot;Polygon&quot;,&quot;coordinates&quot;:[[[8.7599721,49.7103028],[8.759997,49.7102752],[8.7600145,49.7102818],[8.7600762,49.7102133],[8.760178,49.7102516],[8.7600914,49.7103478],[8.7599721,49.7103028]]],&quot;crs&quot;:{&quot;type&quot;:&quot;name&quot;,&quot;properties&quot;:{&quot;name&quot;:&quot;EPSG:4326&quot;}}} } }
174{ &quot;Geometries&quot;: { &quot;id&quot;: 126, &quot;myGeometry&quot;: {&quot;type&quot;:&quot;LineString&quot;,&quot;coordinates&quot;:[[-69.1991349,-12.6006222],[-69.199136,-12.599842],[-69.1982979,-12.5998268],[-69.1982598,-12.599869],[-69.1982188,-12.5998698],[-69.19817,-12.5998707],[-69.198125,-12.5998218],[-69.1973024,-12.5998133],[-69.1972972,-12.6003109],[-69.197394,-12.6003514],[-69.1973906,-12.6009231],[-69.1975115,-12.601026],[-69.1975081,-12.6010968]],&quot;crs&quot;:{&quot;type&quot;:&quot;name&quot;,&quot;properties&quot;:{&quot;name&quot;:&quot;EPSG:4326&quot;}}} } }
175{ &quot;Geometries&quot;: { &quot;id&quot;: 128, &quot;myGeometry&quot;: {&quot;type&quot;:&quot;MultiLineString&quot;,&quot;coordinates&quot;:[[[10,10],[20,20],[10,40]],[[40,40],[30,30],[40,20],[30,10]]],&quot;crs&quot;:null} } }
176{ &quot;Geometries&quot;: { &quot;id&quot;: 132, &quot;myGeometry&quot;: {&quot;type&quot;:&quot;Point&quot;,&quot;coordinates&quot;:[1,2,3,4],&quot;crs&quot;:{&quot;type&quot;:&quot;name&quot;,&quot;properties&quot;:{&quot;name&quot;:&quot;EPSG:4326&quot;}}} } }
177{ &quot;Geometries&quot;: { &quot;id&quot;: 133, &quot;myGeometry&quot;: {&quot;type&quot;:&quot;Polygon&quot;,&quot;coordinates&quot;:[[[743238,2967416],[743265.625,2967416],[743265,2967450],[743238,2967450],[743238,2967416]]],&quot;crs&quot;:{&quot;type&quot;:&quot;name&quot;,&quot;properties&quot;:{&quot;name&quot;:&quot;EPSG:4326&quot;}}} } }
178{ &quot;Geometries&quot;: { &quot;id&quot;: 134, &quot;myGeometry&quot;: {&quot;type&quot;:&quot;LineString&quot;,&quot;coordinates&quot;:[[-113.98,39.198],[-113.981,39.195]],&quot;crs&quot;:{&quot;type&quot;:&quot;name&quot;,&quot;properties&quot;:{&quot;name&quot;:&quot;EPSG:4326&quot;}}} } }
179{ &quot;Geometries&quot;: { &quot;id&quot;: 135, &quot;myGeometry&quot;: {&quot;type&quot;:&quot;LineString&quot;,&quot;coordinates&quot;:[[1,2],[4,5],[7,8]],&quot;crs&quot;:{&quot;type&quot;:&quot;name&quot;,&quot;properties&quot;:{&quot;name&quot;:&quot;EPSG:4326&quot;}}} } }
180{ &quot;Geometries&quot;: { &quot;id&quot;: 136, &quot;myGeometry&quot;: {&quot;type&quot;:&quot;Polygon&quot;,&quot;coordinates&quot;:[[[10,10],[11,10],[11,11],[10,11],[10,10]]],&quot;crs&quot;:{&quot;type&quot;:&quot;name&quot;,&quot;properties&quot;:{&quot;name&quot;:&quot;EPSG:4326&quot;}}} } }
181{ &quot;Geometries&quot;: { &quot;id&quot;: 123, &quot;myGeometry&quot;: {&quot;type&quot;:&quot;Point&quot;,&quot;coordinates&quot;:[-118.4,33.93],&quot;crs&quot;:{&quot;type&quot;:&quot;name&quot;,&quot;properties&quot;:{&quot;name&quot;:&quot;EPSG:4326&quot;}}} } }
182{ &quot;Geometries&quot;: { &quot;id&quot;: 127, &quot;myGeometry&quot;: {&quot;type&quot;:&quot;MultiPoint&quot;,&quot;coordinates&quot;:[[10,40],[40,30],[20,20],[30,10]],&quot;crs&quot;:{&quot;type&quot;:&quot;name&quot;,&quot;properties&quot;:{&quot;name&quot;:&quot;EPSG:4326&quot;}}} } }
183{ &quot;Geometries&quot;: { &quot;id&quot;: 129, &quot;myGeometry&quot;: {&quot;type&quot;:&quot;MultiPolygon&quot;,&quot;coordinates&quot;:[[[[40,40],[20,45],[45,30],[40,40]]],[[[20,35],[10,30],[10,10],[30,5],[45,20],[20,35]],[[30,20],[20,15],[20,25],[30,20]]]],&quot;crs&quot;:null} } }
184{ &quot;Geometries&quot;: { &quot;id&quot;: 130, &quot;myGeometry&quot;: {&quot;type&quot;:&quot;Point&quot;,&quot;coordinates&quot;:[-71.1043443253471,42.3150676015829],&quot;crs&quot;:{&quot;type&quot;:&quot;name&quot;,&quot;properties&quot;:{&quot;name&quot;:&quot;EPSG:4326&quot;}}} } }
185{ &quot;Geometries&quot;: { &quot;id&quot;: 131, &quot;myGeometry&quot;: {&quot;type&quot;:&quot;Point&quot;,&quot;coordinates&quot;:[1,2,3],&quot;crs&quot;:{&quot;type&quot;:&quot;name&quot;,&quot;properties&quot;:{&quot;name&quot;:&quot;EPSG:4326&quot;}}} } }
186{ &quot;Geometries&quot;: { &quot;id&quot;: 137, &quot;myGeometry&quot;: {&quot;type&quot;:&quot;Polygon&quot;,&quot;coordinates&quot;:[[[35,10],[45,45],[15,40],[10,20],[35,10]],[[20,30],[35,35],[30,20],[20,30]]],&quot;crs&quot;:{&quot;type&quot;:&quot;name&quot;,&quot;properties&quot;:{&quot;name&quot;:&quot;EPSG:4326&quot;}}} } }
187</pre></div></div>
188</div>
189<div class="section">
190<h2><a name="Spatial_analysis_functions"></a><a name="query2" id="query2">Spatial analysis functions</a></h2>
191<p>The following query filters out only the geometries of type &#x201c;Polygon&#x201d; and displays the geometry in the Well known text format along with the area of the relevant geometry.</p>
192
193<div>
194<div>
195<pre class="source">USE GISTest;
196
197FROM Geometries as geo
198WHERE geometry_type(geo.myGeometry)='Polygon'
199SELECT VALUE {&quot;Polygon&quot;:st_as_text(geo.myGeometry), &quot;Area&quot;:st_area(geo.myGeometry)};
200</pre></div></div>
201
202<p>result:</p>
203
204<div>
205<div>
206<pre class="source">{ &quot;Polygon&quot;: &quot;POLYGON ((8.7599721 49.7103028, 8.759997 49.7102752, 8.7600145 49.7102818, 8.7600762 49.7102133, 8.760178 49.7102516, 8.7600914 49.7103478, 8.7599721 49.7103028))&quot;, &quot;Area&quot;: 1.3755215000294761E-8 }
207{ &quot;Polygon&quot;: &quot;POLYGON ((743238 2967416, 743265.625 2967416, 743265 2967450, 743238 2967450, 743238 2967416))&quot;, &quot;Area&quot;: 928.625 }
208{ &quot;Polygon&quot;: &quot;POLYGON ((10 10, 11 10, 11 11, 10 11, 10 10))&quot;, &quot;Area&quot;: 1.0 }
209{ &quot;Polygon&quot;: &quot;POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10), (20 30, 35 35, 30 20, 20 30))&quot;, &quot;Area&quot;: 675.0 }
210</pre></div></div>
211</div>
212<div class="section">
213<h2><a name="Spatial_aggregate_example"></a><a name="query2" id="query2">Spatial aggregate example</a></h2>
214<p><tt>st_union</tt> function has been implemented both as a normal and an aggregate function. The following query shows how to query the aggregate version of this function:</p>
215
216<div>
217<div>
218<pre class="source">USE GISTest;
219
220st_union((SELECT VALUE gbu.myGeometry FROM Geometries as gbu));
221</pre></div></div>
222
223<p>result:</p>
224
225<div>
226<div>
227<pre class="source">{&quot;type&quot;:&quot;MultiPolygon&quot;,&quot;coordinates&quot;:[[[[10,10],[30,5],[35,10],[45,20],[38.90243902439025,23.65853658536585],[41.34146341463415,32.19512195121951],[45,30],[42.27272727272727,35.45454545454545],[45,45],[30,42.5],[20,45],[25.434782608695656,41.73913043478261],[15,40],[12.857142857142858,31.428571428571427],[10,30],[10,20],[10,11],[10,10]],[[32.5,27.5],[25.357142857142858,31.785714285714285],[35,35],[32.5,27.5]],[[20,15],[20,16],[21.11111111111111,15.555555555555555],[20,15]]],[[[8.7600762,49.7102133],[8.760178,49.7102516],[8.7600914,49.7103478],[8.7599721,49.7103028],[8.759997,49.7102752],[8.7600145,49.7102818],[8.7600762,49.7102133]]],[[[743238,2967416],[743265.625,2967416],[743265,2967450],[743238,2967450],[743238,2967416]]]],&quot;crs&quot;:null}
228</pre></div></div>
229</div>
230<div class="section">
231<h2><a name="Range_query"></a><a name="range" id="range">Range query</a></h2>
232
233<div>
234<div>
235<pre class="source">USE GISTest;
236
237FROM Geometries geo
238WHERE st_intersects(geo.myGeometry, st_geom_from_text(&quot;POLYGON((1 1,5 1,5 5,1 5,1 1),(2 2, 3 2, 3 3, 2 3,2 2))&quot;))
239SELECT VALUE geo.myGeometry;
240</pre></div></div>
241
242<p>result:</p>
243
244<div>
245<div>
246<pre class="source">{&quot;type&quot;:&quot;Point&quot;,&quot;coordinates&quot;:[1,2,3,4],&quot;crs&quot;:{&quot;type&quot;:&quot;name&quot;,&quot;properties&quot;:{&quot;name&quot;:&quot;EPSG:4326&quot;}}}
247{&quot;type&quot;:&quot;LineString&quot;,&quot;coordinates&quot;:[[1,2],[4,5],[7,8]],&quot;crs&quot;:{&quot;type&quot;:&quot;name&quot;,&quot;properties&quot;:{&quot;name&quot;:&quot;EPSG:4326&quot;}}}
248{&quot;type&quot;:&quot;Point&quot;,&quot;coordinates&quot;:[1,2,3],&quot;crs&quot;:{&quot;type&quot;:&quot;name&quot;,&quot;properties&quot;:{&quot;name&quot;:&quot;EPSG:4326&quot;}}}
249</pre></div></div>
250</div>
251<div class="section">
252<h2><a name="K_Nearest_Neighbor_.28KNN.29_query"></a><a name="knn" id="knn">K Nearest Neighbor (KNN) query</a></h2>
253
254<div>
255<div>
256<pre class="source">USE GISTest;
257
258FROM Geometries geo
259SELECT VALUE geo.myGeometry
260ORDER BY st_distance(geo.myGeometry, st_make_point(1,2))
261LIMIT 5;
262</pre></div></div>
263
264<p>result:</p>
265
266<div>
267<div>
268<pre class="source">{&quot;type&quot;:&quot;Point&quot;,&quot;coordinates&quot;:[1,2,3,4],&quot;crs&quot;:{&quot;type&quot;:&quot;name&quot;,&quot;properties&quot;:{&quot;name&quot;:&quot;EPSG:4326&quot;}}}
269{&quot;type&quot;:&quot;LineString&quot;,&quot;coordinates&quot;:[[1,2],[4,5],[7,8]],&quot;crs&quot;:{&quot;type&quot;:&quot;name&quot;,&quot;properties&quot;:{&quot;name&quot;:&quot;EPSG:4326&quot;}}}
270{&quot;type&quot;:&quot;Point&quot;,&quot;coordinates&quot;:[1,2,3],&quot;crs&quot;:{&quot;type&quot;:&quot;name&quot;,&quot;properties&quot;:{&quot;name&quot;:&quot;EPSG:4326&quot;}}}
271{&quot;type&quot;:&quot;Polygon&quot;,&quot;coordinates&quot;:[[[10,10],[11,10],[11,11],[10,11],[10,10]]],&quot;crs&quot;:{&quot;type&quot;:&quot;name&quot;,&quot;properties&quot;:{&quot;name&quot;:&quot;EPSG:4326&quot;}}}
272{&quot;type&quot;:&quot;MultiLineString&quot;,&quot;coordinates&quot;:[[[10,10],[20,20],[10,40]],[[40,40],[30,30],[40,20],[30,10]]],&quot;crs&quot;:null}
273</pre></div></div>
274</div>
275<div class="section">
276<h2><a name="Spatial_join_query"></a><a name="joint" id="joint">Spatial join query</a></h2>
277<p>For the spatial join query let us create a new dataverse and two new data types:</p>
278
279<div>
280<div>
281<pre class="source">DROP DATAVERSE SJTest IF EXISTS;
282CREATE DATAVERSE SJTest;
283USE SJTest;
284
285CREATE TYPE StateType AS{
286id : int,
287name: string,
288boundary : geometry
289};
290
291CREATE DATASET States (StateType) PRIMARY KEY id;
292
293CREATE TYPE POIType AS {
294id : int,
295longitude : double,
296latitude : double
297};
298
299CREATE DATASET POIS (POIType) PRIMARY KEY id;
300</pre></div></div>
301
302<p>Insert data into states:</p>
303
304<div>
305<div>
306<pre class="source">USE SJTest;
307
308INSERT INTO States ([
309 {&quot;id&quot;: 1, &quot;name&quot;: &quot;Nebraska&quot;, &quot;boundary&quot;: st_geom_from_text(&quot;POLYGON ((-104.05341854507101 41.1705389679833, -104.053028 43.000586999999996, -98.49855 42.99856, -98.01304599999999 42.762299, -97.306677 42.867604, -96.38600699999999 42.474495, -96.06487899999999 41.79623, -96.09200799999999 41.53391, -95.87468899999999 41.307097, -95.88534899999999 40.721092999999996, -95.30829 39.999998, -102.051744 40.003077999999995, -102.051614 41.002376999999996, -104.053249 41.001405999999996, -104.05341854507101 41.1705389679833))&quot;) },
310 {&quot;id&quot;: 2, &quot;name&quot;: &quot;Washington&quot;, &quot;boundary&quot;: st_geom_from_text(&quot;MULTIPOLYGON (((-124.732755385025 48.165328947686795, -124.676262 48.391371, -123.981032 48.164761, -123.10189199999999 48.184951999999996, -122.871992 47.993493, -122.75413 48.1447, -122.610341 47.887343, -122.784553 47.686561, -122.864651 47.804669, -123.157948 47.356235999999996, -122.874586 47.413874, -123.119681 47.385532, -122.525329 47.912335999999996, -122.54636949132416 47.317877648507704, -122.324833 47.348521, -122.43694099999999 47.661719, -122.218982 48.020275999999996, -122.383911 48.227486, -122.47892813788141 48.175746487177165, -122.388048 48.30083, -122.57760827271139 48.38291646865838, -122.505828 48.297677, -122.732358 48.226144, -122.3773 47.905941, -122.769939 48.227548, -122.60660653630984 48.395473767832804, -122.674158 48.424726, -122.425271 48.599522, -122.535803 48.776128, -122.673472 48.733081999999996, -122.75802 49.002356999999996, -117.032351 48.999188, -117.062748 46.353623999999996, -116.915989 45.995413, -118.987129 45.999855, -121.145534 45.607886, -121.533106 45.726541, -122.266701 45.543841, -122.67500799999999 45.618038999999996, -123.004233 46.133823, -124.07776799999999 46.272324, -124.06905 46.647258, -123.953699 46.378845, -123.829356 46.713356, -124.092176 46.741623999999995, -124.138225 46.905533999999996, -123.83890000000001 46.953950999999996, -124.122057 47.04165, -124.173877 46.927234999999996, -124.425195 47.738434, -124.732755385025 48.165328947686795), (-122.56199279209496 47.29381043649037, -122.683943 47.365154999999994, -122.76539771783851 47.18116187703539, -122.678476 47.102742, -122.56199279209496 47.29381043649037), (-122.77734484602688 47.19194045282469, -122.82666 47.405806999999996, -122.871472 47.276861, -122.77734484602688 47.19194045282469)), ((-122.4789801236288 48.17567493623048, -122.358963 48.054851, -122.510562 48.132207, -122.4789801236288 48.17567493623048)), ((-122.526031 47.358906, -122.457246 47.505848, -122.373627 47.388718, -122.526031 47.358906)))&quot;) },
311 {&quot;id&quot;: 3, &quot;name&quot;: &quot;New Mexico&quot;, &quot;boundary&quot;: st_geom_from_text(&quot;POLYGON ((-109.050173 31.480003999999997, -109.045223 36.999083999999996, -103.002199 37.000104, -103.064423 32.000518, -106.618486 32.000495, -106.528242 31.783147999999997, -108.208394 31.783599, -108.208573 31.333395, -109.050044 31.332501999999998, -109.050173 31.480003999999997))&quot;) },
312 {&quot;id&quot;: 4, &quot;name&quot;: &quot;South Dakota&quot;, &quot;boundary&quot;: st_geom_from_text(&quot;POLYGON ((-104.057698 44.997431, -104.045443 45.94531, -96.563672 45.935238999999996, -96.857751 45.605962, -96.45306699999999 45.298114999999996, -96.45326 43.500389999999996, -96.60285999999999 43.450907, -96.436589 43.120841999999996, -96.639704 42.737071, -96.44550799999999 42.490629999999996, -97.23786799999999 42.853139, -98.035034 42.764205, -98.49855 42.99856, -104.053028 43.000586999999996, -104.057698 44.997431))&quot;) }
313 ]);
314</pre></div></div>
315
316<p>Insert data into POIS:</p>
317
318<div>
319<div>
320<pre class="source">USE SJTest;
321
322INSERT INTO POIS ([{&quot;id&quot;: 477884092592037888, &quot;latitude&quot;: 41.1029498, &quot;longitude&quot;: -96.2632202 },
323 {&quot;id&quot;: 477689754977181696, &quot;latitude&quot;: 47.23433434, &quot;longitude&quot;: -122.15083003 },
324 {&quot;id&quot;: 477697263058157569, &quot;latitude&quot;: 35.27988499, &quot;longitude&quot;: -106.6787443 },
325 {&quot;id&quot;: 477833117374611456, &quot;latitude&quot;: 44.11614436, &quot;longitude&quot;: -103.06577797 },
326 {&quot;id&quot;: 477957785909735424, &quot;latitude&quot;: 39.81871193, &quot;longitude&quot;: -75.53023171 },
327 {&quot;id&quot;: 477890178640384001, &quot;latitude&quot;: 37.5688636, &quot;longitude&quot;: -77.4540628 },
328 {&quot;id&quot;: 478004308827717632, &quot;latitude&quot;: 39.14933024, &quot;longitude&quot;: -84.43623134 },
329 {&quot;id&quot;: 478029048799846401, &quot;latitude&quot;: 40.3030824, &quot;longitude&quot;: -121.228368 }
330 ]);
331</pre></div></div>
332
333<p>Now let us perform the spatial join query:</p>
334
335<div>
336<div>
337<pre class="source">USE SJTest;
338
339FROM States, POIS
340WHERE st_contains(States.boundary, st_make_point(POIS.longitude, POIS.latitude))
341SELECT States.name, POIS.id;
342</pre></div></div>
343
344<p>result:</p>
345
346<div>
347<div>
348<pre class="source">{ &quot;name&quot;: &quot;Nebraska&quot;, &quot;id&quot;: 477884092592037888 }
349{ &quot;name&quot;: &quot;Washington&quot;, &quot;id&quot;: 477689754977181696 }
350{ &quot;name&quot;: &quot;South Dakota&quot;, &quot;id&quot;: 477833117374611456 }
351{ &quot;name&quot;: &quot;New Mexico&quot;, &quot;id&quot;: 477697263058157569 }
352</pre></div></div>
353
354<p>You can find a more comprehensive spatial join example <a href="../../resources/data/SJ.sqlpp">here</a>.</p></div>
355 </div>
356 </div>
357 </div>
358 <hr/>
359 <footer>
360 <div class="container-fluid">
361 <div class="row-fluid">
362<div class="row-fluid">Apache AsterixDB, AsterixDB, Apache, the Apache
363 feather logo, and the Apache AsterixDB project logo are either
364 registered trademarks or trademarks of The Apache Software
365 Foundation in the United States and other countries.
366 All other marks mentioned may be trademarks or registered
367 trademarks of their respective owners.
368 </div>
369 </div>
370 </div>
371 </footer>
372 </body>
373</html>