Skip to content

Commit

Permalink
Fix custom column names, fix migration "thing", adds transform srid
Browse files Browse the repository at this point in the history
  • Loading branch information
nofxx committed Jan 10, 2009
1 parent 43186e8 commit af93e66
Show file tree
Hide file tree
Showing 25 changed files with 762 additions and 1,003 deletions.
12 changes: 4 additions & 8 deletions Manifest.txt
Expand Up @@ -12,24 +12,20 @@ lib/postgis_functions.rb
lib/postgis_functions/bbox.rb
lib/postgis_functions/class.rb
lib/postgis_functions/common.rb
lib/postgis_functions/linestring.rb
lib/postgis_functions/point.rb
lib/postgis_functions/polygon.rb
postgis_adapter.gemspec
rails/init.rb
script/console
script/destroy
script/generate
spec/acts_as_geom_spec.rb
spec/common_spatial_adapter_spec.rb
spec/db/database_postgis.yml
spec/db/models_postgis.rb
spec/db/schema_postgis.rb
spec/postgis_adapter/acts_as_geom_spec.rb
spec/postgis_adapter/common_spatial_adapter_spec.rb
spec/postgis_adapter_spec.rb
spec/postgis_functions/bbox_spec.rb
spec/postgis_functions/linestring_spec.rb
spec/postgis_functions/point_spec.rb
spec/postgis_functions/polygon_spec.rb
spec/postgis_functions/class_spec.rb
spec/postgis_functions/common_spec.rb
spec/postgis_functions_spec.rb
spec/spec.opts
spec/spec_helper.rb
Expand Down
7 changes: 1 addition & 6 deletions lib/postgis_adapter.rb
Expand Up @@ -10,31 +10,26 @@
require 'postgis_functions'
require 'postgis_functions/common'
require 'postgis_functions/class'
require 'postgis_functions/point'
require 'postgis_functions/linestring'
require 'postgis_functions/polygon'
require 'postgis_functions/bbox'
require 'postgis_adapter/acts_as_geom'

include GeoRuby::SimpleFeatures
include SpatialAdapter

module PostgisAdapter
VERSION = '0.1.8'
VERSION = '0.2.1'
end

#tables to ignore in migration : relative to PostGIS management of geometric columns
ActiveRecord::SchemaDumper.ignore_tables << "spatial_ref_sys" << "geometry_columns"


#add a method to_yaml to the Geometry class which will transform a geometry in a form suitable to be used in a YAML file (such as in a fixture)
GeoRuby::SimpleFeatures::Geometry.class_eval do
def to_fixture_format
as_hex_ewkb
end
end


ActiveRecord::Base.class_eval do
require 'active_record/version'

Expand Down
7 changes: 4 additions & 3 deletions lib/postgis_adapter/acts_as_geom.rb
Expand Up @@ -14,7 +14,6 @@ module ClassMethods
# acts_as_geom :geom
def acts_as_geom(*columns)
cattr_accessor :postgis_geoms

geoms = columns.map do |g|
geom_type = get_geom_type(g)
case geom_type
Expand All @@ -25,13 +24,15 @@ def acts_as_geom(*columns)
when :line_string
send :include, LineStringFunctions
end
{g => geom_type}
g
end
self.postgis_geoms = {:geoms => geoms}#, :opts => options}
self.postgis_geoms = {:columns => geoms}#, :opts => options}
end

def get_geom_type(column)
self.columns.select { |c| c.name == column.to_s}.first.geometry_type
rescue ActiveRecord::StatementInvalid => e
nil
end
end
end
Expand Down
27 changes: 13 additions & 14 deletions lib/postgis_functions.rb
Expand Up @@ -18,9 +18,8 @@
#
#
module PostgisFunctions
# EARTH_SPHEROID = "'SPHEROID[\"GRS-80\",6378137,298.257222101]'"

EARTH_SPHEROID = "'SPHEROID[\"IERS_2003\",6378136.6,298.25642]'"
EARTH_SPHEROID = "'SPHEROID[\"GRS-80\",6378137,298.257222101]'" # SRID => 4326
#EARTH_SPHEROID = "'SPHEROID[\"IERS_2003\",6378136.6,298.25642]'" # SRID =>

def postgis_calculate(operation, subjects, options = nil)
subjects = [subjects] unless subjects.respond_to?(:map)
Expand All @@ -29,12 +28,13 @@ def postgis_calculate(operation, subjects, options = nil)
raise StandardError, "#{e}"
end


private


def get_column_name
@geo_column ||= postgis_geoms[:columns].first
end

# Construct the postgis sql query
# TODO: ST_Transform() ?? # Convert between distances. Implement this?
#
# Area return in square feet
# Distance/DWithin/Length/Perimeter — in projected units.
Expand All @@ -49,25 +49,24 @@ def construct_geometric_sql(type,geoms,options)
:id => t[:id] }
end

fields = tables.map { |f| "#{f[:uid]}.geom" } # W1.geom
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

#
# Data => SELECT Func(A,B)
# BBox => SELECT (A <=> B)
#
if type == :bbox
opcode = nil
s_join = " #{options} "
else
unless type == :bbox
opcode = type.to_s
opcode = "ST_#{opcode}" unless opcode =~ /th3d|pesinter/
s_join = ","
fields << options if options
fields = fields.join(",")
else
fields = fields.join(" #{options} ")
end

sql = "SELECT #{opcode}(#{fields.join(s_join)}) "
sql = "SELECT #{opcode}(#{fields}) "
sql << "FROM #{tables.join(",")} " if tables
sql << "WHERE #{conditions.join(" AND ")}" if conditions
#p sql; sql
Expand All @@ -94,7 +93,7 @@ def execute_geometrical_calculation(operation, subject, options) #:nodoc:

# Get a unique ID for tables
def unique_identifier
@u_id ||= "W1"
@u_id ||= "T1"
@u_id = @u_id.succ
end

Expand Down

0 comments on commit af93e66

Please sign in to comment.