Skip to content

Commit

Permalink
within/dwithin
Browse files Browse the repository at this point in the history
  • Loading branch information
nofxx committed Feb 26, 2011
1 parent 50c53c3 commit 8b1ed55
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
10 changes: 6 additions & 4 deletions lib/postgis_adapter/functions/class.rb
Expand Up @@ -48,18 +48,20 @@ def by_perimeter sort='asc'
find(:all, :order => "ST_Perimeter(geom) #{sort}" )
end

def all_within(other, margin=1)
# find(:all, :conditions => "ST_DWithin(geom, ST_GeomFromEWKB(E'#{other.as_ewkt}'), #{margin})")
def all_dwithin(other, margin=1)
# find(:all, :conditions => "ST_DWithin(geom, ST_GeomFromEWKB(E'#{other.as_ewkt}'), #{margin})")
find(:all, :conditions => "ST_DWithin(geom, ST_GeomFromEWKT(E'#{other.as_hex_ewkb}'), #{margin})")
end

def all_within(other)
find(:all, :conditions => "ST_Within(geom, ST_GeomFromEWKT(E'#{other.as_hex_ewkb}'))")
end

def by_boundaries sort='asc'
find(:all, :order => "ST_Boundary(geom) #{sort}" )
end

end



end
end
14 changes: 12 additions & 2 deletions spec/postgis_adapter/functions/class_spec.rb
Expand Up @@ -3,10 +3,12 @@
describe "ClassMethods" do
before(:all) do
@c1 ||= City.create!(:data => "CityClass", :geom => Polygon.from_coordinates([[[12,45],[45,41],[4,1],[12,45]],[[2,5],[5,1],[14,1],[2,5]]],4326))
@c2 ||= City.create!(:data => "CityClass", :geom => Polygon.from_coordinates([[[10,10],[10,50],[50,50],[10,10]],[[2,5],[5,1],[14,1],[2,5]]],4326))
@s1 ||= Street.create!(:data => "StreetClass", :geom => LineString.from_coordinates([[1,1],[99,88]],4326))
@s2 ||= Street.create!(:data => "StreetClassTiny", :geom => LineString.from_coordinates([[1,1],[1.1,1.1]],4326))
@p1 ||= Position.create!(:data => "PointClass", :geom => Point.from_x_y(99,99,4326))
@p2 ||= Position.create!(:data => "PointClassClose", :geom => Point.from_x_y(99.9,99.9,4326))
@p3 ||= Position.create!(:data => "PointInsideCity", :geom => Point.from_x_y(15.0,15.0,4326))
end

after(:all) do
Expand Down Expand Up @@ -58,8 +60,16 @@
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)
it "should sort by all dwithin" do
City.all_dwithin(@s1.geom).should eql([@c1, @c2])
end

it "should find all within polygon" do
Position.all_within(@c1.geom).should eql([@p3])#Array)
end

it "should find all within polygon 2" do
Position.all_within(@c2.geom).should eql([])#Array)
end

it "should sort by all within" do
Expand Down

0 comments on commit 8b1ed55

Please sign in to comment.