Class: MODEL_NAME
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- MODEL_NAME
- Defined in:
- lib/generators/census_shapes/setup/templates/models/shape.rb
Direct Known Subclasses
Aiannh, Aits, Anrc, BG, Block, Cbsa, Cd, County, Cousub, Csa, Elsd, Metdiv, Place, Scsd, Sldl, Sldu, State, Submcd, Tract, Unsd, Vtd, Zcta5
Class Method Summary collapse
- .available_shapes ⇒ Object
- .bbox(z, x, y) ⇒ Object
- .coordinateLocation(z, x, y) ⇒ Object
- .features(options = {}) ⇒ Object
- .merge(polygons) ⇒ Object
- .rebuild(polygons) ⇒ Object
- .sort(polygons) ⇒ Object
- .y2lat(y) ⇒ Object
Instance Method Summary collapse
Class Method Details
.available_shapes ⇒ Object
132 133 134 135 136 137 138 139 140 |
# File 'lib/generators/census_shapes/setup/templates/models/shape.rb', line 132 def self.available_shapes return @available_shapes if @available_shapes @available_shapes = CENSUS_SHAPES available_types = MODEL_NAME.unscoped.select("DISTINCT(type)").map{|k,v| k['type'].upcase} @available_shapes.each do |k,v| @available_shapes.delete(k) if !available_types.include?(k) end return @available_shapes end |
.bbox(z, x, y) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/generators/census_shapes/setup/templates/models/shape.rb', line 26 def self.bbox(z,x,y) return false if z.nil? || x.nil? || y.nil? nw=coordinateLocation(z.to_i,x.to_i,y.to_i) se=coordinateLocation(z.to_i,x.to_i+1,y.to_i+1) return "#{nw[:lng]},#{se[:lat]},#{se[:lng]},#{nw[:lat]}" end |
.coordinateLocation(z, x, y) ⇒ Object
33 34 35 36 |
# File 'lib/generators/census_shapes/setup/templates/models/shape.rb', line 33 def self.coordinateLocation(z,x,y) k = 45 / 2 ** (z - 3.001) return {:lng=> (k * x - 180).to_i,:lat=> y2lat(180 - k * y)} end |
.features(options = {}) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/generators/census_shapes/setup/templates/models/shape.rb', line 43 def self.features(={}) features = []; i =1; [:detail]=0.01 if [:detail].nil? model = [:type].capitalize.constantize if ![:detail].nil? geos = model.select("name10, geoid10, ST_AsGeoJson(ST_SimplifyPreserveTopology(geom,#{[:detail]})) AS geojson"); else geos = model.select("name10, geoid10, ST_AsGeoJson(geom) As geojson, geom, ST_Area(geom) AS area, ST_IsValid(geom) AS isvalid"); end if([:bbox]) b = [:bbox].split(",") ids = model.select("geoid10").where("ST_Contains(ST_AsText(ST_Envelope('LINESTRING(#{b[0]} #{b[1]},#{b[2]} #{b[3]})'::geometry)), ST_AsText(latlng))").map{|id| id['geoid10']} if [:grid] geobox = model.select("ST_AsGeoJson(ST_Envelope('LINESTRING(#{b[0]} #{b[1]},#{b[2]} #{b[3]})'::geometry)) AS geojson").first features << {:type => "Feature", :geometry=> JSON.parse(geobox['geojson']), :properties => {:id=> "1", :name => "Box", :class=> 'box'}} end if ids.empty? geos = [] else geos = geos.where(:geoid10 => ids) end end if([:id]) geos = geos.where(:geoid10=>[:id].split(",")) end for g in geos if g['isvalid'] != "t" && [:fix_geom] new_g = g.fix_geometry msg = "Geometry invalid. Rebuilding. Original = #{g['isvalid'] == "t"}, Rebuilt Geometry = #{new_g['isvalid'] == "t"}, Old area (#{g['area']}) == New area (#{new_g['area']})" features << {:type => "Feature", :geometry=> JSON.parse(new_g['geojson']), :properties => {:id=> g.geoid10, :name => g.name10 }} else features << {:type => "Feature", :geometry=> JSON.parse(g['geojson']), :properties => {:id=> g.geoid10, :name => g.name10 }} end i=i+1 end return features end |
.merge(polygons) ⇒ Object
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/generators/census_shapes/setup/templates/models/shape.rb', line 121 def self.merge(polygons) if polygons.count == 1 return polygons[0] else array = polygons.map { |p| "ST_GeomFromText('#{p}')" } geo = ActiveRecord::Base.connection.execute("SELECT ST_Collect(ARRAY[#{array.join(',')}]) AS geo").first['geo'] sql = "SELECT '#{geo}' AS geo, ST_AsGeoJson('#{geo}') AS geojson, ST_Area('#{geo}') AS area, ST_IsValid('#{geo}') AS isvalid;" return ActiveRecord::Base.connection.execute(sql).first end end |
.rebuild(polygons) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/generators/census_shapes/setup/templates/models/shape.rb', line 106 def self.rebuild(polygons) polygons = MODEL_NAME.sort(polygons) polygons.reverse.each do |p1| polygons.each do |p2| if p1 != p2 && ActiveRecord::Base.connection.execute("SELECT ST_Contains('#{p1}', '#{p2}') AS contains").first['contains'] == "t" temp = ActiveRecord::Base.connection.execute("SELECT ST_AsText(ST_Difference('#{p1}', '#{p2}')) AS union") polygons.delete(p1); polygons.delete(p2) polygons << temp.first['union'] end end end polygons = MODEL_NAME.merge(polygons) return polygons end |
.sort(polygons) ⇒ Object
99 100 101 102 103 104 |
# File 'lib/generators/census_shapes/setup/templates/models/shape.rb', line 99 def self.sort(polygons) return polygons.map{|p| pp = ActiveRecord::Base.connection.execute("SELECT ST_Area('#{p}'), ST_AsText('#{p}') AS geo").first [pp['st_area'].to_f, pp['geo'] ]}.sort {|a, b| a[0] <=> b[0]}.reverse.map{|p| p[1] } end |
.y2lat(y) ⇒ Object
38 39 40 |
# File 'lib/generators/census_shapes/setup/templates/models/shape.rb', line 38 def self.y2lat(y) return (360 / Math::PI * Math.atan(Math.exp(y * Math::PI / 180)) -90).round(2) end |
Instance Method Details
#buffer ⇒ Object
91 92 93 |
# File 'lib/generators/census_shapes/setup/templates/models/shape.rb', line 91 def buffer return self.class.name.constantize.select("geoid10, ST_Buffer(geom,.000001) AS geo, ST_AsGeoJson(ST_Buffer(geom,.000001)) AS geojson, ST_IsValid(ST_Buffer(geom,.000001)) AS isvalid, ST_Area(ST_Buffer(geom,.000001)) AS area").where(:geoid10 => self.geoid10).first end |
#fix_geometry ⇒ Object
81 82 83 84 85 86 87 88 89 |
# File 'lib/generators/census_shapes/setup/templates/models/shape.rb', line 81 def fix_geometry return self if self['isvalid'] == "t" rebuilt_g = MODEL_NAME.rebuild(self.polygons) return rebuilt_g if rebuilt_g['isvalid'] == "t" buffer_g = self.buffer return buffer_g if buffer_g['isvalid'] == "t" puts "ERROR GEOMETRY NOT FIXED" return false end |
#polygons ⇒ Object
95 96 97 |
# File 'lib/generators/census_shapes/setup/templates/models/shape.rb', line 95 def polygons return self.class.name.constantize.select("ST_AsText((ST_DumpRings(((ST_Dump(geom)).geom))).geom) AS g").where(:geoid10 => self.geoid10).map{|p| p['g'] } end |