Created
October 15, 2013 15:58
-
-
Save ddd1600/6993871 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'highline/import' | |
require 'open-uri' | |
def where_any_sql(tags, col) | |
tags.map {|tag| "#{col} = '#{tag}'" }.join(" OR ") | |
end | |
GOOGLE_MAPS_API_KEY = "AIzaSyD95HvG257zlJ-hy_pq3mxcTew6pSEZVdk" | |
#$g = GetGeographic.new | |
$g = lambda { |proj4string, srid, buffer_resolution=16| GetGeographic.new(proj4string, srid, buffer_resolution) } | |
$c = ConvertProjToProj | |
$gnc = GetGeographic.new(2264, "+proj=lcc +lat_1=36.16666666666666 +lat_2=34.33333333333334 +lat_0=33.75 +lon_0=-79 +x_0=609601.2192024384 +y_0=0 +ellps=GRS80 +datum=NAD83 +to_meter=0.3048006096012192 +no_defs") | |
$ncfactory = RGeo::Geos.factory(:srid => 2264) | |
$gsc = GetGeographic.new(3361, "+proj=lcc +lat_1=34.83333333333334 +lat_2=32.5 +lat_0=31.83333333333333 +lon_0=-81 +x_0=609600 +y_0=0 +ellps=GRS80 +to_meter=0.3048 +no_defs ") | |
$scfactory = RGeo::Geos.factory(:srid => 3361) | |
$cp = CharlestonParcel.new | |
###mecklenburg shorteners | |
MeckParcel = MecklenburgParcel | |
MeckParcelZoning = MecklenburgParcelZoning | |
def get_proj4(srid, type="epsg") | |
type.downcase! if type | |
case type | |
when "epsg" | |
open("http://spatialreference.org/ref/epsg/#{srid}/proj4/").read | |
when "esri" | |
open("http://spatialreference.org/ref/esri/#{srid}/proj4/").read | |
end | |
end | |
class ActiveRecord::Base | |
def self.booleans | |
self.columns.select{|c| c.type == :boolean}.map(&:name) | |
end | |
def self.floats | |
self.columns.select{|c| c.type == :float}.map(&:name) | |
end | |
def self.integers | |
self.columns.select{|c| c.type == :integer}.map(&:name) | |
end | |
def self.strings | |
self.columns.select{|c| c.type == :string}.map(&:name) | |
end | |
def self.of_type(type) | |
type = type.to_sym | |
self.columns.select{|c| c.type == type}.map(&:name) | |
end | |
def self.where_not(opts) | |
params = [] | |
sql = opts.map{|k, v| params << v; "#{quoted_table_name}.#{quote_column_name k} != ?"}.join(' AND ') | |
where(sql, *params) | |
end | |
end | |
def stats(ary, as_pctg=false, awesomeprint=true) | |
count = Hash.new(0) | |
ary.each do |str| | |
count[str] += 1 | |
end | |
stats = count.sort_by{|k, v| v}.reverse | |
if as_pctg | |
total = ary.count.to_f | |
x = stats.map { |value, freq| [value, freq, "#{(freq.to_f / total * 100).round(1)}%"] } | |
if awesomeprint | |
ap x | |
else | |
x | |
end | |
else | |
stats | |
if awesomeprint | |
ap stats | |
else | |
stats | |
end | |
end | |
end | |
def mi2m(miles) #miles => meters | |
miles.to_f * 1609.34 | |
end | |
def m2mi(meters) #meters => miles | |
meters.to_f * 0.000621371 | |
end | |
def driving_distance(to, fro)#, true) | |
m2mi(GDirections.new(to, fro, true).distance) | |
end | |
#alias_method :mi2m, :miles_to_meters | |
#alias_method :m2mi, :meters_to_miles | |
def flat_distance(x1, x2, y1, y2) | |
Math.hypot((x2-x1), (y2-y1)) | |
end | |
def geo_distance(lat1, long1, lat2, long2) | |
dtor = Math::PI/180 | |
r = 6378.14*1000 | |
rlat1 = lat1 * dtor | |
rlong1 = long1 * dtor | |
rlat2 = lat2 * dtor | |
rlong2 = long2 * dtor | |
dlon = rlong1 - rlong2 | |
dlat = rlat1 - rlat2 | |
a = power(Math::sin(dlat/2), 2) + Math::cos(rlat1) * Math::cos(rlat2) * power(Math::sin(dlon/2), 2) | |
c = 2 * Math::atan2(Math::sqrt(a), Math::sqrt(1-a)) | |
d = r * c | |
d | |
end | |
module DDDInspector | |
def unique_methods | |
Object.methods - self.methods | |
end | |
def uniq_methods | |
unique_methods | |
end | |
def uniquemethods | |
unique_methods | |
end | |
def umethods | |
unique_methods | |
end | |
end | |
module GPSHelper | |
def whereis(state="nc") | |
if state == "sc" | |
f = $gsc | |
elsif state == "nc" | |
f = $gnc | |
end | |
x = f.proj_to_geo(self) | |
if x.geometry_type.to_s == "Point" | |
x | |
else | |
x.centroid | |
end | |
end | |
def for_google( state="nc") | |
coords = whereis(state).as_text.split("(").last.split(")").first.split(" ") | |
"#{coords.last},#{coords.first}" | |
end# of to_s | |
def google_link(state="nc") | |
url = "https://maps.google.com/maps?hl=en&q=#{self.for_google(state).gsub(" ", "")}" | |
end | |
def get_google_earth_ary | |
puts "ASSUMING NC 3200 ft StatePlane (2264). Modify GPSHelper to handle different projections" | |
coords = [] | |
self.exterior_ring.points.each do |point| | |
coords << "#{$gnc.proj_to_geo(point).to_s}, 100" | |
end | |
coords.join("\n") | |
end# of get_google_earth_ary | |
def to_gps | |
whereis.for_google | |
end | |
end | |
module RGeo | |
module Geos | |
class CAPIPointImpl | |
include GPSHelper | |
include DDDInspector | |
end | |
class CAPIMultiPolygonImpl | |
include GPSHelper | |
include DDDInspector | |
end | |
end #of Geos module | |
module Geographic | |
class ProjectedPointImpl | |
include GPSHelper | |
include DDDInspector | |
end# of ProjectedPointImpl class | |
end# of Geographic module | |
end# of RGeo | |
##EXAMPLE USAGE IN MIGRATION | |
#class ChangeTableAttributes < ActiveRecord::Migration | |
# class << self | |
# include AlterColumn | |
# end | |
# | |
# def self.up | |
# alter_column :sometables, :is_numeric, :boolean, { "1" => true, "else" => false }, true | |
# alter_column :sometables, :multiplier, :integer, "USING CAST(multiplier AS integer)", 1 | |
# end | |
# | |
# def self.down | |
# raise ActiveRecord::IrreversibleMigration.new | |
# end | |
#end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment