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.


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

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

Instance Attribute Details

#sridObject (readonly)

Returns the value of attribute srid


4
5
6
# File 'lib/charta/geo_json.rb', line 4

def srid
  @srid
end

Class Method Details

.feature_collection_to_ewkt(hash) ⇒ Object Also known as: geometry_collection_to_ewkt


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

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

.feature_to_ewkt(hash) ⇒ Object


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

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

.flatten(hash) ⇒ Object


40
41
42
43
44
45
46
47
48
49
50
# File 'lib/charta/geo_json.rb', line 40

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

.flatten_feature(hash) ⇒ Object


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

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

.flatten_feature_collection(hash) ⇒ Object


52
53
54
# File 'lib/charta/geo_json.rb', line 52

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

.flatten_geometry(hash) ⇒ Object


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/charta/geo_json.rb', line 60

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.except('geometries').merge('geometries' => hash['geometries'].map { |g| flatten_geometry(g) })
    else
      raise StandardError, "Cannot handle: #{hash['type']}"
    end

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

.flatten_position(position) ⇒ Object


81
82
83
# File 'lib/charta/geo_json.rb', line 81

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

.line_string_to_ewkt(hash) ⇒ Object


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

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

.multi_line_string_to_ewkt(hash) ⇒ Object


129
130
131
132
133
134
135
136
# File 'lib/charta/geo_json.rb', line 129

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

.multi_point_to_ewkt(hash) ⇒ Object


122
123
124
125
126
127
# File 'lib/charta/geo_json.rb', line 122

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

.multi_polygon_to_ewkt(hash) ⇒ Object


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

def multi_polygon_to_ewkt(hash)
  return 'MULTIPOLYGON EMPTY' if hash['coordinates'].blank?
  '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


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

def object_to_ewkt(hash)
  send("#{hash['type'].underscore}_to_ewkt", hash)
end

.point_to_ewkt(hash) ⇒ Object


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

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

.polygon_to_ewkt(hash) ⇒ Object


113
114
115
116
117
118
119
120
# File 'lib/charta/geo_json.rb', line 113

def polygon_to_ewkt(hash)
  return 'POLYGON EMPTY' if hash['coordinates'].blank?
  '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)

34
35
36
37
38
# File 'lib/charta/geo_json.rb', line 34

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

Instance Method Details

#flattenObject


13
14
15
# File 'lib/charta/geo_json.rb', line 13

def flatten
  self.class.flatten(@json)
end

#geomObject


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

def geom
  Charta.new_geometry(to_ewkt)
end

#to_ewktObject


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

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

#valid?Boolean

Returns:

  • (Boolean)

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

def valid?
  to_ewkt
  true
rescue
  false
end