<commit_before>class Map < ActiveRecord::Base
  before_validation :update_name
  validates_presence_of :name,:author
  validates_presence_of :location, :message => ' cannot be found. Try entering a latitude and longitude if this problem persists.'
  validates_format_of       :name,
                            :with => /[a-zA-Z0-9_-]/,  
                            :message => " must not include spaces and must be alphanumeric, as it'll be used in the URL of your map, like: http://cartagen.org/maps/your-map-name. You may use dashes and underscores.",
                            :on => :create                  
#  has_many :warpables

  def update_name
    self.name = self.name.gsub(/\W/, '-').downcase
  end

  def validate
    self.name != 'untitled'
  end

  def warpables
    Warpable.find :all, :conditions => {:map_id => self.id, :deleted => false} 
  end

  def average_scale
	# determine optimal zoom level
	puts '> calculating scale'
	pxperms = []
	self.warpables.each do |warpable|
		unless warpable.width.nil?
			nodes = warpable.nodes_array
			# haversine might be more appropriate for large images
			scale = 20037508.34
    			y1 = Cartagen.spherical_mercator_lat_to_y(nodes[0].lat,scale)
    			x1 = Cartagen.spherical_mercator_lon_to_x(nodes[0].lon,scale)
    			y2 = Cartagen.spherical_mercator_lat_to_y(nodes[1].lat,scale)
    			x2 = Cartagen.spherical_mercator_lon_to_x(nodes[1].lon,scale)
			dist = Math.sqrt(((y2-y1)*(y2-y1))+((x2-x1)*(x2-x1)))
			puts 'x1,y1: '+x1.to_s+','+y1.to_s+' x2,y2: '+x2.to_s+','+y2.to_s
			puts (x2-x1).to_s+','+(y2-y1).to_s
			pxperms << (warpable.width)/dist
			puts 'scale: '+pxperms.last.to_s+' & dist: '+dist.to_s
		end
	end

	average = (pxperms.inject {|sum, n| sum + n })/pxperms.length
	puts 'average scale = '+average.to_s
        average
  end

  # distort all warpables, returns upper left corner coords in x,y
  def distort_warpables(scale)
	puts '> generating geotiffs of each warpable in GDAL'
	lowest_x=0<commit_msg>nil catch<commit_after>class Map < ActiveRecord::Base
  before_validation :update_name
  validates_presence_of :name,:author
  validates_presence_of :location, :message => ' cannot be found. Try entering a latitude and longitude if this problem persists.'
  validates_format_of       :name,
                            :with => /[a-zA-Z0-9_-]/,  
                            :message => " must not include spaces and must be alphanumeric, as it'll be used in the URL of your map, like: http://cartagen.org/maps/your-map-name. You may use dashes and underscores.",
                            :on => :create                  
#  has_many :warpables

  def update_name
    self.name = self.name.gsub(/\W/, '-').downcase
  end

  def validate
    self.name != 'untitled'
  end

  def warpables
    Warpable.find :all, :conditions => {:map_id => self.id, :deleted => false} 
  end

  def average_scale
	# determine optimal zoom level
	puts '> calculating scale'
	pxperms = []
	self.warpables.each do |warpable|
		unless warpable.width.nil?
			nodes = warpable.nodes_array
			# haversine might be more appropriate for large images
			scale = 20037508.34
    			y1 = Cartagen.spherical_mercator_lat_to_y(nodes[0].lat,scale)
    			x1 = Cartagen.spherical_mercator_lon_to_x(nodes[0].lon,scale)
    			y2 = Cartagen.spherical_mercator_lat_to_y(nodes[1].lat,scale)
    			x2 = Cartagen.spherical_mercator_lon_to_x(nodes[1].lon,scale)
			dist = Math.sqrt(((y2-y1)*(y2-y1))+((x2-x1)*(x2-x1)))
			puts 'x1,y1: '+x1.to_s+','+y1.to_s+' x2,y2: '+x2.to_s+','+y2.to_s
			puts (x2-x1).to_s+','+(y2-y1).to_s
			pxperms << (warpable.width)/dist unless warpable.width.nil? || dist.nil?
			puts 'scale: '+pxperms.last.to_s+' & dist: '+dist.to_s
		end
	end

	average = (pxperms.inject {|sum, n| sum + n })/pxperms.length
	puts 'average scale = '+average.to_s
        average
  end

  # distort all warpables, returns upper left corner coords in x,y
  def distort_warpables(scale)
	puts '> generating geotiffs of each warpable in GDAL'
	lowest_x=0
{'fraction_non_alphanumeric': 0.12693266832917705, 'fraction_numerical': 0.020448877805486283, 'mean_word_length': 3.039274924471299, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3258231', 'n_tokens_mistral': 1600, 'n_tokens_neox': 1472, 'n_words': 337}