st_within.sql 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. -- In the example below two tables are created: one, buildingfootprints,
  2. -- contains a city's building footprints while the other, lots, contains
  3. -- its lots. The city engineer wants to make sure that all the building
  4. -- footprints are completely inside their lots.
  5. -- In both tables the multipolygon data type stores the ST_Geometry
  6. -- of the building footprints and the lots. The database designer selected
  7. -- ST_MultiPolygons for both features because she realizes a lot can be
  8. -- separated by a natural feature such as a river, and a building footprint
  9. -- can be made up of several buildings.
  10. CREATE TABLE buildingfootprints (building_id integer,
  11. lot_id integer,
  12. footprint ST_MultiPolygon);
  13. INSERT INTO buildingfootprints VALUES(
  14. 506, 1010,
  15. 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)
  16. );
  17. INSERT INTO buildingfootprints VALUES(
  18. 543, 2930,
  19. 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)
  20. );
  21. INSERT INTO buildingfootprints VALUES(
  22. 1208, 203,
  23. 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)
  24. );
  25. INSERT INTO buildingfootprints VALUES(
  26. 178, 5192,
  27. 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)
  28. );
  29. CREATE TABLE lots (lot_id integer,
  30. lot ST_MultiPolygon);
  31. INSERT INTO lots VALUES(
  32. 1010,
  33. ST_MPolyFromText('multipolygon (((2 57,21.5 57,21.5 38,2 38,2 57)))',1000)
  34. );
  35. INSERT INTO lots VALUES(
  36. 2930,
  37. ST_MPolyFromText('multipolygon (((21.5 57,40 57,40 38,21.5 38,21.5 57)))',1000)
  38. );
  39. INSERT INTO lots VALUES(
  40. 203,
  41. ST_MPolyFromText('multipolygon (((21.5 38,40 38,40 20,21.5 20,21.5 38)))',1000)
  42. );
  43. INSERT INTO lots VALUES(
  44. 5192,
  45. ST_MPolyFromText('multipolygon (((2 20,2 38,21.5 38,21.5 20,2 20)))',1000)
  46. );
  47. -- The city engineer first selects the buildings that are not completely
  48. -- within a lot.
  49. SELECT building_id
  50. FROM buildingfootprints, lots
  51. WHERE ST_Within(footprint,lot);
  52. -- The city engineer realizes that although the first query will
  53. -- provide her with a list of all building_id that have footprints
  54. -- outside of a lot polygon, it won't tell her if the rest have
  55. -- the correct lot_id assigned to them. This second query performs
  56. -- a data integrity check on the lot_id column of the buildingfootprints table.
  57. SELECT bf.building_id, bf.lot_id bldg_lot_id, lots.lot_id lots_lot_id
  58. FROM buildingfootprints bf, lots
  59. WHERE ST_Within(footprint,lot)
  60. AND lots.lot_id <> bf.lot_id;