#
source:
trunk/workshop-foss4g/equality.rst
@
1

Revision 1, 3.9 KB checked in by djay, 13 years ago (diff) |
---|

Rev | Line | |
---|---|---|

[1] | 1 | .. _equality: |

2 | ||

3 | Section 22: Equality | |

4 | ================================= | |

5 | ||

6 | Equality | |

7 | -------- | |

8 | ||

9 | Determining equality when dealing with geometries can be tricky. PostGIS supports three different functions that can be used to determine different levels of equality, though for clarity we will use the definitions below. To illustrate these functions, we will use the following polygons. | |

10 | ||

11 | .. image:: ./equality/polygon-table.png | |

12 | ||

13 | These polygons are loaded using the following commands. | |

14 | ||

15 | .. code-block:: sql | |

16 | ||

17 | CREATE TABLE polygons (name varchar, poly geometry); | |

18 | ||

19 | INSERT INTO polygons VALUES | |

20 | ('Polygon 1', 'POLYGON((-1 1.732,1 1.732,2 0,1 -1.732, | |

21 | -1 -1.732,-2 0,-1 1.732))'), | |

22 | ('Polygon 2', 'POLYGON((-1 1.732,-2 0,-1 -1.732,1 -1.732, | |

23 | 2 0,1 1.732,-1 1.732))'), | |

24 | ('Polygon 3', 'POLYGON((1 -1.732,2 0,1 1.732,-1 1.732, | |

25 | -2 0,-1 -1.732,1 -1.732))'), | |

26 | ('Polygon 4', 'POLYGON((-1 1.732,0 1.732, 1 1.732,1.5 0.866, | |

27 | 2 0,1.5 -0.866,1 -1.732,0 -1.732,-1 -1.732,-1.5 -0.866, | |

28 | -2 0,-1.5 0.866,-1 1.732))'), | |

29 | ('Polygon 5', 'POLYGON((-2 -1.732,2 -1.732,2 1.732, | |

30 | -2 1.732,-2 -1.732))'); | |

31 | ||

32 | SELECT Populate_Geometry_Columns(); | |

33 | ||

34 | .. image:: ./equality/start13.png | |

35 | ||

36 | Exactly Equal | |

37 | ^^^^^^^^^^^^^ | |

38 | ||

39 | Exact equality is determined by comparing two geometries, vertex by vertex, in order, to ensure they are identical in position. The following examples show how this method can be limited in its effectiveness. | |

40 | ||

41 | .. code-block:: sql | |

42 | ||

43 | SELECT a.name, b.name, CASE WHEN ST_OrderingEquals(a.poly, b.poly) | |

44 | THEN 'Exactly Equal' ELSE 'Not Exactly Equal' end | |

45 | FROM polygons as a, polygons as b; | |

46 | ||

47 | .. image:: ./equality/start14.png | |

48 | ||

49 | In this example, the polygons are only equal to themselves, not to other seemingly equivalent polygons (as in the case of Polygons 1 through 3). In the case of Polygons 1, 2, and 3, the vertices are in identical positions but are defined in differing orders. Polygon 4 has colinear (and thus redundant) vertices on the hexagon edges causing inequality with Polygon 1. | |

50 | ||

51 | Spatially Equal | |

52 | ^^^^^^^^^^^^^^^ | |

53 | ||

54 | As we saw above, exact equality does not take into account the spatial nature of the geometries. There is an function, aptly named :command:`ST_Equals`, available to test the spatial equality or equivalence of geometries. | |

55 | ||

56 | .. code-block:: sql | |

57 | ||

58 | SELECT a.name, b.name, CASE WHEN ST_Equals(a.poly, b.poly) | |

59 | THEN 'Spatially Equal' ELSE 'Not Equal' end | |

60 | FROM polygons as a, polygons as b; | |

61 | ||

62 | .. image:: ./equality/start15.png | |

63 | ||

64 | These results are more in line with our intuitive understanding of equality. Polygons 1 through 4 are considered equal, since they enclose the same area. Note that neither the direction of the polygon is drawn, the starting point for defining the polygon, nor the number of points used are important here. What is important is that the polygons contain the same space. | |

65 | ||

66 | Equal Bounds | |

67 | ^^^^^^^^^^^^ | |

68 | ||

69 | Exact equality requires, in the worst case, comparison of each and every vertex in the geometry to determine equality. This can be slow, and may not be appropriate for comparing huge numbers of geometries. To allow for speedier comparison, the equal bounds operator, :command:`=`, is provided. This operates only on the bounding box (rectangle), ensuring that the geometries occupy the same two dimensional extent, but not necessarily the same space. | |

70 | ||

71 | .. code-block:: sql | |

72 | ||

73 | SELECT a.name, b.name, CASE WHEN a.poly = b.poly | |

74 | THEN 'Equal Bounds' ELSE 'Non-equal Bounds' end | |

75 | FROM polygons as a, polygons as b; | |

76 | ||

77 | .. image:: ./equality/start17.png | |

78 | ||

79 | As you can see, all of our spatially equal geometries also have equal bounds. Unfortunately, Polygon 5 is also returned as equal under this test, because it shares the same bounding box as the other geometries. Why is this useful, then? Although this will be covered in detail later, the shot answer is that this enables the use of spatial indexing that can quickly reduce huge comparison sets into more manageable blocks when joining or filtering data. | |

80 |

**Note:**See TracBrowser for help on using the repository browser.