Module: Charta
- Defined in:
- lib/charta.rb,
lib/charta/gml.rb,
lib/charta/kml.rb,
lib/charta/point.rb,
lib/charta/polygon.rb,
lib/charta/version.rb,
lib/charta/geo_json.rb,
lib/charta/geometry.rb,
lib/charta/line_string.rb,
lib/charta/bounding_box.rb,
lib/charta/multi_polygon.rb,
lib/charta/geometry_collection.rb
Overview
Charta aims to supply easy geom/geog tools
Defined Under Namespace
Classes: BoundingBox, GML, GeoJSON, Geometry, GeometryCollection, KML, LineString, MultiPolygon, Point, Polygon
Constant Summary
collapse
- SRS =
{
WGS84: 4326,
CRS84: 4326,
RGF93: 2143
}.freeze
- VERSION =
'0.1.8'.freeze
Class Method Summary
collapse
-
.camelcase(text, first_letter = :upper) ⇒ Object
-
.empty_geometry(srid = :WGS84) ⇒ Object
-
.find_srid(srname_or_srid) ⇒ Object
Check and returns the SRID matching with srname or SRID.
-
.find_system(_srname) ⇒ Object
-
.find_system_by_srid(_srid) ⇒ Object
-
.find_system_by_urn(_urn) ⇒ Object
-
.from(format, data) ⇒ Object
-
.from_geojson(data, srid = nil) ⇒ Object
-
.from_gml(data, srid = nil, flatten_collection = false) ⇒ Object
-
.from_kml(data, flatten_collection = false) ⇒ Object
-
.generate_ewkt(feature) ⇒ Object
-
.make_line(points, options = {}) ⇒ Object
-
.new_feature(coordinates, srs = nil, format = nil, _flatten_collection = true, _options = {}) ⇒ Object
-
.new_geometry(coordinates, srs = nil, format = nil, _flatten_collection = true, _options = {}) ⇒ Object
-
.new_point(lat, lon, srid = 4326) ⇒ Object
-
.parse_ewkt(coordinates) ⇒ Object
-
.underscore(text) ⇒ Object
Class Method Details
.camelcase(text, first_letter = :upper) ⇒ Object
182
183
184
185
186
|
# File 'lib/charta.rb', line 182
def camelcase(text, first_letter = :upper)
ret = text.split(/[_\-]+/).map { |word| word[0..0].upcase + word[1..-1].downcase }.join
ret = text[0..0].downcase + text[1..-1] if first_letter == :lower
ret
end
|
.empty_geometry(srid = :WGS84) ⇒ Object
.find_srid(srname_or_srid) ⇒ Object
Check and returns the SRID matching with srname or SRID.
140
141
142
143
144
145
146
147
148
149
150
151
|
# File 'lib/charta.rb', line 140
def find_srid(srname_or_srid)
if srname_or_srid.to_s =~ /\Aurn:ogc:def:crs:.*\z/
x = srname_or_srid.split(':').last.upcase.to_sym
SRS[x] || x
elsif srname_or_srid.to_s =~ /\AEPSG::?(\d{4,5})\z/
srname_or_srid.split(':').last
elsif srname_or_srid.to_s =~ /\A\d+\z/
srname_or_srid.to_i
else
SRS[srname_or_srid] || srname_or_srid
end
end
|
.find_system(_srname) ⇒ Object
135
136
137
|
# File 'lib/charta.rb', line 135
def find_system(_srname)
nil
end
|
.find_system_by_srid(_srid) ⇒ Object
127
128
129
|
# File 'lib/charta.rb', line 127
def find_system_by_srid(_srid)
nil
end
|
.find_system_by_urn(_urn) ⇒ Object
131
132
133
|
# File 'lib/charta.rb', line 131
def find_system_by_urn(_urn)
nil
end
|
.from(format, data) ⇒ Object
153
154
155
156
157
158
|
# File 'lib/charta.rb', line 153
def from(format, data)
unless respond_to?("from_#{format}")
raise "Unknown format: #{format.inspect}"
end
send("from_#{format}", data)
end
|
.from_geojson(data, srid = nil) ⇒ Object
168
169
170
|
# File 'lib/charta.rb', line 168
def from_geojson(data, srid = nil)
new_geometry(::Charta::GeoJSON.new(data, srid).to_ewkt)
end
|
.from_gml(data, srid = nil, flatten_collection = false) ⇒ Object
160
161
162
|
# File 'lib/charta.rb', line 160
def from_gml(data, srid = nil, flatten_collection = false)
new_geometry(::Charta::GML.new(data, srid).to_ewkt, nil, nil, flatten_collection)
end
|
.from_kml(data, flatten_collection = false) ⇒ Object
164
165
166
|
# File 'lib/charta.rb', line 164
def from_kml(data, flatten_collection = false)
new_geometry(::Charta::KML.new(data).to_ewkt, nil, nil, flatten_collection)
end
|
.generate_ewkt(feature) ⇒ Object
116
117
118
119
|
# File 'lib/charta.rb', line 116
def generate_ewkt(feature)
generator = RGeo::WKRep::WKTGenerator.new(tag_format: :ewkt, emit_ewkt_srid: true)
generator.generate(feature)
end
|
.make_line(points, options = {}) ⇒ Object
104
105
106
107
108
109
110
|
# File 'lib/charta.rb', line 104
def make_line(points, options = {})
options[:srid] ||= new_geometry(points.first).srid if points.any?
options[:srid] ||= 4326
ewkt = "SRID=#{options[:srid]};LINESTRING(" + points.map { |wkt| p = new_geometry(wkt); "#{p.x} #{p.y}" }.join(', ') + ')'
new_geometry(ewkt)
end
|
.new_feature(coordinates, srs = nil, format = nil, _flatten_collection = true, _options = {}) ⇒ Object
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
|
# File 'lib/charta.rb', line 28
def new_feature(coordinates, srs = nil, format = nil, _flatten_collection = true, _options = {})
geom_ewkt = nil
if coordinates.is_a?(RGeo::Feature::Instance)
return Geometry.feature(coordinates)
elsif coordinates.is_a?(::Charta::Geometry)
return coordinates
elsif coordinates.to_s =~ /\A[[:space:]]*\z/
geom_ewkt = empty_geometry(srs).to_ewkt
elsif coordinates.is_a?(Hash) || (coordinates.is_a?(String) && ::Charta::GeoJSON.valid?(coordinates)) srid = srs ? find_srid(srs) : :WGS84
geom_ewkt = ::Charta::GeoJSON.new(coordinates, srid).to_ewkt
elsif coordinates.is_a?(String)
geom_ewkt = if coordinates =~ /\A[A-F0-9]+\z/ if srs && srid = find_srid(srs)
generate_ewkt RGeo::Geos.factory(srid: srid).parse_wkb(coordinates)
else
generate_ewkt Geometry.factory.parse_wkb(coordinates)
end
elsif format == 'gml' && ::Charta::GML.valid?(coordinates)
::Charta::GML.new(coordinates, srid).to_ewkt
elsif format == 'kml' && ::Charta::KML.valid?(coordinates)
::Charta::KML.new(coordinates).to_ewkt
elsif coordinates =~ /^SRID\=\d+\;/i
if feature = Geometry.feature(coordinates)
generate_ewkt feature
else
Charta::GeometryCollection.empty.feature
end
else if srs && srid = find_srid(srs)
begin
f = RGeo::Geos.factory(srid: srid).parse_wkt(coordinates)
rescue RGeo::Error::ParseError => e
raise "Invalid EWKT (#{e.message}): #{coordinates}"
end
generate_ewkt f
else
generate_ewkt Geometry.feature(coordinates)
end
end
else geom_ewkt = generate_ewkt coordinates
end
if geom_ewkt.to_s =~ /\A[[:space:]]*\z/
raise ArgumentError, "Invalid data: coordinates=#{coordinates.inspect}, srid=#{srid.inspect}"
end
Geometry.feature(geom_ewkt)
end
|
.new_geometry(coordinates, srs = nil, format = nil, _flatten_collection = true, _options = {}) ⇒ Object
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/charta.rb', line 78
def new_geometry(coordinates, srs = nil, format = nil, _flatten_collection = true, _options = {})
return coordinates if coordinates.is_a?(::Charta::Geometry)
feature = Charta.new_feature(coordinates, srs, format, _flatten_collection, _options)
type = feature.geometry_type
geom = case type
when RGeo::Feature::Point then
Point.new(feature)
when RGeo::Feature::LineString then
LineString.new(feature)
when RGeo::Feature::Polygon then
Polygon.new(feature)
when RGeo::Feature::MultiPolygon then
MultiPolygon.new(feature)
when RGeo::Feature::GeometryCollection then
GeometryCollection.new(feature)
else
Geometry.new(feature)
end
geom
end
|
.new_point(lat, lon, srid = 4326) ⇒ Object
99
100
101
102
|
# File 'lib/charta.rb', line 99
def new_point(lat, lon, srid = 4326)
feature = Charta.new_feature("SRID=#{srid};POINT(#{lon} #{lat})")
Point.new(feature)
end
|
.parse_ewkt(coordinates) ⇒ Object
121
122
123
124
125
|
# File 'lib/charta.rb', line 121
def parse_ewkt(coordinates)
coordinates
end
|
.underscore(text) ⇒ Object
174
175
176
177
178
179
180
|
# File 'lib/charta.rb', line 174
def underscore(text)
text.gsub(/::/, '/')
.gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
.gsub(/([a-z\d])([A-Z])/, '\1_\2')
.tr('-', '_')
.downcase
end
|