st_transform.sql 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. -- This example demonstrates various ways to use the ST_Transform function.
  2. ----------------------------------------------------------------------------
  3. -- Example 1: Changing the false origin of a dataset
  4. ----------------------------------------------------------------------------
  5. -- Step 1. Suppose you created a spatial_references table entry suitable
  6. -- for Australia. You can do this with the SE_CreateSrid function
  7. -- as follows:
  8. EXECUTE FUNCTION SE_CreateSrid (110, -45, 156, -10,
  9. "Australia: lat/lon coords");
  10. -- Step 2. Load all of your data for Australia.
  11. -- In this example we will just create a table with a few points.
  12. CREATE TABLE aus_locns (name varchar(128), locn ST_Point);
  13. INSERT INTO aus_locns VALUES ("Adelaide", '1002 point(139.14 -34.87)');
  14. INSERT INTO aus_locns VALUES ("Brisbane", '1002 point(153.36 -27.86)');
  15. INSERT INTO aus_locns VALUES ("Canberra", '1002 point(148.84 -35.56)');
  16. INSERT INTO aus_locns VALUES ("Melbourne", '1002 point(145.01 -37.94)');
  17. INSERT INTO aus_locns VALUES ("Perth", '1002 point(116.04 -32.12)');
  18. INSERT INTO aus_locns VALUES ("Sydney", '1002 point(151.37 -33.77)');
  19. -- Step 3. After loading all of your data for the Australian mainland,
  20. -- you realize you need to include data for some of the outlying
  21. -- islands, like Norfolk Island and the Cocos Islands, but the false origin
  22. -- and scale factor that you chose for SRID 1002 will not work:
  23. INSERT INTO aus_locns VALUES ("Norfolk Is.", '1002 point(167.83 -29.24)');
  24. INSERT INTO aus_locns VALUES ("Cocos Is.", '1002 point( 96.52 -12.08)');
  25. -- Step 4. The solution is to create a new spatial_references table
  26. -- entry with a false origin and scale factor that will accommodate
  27. -- both the old data and new data, and then update the old data:
  28. EXECUTE FUNCTION SE_CreateSrid (95, -55, 170, -10,
  29. "Australia + outer islands: lat/lon coords");
  30. INSERT INTO aus_locns VALUES ("Norfolk Is.", '1003 point(167.83 -29.24)');
  31. INSERT INTO aus_locns VALUES ("Cocos Is.", '1003 point( 96.52 -12.08)');
  32. UPDATE aus_locns
  33. SET locn = ST_Transform(locn, 1003)::ST_Point
  34. WHERE ST_Srid(locn) = 1002;
  35. ----------------------------------------------------------------------------
  36. -- Example 2: Projecting data on the fly
  37. ----------------------------------------------------------------------------
  38. -- In a typical application, spatial data are stored in unprojected
  39. -- lat/lon format. Then, when you want to draw a map, you can
  40. -- retrieve the data in a particular projection, letting the Spatial
  41. -- DataBlade do all the transformations as it retrieves data from
  42. -- a table.
  43. -- Step 1: Create a spatial_references table entry which is suitable
  44. -- for your unprojected data. For this example we will use the
  45. -- 1983 North American Datum. Because this is a well-known,
  46. -- standard datum we can use the SE_CreateSrtext function to create
  47. -- the srtext field:
  48. INSERT INTO spatial_references
  49. (srid, description, falsex, falsey, xyunits,
  50. falsez, zunits, falsem, munits, srtext)
  51. VALUES (1004, "Unprojected lat/lon, NAD 83 datum",
  52. -180, -90, 5000000, 0, 1000, 0, 1000,
  53. SE_CreateSrtext(4269));
  54. -- Step 2: Create a table and load your data.
  55. CREATE TABLE airports (id char(4),
  56. name varchar(128),
  57. locn ST_Point);
  58. INSERT INTO airports VALUES(
  59. 'BTM', 'Bert Mooney', '1004 point(-112.4975 45.9548)');
  60. INSERT INTO airports VALUES(
  61. 'BZN', 'Gallatin Field', '1004 point(-111.1530 45.7769)');
  62. INSERT INTO airports VALUES(
  63. 'COD', 'Yellowstone Regional', '1004 point(-109.0238 44.5202)');
  64. INSERT INTO airports VALUES(
  65. 'JAC', 'Jackson Hole', '1004 point(-110.7377 43.6073)');
  66. INSERT INTO airports VALUES(
  67. 'IDA', 'Fanning Field', '1004 point(-112.0702 43.5146)');
  68. -- Step 3: Create one or more spatial_references table entries for
  69. -- any projections which you will need. Be sure that the underlying
  70. -- geographic coordinate system (in this case, NAD 83) is the same;
  71. -- this version of the Spatial DataBlade does not perform datum
  72. -- conversions.
  73. INSERT INTO spatial_references
  74. (srid, description, falsex, falsey, xyunits,
  75. falsez, zunits, falsem, munits, srtext)
  76. VALUES (1005, "UTM zone 12N, NAD 83 datum",
  77. 336000, 4760000, 1000, 0, 1000, 0, 1000,
  78. SE_CreateSrtext(26912));
  79. -- Step 4: Transform the data to a projected coordinate system on
  80. -- as needed basis.
  81. SELECT id, ST_Transform(locn, 1005) as utm FROM airports;