Last active
December 27, 2015 06:29
-
-
Save ddd1600/7282301 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' | |
##UNIX CALLS (FROM RAILS CONSOLE) | |
def open | |
`open .` | |
end | |
def mate | |
`mate .` | |
end | |
def latestmigration | |
`mate #{Dir.glob(File.join(Rails.root, 'db', 'migrate', '*.rb')).max { |a,b| File.ctime(a) <=> File.ctime(b)} }` | |
end | |
def migrate | |
`rake db:migrate` | |
end | |
def rollback | |
`rake db:rollback` | |
end | |
##### | |
##### | |
def sap(record) | |
cols = record.class.columns.select{|c| c.type != :spatial}.map(&:name) | |
ap record.attributes.slice(*cols) | |
end | |
class Array | |
def like(this, these=nil) | |
select {|s| s=~ /#{this}/} | |
end | |
end | |
def export_geojson(fn, wgs_shapes) | |
puts "NOTE: this method DOES NOT transform your shape" | |
features = [] | |
f = RGeo::GeoJSON::EntityFactory.new | |
wgs_shapes.each_with_index do |wgs_shape, i| | |
features << f.feature(wgs_shape, i, nil) | |
end | |
coll = f.feature_collection(features) | |
geojson = RGeo::GeoJSON.encode(coll).to_json | |
File.open("#{fn}.geojson", "w") {|f| f.write(geojson)} | |
end | |
def r! | |
reload! | |
end | |
def any(sqls) | |
sqls.map{|sql| "(#{sql})"}.join(" OR ") | |
end | |
def where_equals_any_sql(tags, col) | |
tags.map {|tag| "#{col} = '#{tag}'" }.join(" OR ") | |
end | |
def sql_equals_any(tags, col) | |
where_equals_any_sql(tags, col) | |
end | |
def where_any_sql(tags, col) | |
where_equals_any_sql(tags, col) | |
end | |
def sql_like_any(tags, col) | |
puts "NOTE: This method implements the 'ILIKE' function meaning that it is not caps-sensitive" | |
tags.map {|tag| "#{col.to_s} ILIKE '#{tag}'" }.join(" OR ") | |
end | |
def like_any(tags, col) | |
sql_like_any(tags, col) | |
end | |
def like_any_sql(tags, col) | |
sql_like_any(tags, col) | |
end | |
#$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) | |
def list_models | |
ActiveRecord::Base.subclasses.map(&:name) | |
end | |
$ncload = ImportShapefile.first | |
#cumberland fixes | |
CumberlandParcel = CumbParcel | |
CumberlandParcelLocation = CumbParcelLocation | |
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, awesomeprint=true) | |
count = Hash.new(0) | |
ary.each do |str| | |
count[str] += 1 | |
end | |
stats = count.sort_by{|k, v| v}.reverse | |
total = ary.count.to_f | |
x = stats.map { |value, freq| [value, freq, "#{(freq.to_f / total * 100).round(1)}%"] }.sort_by{|k,v| v}.reverse | |
if awesomeprint | |
ap x | |
else | |
x | |
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, y1, x2, y2) | |
Math.hypot((x2-x1), (y2-y1)) | |
end | |
def geo_distance(lat1, long1, lat2, long2) | |
Haversine.distance(lat1, ) | |
end | |
module GPSHelper | |
def for_google | |
if self.geometry_type.to_s =~ /MultiPolygon/ | |
x = self.centroid | |
else | |
x = self | |
end | |
x.as_text.split("(").last.split(")").first.split(" ").reverse.join(",") | |
end | |
def as_array | |
points = for_google.split(",") | |
[points[0].to_f, points[1].to_f] | |
end | |
def google_link | |
"https://maps.google.com/maps?hl=en&q=#{self.for_google.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 | |
end | |
module RGeo | |
module Geos | |
class CAPIPointImpl | |
include GPSHelper | |
end | |
class CAPIMultiPolygonImpl | |
include GPSHelper | |
end | |
end #of Geos module | |
module Geographic | |
class ProjectedPointImpl | |
include GPSHelper | |
end# of ProjectedPointImpl class | |
class ProjectedMultiPolygonImpl | |
include GPSHelper | |
end | |
class SphericalPointImpl | |
include GPSHelper | |
end | |
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