From d04bf4acb8de10e43129ddf4d6f329eed42e84c3 Mon Sep 17 00:00:00 2001 From: Marcos Piccinini Date: Mon, 20 Sep 2010 18:08:46 -0300 Subject: [PATCH] add/fixes some specs --- lib/postgis_adapter.rb | 4 +-- lib/postgis_adapter/common_spatial_adapter.rb | 2 +- lib/postgis_adapter/functions/common.rb | 26 ++++++++++++++++--- spec/postgis_adapter/functions/class_spec.rb | 4 +++ spec/postgis_adapter/functions/common_spec.rb | 6 +++++ 5 files changed, 36 insertions(+), 6 deletions(-) diff --git a/lib/postgis_adapter.rb b/lib/postgis_adapter.rb index ee8a156..559dfb4 100644 --- a/lib/postgis_adapter.rb +++ b/lib/postgis_adapter.rb @@ -254,10 +254,10 @@ def columns(table_name, name = nil) #:nodoc: alias :original_disable_referential_integrity :disable_referential_integrity def disable_referential_integrity(&block) #:nodoc: ignore_tables = %w{ geometry_columns spatial_ref_sys geography_columns } - execute(tables.select { |name| !ignore_tables.include?(name) }.collect { |name| "ALTER TABLE #{quote_table_name(name)} DISABLE TRIGGER ALL" }.join(";")) + execute(tables.select { |name| !ignore_tables.include?(name) }.map { |name| "ALTER TABLE #{quote_table_name(name)} DISABLE TRIGGER ALL" }.join(";")) yield ensure - execute(tables.select { |name| !ignore_tables.include?(name)}.collect { |name| "ALTER TABLE #{quote_table_name(name)} ENABLE TRIGGER ALL" }.join(";")) + execute(tables.select { |name| !ignore_tables.include?(name)}.map { |name| "ALTER TABLE #{quote_table_name(name)} ENABLE TRIGGER ALL" }.join(";")) end end diff --git a/lib/postgis_adapter/common_spatial_adapter.rb b/lib/postgis_adapter/common_spatial_adapter.rb index 33e60bd..85aee5d 100644 --- a/lib/postgis_adapter/common_spatial_adapter.rb +++ b/lib/postgis_adapter/common_spatial_adapter.rb @@ -49,7 +49,7 @@ def table(table, stream) columns.each do |column| - raise StandardError, "Unknown type '#{column.sql_type}' for column '#{column.name}'" if @types[column.type].nil? + raise StandardError, "Unknown type '#{column.sql_type}' for column '#{column.name}' in table '#{table}'" if @types[column.type].nil? next if column.name == pk #need to use less_simplified_type here or have each specific geometry type be simplified to a specific simplified type in Column and each one treated separately in the Column methods if column.is_a?(SpatialColumn) diff --git a/lib/postgis_adapter/functions/common.rb b/lib/postgis_adapter/functions/common.rb index 804af6d..8b4b843 100644 --- a/lib/postgis_adapter/functions/common.rb +++ b/lib/postgis_adapter/functions/common.rb @@ -9,6 +9,13 @@ module PostgisAdapter module Functions + # + # Test if a geometry is well formed. + # + def valid_geom? + postgis_calculate(:isvalid, self) + end + # # True if the given geometries represent the same geometry. # Directionality is ignored. @@ -233,12 +240,17 @@ def simplify(tolerance=0.1) postgis_calculate(:simplify, self, tolerance) end - def simplify!(tolerance=0.1) #FIXME: not good.. self.update_attribute(geo_columns.first, simplify) end + # + # + def buffer(width=0.1) + postgis_calculate(:buffer, self, width) + end + # # Returns a "simplified" version of the given geometry using the Douglas-Peuker # algorithm. Will avoid creating derived geometries (polygons in particular) that @@ -510,10 +522,18 @@ def to_utm # # http://geojson.org/ # - def as_geo_json(precision=15, bbox=0) + def as_geo_json(precision=15, bbox = 0) postgis_calculate(:AsGeoJSON, self, [precision, bbox]) end + # + # ST_PointOnSurface — Returns a POINT guaranteed to lie on the surface. + # + # geometry ST_PointOnSurface(geometry g1);eometry A, geometry B); + # + def point_on_surface + postgis_calculate(:pointonsurface, self) + end # # @@ -875,7 +895,7 @@ def covers? other end end - + # # # diff --git a/spec/postgis_adapter/functions/class_spec.rb b/spec/postgis_adapter/functions/class_spec.rb index 0c5789b..08e34de 100644 --- a/spec/postgis_adapter/functions/class_spec.rb +++ b/spec/postgis_adapter/functions/class_spec.rb @@ -9,6 +9,10 @@ @p2 ||= Position.create!(:data => "PointClassClose", :geom => Point.from_x_y(99.9,99.9,4326)) end + after(:all) do + [City, Street, Position].each { |m| m.delete_all } + end + it "should find the closest other point" do Position.close_to(Point.from_x_y(99,99,4326), :srid => 4326)[0].data.should == @p1.data end diff --git a/spec/postgis_adapter/functions/common_spec.rb b/spec/postgis_adapter/functions/common_spec.rb index eb8fbdf..e5e5852 100644 --- a/spec/postgis_adapter/functions/common_spec.rb +++ b/spec/postgis_adapter/functions/common_spec.rb @@ -126,6 +126,8 @@ describe "Polygon" do + it { City.first.data.should eql("City1") } + it "sort by area size" do City.by_area.first.data.should == "City1" #[@c1, @c2, @c3] end @@ -268,6 +270,10 @@ @s1.intersection(@p2).should be_instance_of(GeometryCollection) end + it "have a point on surface" do + @s3.point_on_surface.should be_a GeoRuby::SimpleFeatures::Point + end + describe "Self" do it do