Geography Reference
In-Depth Information
longitude = record[37..44]
-
longitude_direction = record[46..46]
30
depth = record[50..54]
-
magnitude = record[66..69]
-
# if the longitude is in the western hemisphere, it must be
-
# negative
-
longitude = -1 * longitude.to_f if longitude_direction == 'W'
35
magnitude = '0' if magnitude.strip.length == 0
-
key = key + 1
-
# create a point using geo_ruby
-
pt = Point.from_x_y(longitude, latitude)
-
# insert the row
40
result = conn.query("insert into quake_demo values(#{key}, " +
-
"'#{event_date}', '#{event_time}',#{latitude}, #{longitude}, " +
-
"#{depth}, #{magnitude}, GeometryFromText('#{pt.as_wkt}',4326))")
-
-
45
end
# create the spatial index
-
result=conn.query("create index sidx_quake_demo on quake_demo " +
-
"using GIST (geom GIST_GEOMETRY_OPS)")
-
-
50
# analyze the table
result = conn.query("vacuum analyze quake_demo")
-
-
-
conn.close
f.close
-
Let's take a look at how this works. First we need to pull in the needed
dependencies; then in line 7, we make connection to the database. If
you want to try this, just change the parameters to match your setup
(database name, username, password, and connection information).
Once we have a connection, the next step is to create the table. We
do this with a simple DDL 10 query beginning with line 12. The rest
of the script is pretty much the same as the one we used to prepare
the delimited data, until you get down to line 39 where we use one of
the GeoRuby classes to simplify things a bit. By using the Point object,
we can easily get the WKT needed for our insert statement. Sure, we
could have cobbled it together from the latitude and longitude, but that
wouldn't be any fun. Once we have the point, we create the insert state-
ment (41). As you can see from the code, this is done for each line in
the input file, resulting in more than 12,000 records in the database.
Once all the records are loaded, we have a couple of things left to do.
First we need a spatial index on our new table. Without it, retrieving
10. Data Definition Language
 
 
Search WWH ::




Custom Search