Skip to content

Commit

Permalink
add merge line
Browse files Browse the repository at this point in the history
  • Loading branch information
nofxx committed Jan 28, 2010
1 parent 84cd8e7 commit 85ddc3a
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 6 deletions.
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
0.7.2
0.7.3
1 change: 1 addition & 0 deletions lib/postgis_adapter/acts_as_geom.rb
Expand Up @@ -24,6 +24,7 @@ def has_geom(*geom)
when :point then PointFunctions
when :polygon then PolygonFunctions
when :line_string then LineStringFunctions
when :multi_line_string then MultiLineStringFunctions
end unless geom[0].kind_of? Symbol
end
alias :acts_as_geom :has_geom
Expand Down
17 changes: 13 additions & 4 deletions lib/postgis_functions/common.rb
Expand Up @@ -664,10 +664,6 @@ def interpolate_point(fraction)
postgis_calculate(:line_interpolate_point, self, fraction)
end

def line_merge
postgis_calculate(:line_merge, self)
end

#
# Return a linestring being a substring of the input one starting and ending
# at the given fractions of total 2d length. Second and third arguments are
Expand All @@ -693,7 +689,20 @@ def line_substring(s,e)
#end
end

module MultiLineStringFunctions

#
# Returns a (set of) LineString(s) formed by sewing together a MULTILINESTRING.
#
# Only use with MULTILINESTRING/LINESTRINGs. If you feed a polygon or geometry collection into this function, it will return an empty GEOMETRYCOLLECTION
#
# Returns geometry ST_LineMerge(geometry amultilinestring);
#
def line_merge
postgis_calculate(:lineMerge, self)
end

end
#
#
#
Expand Down
4 changes: 4 additions & 0 deletions spec/db/models_postgis.rb
Expand Up @@ -52,6 +52,10 @@ class Street < ActiveRecord::Base
acts_as_geom :geom => :line_string
end

class Road < ActiveRecord::Base
acts_as_geom :geom => :multi_line_string
end

class CommonGeo < ActiveRecord::Base
acts_as_geom :geom => :point
end
Expand Down
6 changes: 6 additions & 0 deletions spec/db/schema_postgis.rb
Expand Up @@ -78,6 +78,12 @@
t.line_string :geom, :null => false, :srid => 4326
end

create_table :roads, :force => true do |t|
t.string :data, :limit => 100
t.integer :value
t.multi_line_string :geom, :null => false, :srid => 4326
end

create_table :common_geos, :force => true do |t|
t.string :data, :limit => 100
t.integer :value
Expand Down
26 changes: 25 additions & 1 deletion spec/postgis_functions/common_spec.rb
Expand Up @@ -8,6 +8,7 @@
@c2 ||= City.create!(:data => "City1", :geom => Polygon.from_coordinates([[[22,66],[65,65],[20,10],[22,66]],[[10,15],[15,11],[34,14],[10,15]]],4326))
@c3 ||= City.create!(:data => "City3", :geom => Polygon.from_coordinates([[[12.4,-45.3],[45.4,41.6],[4.456,1.0698],[12.4,-45.3]],[[2.4,5.3],[5.4,1.4263],[14.46,1.06],[2.4,5.3]]],4326))
@s1 ||= Street.create!(:data => "Street1", :geom => LineString.from_coordinates([[1,1],[2,2]],4326))
@sx ||= Street.create!(:data => "Street1", :geom => LineString.from_coordinates([[2,2],[4,4]],4326))
@s2 ||= Street.create!(:data => "Street2", :geom => LineString.from_coordinates([[4,4],[7,7]],4326))
@s3 ||= Street.create!(:data => "Street3", :geom => LineString.from_coordinates([[8,8],[18,18],[20,20],[25,25],[30,30],[38,38]],4326))
@s4 ||= Street.create!(:data => "Street4", :geom => LineString.from_coordinates([[10,8],[15,18]],4326))
Expand All @@ -16,6 +17,8 @@
@p3 ||= Position.create!(:data => "Point3", :geom => Point.from_x_y(8,8,4326))
@p4 ||= Position.create!(:data => "Point4", :geom => Point.from_x_y(18.1,18,4326))
@p5 ||= Position.create!(:data => "Point5", :geom => Point.from_x_y(30,30,4326))
@m1 ||= Road.create(:data => "MG-050", :geom => MultiLineString.from_line_strings([@s1.geom, @sx.geom]))
@m2 ||= Road.create(:data => "MG-050", :geom => MultiLineString.from_line_strings([@s3.geom, @s4.geom]))
end

describe "Point" do
Expand Down Expand Up @@ -239,6 +242,10 @@
end
end

it "should have 1 geometry" do
@s1.should_not respond_to(:geometries)
end

it "should intersect with linestring" do
@s4.intersects?(@s3).should be_true
end
Expand Down Expand Up @@ -293,7 +300,6 @@
@s1.as_geo_json.should eql("{\"type\":\"LineString\",\"coordinates\":[[1,1],[2,2]]}")
end
end

end

describe "Distance" do
Expand Down Expand Up @@ -369,6 +375,24 @@
str.geom[1].x.should be_close(7,0.0000001)
str.geom[1].y.should be_close(7,0.0000001)
end

describe "MultiLineString" do

it "should write nicely" do
@m1.geom.should be_instance_of(MultiLineString)
end

it "should have 2 geometries" do
@m1.geom.should have(2).geometries
end

it "should line merge!" do
merged = @m1.line_merge
p merged, @m1.geom
merged.should be_instance_of(LineString)
p merged.class
end
end
end

end

0 comments on commit 85ddc3a

Please sign in to comment.