Class: Charta::GeoJSON

Inherits:
Object
  • Object
show all
Defined in:
lib/charta/geo_json.rb

Overview

Represents a Geometry with SRID

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, srid = :WGS84) ⇒ GeoJSON

Returns a new instance of GeoJSON.



8
9
10
11
12
13
14
15
16
# File 'lib/charta/geo_json.rb', line 8

def initialize(data, srid = :WGS84)
  srid ||= :WGS84
  @json = self.class.flatten(data.is_a?(Hash) ? data : JSON.parse(data))
  lsrid = @json['crs']['properties']['name'] if @json.is_a?(Hash) &&
                                                @json['crs'].is_a?(Hash) &&
                                                @json['crs']['properties'].is_a?(Hash)
  lsrid ||= srid
  @srid = ::Charta.find_srid(lsrid)
end

Instance Attribute Details

#sridObject (readonly)

Returns the value of attribute srid.



6
7
8
# File 'lib/charta/geo_json.rb', line 6

def srid
  @srid
end

Class Method Details

.feature_collection_to_ewkt(hash) ⇒ Object



94
95
96
97
98
99
# File 'lib/charta/geo_json.rb', line 94

def feature_collection_to_ewkt(hash)
  return 'GEOMETRYCOLLECTION EMPTY' if hash['features'].nil?
  'GEOMETRYCOLLECTION(' + hash['features'].collect do |feature|
    object_to_ewkt(feature)
  end.join(', ') + ')'
end

.feature_to_ewkt(hash) ⇒ Object



108
109
110
# File 'lib/charta/geo_json.rb', line 108

def feature_to_ewkt(hash)
  object_to_ewkt(hash['geometry'])
end

.flatten(hash) ⇒ Object

Force coordinates to 2D



46
47
48
49
50
51
52
53
54
# File 'lib/charta/geo_json.rb', line 46

def flatten(hash)
  if hash['type'] == 'FeatureCollection'
    flatten_feature_collection(hash)
  elsif hash['type'] == 'Feature'
    flatten_feature(hash)
  else
    flatten_geometry(hash)
  end
end

.flatten_feature(hash) ⇒ Object



60
61
62
# File 'lib/charta/geo_json.rb', line 60

def flatten_feature(hash)
  hash.merge('geometry' => flatten_geometry(hash['geometry']))
end

.flatten_feature_collection(hash) ⇒ Object



56
57
58
# File 'lib/charta/geo_json.rb', line 56

def flatten_feature_collection(hash)
  hash.merge('features' => hash['features'].map { |f| flatten_feature(f) })
end

.flatten_geometry(hash) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/charta/geo_json.rb', line 64

def flatten_geometry(hash)
  coordinates = hash['coordinates']
  flattened =
    case hash['type']
    when 'Point' then
      flatten_position(coordinates)
    when 'MultiPoint', 'LineString'
      coordinates.map { |p| flatten_position(p) }
    when 'MultiLineString', 'Polygon'
      coordinates.map { |l| l.map { |p| flatten_position(p) } }
    when 'MultiPolygon'
      coordinates.map { |m| m.map { |l| l.map { |p| flatten_position(p) } } }
    when 'GeometryCollection' then
      return hash.merge('geometries' => hash['geometries'].map { |g| flatten_geometry(g) })
    else
      raise StandardError, "Cannot handle: #{hash['type'].inspect}. In #{hash.inspect}"
    end

  hash.merge('coordinates' => flattened)
end

.flatten_position(position) ⇒ Object



85
86
87
# File 'lib/charta/geo_json.rb', line 85

def flatten_position(position)
  position[0..1]
end

.geometry_collection_to_ewkt(hash) ⇒ Object



101
102
103
104
105
106
# File 'lib/charta/geo_json.rb', line 101

def geometry_collection_to_ewkt(hash)
  return 'GEOMETRYCOLLECTION EMPTY' if hash['geometries'].nil?
  'GEOMETRYCOLLECTION(' + hash['geometries'].collect do |feature|
    object_to_ewkt(feature)
  end.join(', ') + ')'
end

.line_string_to_ewkt(hash) ⇒ Object



117
118
119
120
121
122
# File 'lib/charta/geo_json.rb', line 117

