Skip to content

Commit 0613fa7

Browse files
committed
geojson formatting for printing
1 parent d59f353 commit 0613fa7

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

lib/redmine_gtt/patches/geojson_attribute.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,25 @@ def geojson
6363
end
6464
end
6565

66+
# returns a hash with two values:
67+
# :geojson is the feature with coordinates transformed to EPS 3857
68+
# :center is the geometric center of the geometry as computed by
69+
# ST_Centroid, in EPS 3857 as well.
70+
#
71+
# TODO for printing of multiple issues this should be optimized to avoid
72+
# doing a single select for each issue
73+
def geodata_for_print
74+
if row = self.class.where(id: id).where.not(geom: nil).
75+
pluck("ST_AsGeoJson(ST_Transform(geom, 3857)) as geojson, ST_Transform(ST_Centroid(geom), 3857) as center").
76+
first
77+
json, center = *row
78+
{
79+
geojson: Conversions.to_feature(json),
80+
center: [center.x, center.y]
81+
}
82+
end
83+
end
84+
6685
# sets the geojson attribute for reading / writing to the DB
6786
def geojson=(geometry)
6887
@geojson = geometry

test/test_helper.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
# Load the Redmine helper
22
require File.expand_path(File.dirname(__FILE__) + '/../../../test/test_helper')
33

4-
class GttTest < ActiveSupport::TestCase
4+
module GttTestData
5+
def test_geojson
6+
{'type'=>'Feature','geometry'=>{ 'type'=>'Polygon','coordinates'=> test_coordinates}}.to_json
7+
end
58

69
def test_coordinates
710
[[[135.220734222412,34.7056906000311],[135.302273376465,34.6990600142143],[135.300041778564,34.6709699843709],[135.252834899902,34.6760523038894],[135.194212539673,34.6766840435102],[135.220734222412,34.7056906000311]]]
811
end
912

10-
def test_geojson
11-
{'type'=>'Feature','geometry'=>{ 'type'=>'Polygon','coordinates'=> test_coordinates}}.to_json
12-
end
13-
1413
def test_geom
1514
RedmineGtt::Conversions::WkbToGeom.("01030000000100000006000000C84B374110E76040381DD011545A4140C84B3739ACE96040F07E6DCC7A594140C84B37F199E960403CBC2D58E2554140C84B373917E8604098CBC3E188564140C84B37FD36E66040F24C2E959D564140C84B374110E76040381DD011545A4140")
1615
end
16+
end
17+
18+
class GttTest < ActiveSupport::TestCase
19+
include GttTestData
1720

1821
def assert_geojson(json)
1922
json = JSON.parse json if json.is_a?(String)

test/unit/issue_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ class IssueTest < GttTest
2828
assert_geojson @issue.geojson
2929
end
3030

31+
test 'should have geojson for print' do
32+
assert d = @issue.geodata_for_print
33+
assert center = d[:center]
34+
assert_equal 2, center.size
35+
assert geom = d[:geojson]['geometry']
36+
assert coords = geom['coordinates']
37+
assert_equal 15052703.2783315, coords.flatten.first
38+
end
39+
3140
test 'should render properties in as_geojson' do
3241
j = @issue.as_geojson include_properties: true
3342
assert_geojson j

0 commit comments

Comments
 (0)