Skip to content

Instantly share code, notes, and snippets.

@hfu
Created September 8, 2008 13:48
Show Gist options
  • Save hfu/9440 to your computer and use it in GitHub Desktop.
Save hfu/9440 to your computer and use it in GitHub Desktop.
Extend Ruby String to support PostGIS
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