def line_string_to_ewkt(hash)
  return 'LINESTRING EMPTY' if hash['coordinates'].nil?
  'LINESTRING(' + hash['coordinates'].collect do |point|
    point.join(' ')
  end.join(', ') + ')'
end

.multi_line_string_to_ewkt(hash) ⇒ Object



140
141
142
143
144
145
146
147
# File 'lib/charta/geo_json.rb', line 140

def multi_line_string_to_ewkt(hash)
  return 'MULTILINESTRING EMPTY' if hash['coordinates'].nil?
  'MULTILINESTRING(' + hash['coordinates'].collect do |line|
    '(' + line.collect do |point|
      point.join(' ')
    end.join(', ') + ')'
  end.join(', ') + ')'
end

.multi_point_to_ewkt(hash) ⇒ Object



133
134
135
136
137
138
# File 'lib/charta/geo_json.rb', line 133

def multi_point_to_ewkt(hash)
  return 'MULTIPOINT EMPTY' if hash['coordinates'].nil?
  'MULTIPOINT(' + hash['coordinates'].collect do |point|
    '(' + point.join(' ') + ')'
  end.join(', ') + ')'
end

.multi_polygon_to_ewkt(hash) ⇒ Object

for PostGIS ST_ASGeoJSON compatibility



161
162
163
164
165
166
167
168
169
170
# File 'lib/charta/geo_json.rb', line 161

def multi_polygon_to_ewkt(hash)
  return 'MULTIPOLYGON EMPTY' if hash['coordinates'].nil?
  'MULTIPOLYGON(' + hash['coordinates'].collect do |polygon|
    '(' + polygon.collect do |hole|
      '(' + hole.collect do |point|
        point.join(' ')
      end.join(', ') + ')'
    end.join(', ') + ')'
  end.join(', ') + ')'
end

.multipolygon_to_ewkt(hash) ⇒ Object



149
150
151
152
153
154
155
156
157
158
# File 'lib/charta/geo_json.rb', line 149

def multipolygon_to_ewkt(hash)
  return 'MULTIPOLYGON EMPTY' if hash['coordinates'].nil?
  'MULTIPOLYGON(' + hash['coordinates'].collect do |polygon|
    '(' + polygon.collect do |hole|
      '(' + hole.collect do |point|
        point.join(' ')
      end.join(', ') + ')'
    end.join(', ') + ')'
  end.join(', ') + ')'
end

.object_to_ewkt(hash) ⇒ Object



89
90
91
92
# File 'lib/charta/geo_json.rb', line 89

def object_to_ewkt(hash)
  type = hash[:type] || hash['type']
  send("#{type.gsub(/(.)([A-Z])/, '\1_\2').downcase}_to_ewkt", hash)
end

.point_to_ewkt(hash) ⇒ Object



112
113
114
115
# File 'lib/charta/geo_json.rb', line 112

def point_to_ewkt(hash)
  return 'POINT EMPTY' if hash['coordinates'].nil?
  'POINT(' + hash['coordinates'].join(' ') + ')'
end

.polygon_to_ewkt(hash) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/charta/geo_json.rb', line 124

def polygon_to_ewkt(hash)
  return 'POLYGON EMPTY' if hash['coordinates'].nil?
  'POLYGON(' + hash['coordinates'].collect do |hole|
    '(' + hole.collect do |point|
      point.join(' ')
    end.join(', ') + ')'
  end.join(', ') + ')'
end

.valid?(data, srid = :WGS84) ⇒ Boolean

Test is given data is a valid GeoJSON

Returns:

  • (Boolean)


39
40
41
42
43
# File 'lib/charta/geo_json.rb', line 39

def valid?(data, srid = :WGS84)
  new(data, srid).valid?
rescue
  false
end

Instance Method Details

#geomObject



18
19
20
# File 'lib/charta/geo_json.rb', line 18

def geom
  Charta.new_geometry(to_ewkt)
end

#to_ewktObject



26
27
28
# File 'lib/charta/geo_json.rb', line 26

def to_ewkt
  "SRID=#{@srid};" + self.class.object_to_ewkt(@json)
end

#to_hashObject



22
23
24
# File 'lib/charta/geo_json.rb', line 22

def to_hash
  @json
end

#valid?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
# File 'lib/charta/geo_json.rb', line 30

def valid?
  to_ewkt
  true
rescue
  false
end