Class: GeoCombine::CkanMetadata

Inherits:
Object
  • Object
show all
Defined in:
lib/geo_combine/ckan_metadata.rb

Constant Summary collapse

MAX_STRING_LENGTH =

Solr limit

32765

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metadata) ⇒ CkanMetadata



6
7
8
# File 'lib/geo_combine/ckan_metadata.rb', line 6

def initialize()
   = JSON.parse()
end

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



5
6
7
# File 'lib/geo_combine/ckan_metadata.rb', line 5

def 
  
end

Instance Method Details

#downloadable?Boolean



97
98
99
# File 'lib/geo_combine/ckan_metadata.rb', line 97

def downloadable?
  resource_urls('download').first =~ /.*\.zip/im
end

#envelopeObject



41
42
43
44
45
# File 'lib/geo_combine/ckan_metadata.rb', line 41

def envelope
  return envelope_from_bbox unless envelope_from_bbox.nil?
  return envelope_from_spatial(',') unless envelope_from_spatial(',').nil?
  return envelope_from_spatial(' ') unless envelope_from_spatial(' ').nil?
end

#envelope_from_bboxObject



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/geo_combine/ckan_metadata.rb', line 47

def envelope_from_bbox
  bbox = GeoCombine::BoundingBox.new(
    west: extras('bbox-west-long'),
    south: extras('bbox-south-lat'),
    east: extras('bbox-east-long'),
    north: extras('bbox-north-lat')
  )
  begin
    return bbox.to_envelope if bbox.valid?
  rescue GeoCombine::Exceptions::InvalidGeometry
    return nil
  end
end

#envelope_from_spatial(delimiter) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/geo_combine/ckan_metadata.rb', line 61

def envelope_from_spatial(delimiter)
  bbox = GeoCombine::BoundingBox.from_string_delimiter(
    extras('spatial'),
    delimiter: delimiter
  )
  begin
    return bbox.to_envelope if bbox.valid?
  rescue GeoCombine::Exceptions::InvalidGeometry
    return nil
  end
end

#external_referencesObject



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/geo_combine/ckan_metadata.rb', line 85

def external_references
  h = {
    'http://schema.org/url' => resource_urls('information').first
  }

  if downloadable?
    h['http://schema.org/downloadUrl'] = resource_urls('download').first
  end

  h.select { |_k, v| !v.nil? }
end

#extras(key) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/geo_combine/ckan_metadata.rb', line 77

def extras(key)
  if ['extras']
    ['extras'].select { |h| h['key'] == key }.collect { |v| v['value'] }[0] || ''
  else
    ''
  end
end

#geoblacklight_termsHash

Builds a Geoblacklight Schema type hash from Esri Open Data portal metadata



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/geo_combine/ckan_metadata.rb', line 21

def geoblacklight_terms
  {
    dc_identifier_s: ['id'],
    dc_title_s: ['title'],
    dc_rights_s: 'Public',
    layer_geom_type_s: 'Not Specified',
    dct_provenance_s: organization['title'],
    dc_description_s: ['notes'].respond_to?(:[]) ? ['notes'][0..MAX_STRING_LENGTH] : nil,
    layer_slug_s: ['name'],
    solr_geom: envelope,
    dc_subject_sm: subjects,
    dct_references_s: external_references.to_json.to_s,
    dc_format_s: downloadable? ? 'ZIP' : nil # TODO: we only allow direct ZIP file downloads
  }.select { |_k, v| !v.nil? }
end

#organizationObject



37
38
39
# File 'lib/geo_combine/ckan_metadata.rb', line 37

def organization
  ['organization'] || { title: '' }
end

#resource_urls(type) ⇒ Object



106
107
108
109
110
# File 'lib/geo_combine/ckan_metadata.rb', line 106

def resource_urls(type)
  resources(type).collect do |resource|
    resource['url'] if resource.respond_to?(:[])
  end
end

#resources(type) ⇒ Object



101
102
103
104
# File 'lib/geo_combine/ckan_metadata.rb', line 101

def resources(type)
  return [] if ['resources'].nil?
  ['resources'].select { |resource| resource['resource_locator_function'] == type }
end

#subjectsObject



73
74
75
# File 'lib/geo_combine/ckan_metadata.rb', line 73

def subjects
  extras('tags').split(',').map(&:strip)
end

#to_geoblacklightGeoCombine::Geoblacklight

Creates and returns a Geoblacklight schema object from this metadata



13
14
15
# File 'lib/geo_combine/ckan_metadata.rb', line 13

def to_geoblacklight
  GeoCombine::Geoblacklight.new(geoblacklight_terms.to_json)
end