Skip to content

Commit

Permalink
playing a lil with class methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nofxx committed Apr 13, 2009
1 parent abc7b6f commit 7f30492
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 53 deletions.
4 changes: 0 additions & 4 deletions lib/postgis_adapter.rb
Expand Up @@ -386,10 +386,6 @@ def self.create_simplified(name,default,null = true)
new(name,default,"geometry",null,nil,nil,nil)
end

def as_kml

end

end
end
end
41 changes: 20 additions & 21 deletions lib/postgis_functions/class.rb
@@ -1,31 +1,30 @@
module PostgisFunctions


###
##

#
# Class Methods
#
# Falling back to AR here.
#
module ClassMethods

#
# Returns the closest record
#
def closest_to(p, srid=4326)
find(:first, :order => "ST_Distance(geom, GeomFromText('POINT(#{p.x} #{p.y})', #{srid}))" )
# Returns the closest record
def closest_to(p, opts = {})
srid = opts.delete(:srid) || 4326
opts.merge!(:order => "ST_Distance(geom, GeomFromText('POINT(#{p.x} #{p.y})', #{srid}))")
find(:first, opts)
end

def close_to(p, srid=4326)
find(:all, :order => "ST_Distance(geom, GeomFromText('POINT(#{p.x} #{p.y})', #{srid}))" )
#
# Order by distance
def close_to(p, opts = {})
srid = opts.delete(:srid) || 4326
opts.merge!(:order => "ST_Distance(geom, GeomFromText('POINT(#{p.x} #{p.y})', #{srid}))")
find(:all, opts)
end

#
#
#
def by_length sort='asc'
find(:all, :order => "ST_length(geom) #{sort}" )
def by_length opts = {}
sort = opts.delete(:sort) || 'asc'
opts.merge!(:order => "ST_length(geom) #{sort}")
find(:all, opts)
end

def longest
Expand Down Expand Up @@ -58,7 +57,7 @@ def by_boundaries sort='asc'
end

end
end



end
54 changes: 54 additions & 0 deletions spec/postgis_functions/class_spec.rb
@@ -0,0 +1,54 @@
require File.dirname(__FILE__) + '/../spec_helper.rb'

describe "Class Functions" do
before(:all) do
@c1 ||= City.create!(:data => "City1", :geom => Polygon.from_coordinates([[[12,45],[45,41],[4,1],[12,45]],[[2,5],[5,1],[14,1],[2,5]]],4326))
@s1 ||= Street.create!(:data => "Street1", :geom => LineString.from_coordinates([[1,1],[88,88]],4326))
@p1 ||= Position.create!(:data => "Point1", :geom => Point.from_x_y(1,1,4326))
end

it "should find the closest other point" do
Position.close_to(@p1.geom, :srid => 4326)[0].data.should == @p1.data
end

it "should find the closest other point and limit" do
Position.close_to(@p1.geom, :limit => 10).should have(10).positions
end

it "should find the closest other point" do
Position.closest_to(@p1.geom).data.should == @p1.data
end

it "should sort by linestring length" do
Street.by_length.should be_instance_of(Array)
end

it "should sort by linestring length" do
Street.by_length(:limit => 10).should have(10).streets
end

it "should find the longest" do
Street.longest.should be_instance_of(Street)
end

it "should find all dwithin one" do
Position.all_within(@s1.geom).should be_instance_of(Array)
end

it "should find all dwithin one" do
City.by_perimeter.should be_instance_of(Array)
end

it "should sort by polygon area" do
City.by_area.should be_instance_of(Array)
end

it "should sort by all within" do
City.all_within(@s1.geom).should be_instance_of(Array)
end

it "should sort by all within" do
City.by_boundaries.should be_instance_of(Array)
end

end
4 changes: 2 additions & 2 deletions spec/postgis_functions/common_spec.rb
Expand Up @@ -20,11 +20,11 @@
describe "Point" do

it "should find the closest other point" do
Position.close_to(@p1.geom,4326)[0].data.should == @p1.data
Position.close_to(@p1.geom)[0].data.should == @p1.data
end

it "should find the closest other point" do
Position.closest_to(@p1.geom,4326).data.should == @p1.data
Position.closest_to(@p1.geom).data.should == @p1.data
end

it { @p1.distance_to(@s2).should be_close(4.24264068711928, 0.0001) }
Expand Down
27 changes: 1 addition & 26 deletions spec/postgis_functions_spec.rb
Expand Up @@ -20,7 +20,7 @@
end

it "should calculate distance point to line" do
@p1.geom.as_kml.should be_close(0.248069469178417, 0.00000001)
@p1.geom.as_kml.should eql("<Point>\n<coordinates>-43,-22</coordinates>\n</Point>\n")
end

it "should calculate inside a city" do
Expand All @@ -39,29 +39,4 @@

end

#TODO is sorted rspec helper
describe "Class methods" do

it "should find all dwithin one" do
Position.all_within(@s1.geom).should be_instance_of(Array)
end

it "should find all dwithin one" do
City.by_perimeter.should be_instance_of(Array)
end

it "should sort by polygon area" do
City.by_area.should be_instance_of(Array)
end

it "should sort by all within" do
City.all_within(@s1.geom).should be_instance_of(Array)
end

it "should sort by all within" do
City.by_boundaries.should be_instance_of(Array)
end

end

end

0 comments on commit 7f30492

Please sign in to comment.