Class: GeoCombine::CkanMetadata

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

Constant Summary collapse

MAX_STRING_LENGTH =

Solr limit

32_765

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metadata) ⇒ CkanMetadata

Returns a new instance of CkanMetadata.



9
10
11
# File 'lib/geo_combine/ckan_metadata.rb', line 9

def initialize()
  @metadata = JSON.parse()
end

Instance Attribute Details

#metadataObject (readonly)

Returns the value of attribute metadata.



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

def 
  @metadata
end

Instance Method Details

#downloadable?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/geo_combine/ckan_metadata.rb', line 99

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

#envelopeObject



44
45
46
47
48
49
# File 'lib/geo_combine/ckan_metadata.rb', line 44

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

  envelope_from_spatial(' ') unless envelope_from_spatial(' ').nil?
end

#envelope_from_bboxObject



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/geo_combine/ckan_metadata.rb', line 51

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
    bbox.to_envelope if bbox.valid?
  rescue GeoCombine::Exceptions::InvalidGeometry
    nil
  end
end

#envelope_from_spatial(delimiter) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/geo_combine/ckan_metadata.rb', line 65

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

#external_referencesObject



89
90
91
92
93
94
95
96
97
# File 'lib/geo_combine/ckan_metadata.rb', line 89

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

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

  h.compact
end

#extras(key) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/geo_combine/ckan_metadata.rb', line 81

def extras(key)
  if @metadata['extras']
    @metadata['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

Returns:

  • (Hash)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/geo_combine/ckan_metadata.rb', line 24

def geoblacklight_terms
  {
    dc_identifier_s: @metadata['id'],
    dc_title_s: @metadata['title'],
    dc_rights_s: 'Public',
    layer_geom_type_s: 'Not Specified',
    dct_provenance_s: organization['title'],
    dc_description_s: @metadata['notes'].respond_to?(:[]) ? @metadata['notes'][0..MAX_STRING_LENGTH] : nil,
    layer_slug_s: @metadata['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
  }.compact
end

#organizationObject



40
41
42
# File 'lib/geo_combine/ckan_metadata.rb', line 40

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

#resource_urls(type) ⇒ Object



109
110
111
112
113
# File 'lib/geo_combine/ckan_metadata.rb', line 109

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

#resources(type) ⇒ Object



103
104
105
106
107
# File 'lib/geo_combine/ckan_metadata.rb', line 103

def resources(type)
  return [] if @metadata['resources'].nil?

  @metadata['resources'].select { |resource| resource['resource_locator_function'] == type }
end

#subjectsObject



77
78
79
# File 'lib/geo_combine/ckan_metadata.rb', line 77

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

#to_geoblacklightGeoCombine::Geoblacklight

Creates and returns a Geoblacklight schema object from this metadata



16
17
18
# File 'lib/geo_combine/ckan_metadata.rb', line 16

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