-
Notifications
You must be signed in to change notification settings - Fork 40
/
encode_geometry_test.rb
147 lines (137 loc) · 4.44 KB
/
encode_geometry_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# frozen_string_literal: true
require_relative "../test_helper"
class CoderTest < Minitest::Test # :nodoc:
include TestHelper
def setup
@geo_factory = RGeo::Cartesian.simple_factory(srid: 4326)
@entity_factory = RGeo::GeoJSON::EntityFactory.instance
@coder = RGeo::GeoJSON::Coder.new(
geo_factory: @geo_factory,
entity_factory: @entity_factory
)
end
def test_encode_geometry_point
point = rand_point
assert_equal(
{ "type" => "Point", "coordinates" => [point.x, point.y] },
@coder.send(:encode_geometry, point)
)
end
def test_encode_geometry_linestring
point1 = rand_point
point2 = rand_point
point3 = rand_point
line = @geo_factory.line_string([point1, point2, point3])
assert_equal(
{ "type" => "LineString", "coordinates" => [
[point1.x, point1.y], [point2.x, point2.y], [point3.x, point3.y]
] },
@coder.send(:encode_geometry, line)
)
end
def test_encode_geometry_polygon
point1 = rand_point
point2 = rand_point
point3 = rand_point
exterior = @geo_factory.linear_ring([point1, point2, point3, point1])
polygon = @geo_factory.polygon(exterior)
assert_equal(
{ "type" => "Polygon", "coordinates" => [[
[point1.x, point1.y],
[point2.x, point2.y],
[point3.x, point3.y],
[point1.x, point1.y]
]] },
@coder.send(:encode_geometry, polygon)
)
end
def test_encode_geometry_polygon_with_one_hole
point1 = @geo_factory.point(0.1, 0.2)
point2 = @geo_factory.point(0.1, 9.2)
point3 = @geo_factory.point(10.1, 9.2)
point4 = @geo_factory.point(10.1, 0.2)
point5 = @geo_factory.point(4.1, 4.2)
point6 = @geo_factory.point(5.1, 6.2)
point7 = @geo_factory.point(6.1, 4.2)
exterior = @geo_factory.linear_ring([point1, point2, point3, point4, point1])
interior = @geo_factory.linear_ring([point5, point6, point7, point5])
polygon = @geo_factory.polygon(exterior, [interior])
assert_equal(
{ "type" => "Polygon", "coordinates" => [
[
[point1.x, point1.y],
[point2.x, point2.y],
[point3.x, point3.y],
[point4.x, point4.y],
[point1.x, point1.y]
],
[
[point5.x, point5.y],
[point6.x, point6.y],
[point7.x, point7.y],
[point5.x, point5.y]
]
] },
@coder.send(:encode_geometry, polygon)
)
end
def test_encode_geometry_multipoint
point1 = rand_point
point2 = rand_point
multipoint = @geo_factory.multi_point([point1, point2])
assert_equal(
{ "type" => "MultiPoint", "coordinates" => [
[point1.x, point1.y], [point2.x, point2.y]
] },
@coder.send(:encode_geometry, multipoint)
)
end
def test_encode_geometry_multilinestring
linestring1 = rand_linestring(2)
linestring2 = rand_linestring(3)
multilinestring = @geo_factory.multi_line_string([linestring1, linestring2])
assert_equal(
{ "type" => "MultiLineString", "coordinates" => [
linestring1.coordinates,
linestring2.coordinates
] },
@coder.send(:encode_geometry, multilinestring)
)
end
def test_encode_geometry_multipolygon
point1 = @geo_factory.point(0, 0)
point2 = @geo_factory.point(0, 10)
point3 = @geo_factory.point(10, 10)
point4 = @geo_factory.point(10, 0)
point5 = @geo_factory.point(4, 4)
point6 = @geo_factory.point(5, 6)
point7 = @geo_factory.point(6, 4)
point8 = @geo_factory.point(0, -10)
point9 = @geo_factory.point(-10, 0)
exterior1 = @geo_factory.linear_ring([point1, point8, point9, point1])
exterior2 = @geo_factory.linear_ring([point1, point2, point3, point4, point1])
interior2 = @geo_factory.linear_ring([point5, point6, point7, point5])
exterior3 = @geo_factory.linear_ring([point1, point2, point3, point1])
poly1 = @geo_factory.polygon(exterior1)
poly2 = @geo_factory.polygon(exterior2, [interior2])
poly3 = @geo_factory.polygon(exterior3)
multypolygon = @geo_factory.multi_polygon([poly1, poly2, poly3])
assert_equal(
{ "type" => "MultiPolygon", "coordinates" => [
[ # poly1
exterior1.coordinates
],
[ # poly2
exterior2.coordinates,
interior2.coordinates
],
[ # poly3
exterior3.coordinates
]
] },
@coder.send(:encode_geometry, multypolygon)
)
end
def test_encode_geometry_geometrycollection
end
end