geekdaily-georuby 2.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/ISSUE_TEMPLATE/bug_report.md +38 -0
- data/.github/ISSUE_TEMPLATE/code-of-conduct-issue.md +10 -0
- data/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
- data/.gitignore +7 -0
- data/.rubocop.yml +8 -0
- data/.travis.yml +24 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/CONTRIBUTING.md +19 -0
- data/Gemfile +4 -0
- data/Guardfile +16 -0
- data/History.txt +4 -0
- data/MIT-LICENSE +7 -0
- data/README.md +237 -0
- data/Rakefile +32 -0
- data/bench.rb +35 -0
- data/georuby.gemspec +38 -0
- data/lib/geo_ruby.rb +11 -0
- data/lib/geo_ruby/ewk.rb +2 -0
- data/lib/geo_ruby/ewk/ewkb_parser.rb +206 -0
- data/lib/geo_ruby/ewk/ewkt_parser.rb +321 -0
- data/lib/geo_ruby/geojson.rb +139 -0
- data/lib/geo_ruby/georss.rb +156 -0
- data/lib/geo_ruby/gpx.rb +113 -0
- data/lib/geo_ruby/kml.rb +96 -0
- data/lib/geo_ruby/shp.rb +2 -0
- data/lib/geo_ruby/shp4r/dbf.rb +60 -0
- data/lib/geo_ruby/shp4r/shp.rb +726 -0
- data/lib/geo_ruby/simple_features.rb +21 -0
- data/lib/geo_ruby/simple_features/circle.rb +59 -0
- data/lib/geo_ruby/simple_features/envelope.rb +174 -0
- data/lib/geo_ruby/simple_features/geometry.rb +252 -0
- data/lib/geo_ruby/simple_features/geometry_collection.rb +143 -0
- data/lib/geo_ruby/simple_features/geometry_factory.rb +84 -0
- data/lib/geo_ruby/simple_features/helper.rb +18 -0
- data/lib/geo_ruby/simple_features/line_string.rb +224 -0
- data/lib/geo_ruby/simple_features/linear_ring.rb +34 -0
- data/lib/geo_ruby/simple_features/multi_line_string.rb +56 -0
- data/lib/geo_ruby/simple_features/multi_point.rb +52 -0
- data/lib/geo_ruby/simple_features/multi_polygon.rb +55 -0
- data/lib/geo_ruby/simple_features/point.rb +463 -0
- data/lib/geo_ruby/simple_features/polygon.rb +172 -0
- data/lib/geo_ruby/version.rb +3 -0
- data/lib/georuby.rb +2 -0
- data/spec/data/geojson/feature.json +9 -0
- data/spec/data/geojson/feature_collection.json +33 -0
- data/spec/data/georss/atom.xml +21 -0
- data/spec/data/georss/gml.xml +40 -0
- data/spec/data/georss/w3c.xml +22 -0
- data/spec/data/gpx/fells_loop.gpx +1077 -0
- data/spec/data/gpx/long.gpx +1642 -0
- data/spec/data/gpx/long.kml +31590 -0
- data/spec/data/gpx/long.nmea +2220 -0
- data/spec/data/gpx/short.gpx +13634 -0
- data/spec/data/gpx/short.kml +130 -0
- data/spec/data/gpx/tracktreks.gpx +706 -0
- data/spec/data/multipoint.dbf +0 -0
- data/spec/data/multipoint.shp +0 -0
- data/spec/data/multipoint.shx +0 -0
- data/spec/data/point.dbf +0 -0
- data/spec/data/point.shp +0 -0
- data/spec/data/point.shx +0 -0
- data/spec/data/polygon.dbf +0 -0
- data/spec/data/polygon.shp +0 -0
- data/spec/data/polygon.shx +0 -0
- data/spec/data/polyline.dbf +0 -0
- data/spec/data/polyline.shp +0 -0
- data/spec/data/polyline.shx +0 -0
- data/spec/geo_ruby/ewk/ewkb_parser_spec.rb +157 -0
- data/spec/geo_ruby/ewk/ewkt_parser_spec.rb +178 -0
- data/spec/geo_ruby/geojson_spec.rb +164 -0
- data/spec/geo_ruby/georss_spec.rb +238 -0
- data/spec/geo_ruby/gpx_spec.rb +103 -0
- data/spec/geo_ruby/kml_spec.rb +102 -0
- data/spec/geo_ruby/shp4r/shp_spec.rb +246 -0
- data/spec/geo_ruby/simple_features/circle_spec.rb +14 -0
- data/spec/geo_ruby/simple_features/envelope_spec.rb +47 -0
- data/spec/geo_ruby/simple_features/geometry_collection_spec.rb +55 -0
- data/spec/geo_ruby/simple_features/geometry_factory_spec.rb +11 -0
- data/spec/geo_ruby/simple_features/geometry_spec.rb +37 -0
- data/spec/geo_ruby/simple_features/line_string_spec.rb +268 -0
- data/spec/geo_ruby/simple_features/linear_ring_spec.rb +24 -0
- data/spec/geo_ruby/simple_features/multi_line_string_spec.rb +54 -0
- data/spec/geo_ruby/simple_features/multi_point_spec.rb +35 -0
- data/spec/geo_ruby/simple_features/multi_polygon_spec.rb +50 -0
- data/spec/geo_ruby/simple_features/point_spec.rb +547 -0
- data/spec/geo_ruby/simple_features/polygon_spec.rb +121 -0
- data/spec/geo_ruby_spec.rb +22 -0
- data/spec/spec_helper.rb +73 -0
- metadata +322 -0
@@ -0,0 +1,21 @@
|
|
1
|
+
module GeoRuby
|
2
|
+
module SimpleFeatures
|
3
|
+
%w(
|
4
|
+
geometry
|
5
|
+
circle
|
6
|
+
envelope
|
7
|
+
geometry_collection
|
8
|
+
geometry_factory
|
9
|
+
helper
|
10
|
+
line_string
|
11
|
+
linear_ring
|
12
|
+
multi_line_string
|
13
|
+
multi_point
|
14
|
+
multi_polygon
|
15
|
+
point
|
16
|
+
polygon
|
17
|
+
).each do |rel_file|
|
18
|
+
require File.join(File.dirname(__FILE__), 'simple_features', rel_file)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
module GeoRuby
|
2
|
+
module SimpleFeatures
|
3
|
+
# Represents a point. It is in 3D if the Z coordinate is not +nil+.
|
4
|
+
class Circle < Geometry
|
5
|
+
attr_accessor :radius, :center
|
6
|
+
alias_method :r, :radius
|
7
|
+
alias_method :c, :center
|
8
|
+
|
9
|
+
def initialize(srid = DEFAULT_SRID, with_z = false, with_m = false)
|
10
|
+
super(srid, with_z, with_m)
|
11
|
+
end
|
12
|
+
|
13
|
+
def bounding_box
|
14
|
+
[Point.from_x_y(@center.x - @r, @center.y - @r),
|
15
|
+
Point.from_x_y(@center.x + @r, @center.y + @r)]
|
16
|
+
end
|
17
|
+
|
18
|
+
def diameter
|
19
|
+
radius * 2
|
20
|
+
end
|
21
|
+
|
22
|
+
def m_range
|
23
|
+
fail NotImplementedError
|
24
|
+
end
|
25
|
+
|
26
|
+
def ==(other)
|
27
|
+
return false unless other.is_a?(Circle)
|
28
|
+
@center == other.center && @radius == other.radius
|
29
|
+
end
|
30
|
+
|
31
|
+
def to_json(options = {})
|
32
|
+
{ type: 'Circle',
|
33
|
+
coordinates: @center.to_coordinates,
|
34
|
+
radius: @radius }.to_json(options)
|
35
|
+
end
|
36
|
+
alias_method :as_geojson, :to_json
|
37
|
+
|
38
|
+
def contains_point?(point)
|
39
|
+
@center.euclidian_distance(point) <= radius
|
40
|
+
end
|
41
|
+
|
42
|
+
class << self
|
43
|
+
def from_x_y_r(x, y, r, srid = DEFAULT_SRID)
|
44
|
+
circle = new(srid)
|
45
|
+
circle.center = Point.from_x_y(x, y, srid)
|
46
|
+
circle.radius = r
|
47
|
+
circle
|
48
|
+
end
|
49
|
+
|
50
|
+
def from_coordinates(center, r, srid = DEFAULT_SRID)
|
51
|
+
circle = new(srid)
|
52
|
+
circle.center = Point.from_coordinates(center)
|
53
|
+
circle.radius = r
|
54
|
+
circle
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,174 @@
|
|
1
|
+
module GeoRuby
|
2
|
+
module SimpleFeatures
|
3
|
+
# Contains the bounding box of a geometry
|
4
|
+
class Envelope
|
5
|
+
attr_accessor :lower_corner, :upper_corner
|
6
|
+
attr_accessor :srid, :with_z, :zoom
|
7
|
+
|
8
|
+
# Creates a enw Envelope with +lower_corner+ as the first element
|
9
|
+
# of the corners array and +upper_corner+ as the second element
|
10
|
+
def initialize(srid = DEFAULT_SRID, with_z = false)
|
11
|
+
@srid = srid
|
12
|
+
@with_z = with_z
|
13
|
+
end
|
14
|
+
|
15
|
+
# Merges the argument with the current evelope
|
16
|
+
def extend!(envelope)
|
17
|
+
lower_corner.x = [lower_corner.x, envelope.lower_corner.x].min
|
18
|
+
lower_corner.y = [lower_corner.y, envelope.lower_corner.y].min
|
19
|
+
upper_corner.x = [upper_corner.x, envelope.upper_corner.x].max
|
20
|
+
upper_corner.y = [upper_corner.y, envelope.upper_corner.y].max
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
# Merges the argument with the current evelope and sends back a new
|
25
|
+
# envelope without changing the current one
|
26
|
+
def extend(envelope)
|
27
|
+
e = Envelope.from_points([Point.from_x_y(lower_corner.x, lower_corner.y),
|
28
|
+
Point.from_x_y(upper_corner.x, upper_corner.y)], srid, with_z)
|
29
|
+
e.extend!(envelope)
|
30
|
+
e
|
31
|
+
end
|
32
|
+
|
33
|
+
# def bounding_box(markers)
|
34
|
+
# max_lat, max_lon, min_lat, min_lon = -Float::MAX, -Float::MAX, Float::MAX, Float::MAX
|
35
|
+
# markers.each do |marker|
|
36
|
+
# coord = marker.point
|
37
|
+
# max_lat = coord.lat if coord.lat > max_lat
|
38
|
+
# min_lat = coord.lat if coord.lat < min_lat
|
39
|
+
# max_lon = coord.lng if coord.lng > max_lon
|
40
|
+
# min_lon = coord.lng if coord.lng < min_lon
|
41
|
+
# end
|
42
|
+
# min_point = Point.from_x_y(min_lat,min_lon)
|
43
|
+
# max_point = Point.from_x_y(max_lat,max_lon)
|
44
|
+
|
45
|
+
# end
|
46
|
+
# centrelat = (max_lat + min_lat)/2
|
47
|
+
# centrelng = (max_lon + min_lon)/2
|
48
|
+
# # logger.info("distance[#{distance}],zoom[#{zoom}]")
|
49
|
+
# #return GLatLngBounds.new(GLatLng.new(min_point),GLatLng.new(max_point)), [centrelat,centrelng], zoom
|
50
|
+
# return [centrelat,centrelng], zoom
|
51
|
+
|
52
|
+
# Sends back the center of the envelope
|
53
|
+
def center
|
54
|
+
Point.from_x_y((lower_corner.x + upper_corner.x) / 2, (lower_corner.y + upper_corner.y) / 2, srid)
|
55
|
+
end
|
56
|
+
|
57
|
+
# Zoom level
|
58
|
+
def zoom
|
59
|
+
distance = lower_corner.spherical_distance(upper_corner) / 10_000
|
60
|
+
@zoom = case distance
|
61
|
+
when 150..9000 then 5
|
62
|
+
when 80..149 then 6
|
63
|
+
when 50..79 then 7
|
64
|
+
when 20..49 then 8
|
65
|
+
when 10..19 then 9
|
66
|
+
when 5..9 then 10
|
67
|
+
else 13
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# Tests the equality of line strings
|
72
|
+
def ==(other_envelope)
|
73
|
+
if other_envelope.class != self.class
|
74
|
+
false
|
75
|
+
else
|
76
|
+
upper_corner == other_envelope.upper_corner && lower_corner == other_envelope.lower_corner
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# georss serialization: Dialect can be passed as option <tt>:dialect</tt>
|
81
|
+
# and set to <tt>:simple</tt> (default) <tt>:w3cgeo</tt> or <tt>:gml</tt>.
|
82
|
+
# Options <tt>:featuretypetag
|
83
|
+
def as_georss(options = {})
|
84
|
+
dialect = options[:dialect] || :simple
|
85
|
+
case (dialect)
|
86
|
+
when :simple
|
87
|
+
geom_attr = ''
|
88
|
+
if options[:featuretypetag]
|
89
|
+
geom_attr += " featuretypetag=\"#{options[:featuretypetag]}\""
|
90
|
+
end
|
91
|
+
if options[:relationshiptag]
|
92
|
+
geom_attr += " relationshiptag=\"#{options[:relationshiptag]}\""
|
93
|
+
end
|
94
|
+
geom_attr += " floor=\"#{options[:floor]}\"" if options[:floor]
|
95
|
+
geom_attr += " radius=\"#{options[:radius]}\"" if options[:radius]
|
96
|
+
geom_attr += " elev=\"#{options[:elev]}\"" if options[:elev]
|
97
|
+
|
98
|
+
georss_simple_representation(options.merge(geom_attr: geom_attr))
|
99
|
+
when :w3cgeo
|
100
|
+
georss_w3cgeo_representation(options)
|
101
|
+
when :gml
|
102
|
+
georss_gml_representation(options)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
# georss simple representation
|
107
|
+
def georss_simple_representation(options = {}) #:nodoc:
|
108
|
+
georss_ns = options[:georss_ns] || 'georss'
|
109
|
+
geom_attr = options[:geom_attr]
|
110
|
+
"<#{georss_ns}:box#{geom_attr}>#{lower_corner.y} #{lower_corner.x} #{upper_corner.y} #{upper_corner.x}</#{georss_ns}:box>\n"
|
111
|
+
end
|
112
|
+
|
113
|
+
# georss w3c representation : outputs the first point of the line
|
114
|
+
def georss_w3cgeo_representation(options = {}) #:nodoc:
|
115
|
+
w3cgeo_ns = options[:w3cgeo_ns] || 'geo'
|
116
|
+
point = center
|
117
|
+
"<#{w3cgeo_ns}:lat>#{point.y}</#{w3cgeo_ns}:lat>\n<#{w3cgeo_ns}:long>#{point.x}</#{w3cgeo_ns}:long>\n"
|
118
|
+
end
|
119
|
+
|
120
|
+
# georss gml representation
|
121
|
+
def georss_gml_representation(options = {}) #:nodoc:
|
122
|
+
georss_ns = options[:georss_ns] || 'georss'
|
123
|
+
gml_ns = options[:gml_ns] || 'gml'
|
124
|
+
result = "<#{georss_ns}:where>\n<#{gml_ns}:Envelope>\n"
|
125
|
+
result += "<#{gml_ns}:LowerCorner>#{lower_corner.y} #{lower_corner.x}"\
|
126
|
+
"</#{gml_ns}:LowerCorner>"
|
127
|
+
result += "<#{gml_ns}:UpperCorner>#{upper_corner.y} #{upper_corner.x}"\
|
128
|
+
"</#{gml_ns}:UpperCorner>"
|
129
|
+
result + "</#{gml_ns}:Envelope>\n</#{georss_ns}:where>\n"
|
130
|
+
end
|
131
|
+
|
132
|
+
# Sends back a latlonaltbox
|
133
|
+
def as_kml(options = {})
|
134
|
+
geom_data = ''
|
135
|
+
geom_data = "<altitudeMode>#{options[:altitude_mode]}</altitudeMode>\n" if options[:altitude_mode]
|
136
|
+
|
137
|
+
allow_z = with_z && (!options[:altitude_mode].nil?) && options[:atitude_mode] != 'clampToGround'
|
138
|
+
|
139
|
+
kml_representation(options.merge(geom_data: geom_data, allow_z: allow_z))
|
140
|
+
end
|
141
|
+
|
142
|
+
def kml_representation(options = {}) #:nodoc:
|
143
|
+
result = "<LatLonAltBox>\n"
|
144
|
+
result += options[:geom_data]
|
145
|
+
result += "<north>#{upper_corner.y}</north>\n"
|
146
|
+
result += "<south>#{lower_corner.y}</south>\n"
|
147
|
+
result += "<east>#{upper_corner.x}</east>\n"
|
148
|
+
result += "<west>#{lower_corner.x}</west>\n"
|
149
|
+
|
150
|
+
if with_z
|
151
|
+
result += "<minAltitude>#{lower_corner.z}</minAltitude>"
|
152
|
+
result += "<maxAltitude>#{upper_corner.z}</maxAltitude>"
|
153
|
+
end
|
154
|
+
|
155
|
+
result + "</LatLonAltBox>\n"
|
156
|
+
end
|
157
|
+
|
158
|
+
# Creates a new envelope. Accept an array of 2 points as argument
|
159
|
+
def self.from_points(points, srid = DEFAULT_SRID, with_z = false)
|
160
|
+
fail 'Not an array' unless points.class == Array
|
161
|
+
e = Envelope.new(srid, with_z)
|
162
|
+
e.lower_corner, e.upper_corner = points
|
163
|
+
e
|
164
|
+
end
|
165
|
+
|
166
|
+
# Creates a new envelope. Accept a sequence of point coordinates as argument : ((x,y),(x,y))
|
167
|
+
def self.from_coordinates(points, srid = DEFAULT_SRID, with_z = false)
|
168
|
+
e = Envelope.new(srid, with_z)
|
169
|
+
e.lower_corner, e.upper_corner = points.collect { |point_coords| Point.from_coordinates(point_coords, srid, with_z) }
|
170
|
+
e
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
@@ -0,0 +1,252 @@
|
|
1
|
+
module GeoRuby #:nodoc:
|
2
|
+
module SimpleFeatures
|
3
|
+
# TODO: Should this move to simple_features.rb instead?
|
4
|
+
# Default SRID is WGS84, see https://en.wikipedia.org/wiki/Spatial_reference_system
|
5
|
+
DEFAULT_SRID = 4326 unless defined? DEFAULT_SRID
|
6
|
+
|
7
|
+
# Root of all geometric data classes.
|
8
|
+
# Objects of class Geometry should not be instantiated.
|
9
|
+
class Geometry
|
10
|
+
# SRID of the geometry
|
11
|
+
attr_reader :srid # writer defined below
|
12
|
+
# Flag indicating if the z ordinate of the geometry is meaningful
|
13
|
+
attr_accessor :with_z
|
14
|
+
alias_method :with_z?, :with_z
|
15
|
+
# Flag indicating if the m ordinate of the geometry is meaningful
|
16
|
+
attr_accessor :with_m
|
17
|
+
alias_method :with_m?, :with_m
|
18
|
+
|
19
|
+
def initialize(srid = DEFAULT_SRID, with_z = false, with_m = false)
|
20
|
+
@srid = srid
|
21
|
+
@with_z = with_z
|
22
|
+
@with_m = with_m
|
23
|
+
end
|
24
|
+
|
25
|
+
def srid=(new_srid)
|
26
|
+
@srid = new_srid
|
27
|
+
unless self.is_a?(Point)
|
28
|
+
each do |geom|
|
29
|
+
geom.srid = new_srid
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# to be implemented in subclasses
|
35
|
+
def bounding_box
|
36
|
+
end
|
37
|
+
|
38
|
+
# to be implemented in subclasses
|
39
|
+
def m_range
|
40
|
+
end
|
41
|
+
|
42
|
+
#
|
43
|
+
# Returns an Envelope object for the geometry
|
44
|
+
#
|
45
|
+
def envelope
|
46
|
+
Envelope.from_points(bounding_box, srid, with_z)
|
47
|
+
end
|
48
|
+
|
49
|
+
#
|
50
|
+
# Outputs the geometry as an EWKB string.
|
51
|
+
#
|
52
|
+
# The +allow_srid+, +allow_z+ and +allow_m+ arguments allow the output
|
53
|
+
# to include srid, z and m respectively if they are present.
|
54
|
+
# If these arguments are set to false, srid, z and m are not included,
|
55
|
+
# even if they are present in the geometry.
|
56
|
+
# By default, the output string contains all the information in the object.
|
57
|
+
#
|
58
|
+
def as_ewkb(allow_srid = true, allow_z = true, allow_m = true)
|
59
|
+
ewkb = 1.chr # little_endian by default
|
60
|
+
|
61
|
+
type = binary_geometry_type
|
62
|
+
type |= Z_MASK if @with_z && allow_z
|
63
|
+
type |= M_MASK if @with_m && allow_m
|
64
|
+
|
65
|
+
if allow_srid
|
66
|
+
type = type | SRID_MASK
|
67
|
+
ewkb << [type, @srid].pack('VV')
|
68
|
+
else
|
69
|
+
ewkb << [type].pack('V')
|
70
|
+
end
|
71
|
+
|
72
|
+
ewkb << binary_representation(allow_z, allow_m)
|
73
|
+
end
|
74
|
+
|
75
|
+
#
|
76
|
+
# Outputs the geometry as a strict WKB string.
|
77
|
+
#
|
78
|
+
def as_wkb
|
79
|
+
as_ewkb(false, false, false)
|
80
|
+
end
|
81
|
+
|
82
|
+
#
|
83
|
+
# Outputs the geometry as a HexEWKB string.
|
84
|
+
# It is almost the same as a WKB string, except that each byte of a WKB
|
85
|
+
# string is replaced by its hex 2-char representation in a HexEWKB string.
|
86
|
+
#
|
87
|
+
def as_hex_ewkb(allow_srid = true, allow_z = true, allow_m = true)
|
88
|
+
as_ewkb(allow_srid, allow_z, allow_m).unpack('H*').join('').upcase
|
89
|
+
end
|
90
|
+
|
91
|
+
#
|
92
|
+
# Outputs the geometry as a strict HexWKB string
|
93
|
+
#
|
94
|
+
def as_hex_wkb
|
95
|
+
as_hex_ewkb(false, false, false)
|
96
|
+
end
|
97
|
+
|
98
|
+
#
|
99
|
+
# Outputs the geometry as an EWKT string.
|
100
|
+
#
|
101
|
+
def as_ewkt(allow_srid = true, allow_z = true, allow_m = true)
|
102
|
+
if allow_srid
|
103
|
+
ewkt = "SRID=#{@srid};"
|
104
|
+
else
|
105
|
+
ewkt = ''
|
106
|
+
end
|
107
|
+
ewkt << text_geometry_type
|
108
|
+
# to distinguish the M from the Z when there is actually no Z...
|
109
|
+
ewkt << 'M' if @with_m && allow_m && (!@with_z || !allow_z)
|
110
|
+
ewkt << '(' << text_representation(allow_z, allow_m) << ')'
|
111
|
+
end
|
112
|
+
|
113
|
+
#
|
114
|
+
# Outputs the geometry as strict WKT string.
|
115
|
+
#
|
116
|
+
def as_wkt
|
117
|
+
as_ewkt(false, false, false)
|
118
|
+
end
|
119
|
+
|
120
|
+
# Outputs the geometry in georss format.
|
121
|
+
# Assumes the geometries are in latlon format, with x as lon and y as lat.
|
122
|
+
# Pass the <tt>:dialect</tt> option to swhit format. Possible values are:
|
123
|
+
# <tt>:simple</tt> (default), <tt>:w3cgeo</tt> and <tt>:gml</tt>.
|
124
|
+
def as_georss(options = {})
|
125
|
+
dialect = options[:dialect] || :simple
|
126
|
+
case (dialect)
|
127
|
+
when :simple
|
128
|
+
geom_attr = ''
|
129
|
+
if options[:featuretypetag]
|
130
|
+
geom_attr += " featuretypetag=\"#{options[:featuretypetag]}\""
|
131
|
+
end
|
132
|
+
if options[:relationshiptag]
|
133
|
+
geom_attr += " relationshiptag=\"#{options[:relationshiptag]}\""
|
134
|
+
end
|
135
|
+
geom_attr += " floor=\"#{options[:floor]}\"" if options[:floor]
|
136
|
+
geom_attr += " radius=\"#{options[:radius]}\"" if options[:radius]
|
137
|
+
geom_attr += " elev=\"#{options[:elev]}\"" if options[:elev]
|
138
|
+
georss_simple_representation(options.merge(geom_attr: geom_attr))
|
139
|
+
when :w3cgeo
|
140
|
+
georss_w3cgeo_representation(options)
|
141
|
+
when :gml
|
142
|
+
georss_gml_representation(options)
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
# Iutputs the geometry in kml format : options are <tt>:id</tt>,
|
147
|
+
# <tt>:tesselate</tt>, <tt>:extrude</tt>, <tt>:altitude_mode</tt>.
|
148
|
+
# If the altitude_mode option is not present, the Z (if present) will not
|
149
|
+
# be output (since it won't be used by GE: clampToGround is the default)
|
150
|
+
def as_kml(options = {})
|
151
|
+
id_attr = ''
|
152
|
+
id_attr = " id=\"#{options[:id]}\"" if options[:id]
|
153
|
+
|
154
|
+
geom_data = ''
|
155
|
+
if options[:extrude]
|
156
|
+
geom_data += "<extrude>#{options[:extrude]}</extrude>\n"
|
157
|
+
end
|
158
|
+
if options[:tesselate]
|
159
|
+
geom_data += "<tesselate>#{options[:tesselate]}</tesselate>\n"
|
160
|
+
end
|
161
|
+
if options[:altitude_mode]
|
162
|
+
geom_data += "<altitudeMode>#{options[:altitude_mode]}</altitudeMode>\n"
|
163
|
+
end
|
164
|
+
|
165
|
+
allow_z = (with_z || !options[:altitude].nil?) &&
|
166
|
+
(!options[:altitude_mode].nil?) &&
|
167
|
+
options[:atitude_mode] != 'clampToGround'
|
168
|
+
fixed_z = options[:altitude]
|
169
|
+
|
170
|
+
kml_representation(options.merge(id_attr: id_attr, geom_data: geom_data,
|
171
|
+
allow_z: allow_z, fixed_z: fixed_z))
|
172
|
+
end
|
173
|
+
|
174
|
+
# simple geojson representation
|
175
|
+
# TODO add CRS / SRID support?
|
176
|
+
def to_json(options = {})
|
177
|
+
as_json(options).to_json(options)
|
178
|
+
end
|
179
|
+
|
180
|
+
def as_json(_options = {})
|
181
|
+
# Implemented by each class
|
182
|
+
end
|
183
|
+
alias_method :as_geojson, :as_json
|
184
|
+
|
185
|
+
# Creates a geometry based on a EWKB string. The actual class returned
|
186
|
+
# depends of the content of the string passed as argument.
|
187
|
+
# Since WKB strings are a subset of EWKB, they are also valid.
|
188
|
+
def self.from_ewkb(ewkb)
|
189
|
+
factory = GeometryFactory.new
|
190
|
+
ewkb_parser = EWKBParser.new(factory)
|
191
|
+
ewkb_parser.parse(ewkb)
|
192
|
+
factory.geometry
|
193
|
+
end
|
194
|
+
|
195
|
+
# Creates a geometry based on a HexEWKB string given.
|
196
|
+
def self.from_hex_ewkb(hexewkb)
|
197
|
+
factory = GeometryFactory.new
|
198
|
+
hexewkb_parser = HexEWKBParser.new(factory)
|
199
|
+
hexewkb_parser.parse(hexewkb)
|
200
|
+
factory.geometry
|
201
|
+
end
|
202
|
+
|
203
|
+
# Creates a geometry based on a EWKT string. Since WKT strings are a
|
204
|
+
# subset of EWKT, they are also valid.
|
205
|
+
def self.from_ewkt(ewkt)
|
206
|
+
factory = GeometryFactory.new
|
207
|
+
ewkt_parser = EWKTParser.new(factory)
|
208
|
+
ewkt_parser.parse(ewkt)
|
209
|
+
factory.geometry
|
210
|
+
end
|
211
|
+
|
212
|
+
# Creates a geometry based on the GeoRSS string given.
|
213
|
+
def self.from_georss(georss)
|
214
|
+
georss_parser = GeorssParser.new
|
215
|
+
georss_parser.parse(georss)
|
216
|
+
georss_parser.geometry
|
217
|
+
end
|
218
|
+
|
219
|
+
# sends back an array: The first element is the goemetry based on the
|
220
|
+
# GeoRSS string passed as argument. The second one is the GeoRSSTags
|
221
|
+
# (found only with the Simple format)
|
222
|
+
def self.from_georss_with_tags(georss)
|
223
|
+
georss_parser = GeorssParser.new
|
224
|
+
georss_parser.parse(georss, true)
|
225
|
+
[georss_parser.geometry, georss_parser.georss_tags]
|
226
|
+
end
|
227
|
+
|
228
|
+
# Sends back a geometry from a KML encoded geometry string.
|
229
|
+
def self.from_kml(kml)
|
230
|
+
factory = GeometryFactory.new
|
231
|
+
parser = KmlParser.new(factory)
|
232
|
+
parser.parse(kml)
|
233
|
+
end
|
234
|
+
|
235
|
+
# Some GeoJSON files do not include srid info, so
|
236
|
+
# we provide an optional parameter
|
237
|
+
def self.from_geojson(geojson, srid = DEFAULT_SRID)
|
238
|
+
geojson_parser = GeoJSONParser.new
|
239
|
+
geojson_parser.parse(geojson, srid)
|
240
|
+
geojson_parser.geometry
|
241
|
+
end
|
242
|
+
|
243
|
+
private
|
244
|
+
|
245
|
+
def self.split_coords(coords)
|
246
|
+
coords.split(' ').collect do |coord|
|
247
|
+
coord.gsub(',', ' ')
|
248
|
+
end
|
249
|
+
end
|
250
|
+
end
|
251
|
+
end
|
252
|
+
end
|