Geography Reference
In-Depth Information
Now let's quickly test the GeoRuby install using the following script to
create a point and print it out in WKT format:
require 'rubygems'
require 'geo_ruby'
include GeoRuby::SimpleFeatures
p = Point.from_x_y(-151.25,61.75)
puts p.as_wkt
The output from the script is a whopping:
POINT(-151.25 61.75)
Although not very earth-shattering, it proves we have the environment
set up right and now can move on to doing something useful with our
PostgreSQL data and Ruby.
Remember in Section 8.2 , Importing Data, on page 122 , where we wrote
a little script to prepare the historic earthquake data for import? Well,
we're going to modify that a bit and use it to load the earthquake data
directly into PostGIS. Here's the script with the new bits added in:
Download load_earthquakes.rb
#!/usr/local/bin/ruby
Line 1
require 'rubygems'
-
require 'postgres-pr/connection'
-
require 'geo_ruby'
-
include GeoRuby::SimpleFeatures
5
# load earthquake data into PostGIS
-
conn = PostgresPR::Connection.new('gis_data',
-
'gsherman',
-
'',
-
'tcp://madison:5432')
10
# create the database table
-
conn.query('create table quake_demo (id int4 primary key, event_date date,' +
-
'event_time varchar(10), latitude float, longitude float, depth float,' +
-
'magnitude float, geom geometry)')
-
# Open the file
15
f = File.open("db_search2291")
-
# Skip the first two header records
-
2.times{f.gets}
-
# Member for the primary key
-
key = 0
20
# process the earthquake records
-
while not f.eof
-
record = f.gets
-
# used a fixed length approach to get the fields we want since
-
# splitting on white space isn't feasible
25
event_date = record[1..10]
-
event_time = record[13..22]
-
latitude = record[26..32]
-
 
 
Search WWH ::




Custom Search