-
Notifications
You must be signed in to change notification settings - Fork 40
/
entities.rb
225 lines (170 loc) · 6.42 KB
/
entities.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# frozen_string_literal: true
module RGeo
module GeoJSON
# This is a GeoJSON wrapper entity that corresponds to the GeoJSON
# "Feature" type. It is an immutable type.
#
# This is the default implementation that is generated by
# RGeo::GeoJSON::EntityFactory. You may replace this implementation
# by writing your own entity factory. Note that an alternate Feature
# implementation need not subclass or even duck-type this class.
# the entity factory mediates all interaction between the GeoJSON
# engine and features.
class Feature
# Create a feature wrapping the given geometry, with the given ID
# and properties.
def initialize(geometry, id = nil, properties = {})
@geometry = geometry
@id = id
@properties = {}
properties.each do |k, v|
@properties[k.to_s] = v
end
end
def inspect
"#<#{self.class}:0x#{object_id.to_s(16)} id=#{@id.inspect} geom=#{@geometry ? @geometry.as_text.inspect : 'nil'}>"
end
def to_s
inspect
end
def hash
@geometry.hash + @id.hash + @properties.hash
end
# Two features are equal if their geometries, IDs, and properties
# are all equal.
# This method uses the eql? method to test geometry equality, which
# may behave differently than the == operator.
def eql?(other)
other.is_a?(Feature) && @geometry.eql?(other.geometry) && @id.eql?(other.feature_id) && @properties.eql?(other.instance_variable_get(:@properties))
end
# Two features are equal if their geometries, IDs, and properties
# are all equal.
# This method uses the == operator to test geometry equality, which
# may behave differently than the eql? method.
def ==(other)
other.is_a?(Feature) && @geometry == other.geometry && @id == other.feature_id && @properties == other.instance_variable_get(:@properties)
end
# Returns the geometry contained in this feature, which may be nil.
attr_reader :geometry
# Returns the ID for this feature, which may be nil.
def feature_id
@id
end
# Returns a copy of the properties for this feature.
def properties
@properties.dup
end
# Gets the value of the given named property.
# Returns nil if the given property is not found.
def property(key)
@properties[key.to_s]
end
alias [] property
# Gets an array of the known property keys in this feature.
def keys
@properties.keys
end
end
# This is a GeoJSON wrapper entity that corresponds to the GeoJSON
# "FeatureCollection" type. It is an immutable type.
#
# This is the default implementation that is generated by
# RGeo::GeoJSON::EntityFactory. You may replace this implementation
# by writing your own entity factory. Note that an alternate
# FeatureCollection implementation need not subclass or even
# duck-type this class. The entity factory mediates all interaction
# between the GeoJSON engine and feature collections.
class FeatureCollection
include Enumerable
# Create a new FeatureCollection with the given features, which must
# be provided as an Enumerable.
def initialize(features = [])
@features = []
features.each { |f| @features << f if f.is_a?(Feature) }
end
def inspect
"#<#{self.class}:0x#{object_id.to_s(16)}>"
end
def to_s
inspect
end
def hash
@features.hash
end
# Two feature collections are equal if they contain the same
# features in the same order.
# This methods uses the eql? method to test geometry equality, which
# may behave differently than the == operator.
def eql?(other)
other.is_a?(FeatureCollection) && @features.eql?(other.instance_variable_get(:@features))
end
# Two feature collections are equal if they contain the same
# features in the same order.
# This methods uses the == operator to test geometry equality, which
# may behave differently than the eql? method.
def ==(other)
other.is_a?(FeatureCollection) && @features == other.instance_variable_get(:@features)
end
# Iterates or returns an iterator for the features.
def each(&block)
@features.each(&block)
end
# Returns the number of features contained in this collection.
def size
@features.size
end
# Access a feature by index.
def [](index)
@features[index]
end
end
# This is the default entity factory. It creates objects of type
# RGeo::GeoJSON::Feature and RGeo::GeoJSON::FeatureCollection.
# You may create your own entity factory by duck-typing this class.
class EntityFactory
# Create and return a new feature, given geometry, ID, and
# properties hash. Note that, per the GeoJSON spec, geometry and/or
# properties may be nil.
def feature(geometry, id = nil, properties = nil)
Feature.new(geometry, id, properties || {})
end
# Create and return a new feature collection, given an enumerable
# of feature objects.
def feature_collection(features = [])
FeatureCollection.new(features)
end
# Returns true if the given object is a feature created by this
# entity factory.
def is_feature?(object)
object.is_a?(Feature)
end
# Returns true if the given object is a feature collection created
# by this entity factory.
def is_feature_collection?(object)
object.is_a?(FeatureCollection)
end
# Run Enumerable#map on the features contained in the given feature
# collection.
def map_feature_collection(object, &block)
object.map(&block)
end
# Returns the geometry associated with the given feature.
def get_feature_geometry(object)
object.geometry
end
# Returns the ID of the given feature, or nil for no ID.
def get_feature_id(object)
object.feature_id
end
# Returns the properties of the given feature as a hash. Editing
# this hash does not change the state of the feature.
def get_feature_properties(object)
object.properties
end
# Return the singleton instance of EntityFactory.
def self.instance
@instance ||= new
end
end
end
end