Module: Gis::GeoJSON

Defined in:
lib/gis/geo_json.rb

Class Method Summary collapse

Class Method Details

.aggregation(objects, properties = nil) ⇒ JSON object

Parameters:

  • objects (Array feature_collections)

Returns:

  • (JSON object)


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/gis/geo_json.rb', line 6

def self.aggregation(objects, properties = nil)
  count  = 0
  result = {
    'type'     => 'Aggregation',
    'features' => []
  }
  objects.each_with_index do |object, i|
    unless object.nil?
      if object.class.to_s == 'Hash'
        json = object.merge('id' => i + 1)
      else
        json = object.to_geo_json_feature.merge('id' => i + 1) # offset by one, since google getFeatureById(0) fails
      end
      result['features'].push(json)
      count += 1
    end
  end
  unless properties.nil?
    result['properties'] = {properties.to_s => count}
  end
  result
end

.feature(object) ⇒ Hash

Parameters:

  • object (Object)

Returns:

  • (Hash)


56
57
58
59
60
61
62
63
# File 'lib/gis/geo_json.rb', line 56

def self.feature(object)
  result = {
    'type'     => 'FeatureCollection',
    'features' => []
  }
  result['features'].push(object.to_geo_json_feature)
  result
end

.feature_collection(objects, properties = nil) ⇒ geo_JSON object

Parameters:

  • objects (Array of instances that respond to .to_geo_json_feature)

Returns:

  • (geo_JSON object)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gis/geo_json.rb', line 31

def self.feature_collection(objects, properties = nil)
  count  = 0
  result = {
    'type'     => 'FeatureCollection',
    'features' => []
  }
  objects.each_with_index do |object, i|
    unless object.nil?
      if object.class.to_s == 'Hash'
        json = object.merge('id' => i + 1)
      else
        json = object.to_geo_json_feature.merge('id' => i + 1) # offset by one, since google getFeatureById(0) fails
      end
      result['features'].push(json)
      count += 1
    end
  end
  unless properties.nil?
    result['properties'] = {properties => {'count' => count}}
  end
  result
end

.quick_geo_json(geographic_item_id) ⇒ GeoJSON

Returns content for geometry.

Returns:

  • (GeoJSON)

    content for geometry



82
83
84
# File 'lib/gis/geo_json.rb', line 82

def self.quick_geo_json(geographic_item_id)
  JSON.parse(quick_geo_json_string(geographic_item_id))
end

.quick_geo_json_string(geographic_item_id) ⇒ Object

The point is to not instantiate a whole AR object, but query as directly as possible to get the GeoJSON string value. It’s unclear as to whether this saves that much time.

Returns:

  • String, nil a GeoJSON string



70
71
72
73
74
75
76
77
78
79
# File 'lib/gis/geo_json.rb', line 70

def self.quick_geo_json_string(geographic_item_id)
  return nil if geographic_item_id.nil?

  query = "ST_AsGeoJSON(#{GeographicItem::GEOMETRY_SQL.to_sql}::geometry) geo_json_str"
  a = GeographicItem.where(id: geographic_item_id)
    .select(query)
    .limit(1)
  ::GeographicItem.connection.select_all(a.to_sql)
    .first['geo_json_str']
end