class Geoblacklight::Geometry

Transforms and parses geometry expressed in WKT or CSW WKT ENVELOPE syntax

Attributes

geom[R]

Public Class Methods

new(geom) click to toggle source

@param [String] geom WKT or WKT ENVELOPE syntax formatted string

# File lib/geoblacklight/geometry.rb, line 11
def initialize(geom)
  @geom = geom
end

Public Instance Methods

bounding_box() click to toggle source

Generate a wsen bounding box from the geometry @return [String] bounding box as comma delimited wsen “w, s, e, n”

# File lib/geoblacklight/geometry.rb, line 27
def bounding_box
  obj = factory.parse_wkt(geometry_as_wkt)

  # Get the minimum bounding box for the geometry as a Polygon
  bbox = obj.envelope

  # Return as wsen string
  minx = bbox.coordinates[0][0][0]
  miny = bbox.coordinates[0][0][1]
  maxx = bbox.coordinates[0][1][0]
  maxy = bbox.coordinates[0][2][1]
  "#{minx}, #{miny}, #{maxx}, #{maxy}"
rescue RGeo::Error::ParseError
  Geoblacklight.logger.warn "Error parsing geometry: #{geom}"
  default_extent
end
geojson() click to toggle source

Convert geometry to GeoJSON @return [String]

# File lib/geoblacklight/geometry.rb, line 17
def geojson
  obj = factory.parse_wkt(geometry_as_wkt)
  RGeo::GeoJSON.encode(obj).to_json
rescue StandardError
  Geoblacklight.logger.warn "Geometry is not valid: #{geom}"
  default_extent
end

Private Instance Methods

default_extent() click to toggle source

Default extent as GeoJSON @return [String]

# File lib/geoblacklight/geometry.rb, line 48
def default_extent
  {
    'type' => 'Polygon',
    'coordinates' => [
      [
        [-180.0, 90.0], [-180.0, -90.0], [180.0, -90.0], [180.0, 90.0], [-180.0, 90.0]
      ]
    ]
  }.to_json
end
envelope_to_polygon() click to toggle source

Convert WKT ENVELOPE string to WKT POLYGON string @return [String]

# File lib/geoblacklight/geometry.rb, line 61
def envelope_to_polygon
  exp = /^\s*ENVELOPE\(
              \s*([-.\d]+)\s*,
              \s*([-.\d]+)\s*,
              \s*([-.\d]+)\s*,
              \s*([-.\d]+)\s*
              \)\s*$/x # uses 'x' option for free-spacing mode
  bbox_match = exp.match(geom)
  minx, maxx, maxy, miny = bbox_match.captures
  "POLYGON ((#{minx} #{maxy}, #{minx} #{miny}, #{maxx} #{miny}, #{maxx} #{maxy}, #{minx} #{maxy}))"
end
factory() click to toggle source
# File lib/geoblacklight/geometry.rb, line 73
def factory
  @factory ||= RGeo::Cartesian.factory
end
geometry_as_wkt() click to toggle source

Return geometry as valid WKT string @return [String]

# File lib/geoblacklight/geometry.rb, line 79
def geometry_as_wkt
  return geom unless geom.match?(/ENVELOPE/)

  envelope_to_polygon
end