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/coordinates.rb,
lib/charta/line_string.rb,
lib/charta/bounding_box.rb,
lib/charta/multi_polygon.rb,
lib/charta/ewkt_serializer.rb,
lib/charta/geometry_collection.rb
Overview
Charta aims to supply easy geom/geog tools
Defined Under Namespace
Modules: Coordinates, EwktSerializer
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.17'.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
186
187
188
189
190
|
# File 'lib/charta.rb', line 186
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.
144
145
146
147
148
149
150
151
152
153
154
155
|
# File 'lib/charta.rb', line 144
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
139
140
141
|
# File 'lib/charta.rb', line 139
def find_system(_srname)
nil
end
|
.find_system_by_srid(_srid) ⇒ Object
131
132
133
|
# File 'lib/charta.rb', line 131
def find_system_by_srid(_srid)
nil
end
|
.find_system_by_urn(_urn) ⇒ Object
135
136
137
|
# File 'lib/charta.rb', line 135
def find_system_by_urn(_urn)
nil
end
|
.from(format, data) ⇒ Object
157
158
159
160
161
162
|
# File 'lib/charta.rb', line 157
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
172
173
174
|
# File 'lib/charta.rb', line 172
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
164
165
166
|
# File 'lib/charta.rb', line 164
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
168
169
170
|
# File 'lib/charta.rb', line 168
def from_kml(data, flatten_collection = false)
new_geometry(::Charta::KML.new(data).to_ewkt, nil, nil, flatten_collection)
end
|
.generate_ewkt(feature) ⇒ Object
120
121
122
123
|
# File 'lib/charta.rb', line 120
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
108
109
110
111
112
113
114
|
# File 'lib/charta.rb', line 108
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
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
|
# File 'lib/charta.rb', line 32
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
# File 'lib/charta.rb', line 82
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
103
104
105
106
|
# File 'lib/charta.rb', line 103
def new_point(lat, lon, srid = 4326)
feature = Charta.new_feature("SRID=#{srid};POINT(#{lon} #{lat})")
Point.new(feature)
end
|
.parse_ewkt(coordinates) ⇒ Object
125
126
127
128
129
|
# File 'lib/charta.rb', line 125
def parse_ewkt(coordinates)
coordinates
end
|
.underscore(text) ⇒ Object
178
179
180
181
182
183
184
|
# File 'lib/charta.rb', line 178
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
|