Module: GbMapfishAppserver::Array

Defined in:
lib/gb_mapfish_appserver/core_extensions.rb

Instance Method Summary collapse

Instance Method Details

#to_geojson(options = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/gb_mapfish_appserver/core_extensions.rb', line 23

def to_geojson(options = {})
  # get CRS from first feature
  if any? && first.respond_to?('geojson_srid')
    crs = first.geojson_srid
    # skip GeoJSON CRS in features
    options.merge!({:skip_feature_crs => true})
  end

  geojson = '{"type": "FeatureCollection", "features": ['
  geojson << collect {|e| e.to_geojson(options) }.join(',')
  geojson << ']'

  unless crs.blank?
    # add top level GeoJSON CRS
    geojson << ', "crs": '
    geojson << {
      :type => 'name',
      :properties => {
        :name => crs
      }
    }.to_json
  end

  geojson << '}'

  geojson
end