class GeoCombine::OGP

Data model for OpenGeoPortal metadata

Constants

OGP_REQUIRED_FIELDS
SLUG_BLACKLIST

Attributes

metadata[R]

Public Class Methods

new(metadata) click to toggle source

Initializes an OGP object for parsing @param [String] metadata a valid serialized JSON string from OGP instance @raise [InvalidMetadata]

# File lib/geo_combine/ogp.rb, line 15
def initialize(metadata)
  @metadata = JSON.parse(metadata)
  raise InvalidMetadata unless valid?
end

Public Instance Methods

date() click to toggle source
# File lib/geo_combine/ogp.rb, line 79
def date
  begin
    DateTime.rfc3339(metadata['ContentDate'])
  rescue
    nil
  end
end
envelope() click to toggle source

Builds a Solr Envelope using CQL syntax @return [String]

# File lib/geo_combine/ogp.rb, line 125
def envelope
  raise ArgumentError unless west >= -180 && west <= 180 &&
                             east >= -180 && east <= 180 &&
                             north >= -90 && north <= 90 &&
                             south >= -90 && south <= 90 &&
                             west <= east && south <= north
  "ENVELOPE(#{west}, #{east}, #{north}, #{south})"
end
fgdc() click to toggle source
# File lib/geo_combine/ogp.rb, line 142
def fgdc
  GeoCombine::Fgdc.new(metadata['FgdcText']) if metadata['FgdcText']
end
geoblacklight_terms() click to toggle source

Builds a Geoblacklight Schema type hash from Esri Open Data portal metadata @return [Hash]

# File lib/geo_combine/ogp.rb, line 49
def geoblacklight_terms
  {
    # Required fields
    dc_identifier_s: identifier,
    layer_slug_s: slug,
    dc_title_s: metadata['LayerDisplayName'],
    solr_geom: envelope,
    dct_provenance_s: institution,
    dc_rights_s: metadata['Access'],
    geoblacklight_version: '1.0',

    # Recommended fields
    dc_description_s: metadata['Abstract'],
    layer_geom_type_s: ogp_geom,
    dct_references_s: references,
    layer_id_s: "#{metadata['WorkspaceName']}:#{metadata['Name']}",

    # Optional
    dct_temporal_sm: [metadata['ContentDate']],
    dc_format_s: ogp_formats,
    # dct_issued_dt
    # dc_language_s
    dct_spatial_sm: placenames,
    solr_year_i: year,
    dc_publisher_s: metadata['Publisher'],
    dc_subject_sm: subjects,
    dc_type_s: 'Dataset'
  }.delete_if { |_k, v| v.nil? }
end
ogp_formats() click to toggle source

OGP doesn't ship format types, so we just try and be clever here.

# File lib/geo_combine/ogp.rb, line 104
def ogp_formats
  case metadata['DataType']
  when 'Paper Map', 'Raster'
    return 'GeoTIFF'
  when 'Polygon', 'Point', 'Line'
    return 'Shapefile'
  else
    raise ArgumentError, metadata['DataType']
  end
end
ogp_geom() click to toggle source

Convert “Paper Map” to Raster, assumes all OGP “Paper Maps” have WMS

# File lib/geo_combine/ogp.rb, line 93
def ogp_geom
  case metadata['DataType']
  when 'Paper Map'
    'Raster'
  else
    metadata['DataType']
  end
end
placenames() click to toggle source
# File lib/geo_combine/ogp.rb, line 138
def placenames
  fgdc.metadata.xpath('//placekey').map(&:text) if fgdc
end
references() click to toggle source

Converts references to json @return [String]

# File lib/geo_combine/ogp.rb, line 118
def references
  references_hash.to_json
end
subjects() click to toggle source
# File lib/geo_combine/ogp.rb, line 134
def subjects
  fgdc.metadata.xpath('//themekey').map(&:text) if fgdc
end
to_geoblacklight() click to toggle source

Creates and returns a Geoblacklight schema object from this metadata @return [GeoCombine::Geoblacklight]

# File lib/geo_combine/ogp.rb, line 41
def to_geoblacklight
  GeoCombine::Geoblacklight.new(geoblacklight_terms.to_json)
end
valid?() click to toggle source

Runs validity checks on OGP metadata to ensure fields are present

# File lib/geo_combine/ogp.rb, line 34
def valid?
  OGP_REQUIRED_FIELDS.all? { |k| metadata[k].present? }
end
year() click to toggle source
# File lib/geo_combine/ogp.rb, line 87
def year
  date.year unless date.nil?
end

Private Instance Methods

download_uri() click to toggle source
# File lib/geo_combine/ogp.rb, line 166
def download_uri
  return 'http://schema.org/DownloadAction' if institution == 'Harvard'
  'http://schema.org/downloadUrl'
end
east() click to toggle source
# File lib/geo_combine/ogp.rb, line 185
def east
  metadata['MaxX'].to_f
end
filter_name(name) click to toggle source
# File lib/geo_combine/ogp.rb, line 217
def filter_name(name)
  # strip out schema and usernames
  SLUG_BLACKLIST.each do |blacklisted|
    name.sub!(blacklisted, '')
  end
  unless name.size > 1
    # use first word of title is empty name
    name = metadata['LayerDisplayName'].split.first
  end
  name
end
identifier() click to toggle source
# File lib/geo_combine/ogp.rb, line 197
def identifier
  CGI.escape(metadata['LayerId']) # TODO: why are we using CGI.escape?
end
institution() click to toggle source
# File lib/geo_combine/ogp.rb, line 193
def institution
  metadata['Institution']
end
location() click to toggle source

OGP “Location” field parsed

# File lib/geo_combine/ogp.rb, line 173
def location
  JSON.parse(metadata['Location'])
end
north() click to toggle source
# File lib/geo_combine/ogp.rb, line 177
def north
  metadata['MaxY'].to_f
end
references_hash() click to toggle source

Builds references used for dct_references @return [Hash]

# File lib/geo_combine/ogp.rb, line 151
def references_hash
  results = {
    'http://www.opengis.net/def/serviceType/ogc/wfs' => location['wfs'],
    'http://www.opengis.net/def/serviceType/ogc/wms' => location['wms'],
    'http://schema.org/url' => location['url'],
    download_uri => location['download']
  }

  # Handle null, "", and [""]
  results.map { |k, v| { k => ([] << v).flatten.first } if v }
         .flatten
         .compact
         .reduce({}, :merge)
end
slug() click to toggle source
# File lib/geo_combine/ogp.rb, line 201
def slug
  name = metadata['LayerId'] || metadata['Name'] || ''
  name = [institution, name].join('-') if institution.present? &&
                                          !name.downcase.start_with?(institution.downcase)
  sluggify(filter_name(name))
end
south() click to toggle source
# File lib/geo_combine/ogp.rb, line 181
def south
  metadata['MinY'].to_f
end
west() click to toggle source
# File lib/geo_combine/ogp.rb, line 189
def west
  metadata['MinX'].to_f
end