Sha256: ded9c38a5738b1da39ee9445127dfa73a78be5ba16f8ddb7ec6c94dc5b85b8b4

Contents?: true

Size: 1.47 KB

Versions: 6

Compression:

Stored size: 1.47 KB

Contents

class ArcGISUpdateFeaturesJob < Struct.new(:options)
  def perform
    model = options[:spatial_model_type].find(options[:spatial_model_id])

    if model.update_features!
      options[:cache_classes].each {|klass| SpatialFeatures.cache_record_proximity(model, klass) }
    end
  rescue => e
    raise "Can't refresh geometry: #{normalize_message(e.message)}"
  end

  private

  NUMBER_REGEX = /-?\d+\.\d+/

  def normalize_message(message)
    normalized_messages = []

    normalized_messages << "ArcGIS Server is not responding." if message =~ /\Agetaddrinfo/

    if message =~ /invalid KML representation/
      normalized_messages += invalid_kml_reason(message).presence || ["invalid KML being generated by ArcGIS."]
    end

    if message =~ /Self-intersection/
      normalized_messages += message.scan(/\[(#{NUMBER_REGEX}) (#{NUMBER_REGEX})\]/).collect do |lng, lat|
        "Self-intersection at #{lng},#{lat}"
      end
    end

    if normalized_messages.present?
      return '<ul><li>' + normalized_messages.join('</li><li>') + '</li></ul>'
    else
      return message
    end
  end

  COORDINATE_REGEX = /<LinearRing><coordinates>\s*((?:#{NUMBER_REGEX},#{NUMBER_REGEX},#{NUMBER_REGEX}\s*)+)<\/coordinates><\/LinearRing>/
  def invalid_kml_reason(message)
    message.scan(COORDINATE_REGEX).collect do |match|
      coords = match[0].split(/(?:,0\.0+)?\s+/).chunk {|c| c }.map(&:first)

      "Sliver polygon detected at #{coords.first}" if coords.length < 4
    end.compact
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
spatial_features-1.2.4 lib/spatial_features/workers/arcgis_update_features_job.rb
spatial_features-1.2.3 lib/spatial_features/workers/arcgis_update_features_job.rb
spatial_features-1.2.2 lib/spatial_features/workers/arcgis_update_features_job.rb
spatial_features-1.2.1 lib/spatial_features/workers/arcgis_update_features_job.rb
spatial_features-1.2.0 lib/spatial_features/workers/arcgis_update_features_job.rb
spatial_features-1.1.1 lib/spatial_features/workers/arcgis_update_features_job.rb