Ian Maxon | 858061a | 2022-05-12 19:11:28 -0700 | [diff] [blame^] | 1 | <!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 – 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; |
| 123 | CREATE DATAVERSE GISTest; |
| 124 | USE GISTest; |
| 125 | |
| 126 | CREATE TYPE GeometryType AS{ |
| 127 | id : int, |
| 128 | myGeometry : geometry |
| 129 | }; |
| 130 | |
| 131 | CREATE 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 | |
| 141 | INSERT INTO Geometries ([ |
| 142 | {"id": 123, "myGeometry": st_geom_from_geojson({"type":"Point","coordinates":[-118.4,33.93]})}, |
| 143 | {"id": 124, "myGeometry": st_geom_from_geojson({"type":"Polygon","coordinates":[[[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 | {"id": 126, "myGeometry": st_geom_from_geojson({"type":"LineString","coordinates":[[-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 | {"id": 127, "myGeometry": st_geom_from_geojson({"type": "MultiPoint","coordinates": [[10, 40], [40, 30], [20, 20], [30, 10]]})}, |
| 146 | {"id": 128, "myGeometry": st_geom_from_geojson({"type": "MultiLineString","coordinates": [[[10, 10], [20, 20], [10, 40]],[[40, 40], [30, 30], [40, 20], [30, 10]]]})}, |
| 147 | {"id": 129, "myGeometry": st_geom_from_geojson({"type": "MultiPolygon","coordinates": [[[[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 | {"id": 130, "myGeometry": st_make_point(-71.1043443253471, 42.3150676015829)}, |
| 149 | {"id": 131, "myGeometry": st_make_point(1.0,2.0,3.0)}, |
| 150 | {"id": 132, "myGeometry": st_make_point(1.0,2.0,3.0,4.0)}, |
| 151 | {"id": 133, "myGeometry": st_geom_from_text('POLYGON((743238 2967416,743238 2967450,743265 2967450,743265.625 2967416,743238 2967416))')}, |
| 152 | {"id": 134, "myGeometry": st_geom_from_wkb(hex("0102000000020000001F85EB51B87E5CC0D34D621058994340105839B4C87E5CC0295C8FC2F5984340"))}, |
| 153 | {"id": 135, "myGeometry": st_line_from_multipoint(st_geom_from_text('MULTIPOINT(1 2 , 4 5 , 7 8 )'))}, |
| 154 | {"id": 136, "myGeometry": st_make_envelope(10, 10, 11, 11, 4326)}, |
| 155 | {"id": 137, "myGeometry": st_geom_from_text("POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10),(20 30, 35 35, 30 20, 20 30))")} |
| 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 | |
| 166 | FROM Geometries SELECT *; |
| 167 | </pre></div></div> |
| 168 | |
| 169 | <p>result:</p> |
| 170 | |
| 171 | <div> |
| 172 | <div> |
| 173 | <pre class="source">{ "Geometries": { "id": 124, "myGeometry": {"type":"Polygon","coordinates":[[[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]]],"crs":{"type":"name","properties":{"name":"EPSG:4326"}}} } } |
| 174 | { "Geometries": { "id": 126, "myGeometry": {"type":"LineString","coordinates":[[-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]],"crs":{"type":"name","properties":{"name":"EPSG:4326"}}} } } |
| 175 | { "Geometries": { "id": 128, "myGeometry": {"type":"MultiLineString","coordinates":[[[10,10],[20,20],[10,40]],[[40,40],[30,30],[40,20],[30,10]]],"crs":null} } } |
| 176 | { "Geometries": { "id": 132, "myGeometry": {"type":"Point","coordinates":[1,2,3,4],"crs":{"type":"name","properties":{"name":"EPSG:4326"}}} } } |
| 177 | { "Geometries": { "id": 133, "myGeometry": {"type":"Polygon","coordinates":[[[743238,2967416],[743265.625,2967416],[743265,2967450],[743238,2967450],[743238,2967416]]],"crs":{"type":"name","properties":{"name":"EPSG:4326"}}} } } |
| 178 | { "Geometries": { "id": 134, "myGeometry": {"type":"LineString","coordinates":[[-113.98,39.198],[-113.981,39.195]],"crs":{"type":"name","properties":{"name":"EPSG:4326"}}} } } |
| 179 | { "Geometries": { "id": 135, "myGeometry": {"type":"LineString","coordinates":[[1,2],[4,5],[7,8]],"crs":{"type":"name","properties":{"name":"EPSG:4326"}}} } } |
| 180 | { "Geometries": { "id": 136, "myGeometry": {"type":"Polygon","coordinates":[[[10,10],[11,10],[11,11],[10,11],[10,10]]],"crs":{"type":"name","properties":{"name":"EPSG:4326"}}} } } |
| 181 | { "Geometries": { "id": 123, "myGeometry": {"type":"Point","coordinates":[-118.4,33.93],"crs":{"type":"name","properties":{"name":"EPSG:4326"}}} } } |
| 182 | { "Geometries": { "id": 127, "myGeometry": {"type":"MultiPoint","coordinates":[[10,40],[40,30],[20,20],[30,10]],"crs":{"type":"name","properties":{"name":"EPSG:4326"}}} } } |
| 183 | { "Geometries": { "id": 129, "myGeometry": {"type":"MultiPolygon","coordinates":[[[[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]]]],"crs":null} } } |
| 184 | { "Geometries": { "id": 130, "myGeometry": {"type":"Point","coordinates":[-71.1043443253471,42.3150676015829],"crs":{"type":"name","properties":{"name":"EPSG:4326"}}} } } |
| 185 | { "Geometries": { "id": 131, "myGeometry": {"type":"Point","coordinates":[1,2,3],"crs":{"type":"name","properties":{"name":"EPSG:4326"}}} } } |
| 186 | { "Geometries": { "id": 137, "myGeometry": {"type":"Polygon","coordinates":[[[35,10],[45,45],[15,40],[10,20],[35,10]],[[20,30],[35,35],[30,20],[20,30]]],"crs":{"type":"name","properties":{"name":"EPSG:4326"}}} } } |
| 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 “Polygon” 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 | |
| 197 | FROM Geometries as geo |
| 198 | WHERE geometry_type(geo.myGeometry)='Polygon' |
| 199 | SELECT VALUE {"Polygon":st_as_text(geo.myGeometry), "Area":st_area(geo.myGeometry)}; |
| 200 | </pre></div></div> |
| 201 | |
| 202 | <p>result:</p> |
| 203 | |
| 204 | <div> |
| 205 | <div> |
| 206 | <pre class="source">{ "Polygon": "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))", "Area": 1.3755215000294761E-8 } |
| 207 | { "Polygon": "POLYGON ((743238 2967416, 743265.625 2967416, 743265 2967450, 743238 2967450, 743238 2967416))", "Area": 928.625 } |
| 208 | { "Polygon": "POLYGON ((10 10, 11 10, 11 11, 10 11, 10 10))", "Area": 1.0 } |
| 209 | { "Polygon": "POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10), (20 30, 35 35, 30 20, 20 30))", "Area": 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 | |
| 220 | st_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">{"type":"MultiPolygon","coordinates":[[[[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]]]],"crs":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 | |
| 237 | FROM Geometries geo |
| 238 | WHERE st_intersects(geo.myGeometry, st_geom_from_text("POLYGON((1 1,5 1,5 5,1 5,1 1),(2 2, 3 2, 3 3, 2 3,2 2))")) |
| 239 | SELECT VALUE geo.myGeometry; |
| 240 | </pre></div></div> |
| 241 | |
| 242 | <p>result:</p> |
| 243 | |
| 244 | <div> |
| 245 | <div> |
| 246 | <pre class="source">{"type":"Point","coordinates":[1,2,3,4],"crs":{"type":"name","properties":{"name":"EPSG:4326"}}} |
| 247 | {"type":"LineString","coordinates":[[1,2],[4,5],[7,8]],"crs":{"type":"name","properties":{"name":"EPSG:4326"}}} |
| 248 | {"type":"Point","coordinates":[1,2,3],"crs":{"type":"name","properties":{"name":"EPSG:4326"}}} |
| 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 | |
| 258 | FROM Geometries geo |
| 259 | SELECT VALUE geo.myGeometry |
| 260 | ORDER BY st_distance(geo.myGeometry, st_make_point(1,2)) |
| 261 | LIMIT 5; |
| 262 | </pre></div></div> |
| 263 | |
| 264 | <p>result:</p> |
| 265 | |
| 266 | <div> |
| 267 | <div> |
| 268 | <pre class="source">{"type":"Point","coordinates":[1,2,3,4],"crs":{"type":"name","properties":{"name":"EPSG:4326"}}} |
| 269 | {"type":"LineString","coordinates":[[1,2],[4,5],[7,8]],"crs":{"type":"name","properties":{"name":"EPSG:4326"}}} |
| 270 | {"type":"Point","coordinates":[1,2,3],"crs":{"type":"name","properties":{"name":"EPSG:4326"}}} |
| 271 | {"type":"Polygon","coordinates":[[[10,10],[11,10],[11,11],[10,11],[10,10]]],"crs":{"type":"name","properties":{"name":"EPSG:4326"}}} |
| 272 | {"type":"MultiLineString","coordinates":[[[10,10],[20,20],[10,40]],[[40,40],[30,30],[40,20],[30,10]]],"crs":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; |
| 282 | CREATE DATAVERSE SJTest; |
| 283 | USE SJTest; |
| 284 | |
| 285 | CREATE TYPE StateType AS{ |
| 286 | id : int, |
| 287 | name: string, |
| 288 | boundary : geometry |
| 289 | }; |
| 290 | |
| 291 | CREATE DATASET States (StateType) PRIMARY KEY id; |
| 292 | |
| 293 | CREATE TYPE POIType AS { |
| 294 | id : int, |
| 295 | longitude : double, |
| 296 | latitude : double |
| 297 | }; |
| 298 | |
| 299 | CREATE 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 | |
| 308 | INSERT INTO States ([ |
| 309 | {"id": 1, "name": "Nebraska", "boundary": st_geom_from_text("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))") }, |
| 310 | {"id": 2, "name": "Washington", "boundary": st_geom_from_text("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)))") }, |
| 311 | {"id": 3, "name": "New Mexico", "boundary": st_geom_from_text("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))") }, |
| 312 | {"id": 4, "name": "South Dakota", "boundary": st_geom_from_text("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))") } |
| 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 | |
| 322 | INSERT INTO POIS ([{"id": 477884092592037888, "latitude": 41.1029498, "longitude": -96.2632202 }, |
| 323 | {"id": 477689754977181696, "latitude": 47.23433434, "longitude": -122.15083003 }, |
| 324 | {"id": 477697263058157569, "latitude": 35.27988499, "longitude": -106.6787443 }, |
| 325 | {"id": 477833117374611456, "latitude": 44.11614436, "longitude": -103.06577797 }, |
| 326 | {"id": 477957785909735424, "latitude": 39.81871193, "longitude": -75.53023171 }, |
| 327 | {"id": 477890178640384001, "latitude": 37.5688636, "longitude": -77.4540628 }, |
| 328 | {"id": 478004308827717632, "latitude": 39.14933024, "longitude": -84.43623134 }, |
| 329 | {"id": 478029048799846401, "latitude": 40.3030824, "longitude": -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 | |
| 339 | FROM States, POIS |
| 340 | WHERE st_contains(States.boundary, st_make_point(POIS.longitude, POIS.latitude)) |
| 341 | SELECT States.name, POIS.id; |
| 342 | </pre></div></div> |
| 343 | |
| 344 | <p>result:</p> |
| 345 | |
| 346 | <div> |
| 347 | <div> |
| 348 | <pre class="source">{ "name": "Nebraska", "id": 477884092592037888 } |
| 349 | { "name": "Washington", "id": 477689754977181696 } |
| 350 | { "name": "South Dakota", "id": 477833117374611456 } |
| 351 | { "name": "New Mexico", "id": 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> |