Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am rywall on github.
  • I am rywall (https://keybase.io/rywall) on keybase.
  • I have a public key ASBA1dWlR7lwE7osmOivioSsalsiwSjvKerE4QakDLPH2go

To claim this, I am signing this object:

Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@rywall
rywall / blinder.command
Last active May 17, 2018 13:08
Ruby Script to randomize filenames
#!/usr/bin/env ruby
require 'csv'
Dir.chdir File.dirname(__FILE__)
class String
def green; "\e[32m#{self}\e[0m" end
def yellow; "\e[33m#{self}\e[0m" end
end
@rywall
rywall / en.yml
Created August 6, 2015 23:45
RRN CMS Translation
en:
new: "New"
edit: "Edit"
delete: "Delete"
cancel: "Cancel"
import: "Import"
error: "Error"
toggle_navigation: "Toggle Navigation"
@rywall
rywall / test.rb
Created December 4, 2014 01:52
hm:t association with order clause that references join table cannot be eager loaded
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'rack', github: 'rack/rack'
gem 'i18n', github: 'svenfuchs/i18n'
gem 'sqlite3'
GEMFILE
class CreateModels < ActiveRecord::Migration
def change
create_table :items do |t|
end
end
end
class Item < ActiveRecord::Base
end
# Calculate the intake fraction where iF = SUM(popDensN * DispN)
# where popDensN is the population density in an N meter buffer minus the previous buffer
def intake_fraction
fraction = 0
inner_pop = 0
inner_area = 0
DISPERSION_FACTORS.each do |buffer, value|
pop_in_buffer = send("pop#{buffer}m").to_i
area_in_buffer = send("area#{buffer}m").to_i
DISPERSION_FACTORS = {50 => 0.5341, 100 => 0.3610, 200 => 0.5145, 500 => 0.9159, 1000 => 0.9088, 5000 => 4.662}
# Calculate the intake fraction where iF = SUM(popN * DispN) / 1000
# where popN is the population in an N meter buffer minus the previous population buffer
def intake_fraction
fraction = 0
inner_pop = 0
DISPERSION_FACTORS.each do |buffer, value|
pop_in_buffer = send("pop#{buffer}m").to_i
fraction += (pop_in_buffer - inner_pop) * value * 0.62 * 14.5
[50, 100, 200, 500, 1000, 5000].each do |buffer|
puts "Calculating population within #{buffer} m of each shape_geometry"
Shape.find_each do |s|
execute "UPDATE shapes SET pop#{buffer}m =
(SELECT SUM((ST_SummaryStats(ST_Clip(rast,1,ST_Buffer(geom::geography, #{buffer})::geometry))).sum)
FROM shapes, population_tiles
WHERE id = #{s.id} AND ST_Intersects(ST_Buffer(geom::geography, #{buffer})::geometry,rast)
GROUP BY id)
WHERE id = #{s.id}"
end