Class: PPE_API::ProtectedArea

Inherits:
Object
  • Object
show all
Includes:
HappyMapper
Defined in:
lib/ppe_api/protected_area.rb

Constant Summary collapse

@@simple_atts =
["english_name", "id", "local_name", "state", "status_date", "total_area", "total_area_marine", "valid_from", "legally_reclassified", "legally_reduced", "legally_extended"]
@@xml_atts =
["english_name", "id", "local_name", "status_date", "total_area", "total_area_marine", "legally_reclassified", "legally_reduced", "legally_extended", "geometry_as_hex_ewkb", "countries", "dataset", "iucn_category", "designation"]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/ppe_api/protected_area.rb', line 101

def self.exists?(id)
  !self.get_by_id(id).nil?
end

.get_by_id(id, has_geom = true) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/ppe_api/protected_area.rb', line 89

def self.get_by_id(id, has_geom=true)
  if id.to_i != 0
    path = "/api/protected_area/#{id}"
    path = path + "?has_geometry=true" if has_geom
    res, data = Config.http.get(path)
    res.value
    return ProtectedArea.parse(data) unless data.nil? || data.empty?
  end    
  return nil
end

Instance Method Details

#countries_isoObject



31
32
33
# File 'lib/ppe_api/protected_area.rb', line 31

def countries_iso
  countries.collect{|a| a.iso+", "}.to_s.chop.chop
end

#geometryObject



35
36
37
38
39
# File 'lib/ppe_api/protected_area.rb', line 35

def geometry
  puts "in protected area looking at what i am #{self.inspect}"
  @geometry = GeoRuby::SimpleFeatures::Geometry.from_hex_ewkb(@geometry_as_hex_ewkb) if (!@geometry.nil? && !@geometry_as_hex_ewkb.nil?)
  @geometry
end

#geometry_geo_jsonObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ppe_api/protected_area.rb', line 41

def geometry_geo_json
  unless (geometry.nil?)
    if geometry.is_a? GeoRuby::SimpleFeatures::MultiPolygon
      geo = TempGeometry.create(:polygon=>geometry) 
    else
      geo = TempGeometry.create(:point=>geometry) 
    end
    geo.save!
    json = geo.get_json
    geo.destroy
    json
  end
  
end

#get_nonnil_writable_attribute(attr) ⇒ Object

returns printable attributes



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/ppe_api/protected_area.rb', line 67

def get_nonnil_writable_attribute(attr)
  if @@simple_atts.count(attr) > 0
    return self.send(attr)
  elsif attr == "iucn_category"
    return iucn_category ? iucn_category.name : ""
  elsif attr == "designation"
    return designation ? designation.name : ""
  elsif attr == "legal_status"
    return legal_status ? legal_status.name : ""
  elsif attr == "countries"
    return countries.length > 0 ? countries.collect{|a| a.iso+", "}.to_s.chop.chop : ""
  elsif attr == "citation"
    return self.dataset.citation
  elsif attr == "author"
    return self.dataset.author
  elsif attr == "wdpa_code"
    return self.site.id
  end
  return ""
end

#get_obj_attributes(attr) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/ppe_api/protected_area.rb', line 56

def get_obj_attributes(attr)
  if attr == "wdpa_code"
    return self.site
  elsif attr == "geometry"
    return self.geometry_as_hex_ewkb
  else
    return self.send(attr)
  end
end

#to_xml(options = {}) ⇒ Object



105
106
107
# File 'lib/ppe_api/protected_area.rb', line 105

def to_xml(options={})
  self.as_json.to_xml({:root => "protected-area"}.merge(options))
end