Skip to content

Commit 9c9fa48

Browse files
committed
use ST_Intersects to also find issues with complex geometries. #51
1 parent c19c1b7 commit 9c9fa48

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

lib/redmine_gtt/patches/issue_query_patch.rb

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -111,17 +111,13 @@ def sql_for_bbox_field(field, operator, value)
111111
# box = "ST_Polygon(ST_GeomFromText('LINESTRING(#{coordinates})'), 4326)"
112112
# "#{not_in}ST_Contains(#{box}, ST_SetSRID(#{db_table}.geom, 4326))"
113113

114+
114115
# So instead, I came up with this:
115-
"#{not_in}ST_MakeEnvelope(#{lng1},#{lat1},#{lng2},#{lat2}, 4326) ~ #{Issue.table_name}.geom"
116-
# I am not sure about the implications of using ~ instead of one of
117-
# the ST_ functions. Appears that it is a PostgreSQL geometric
118-
# operator, leading to casting the PostGIS values to a PostgreSQL
119-
# geometric type. Fact is, this statement does not seem to care
120-
# about the SRID (can even leave it out of the MakeEnvelope call).
121-
# Doesn't feel right but works for my simple test case and is
122-
# probably good enough for this simple case (bbox is a rectangle and
123-
# other geometry is a point). We should check if indizes are actually
124-
# used though.
116+
# "#{not_in}ST_MakeEnvelope(#{lng1},#{lat1},#{lng2},#{lat2}, 4326) ~ #{Issue.table_name}.geom"
117+
#
118+
# And then with this, which also handles geometries that are not simple
119+
# points:
120+
"#{not_in} ST_Intersects(#{Issue.table_name}.geom, ST_MakeEnvelope(#{lng1},#{lat1},#{lng2},#{lat2}, 4326))"
125121
end
126122

127123
private

test/integration/issue_filter_api_test.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ class IssueFilterApiTest < Redmine::ApiTest::Base
4848
}
4949
}
5050

51+
POLY_IN = {
52+
'type' => 'Feature',
53+
'geometry' => {
54+
'type' => 'Polygon',
55+
'coordinates' => [
56+
[[123.269691,9.305099], [123.279691,9.305099],[123.279691,9.405099],[123.269691,9.405099]]
57+
]
58+
}
59+
}
60+
5161
# x1,y1,x2,y2 (Lng1,Lat1,...)
5262
BBOX = '123.193645|9.256139|123.331833|9.364216'
5363

@@ -204,4 +214,32 @@ class IssueFilterApiTest < Redmine::ApiTest::Base
204214
assert_equal issue_out.id, data['issues'][0]['id']
205215
end
206216

217+
218+
test 'should find polygon included in bounding box' do
219+
220+
get '/projects/ecookbook/issues.json', params: { status_id: 'o' }
221+
222+
assert_response :success
223+
assert data = JSON.parse(response.body)
224+
assert_equal 6, data['issues'].size
225+
226+
issue_in = @project.issues.find 1
227+
issue_in.update_attribute :geojson, POLY_IN.to_json
228+
229+
issue_out = @project.issues.find 2
230+
issue_out.update_attribute :geojson, POINT_OUT.to_json
231+
232+
# find everyting inside the given box
233+
# using the shorter API parameter format
234+
235+
get '/projects/ecookbook/issues.json', params: {
236+
status_id: 'o', bbox: "=#{BBOX}"
237+
}
238+
assert_response :success
239+
assert data = JSON.parse(response.body)
240+
assert_equal 1, data['issues'].size
241+
assert_equal issue_in.id, data['issues'][0]['id']
242+
end
243+
244+
207245
end

0 commit comments

Comments
 (0)