Skip to content

Commit 08ba9b2

Browse files
committed
refactoring - removes triplicate wkb/json conversion code #50
1 parent 95f9fc9 commit 08ba9b2

File tree

5 files changed

+78
-92
lines changed

5 files changed

+78
-92
lines changed

lib/redmine_gtt/conversions.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module RedmineGtt
2+
3+
# for some reason these conversions are not reversible, i.e. when I try to
4+
# convert from json to wkb and back to json I get
5+
#
6+
# RGeo::Error::ParseError: Bad endian byte value: 123
7+
#
8+
# It appears to work when the wkb gets written to / read from database in
9+
# between.
10+
module Conversions
11+
12+
# Turns database WKB into geometry attribute string
13+
def self.wkb_to_json(wkb, id: nil, properties: {})
14+
factory = RGeo::GeoJSON::EntityFactory.instance
15+
geometry = RGeo::WKRep::WKBParser.new(
16+
support_ewkb: true,
17+
default_srid: 4326
18+
).parse(wkb)
19+
RGeo::GeoJSON.encode factory.feature(geometry, id, properties)
20+
end
21+
22+
# Turn geometry attribute string into WKB for database use
23+
def self.to_wkb(geometry)
24+
geojson = JSON.parse(geometry)
25+
feature = RGeo::GeoJSON.decode(geojson, json_parser: :json)
26+
ewkb = RGeo::WKRep::WKBGenerator.new(
27+
type_format: :ewkb,
28+
emit_ewkb_srid: true,
29+
hex_format: true
30+
)
31+
ewkb.generate feature.geometry
32+
rescue JSON::ParserError
33+
# The Gemetry is likely to be already in WKB format
34+
geometry
35+
end
36+
37+
end
38+
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module RedmineGtt
2+
module Patches
3+
4+
# adds wkb/geojson conversion logic to the class it's included in
5+
module GeojsonAttribute
6+
7+
def geojson_additional_properties
8+
as_json except: [:geom]
9+
end
10+
11+
def geojson
12+
if geom.present?
13+
Conversions.wkb_to_json(
14+
geom, id: id, properties: geojson_additional_properties
15+
)
16+
end
17+
end
18+
19+
def geom=(geometry)
20+
if (geometry.present?)
21+
self[:geom] = Conversions.to_wkb geometry
22+
else
23+
self[:geom] = nil
24+
end
25+
end
26+
27+
end
28+
end
29+
end

lib/redmine_gtt/patches/issue_patch.rb

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,14 @@ module IssuePatch
66
def self.apply
77
unless Issue < self
88
Issue.prepend self
9+
Issue.prepend GeojsonAttribute
910
Issue.class_eval do
1011
safe_attributes "geom",
1112
if: ->(issue, user){ user.allowed_to?(:edit_issues, issue.project)}
1213
end
1314
end
1415
end
1516

16-
def geojson
17-
unless self.geom.nil?
18-
factory = RGeo::GeoJSON::EntityFactory.instance
19-
wkb = RGeo::WKRep::WKBParser.new().parse(self.geom)
20-
RGeo::GeoJSON.encode factory.feature(wkb, self.id, self.as_json)
21-
else
22-
nil
23-
end
24-
end
25-
26-
def geom=(g)
27-
# Turn geometry attribute into WKB for database use
28-
if (g.present?)
29-
begin
30-
geojson = JSON.parse(g)
31-
feature = RGeo::GeoJSON.decode(geojson, json_parser: :json)
32-
wkb = RGeo::WKRep::WKBGenerator.new(
33-
:hex_format => true
34-
)
35-
self[:geom] = wkb.generate(feature.geometry)
36-
rescue
37-
# The Gemetry is likely to be already in WKB format
38-
self[:geom] = g
39-
end
40-
else
41-
self[:geom] = nil
42-
end
43-
end
4417
end
4518

4619
end

lib/redmine_gtt/patches/project_patch.rb

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ module RedmineGtt
22
module Patches
33

44
module ProjectPatch
5+
56
def self.apply
67
unless Project < self
78
Project.prepend self
9+
Project.prepend GeojsonAttribute
810
Project.class_eval do
911
safe_attributes "geom"
1012
has_and_belongs_to_many :gtt_tile_sources
@@ -13,37 +15,6 @@ def self.apply
1315
end
1416
end
1517

16-
def geojson
17-
unless self.geom.nil?
18-
factory = RGeo::GeoJSON::EntityFactory.instance
19-
wkb = RGeo::WKRep::WKBParser.new(
20-
:support_ewkb => true,
21-
:default_srid => 4326
22-
).parse(self.geom)
23-
properties = self.as_json({except: [ :geom ]})
24-
RGeo::GeoJSON.encode factory.feature(wkb, self.id, properties)
25-
else
26-
nil
27-
end
28-
end
29-
30-
def geom=(g)
31-
# Turn geometry attribute into WKB for database use
32-
if (g.present?)
33-
geojson = JSON.parse(g)
34-
feature = RGeo::GeoJSON.decode(geojson, json_parser: :json)
35-
36-
ewkb = RGeo::WKRep::WKBGenerator.new(
37-
:type_format => :ewkb,
38-
:emit_ewkb_srid => true,
39-
:hex_format => true
40-
)
41-
self[:geom] = ewkb.generate(feature.geometry)
42-
else
43-
self[:geom] = nil
44-
end
45-
end
46-
4718
def enabled_module_names=(*_)
4819
super
4920
set_default_tile_sources
@@ -57,6 +28,7 @@ def set_default_tile_sources
5728
private :set_default_tile_sources
5829

5930
end
31+
6032
end
6133
end
6234

lib/redmine_gtt/patches/user_patch.rb

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,19 @@ module Patches
44
module UserPatch
55
def self.apply
66
unless User < self
7-
User.prepend self
7+
User.prepend GeojsonAttribute
8+
User.prepend self
89
User.class_eval do
910
safe_attributes "geom"
1011
end
1112
end
1213
end
1314

14-
def geojson
15-
unless self.geom.nil?
16-
factory = RGeo::GeoJSON::EntityFactory.instance
17-
wkb = RGeo::WKRep::WKBParser.new(
18-
:support_ewkb => true,
19-
:default_srid => 4326
20-
).parse(self.geom)
21-
properties = self.as_json({except: [
22-
:hashed_password, :hashed_password, :salt, :must_change_passwd,
23-
:passwd_changed_on, :auth_source_id, :geom
24-
]})
25-
RGeo::GeoJSON.encode factory.feature(wkb, self.id, properties)
26-
else
27-
nil
28-
end
29-
end
30-
31-
def geom=(g)
32-
# Turn geometry attribute into WKB for database use
33-
if (g.present?)
34-
geojson = JSON.parse(g)
35-
feature = RGeo::GeoJSON.decode(geojson, json_parser: :json)
36-
37-
ewkb = RGeo::WKRep::WKBGenerator.new(
38-
:type_format => :ewkb,
39-
:emit_ewkb_srid => true,
40-
:hex_format => true
41-
)
42-
self[:geom] = ewkb.generate(feature.geometry)
43-
else
44-
self[:geom] = nil
45-
end
15+
def geojson_additional_properties
16+
as_json except: %i(
17+
hashed_password hashed_password salt must_change_passwd
18+
passwd_changed_on auth_source_id geom
19+
)
4620
end
4721

4822
end

0 commit comments

Comments
 (0)