Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lib/active_record/connection_adapters/postgis/quoting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ module ConnectionAdapters
module PostGIS
module Quoting
def type_cast(value)
case value
when RGeo::Feature::Instance
if RGeo::Feature::Geometry.check_type(value)
RGeo::WKRep::WKBGenerator.new(hex_format: true, type_format: :ewkb, emit_ewkb_srid: true).generate(value)
elsif value.is_a?(RGeo::Cartesian::BoundingBox)
value.to_s
else
super
Expand Down
18 changes: 18 additions & 0 deletions test/cases/spatial_queries_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,23 @@ def test_geo_safe_where
assert_equal 1, SpatialModel.where("ST_DWITHIN(latlon_geo, ?, 500)", geographic_factory.point(-72.099, 42.099)).count
end

def test_geo_query_matches_sql
value = geographic_factory.point(-72.099, 42.099)

assert_match(
RGeo::WKRep::WKBGenerator.new(hex_format: true, type_format: :ewkb, emit_ewkb_srid: true).generate(value),
SpatialModel.where("ST_DWITHIN(latlon_geo, ?, 500)", value).explain.inspect
)
end

def test_srid_aware_query
center = RGeo::Geos.factory(srid: 4326).point(3.808591, 43.606092)
polygon = center.buffer(10_000)
model = SpatialModel.create!(geometry: polygon)

assert_equal [model], SpatialModel.where("ST_Within(?, geometry)", center)
end

private

def create_model
Expand All @@ -130,6 +147,7 @@ def create_model
t.column "latlon_geo", :st_point, srid: 4326, geographic: true
t.column "points", :multi_point, srid: 3785
t.column "path", :line_string, srid: 3785
t.column "geometry", :geometry, srid: 4326
end
SpatialModel.reset_column_information
end
Expand Down
Loading