lib/postgis_functions.rb in nofxx-postgis_adapter-0.2.2 vs lib/postgis_functions.rb in nofxx-postgis_adapter-0.3.0
- old
+ new
@@ -1,11 +1,10 @@
-# #
+# -*- coding: utf-8 -*-
#
# PostGIS Adapter - http://github.com/nofxx/postgis_adapter
#
-# Hope you enjoy this plugin.
-#
+# Hope you enjoy this plugin.
#
#
# Post any bugs/suggestions to the lighthouse tracker:
# http://nofxx.lighthouseapp.com/projects/20712-postgisadapter
#
@@ -14,16 +13,15 @@
#
# PostGis Manual - http://postgis.refractions.net/documentation/manual-svn/ch07.html
# Earth Spheroid - http://en.wikipedia.org/wiki/Figure_of_the_Earth
#
#
-#
module PostgisFunctions
EARTH_SPHEROID = "'SPHEROID[\"GRS-80\",6378137,298.257222101]'" # SRID => 4326
- #EARTH_SPHEROID = "'SPHEROID[\"IERS_2003\",6378136.6,298.25642]'" # SRID =>
+ #EARTH_SPHEROID = "'SPHEROID[\"IERS_2003\",6378136.6,298.25642]'" # SRID =>
- def postgis_calculate(operation, subjects, options = nil)
+ def postgis_calculate(operation, subjects, options = {})
subjects = [subjects] unless subjects.respond_to?(:map)
return execute_geometrical_calculation(operation, subjects, options)
rescue Exception => e
raise StandardError, "#{e}"
end
@@ -31,11 +29,12 @@
private
def get_column_name
@geo_column ||= postgis_geoms[:columns].first
end
-
+
+ #
# Construct the postgis sql query
#
# Area return in square feet
# Distance/DWithin/Length/Perimeter — in projected units.
# DistanceSphere/Spheroid — in meters.
@@ -45,20 +44,28 @@
tables = geoms.map do |t| {
:class => t.class.to_s.downcase.pluralize,
:uid => unique_identifier,
:id => t[:id] }
end
-
- fields = tables.map { |f| "#{f[:uid]}.#{get_column_name}" } # W1.geom
- conditions = tables.map { |f| "#{f[:uid]}.id = #{f[:id]}" } # W1.id = 5
- tables.map! { |f| "#{f[:class]} #{f[:uid]}" } # streets W1
+ # Implement a better way for options?
+ if options.instance_of? Hash
+ transform = options.delete(:transform)
+ options = nil
+ end
+
+ fields = tables.map { |f| "#{f[:uid]}.#{get_column_name}" } # W1.geom
+ fields.map! { |f| "ST_Transform(#{f}, #{transform})" } if transform # ST_Transform(W1.geom,x)
+ conditions = tables.map { |f| "#{f[:uid]}.id = #{f[:id]}" } # W1.id = 5
+ tables.map! { |f| "#{f[:class]} #{f[:uid]}" } # streets W1
+
#
- # Data => SELECT Func(A,B)
- # BBox => SELECT (A <=> B)
- #
- unless type == :bbox
+ # Data => SELECT Func(A,B)
+ # BBox => SELECT (A <=> B)
+ # Func => SELECT Func(Func(A))
+ #
+ if type != :bbox
opcode = type.to_s
opcode = "ST_#{opcode}" unless opcode =~ /th3d|pesinter/
fields << options if options
fields = fields.join(",")
else
@@ -82,11 +89,11 @@
#
def execute_geometrical_calculation(operation, subject, options) #:nodoc:
value = connection.select_value(construct_geometric_sql(operation, subject, options))
return nil unless value
if value =~ /t|f/
- {"f" => false, "t" => true}[value]
+ {"f" => false, "t" => true}[value]
else
GeoRuby::SimpleFeatures::Geometry.from_hex_ewkb(value) rescue value
end
end
@@ -140,10 +147,10 @@
#ST_AsEWKT
#ST_AsHEXEWKB
#ST_AsGML
#ST_AsKML
#ST_AsSVG
-#
+#
# def distance_convert(value, unit, from = nil)
# factor = case unit
# when :km, :kilo then 1
# when :miles,:mile then 0.62137119
# when :cm, :cent then 0.1