pgLatLon

view README.html @ 0:3b70e93cc07d

Version 0.1 (initial commit)
author jbe
date Sun Aug 21 17:43:48 2016 +0200 (2016-08-21)
parents
children 4f07a22f4d45
line source
1 <html><head><title>pgLatLon v0.1 documentation</title></head><body>
2 <h1>pgLatLon v0.1 documentation</h1>
4 <p>pgLatLon is a spatial database extension for the PostgreSQL object-relational
5 database management system providing geographic data types and spatial indexing
6 for the WGS-84 spheroid.</p>
8 <p>While many other spatial databases still use imprecise bounding boxes for many
9 operations, pgLatLon supports more precise geometric calculations for all
10 implemented operators. Efficient indexing of geometric objects is provided
11 using fractal indices. Optimizations on bit level (including logarithmic
12 compression) allow for a highly memory-efficient non-overlapping index suitable
13 for huge datasets.</p>
15 <p>Unlike competing spatial extensions for PostgreSQL, pgLatLon is available under
16 the permissive MIT/X11 license to avoid problems with viral licenses like the
17 GPLv2/v3.</p>
19 <h2>Installation</h2>
21 <h3>Automatic installation</h3>
23 <p>Prerequisites:</p>
25 <ul>
26 <li>Ensure that the <code>pg_config</code> binary is in your path (shipped with PostgreSQL).</li>
27 <li>Ensure that GNU Make is available (either as <code>make</code> or <code>gmake</code>).</li>
28 </ul>
30 <p>Then simply type:</p>
32 <pre><code>make install
33 </code></pre>
35 <h3>Manual installation</h3>
37 <p>It is also possible to compile and install the extension without GNU Make as
38 follows:</p>
40 <pre><code>cc -Wall -O2 -fPIC -shared -I `pg_config --includedir-server` -o latlon-v0001.so latlon-v0001.c
41 cp latlon-v0001.so `pg_config --pkglibdir`
42 cp latlon.control `pg_config --sharedir`/extension/
43 cp latlon--0.1.sql `pg_config --sharedir`/extension/
44 </code></pre>
46 <h3>Loading the extension</h3>
48 <p>After installation, you can create a database and load the extension as
49 follows:</p>
51 <pre><code>% createdb test_database
52 % psql test_database
53 psql (9.5.4)
54 Type "help" for help.
56 test_database=# CREATE EXTENSION latlon;
57 </code></pre>
59 <h2>Reference</h2>
61 <h3>1. Types</h3>
63 <p>pgLatLon provides four geographic types: <code>epoint</code>, <code>ebox</code>, <code>ecircle</code>, and
64 <code>ecluster</code>.</p>
66 <h4><code>epoint</code></h4>
68 <p>A point on the earth spheroid (WGS-84).</p>
70 <p>The text input format is <code>'[N|S]&lt;float&gt; [E|W]&lt;float&gt;'</code>, where each float is in
71 degrees. Note the required white space between the latitude and longitude
72 components. Each floating point number may have a sign, in which case <code>N</code>/<code>S</code>
73 or <code>E</code>/<code>W</code> are switched respectively (e.g. <code>E-5</code> is the same as <code>W5</code>).</p>
75 <p>An <code>epoint</code> may also be created from two floating point numbers by calling
76 <code>epoint(latitude, longitude)</code>, where positive latitudes are used for the
77 northern hemisphere, negative latitudes are used for the southern hemisphere,
78 positive longitudes indicate positions east of the prime meridian, and negative
79 longitudes indicate positions west of the prime meridian.</p>
81 <p>Latitudes exceeding -90 or +90 degrees are truncated to -90 or +90
82 respectively, in which case a warning will be issued. Longitudes exceeding -180
83 or +180 degrees will be converted to values between -180 and +180 (both
84 inclusive) by adding or substracting a multiple of 360 degrees, in which case a
85 notice will be issued.</p>
87 <p>If the latitude is -90 or +90 (south pole or north pole), a longitude value is
88 still stored in the datum, and if a point is on the prime meridian or the
89 180th meridian, the east/west bit is also stored in the datum. In case of the
90 prime meridian, this is done by storing a floating point value of -0 for
91 0 degrees west and a value of +0 for 0 degrees east. In case of the
92 180th meridian, this is done by storing -180 or +180 respectively. The equality
93 operator, however, returns true when the same points on earth are described,
94 i.e. the longitude is ignored for the poles, and 180 degrees west is considered
95 to be equal to 180 degrees east.</p>
97 <h4><code>ebox</code></h4>
99 <p>An area on earth demarcated by a southern and northern latitude, and a western
100 and eastern longitude (all given in WGS-84).</p>
102 <p>The text input format is
103 <code>'{N|S}&lt;float&gt; {E|W}&lt;float&gt; {N|S}&lt;float&gt; {E|W}&lt;float&gt;'</code>, where each float is in
104 degrees. The ordering of the four white-space separated blocks is not
105 significant. To include the 180th meridian, one longitude boundary must be
106 equal to or exceed <code>W180</code> or <code>E180</code>, e.g. <code>'N10 N20 E170 E190'</code>.</p>
108 <p>A special value is the empty area, denoted by the text represenation <code>'empty'</code>.
109 Such an <code>ebox</code> does not contain any point.</p>
111 <p>An <code>ebox</code> may also be created from four floating point numbers by calling
112 <code>ebox(min_latitude, max_latitude, min_longitude, max_longitude)</code>, where
113 positive values are used for north and east, and negative values are used for
114 south and west. If <code>min_latitude</code> is strictly greater than <code>max_latitude</code>, an
115 empty <code>ebox</code> is created. If <code>min_longitude</code> is greater than <code>max_longitude</code> and
116 if both longitudes are between -180 and +180 degrees, then the area oriented in
117 such way that the 180th meridian is included.</p>
119 <p>If the longitude span is less than 120 degrees, an <code>ebox</code> may be alternatively
120 created from two <code>epoints</code> in the following way: <code>ebox(epoint(lat1, lon1),
121 epoint(lat2, lon2))</code>. In this case <code>lat1</code> and <code>lat2</code> as well as <code>lon1</code> and
122 <code>lon2</code> can be swapped without any impact.</p>
124 <h4><code>ecircle</code></h4>
126 <p>An area containing all points not farther away from a given center point
127 (WGS-84) than a given radius.</p>
129 <p>The text input format is <code>'{N|S}&lt;float&gt; {E|W}&lt;float&gt; &lt;float&gt;'</code>, where the first
130 two floats denote the center point in degrees and the third float denotes the
131 radius in meters. A radius equal to minus infinity denotes an empty circle
132 which contains no point at all (despite having a center), while a radius equal
133 to zero denotes a circle that includes a single point.</p>
135 <p>An <code>ecircle</code> may also be created by calling <code>ecircle(epoint(...), radius)</code> or
136 from three floating point numbers by calling <code>ecircle(latitude, longitude,
137 radius)</code>.</p>
139 <h4><code>ecluster</code></h4>
141 <p>A collection of points, paths, polygons, and outlines on the WGS-84 spheroid.
142 Each path, polygon, or outline must cover a longitude range of less than
143 180 degrees to avoid ambiguities.</p>
145 <p>The text input format is a white-space separated list of the following items:</p>
147 <ul>
148 <li><code>point ({N|S}&lt;float&gt; {E|W}&lt;float&gt;)</code></li>
149 <li><code>path ({N|S}&lt;float&gt; {E|W}&lt;float&gt; {N|S}&lt;float&gt; {E|W}&lt;float&gt; ...)</code></li>
150 <li><code>outline ({N|S}&lt;float&gt; {E|W}&lt;float&gt; {N|S}&lt;float&gt; {E|W}&lt;float&gt; {N|S}&lt;float&gt; {E|W}&lt;float&gt; ...)</code></li>
151 <li><code>polygon ({N|S}&lt;float&gt; {E|W}&lt;float&gt; {N|S}&lt;float&gt; {E|W}&lt;float&gt; {N|S}&lt;float&gt; {E|W}&lt;float&gt; ...)</code></li>
152 </ul>
154 <p>Paths are open by default (i.e. there is no connection from the last point in
155 the list to the first point in the list). Outlines and polygons, in contrast,
156 are automatically closed (i.e. there is a line segment from the last point in
157 the list to the first point in the list) which means the first point should not
158 be repeated as last point in the list. Polygons are filled, outlines are not.</p>
160 <h3>2. Indices</h3>
162 <p>Two kinds of indices are supported: B-tree and GiST indices.</p>
164 <h4>B-tree indices</h4>
166 <p>A B-tree index can be used for simple equality searches and is supported by the
167 <code>epoint</code>, <code>ebox</code>, and <code>ecircle</code> data types. B-tree indices can not be used for
168 geographic searches.</p>
170 <h4>GiST indices</h4>
172 <p>For geographic searches, GiST indices must be used. The <code>epoint</code>, <code>ecircle</code>,
173 and <code>ecluster</code> data types support GiST indexing. A GiST index for geographic
174 searches can be created as follows:</p>
176 <pre><code>CREATE TABLE tbl (
177 id serial4 PRIMARY KEY,
178 loc epoint NOT NULL );
180 CREATE INDEX name_of_index ON tbl USING gist (loc);
181 </code></pre>
183 <p>GiST indices also support nearest neighbor searches when using the distance
184 operator (<code>&lt;-&gt;</code>) in the ORDER BY clause.</p>
186 <h4>Indices on other data types (e.g. GeoJSON)</h4>
188 <p>Note that further types can be indexed by using an index on an expression with
189 a conversion function. One conversion function provided by pgLatLon is the
190 <code>GeoJSON_to_ecluster(float8, float8, text)</code> function:</p>
192 <pre><code>CREATE TABLE tbl (
193 id serial4 PRIMARY KEY,
194 loc jsonb NOT NULL );
196 CREATE INDEX name_of_index ON tbl USING gist((GeoJSON_to_ecluster("loc")));
197 </code></pre>
199 <p>When using the conversion function in an expression, the index will be used
200 automatically:</p>
202 <pre><code>SELECT * FROM tbl WHERE GeoJSON_to_ecluster("loc") &amp;&amp; 'N50 E10 10000'::ecircle;
203 </code></pre>
205 <h3>3. Operators</h3>
207 <h4>Equality operator <code>=</code></h4>
209 <p>Tests if two geographic objects are equal.</p>
211 <p>The longitude is ignored for the poles, and 180 degrees west is considered to
212 be equal to 180 degrees east.</p>
214 <p>For boxes and circles, two empty objects are considered equal. (Note that a
215 circle is not empty if the radius is zero but only if it is negative infinity,
216 i.e. smaller than zero.) Two circles with a positive infinite radius are also
217 considered equal.</p>
219 <p>Implemented for:</p>
221 <ul>
222 <li><code>epoint = epoint</code></li>
223 <li><code>ebox = ebox</code></li>
224 <li><code>ecircle = ecircle</code></li>
225 </ul>
227 <p>The negation is the inequality operator (<code>&lt;&gt;</code> or <code>!=</code>).</p>
229 <h4>Linear ordering operators <code>&lt;&lt;&lt;</code>, <code>&lt;&lt;&lt;=</code>, <code>&gt;&gt;&gt;=</code>, <code>&gt;&gt;&gt;</code></h4>
231 <p>These operators create an arbitrary (but well-defined) linear ordering of
232 geographic objects, which is used internally for B-tree indexing and merge
233 joins. These operators will usually not be used by an application programmer.</p>
235 <h4>Overlap operator <code>&amp;&amp;</code></h4>
237 <p>Tests if two geographic objects have at least one point in common. Currently
238 implemented for:</p>
240 <ul>
241 <li><code>epoint &amp;&amp; ebox</code></li>
242 <li><code>epoint &amp;&amp; ecircle</code></li>
243 <li><code>epoint &amp;&amp; ecluster</code></li>
244 <li><code>ebox &amp;&amp; ebox</code></li>
245 <li><code>ecircle &amp;&amp; ecircle</code></li>
246 <li><code>ecircle &amp;&amp; ecluster</code></li>
247 </ul>
249 <p>The <code>&amp;&amp;</code> operator is commutative, i.e. <code>a &amp;&amp; b</code> is the same as <code>b &amp;&amp; a</code>. Each
250 commutation is supported as well.</p>
252 <h4>Distance operator <code>&lt;-&gt;</code></h4>
254 <p>Calculates the shortest distance between two geographic objects in meters (zero
255 if the objects are overlapping). Currently implemented for:</p>
257 <ul>
258 <li><code>epoint &lt;-&gt; epoint</code></li>
259 <li><code>epoint &lt;-&gt; ecircle</code></li>
260 <li><code>epoint &lt;-&gt; ecluster</code></li>
261 <li><code>ecircle &lt;-&gt; ecircle</code></li>
262 <li><code>ecircle &lt;-&gt; ecluster</code></li>
263 </ul>
265 <p>The <code>&lt;-&gt;</code> operator is commutative, i.e. <code>a &lt;-&gt; b</code> is the same as <code>b &lt;-&gt; a</code>.
266 Each commutation is supported as well.</p>
268 <p>For short distances, the result is very accurate (i.e. respects the dimensions
269 of the WGS-84 spheroid). For longer distances in the order of magnitude of
270 earth's radius or greater, the value is only approximate (but the error is
271 still less than 0.2% as long as no polygons with very long edges are involved).</p>
273 <p>The functions <code>distance(epoint, epoint)</code> and <code>distance(ecluster, epoint)</code> can
274 be used as an alias for this operator.</p>
276 <p>Note: In case of radial searches with a fixed radius, this operator should
277 not be used. Instead, an <code>ecircle</code> should be created and used in combination
278 with the overlap operator (<code>&amp;&amp;</code>). Alternatively, the functions
279 <code>distance_within(epoint, epoint, float8)</code> or <code>distance_within(ecluster, epoint,
280 float8)</code> can be used for fixed-radius searches.</p>
282 <h3>4. Functions</h3>
284 <h4><code>center(circle)</code></h4>
286 <p>Returns the center of an <code>ecircle</code> as an <code>epoint</code>.</p>
288 <h4><code>distance(epoint, epoint)</code></h4>
290 <p>Calculates the distance between two <code>epoint</code> datums in meters. This function is
291 an alias for the distance operator <code>&lt;-&gt;</code>.</p>
293 <p>Note: In case of radial searches with a fixed radius, this function should not be
294 used. Use <code>distance_within(epoint, epoint, float8)</code> instead.</p>
296 <h4><code>distance(ecluster, epoint)</code></h4>
298 <p>Calculates the distance from an <code>ecluster</code> to an <code>epoint</code> in meters. This
299 function is an alias for the distance operator <code>&lt;-&gt;</code>.</p>
301 <p>Note: In case of radial searches with a fixed radius, this function should not be
302 used. Use <code>distance_within(epoint, epoint, float8)</code> instead.</p>
304 <h4><code>distance_within(</code>variable <code>epoint,</code> fixed <code>epoint,</code> radius <code>float8)</code></h4>
306 <p>Checks if the distance between two <code>epoint</code> datums is not greater than a given
307 value (search radius).</p>
309 <p>Note: In case of radial searches with a fixed radius, the first argument must
310 be used for the table column, while the second argument must be used for the
311 search center. Otherwise an existing index cannot be used.</p>
313 <h4><code>distance_within(</code>variable <code>ecluster,</code> fixed <code>epoint,</code> radius <code>float8)</code></h4>
315 <p>Checks if the distance from an <code>ecluster</code> to an <code>epoint</code> is not greater than a
316 given value (search radius).</p>
318 <h4><code>ebox(</code>latmin <code>float8,</code> latmax <code>float8,</code> lonmin <code>float8,</code> lonmax <code>float8)</code></h4>
320 <p>Creates a new <code>ebox</code> with the given boundaries.
321 See "1. Types", subsection <code>ebox</code> for details.</p>
323 <h4><code>ebox(epoint, epoint)</code></h4>
325 <p>Creates a new <code>ebox</code>. This function may only be used if the longitude
326 difference is less than or equal to 120 degrees.
327 See "1. Types", subsection <code>ebox</code> for details.</p>
329 <h4><code>ecircle(epoint, float8)</code></h4>
331 <p>Creates an <code>ecircle</code> with the given center point and radius.</p>
333 <h4><code>ecircle(</code>latitude <code>float8,</code> longitude <code>float8,</code> radius <code>float8)</code></h4>
335 <p>Creates an <code>ecircle</code> with the given center point and radius.</p>
337 <h4><code>ecluster_concat(ecluster, ecluster)</code></h4>
339 <p>Combines two clusters to form a new <code>ecluster</code> by uniting all entries of both
340 clusters. Note that two overlapping areas of polygons annihilate each other
341 (which may be used to create polygons with holes).</p>
343 <h4><code>ecluster_concat(ecluster[])</code></h4>
345 <p>Creates a new <code>ecluster</code> that unites all entries of all clusters in the passed
346 array. Note that two overlapping areas of polygons annihilate each other (which
347 may be used to create polygons with holes).</p>
349 <h4><code>ecluster_create_multipoint(epoint[])</code></h4>
351 <p>Creates a new <code>ecluster</code> which contains multiple points.</p>
353 <h4><code>ecluster_create_outline(epoint[])</code></h4>
355 <p>Creates a new <code>ecluster</code> that is an outline given by the passed points.</p>
357 <h4><code>ecluster_create_path(epoint[])</code></h4>
359 <p>Creates a new <code>ecluster</code> that is a path given by the passed points.</p>
361 <h4><code>ecluster_create_polygon(epoint[])</code></h4>
363 <p>Creates a new <code>ecluster</code> that is a polygon given by the passed points.</p>
365 <h4><code>ecluster_extract_outlines(ecluster)</code></h4>
367 <p>Set-returning function that returns the outlines of an <code>ecluster</code> as <code>epoint[]</code>
368 rows.</p>
370 <h4><code>ecluster_extract_paths(ecluster)</code></h4>
372 <p>Set-returning function that returns the paths of an <code>ecluster</code> as <code>epoint[]</code>
373 rows.</p>
375 <h4><code>ecluster_extract_points(ecluster)</code></h4>
377 <p>Set-returning function that returns the points of an <code>ecluster</code> as <code>epoint</code>
378 rows.</p>
380 <h4><code>ecluster_extract_polygons(ecluster)</code></h4>
382 <p>Set-returning function that returns the polygons of an <code>ecluster</code> as <code>epoint[]</code>
383 rows.</p>
385 <h4><code>empty_ebox</code>()</h4>
387 <p>Returns the empty <code>ebox</code>.
388 See "1. Types", subsection <code>ebox</code> for details.</p>
390 <h4><code>epoint(</code>latitude <code>float8,</code> longitude <code>float8)</code></h4>
392 <p>Returns an <code>epoint</code> with the given latitude and longitude.</p>
394 <h4><code>epoint_latlon(</code>latitude <code>float8,</code> longitude <code>float8)</code></h4>
396 <p>Alias for <code>epoint(float8, float8)</code>.</p>
398 <h4><code>epoint_lonlat(</code>longitude <code>float8,</code> latitude <code>float8)</code></h4>
400 <p>Same as <code>epoint(float8, float8)</code> but with arguments reversed.</p>
402 <h4><code>GeoJSON_to_epoint(jsonb, text)</code></h4>
404 <p>Maps a GeoJSON object of type "Point" or "Feature" (which contains a
405 "Point") to an <code>epoint</code> datum. For any other JSON objects, NULL is returned.</p>
407 <p>The second parameter (which defaults to <code>epoint_lonlat</code>) may be set to a name
408 of a conversion function that transforms two coordinates (two <code>float8</code>
409 parameters) to an <code>epoint</code>.</p>
411 <h4><code>GeoJSON_to_ecluster(jsonb, text)</code></h4>
413 <p>Maps a (valid) GeoJSON object to an <code>ecluster</code>. Note that this function
414 does not check whether the JSONB object is a valid GeoJSON object.</p>
416 <p>The second parameter (which defaults to <code>epoint_lonlat</code>) may be set to a name
417 of a conversion function that transforms two coordinates (two <code>float8</code>
418 parameters) to an <code>epoint</code>.</p>
420 <h4><code>max_latitude(ebox)</code></h4>
422 <p>Returns the northern boundary of a given <code>ebox</code> in degrees between -90 and +90.</p>
424 <h4><code>max_longitude(ebox)</code></h4>
426 <p>Returns the eastern boundary of a given <code>ebox</code> in degrees between -180 and +180
427 (both inclusive).</p>
429 <h4><code>min_latitude(ebox)</code></h4>
431 <p>Returns the southern boundary of a given <code>ebox</code> in degrees between -90 and +90.</p>
433 <h4><code>min_longitude(ebox)</code></h4>
435 <p>Returns the western boundary of a given <code>ebox</code> in degrees between -180 and +180
436 (both inclusive).</p>
438 <h4><code>latitude(epoint)</code></h4>
440 <p>Returns the latitude value of an <code>epoint</code> in degrees between -90 and +90.</p>
442 <h4><code>longitude(epoint)</code></h4>
444 <p>Returns the longitude value of an <code>epoint</code> in degrees between -180 and +180
445 (both inclusive).</p>
447 <h4><code>radius(ecircle)</code></h4>
449 <p>Returns the radius of an <code>ecircle</code> in meters.</p>
450 </body></html>

Impressum / About Us