st_equals.sql 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. -- The city GIS technician suspects that some of the data in the
  2. -- buildingfootprints table was somehow duplicated. To alleviate his
  3. -- concern, he queries the table to determine if any of the footprints
  4. -- multipolygons are equal.
  5. -- The buildingfootprints table is created with the following statement.
  6. -- The building_id column uniquely identifies the buildings, the lot_id
  7. -- identifies the building's lot, and the footprint multipolygon stores
  8. -- the building's geometry.
  9. CREATE TABLE buildingfootprints (building_id integer,
  10. lot_id integer,
  11. footprint ST_MultiPolygon);
  12. INSERT INTO buildingfootprints VALUES(
  13. 506, 1010,
  14. ST_MPolyFromText('multipolygon (((7.0 45.0,15.0 45.0,15.0 51.0,18.0 51.0,18.0 54.0,8.0 54.0,8.0 51.0,7.0 51.0,7.0 45.0)))',1000)
  15. );
  16. INSERT INTO buildingfootprints VALUES(
  17. 543, 2930,
  18. ST_MPolyFromText('multipolygon (((26.0 55.0,38.0 55.0,38.0 48.0,34.0 48.0,34.0 50.0,26.0 50.0,26.0 55.0)))',1000)
  19. );
  20. INSERT INTO buildingfootprints VALUES(
  21. 1208, 203,
  22. ST_MPolyFromText('multipolygon (((8.0 39.0,12.0 39.0,12.0 33.0,17.0 33.0,17.0 22.0,8.0 22.0,8.0 39.0)))',1000)
  23. );
  24. INSERT INTO buildingfootprints VALUES(
  25. 178, 5192,
  26. ST_MPolyFromText('multipolygon (((26.0 33.0,38.0 33.0,38.0 24.0,33.0 24.0,33.0 27.0,26.0 27.0,26.0 33.0)))',1000)
  27. );
  28. -- The buildingfootprints table is spatially joined to itself by the
  29. -- ST_Equals predicate, which returns t (TRUE) whenever it finds two
  30. -- multipolygons that are equal. The bf1.building_id <> bf2.building_id
  31. -- condition eliminates the comparison of a geometry to itself.
  32. SELECT bf1.building_id, bf2.building_id
  33. FROM buildingfootprints bf1, buildingfootprints bf2
  34. WHERE ST_Equals(bf1.footprint,bf2.footprint)
  35. AND bf1.building_id <> bf2.building_id;
  36. -- To illustrate the difference between ST_Equals and Equal,
  37. -- consider the following example:
  38. CREATE TABLE equal_test (id integer,
  39. line ST_LineString);
  40. INSERT INTO equal_test VALUES
  41. (1, ST_LineFromText('linestring(10 10, 20 20)', 1000));
  42. INSERT INTO equal_test VALUES
  43. (2, ST_LineFromText('linestring(20 20, 10 10)', 1000));
  44. -- The following query will return both rows, because ST_Equals
  45. -- determines that both linestrings are spatially equivalent:
  46. SELECT id FROM equal_test
  47. WHERE ST_Equals (line, ST_LineFromText('linestring(10 10, 20 20)', 1000));
  48. -- The following query will return only the first row, because Equals
  49. -- only performs a memory comparison of the linestrings:
  50. SELECT id FROM equal_test
  51. WHERE line = ST_LineFromText('linestring(10 10, 20 20)', 1000);