pgLatLon

annotate README.mkd @ 12:b4ec52e7e888

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

Impressum / About Us