st_disjoint.sql 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. -- An insurance company wants to assess the insurance coverage for the town's
  2. -- hospital, nursing homes, and schools. Part of this process includes
  3. -- determining the threat the hazardous waste sites pose to each institution.
  4. -- At this time the insurance company wants to consider only those
  5. -- institutions that are not at risk of contamination. The GIS consultant
  6. -- hired by the insurance company has been commissioned to locate all
  7. -- institutions that are outside a five-mile radius of a hazardous waste
  8. -- storage facility. The sensitive_areas table contains several columns that
  9. -- describe the threatened institutions in addition to the zone column,
  10. -- which stores the institutions' polygon geometries.
  11. CREATE TABLE sensitive_areas (id integer,
  12. name varchar(128),
  13. size float,
  14. type varchar(10),
  15. zone ST_Polygon);
  16. -- The hazardous_sites table stores the identity of the sites in the site_id
  17. -- and name columns, while the actual geographic location of each site is
  18. -- stored in the location point column.
  19. CREATE TABLE hazardous_sites (site_id integer,
  20. name varchar(128),
  21. location ST_Point);
  22. INSERT INTO sensitive_areas VALUES(
  23. 1, 'Johnson County Hospital', 102281.91, 'hospital',
  24. ST_PolyFromText('polygon ((22000 45000,22000 58000,28000 58000,28000 42000,25000 42000,25000 45000,22000 45000))',1000));
  25. INSERT INTO sensitive_areas VALUES(
  26. 2, 'Rydale Nursing Home', 53926.54, 'nursing hm',
  27. ST_PolyFromText('polygon ((22000 18000,28000 18000,28000 12000,22000 12000,22000 18000))',1000));
  28. INSERT INTO sensitive_areas VALUES(
  29. 3, 'Summerhill Elementary School', 67920.64, 'school',
  30. ST_PolyFromText('polygon ((42000 18000,48000 18000,48000 13000,42000 13000,42000 18000))',1000));
  31. INSERT INTO hazardous_sites VALUES(
  32. 102, 'W. H. Kleenare Chemical Repository',
  33. ST_PointFromText('point (7000 47000)',1000)
  34. );
  35. INSERT INTO hazardous_sites VALUES(
  36. 59, 'Landmark Industrial',
  37. ST_PointFromText('point (48000 39000)',1000)
  38. );
  39. -- The select statement lists the names of all sensitive areas that are
  40. -- outside the five-mile radius of a hazardous waste site.
  41. SELECT sa.name
  42. FROM sensitive_areas sa, hazardous_sites hs
  43. WHERE ST_Disjoint(ST_Buffer(hs.location,(5 * 5280)), sa.zone);
  44. -- Tip: You can rewrite the query to use the ST_Intersects function instead,
  45. -- as shown here. ST_Intersects and ST_Disjoint return opposite results,
  46. -- but ST_Intersects can use an index to determine the answer more quickly,
  47. -- while ST_Disjoint must perform a full table scan.
  48. SELECT sa.name
  49. FROM sensitive_areas sa, hazardous_sites hs
  50. WHERE NOT ST_Intersects(ST_Buffer(hs.location,(5 * 5280)), sa.zone);