st_intersects.sql 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. -- The fire marshal wants a list of sensitive areas within a five-mile
  2. -- radius of a hazardous waste site.
  3. -- The sensitive areas are stored in the sensitive_areas table
  4. -- created below. The zone column is defined as a polygon and stores
  5. -- the outline of the sensitive areas.
  6. CREATE TABLE sensitive_areas (id integer,
  7. name varchar(128),
  8. size float,
  9. type varchar(10),
  10. zone ST_Polygon);
  11. INSERT INTO sensitive_areas VALUES(
  12. 1, 'Johnson County Hospital', 102281.91, 'hospital',
  13. ST_PolyFromText('polygon ((22000 45000,22000 58000,28000 58000,28000 42000,25000 42000,25000 45000,22000 45000))',1000)
  14. );
  15. INSERT INTO sensitive_areas VALUES(
  16. 2, 'Rydale Nursing Home', 53926.54, 'nursing hm',
  17. ST_PolyFromText('polygon ((22000 18000,28000 18000,28000 12000,22000 12000,22000 18000))',1000)
  18. );
  19. INSERT INTO sensitive_areas VALUES(
  20. 3, 'Summerhill Elementary School', 67920.64, 'school',
  21. ST_PolyFromText('polygon ((42000 18000,48000 18000,48000 13000,42000 13000,42000 18000))',1000)
  22. );
  23. -- The hazardous sites are stored in the hazardous_sites table created
  24. -- with the CREATE TABLE statement that follows. The location column,
  25. -- defined as a point, stores the geographic center of each hazardous site.
  26. CREATE TABLE hazardous_sites (site_id integer,
  27. name varchar(128),
  28. location ST_Point);
  29. INSERT INTO hazardous_sites VALUES(
  30. 102,
  31. 'W. H. Kleenare Chemical Repository',
  32. ST_PointFromText('point (7000 47000)',1000)
  33. );
  34. INSERT INTO hazardous_sites VALUES(
  35. 59,
  36. 'Landmark Industrial',
  37. ST_PointFromText('point (48000 39000)',1000)
  38. );
  39. -- The query returns a list of sensitive area and hazardous site names
  40. -- for sensitive areas that intersect the five-mile buffer radius
  41. -- of the hazardous sites.
  42. SELECT sa.name, hs.name
  43. FROM sensitive_areas sa, hazardous_sites hs
  44. WHERE ST_Intersects(ST_Buffer(hs.location,(5 * 5280)),sa.zone);