Created
September 8, 2008 13:48
-
-
Save hfu/9440 to your computer and use it in GitHub Desktop.
Extend Ruby String to support PostGIS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'postgres' | |
class String | |
def sql(&block) | |
result = PostGIS::sql(self).result[0] | |
return result unless block | |
result.each do |r| | |
yield r | |
end | |
end | |
def geom(srid = 4326) | |
"SELECT ST_GeometryFromText('#{self}', #{srid})".sql[0] | |
end | |
def wkt | |
"SELECT ST_AsText('#{self}')".sql[0] | |
end | |
def buffer(distance) | |
"SELECT ST_Buffer('#{self}', #{distance})".sql[0] | |
end | |
end | |
module PostGIS | |
def PostGIS::connect(pghost, pgport, pgoptions, pgtty, dbname, login, passwd, &block) | |
@@conn = PGconn.connect(pghost, pgport, pgoptions, pgtty, dbname, login, passwd) | |
if block | |
yield PostGIS | |
close | |
end | |
end | |
def PostGIS::close | |
@@conn.close | |
end | |
def PostGIS::sql(str) | |
@@conn.exec(str) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment