Geography Reference
In-Depth Information
Here's the code:
Download transform_point.rb
#!/usr/local/bin/ruby
Line 1
require 'rubygems'
-
require 'postgres-pr/connection'
-
require 'geo_ruby'
-
include GeoRuby::SimpleFeatures
5
# Connect to the database
-
conn = PostgresPR::Connection.new('gis_data',
-
'gsherman',
-
'',
-
'tcp://madison:5432')
10
# Create a point using geo_ruby from the command line arguments
-
pt = Point.from_x_y(ARGV[0], ARGV[1])
-
# transform the point
-
wkt = "'POINT(#{pt.x} #{pt.y})'"
-
sql = "select transform(GeometryFromText(#{wkt},4267), 4326)"
15
result = conn.query(sql)
-
# Create a WGS84 point from the transform result
-
wgs84_pt = Point.from_hex_ewkb(result.rows[0][0])
-
# print the results
-
print "Input NAD27 point : #{ARGV[0]}, #{ARGV[1]}\n"
20
print "Output WGS84 point: #{wgs84_pt.x}, #{wgs84_pt.y}\n"
-
# Close the database connection
-
conn.close
-
Let's take a look at a couple of things about the code. First, Harrison
must specify the latitude and longitude on the command line, since the
NAD27 point from his map is created on line 12 using the first two
elements of the ARGV array. Since I like to specify coordinates as X
and then Y, that's the way Harrison did it. This means we have to put
the longitude on the command line first, followed by the latitude. The
second thing to note is there is absolutely no error checking—this is a
no-frills script.
When Harrison runs the script, here's what he gets:
$ ruby code/transform_point.rb -151 61
Input NAD27 point : -151, 61
Output WGS84 point: -151.002235480186, 60.9994441802801
He now has his first point in converted to WGS84. He could just as
easily transform the points to a projected coordinate system by sub-
stituting the appropriate SRID in the query. For example, if we wanted
to convert from our NAD27 latitude and longitude coordinates to UTM
Zone 6, NAD83 datum, the query on line 15 would look like this:
select transform(GeometryFromText(#{wkt},4267), 26906)
 
Search WWH ::




Custom Search