Skip to content

Commit

Permalink
find upper and lower state_districts by location
Browse files Browse the repository at this point in the history
  • Loading branch information
msimonborg committed Jun 18, 2017
1 parent c796c7e commit 103ade5
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 12 deletions.
4 changes: 1 addition & 3 deletions .rubocop.yml
Expand Up @@ -5,9 +5,7 @@ AllCops:

Lint/AmbiguousBlockAssociation:
Exclude:
- 'app/models/office_location.rb'
- 'app/models/rep.rb'
- 'app/models/state.rb'
- 'app/models/**/*'

Metrics/LineLength:
Max: 100
Expand Down
10 changes: 10 additions & 0 deletions app/models/concerns/state_district_scopes.rb
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module StateDistrictScopes
def self.included(base)
base.class_eval do
scope :upper, -> { where chamber: 'upper' }
scope :lower, -> { where chamber: 'lower' }
end
end
end
2 changes: 2 additions & 0 deletions app/models/state_district.rb
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class StateDistrict < ApplicationRecord
include StateDistrictScopes

belongs_to :state, foreign_key: :state_code, primary_key: :state_code
has_many :state_district_geoms, foreign_key: :full_code, primary_key: :full_code

Expand Down
1 change: 1 addition & 0 deletions app/models/state_district_geom.rb
Expand Up @@ -2,5 +2,6 @@

class StateDistrictGeom < ApplicationRecord
include Geographic
include StateDistrictScopes
belongs_to :state_district, foreign_key: :full_code, primary_key: :full_code
end
28 changes: 21 additions & 7 deletions app/services/coordinates.rb
Expand Up @@ -43,18 +43,31 @@ def districts
NullObject.new
else
{
congress: find_district_geom.district,
state: find_state_district_geom.state_district
congress: district_geom.district,
state_lower: state_lower_district_geom.state_district,
state_upper: state_upper_district_geom.state_district
}
end
end

def find_district_geom
DistrictGeom.containing_latlon(lat, lon).includes(district: :state).take || NullObject.new
def district_geom
@_district_geom ||= begin
DistrictGeom.containing_latlon(lat, lon).includes(district: :state).take || NullObject.new
end
end

def state_district_geoms
@_state_district_geoms ||= begin
StateDistrictGeom.containing_latlon(lat, lon).includes(:state_district) || NullObject.new
end
end

def state_lower_district_geom
state_district_geoms.lower.take || NullObject.new
end

def find_state_district_geom
StateDistrictGeom.containing_latlon(lat, lon).includes(:state_district).take || NullObject.new
def state_upper_district_geom
state_district_geoms.upper.take || NullObject.new
end

def state
Expand All @@ -63,7 +76,8 @@ def state
end

def mismatching_states?
if !districts[:state].blank? && districts[:congress].state != districts[:state].state
if !districts[:state_upper].blank? &&
districts[:congress].state != districts[:state_upper].state
true
else
false
Expand Down
8 changes: 6 additions & 2 deletions app/services/geo_lookup.rb
Expand Up @@ -42,7 +42,11 @@ def congressional_district
districts[:congress]
end

def state_district
districts[:state]
def state_lower_district
districts[:state_lower]
end

def state_upper_district
districts[:state_upper]
end
end
1 change: 1 addition & 0 deletions db/migrate/20170618161141_create_state_district_geoms.rb
Expand Up @@ -4,6 +4,7 @@ class CreateStateDistrictGeoms < ActiveRecord::Migration[5.0]
def change
create_table :state_district_geoms do |t|
t.string :full_code
t.string :chamber
t.geometry :geom, srid: 3857

t.timestamps
Expand Down
1 change: 1 addition & 0 deletions db/schema.rb
Expand Up @@ -133,6 +133,7 @@

create_table 'state_district_geoms', force: :cascade do |t|
t.string 'full_code'
t.string 'chamber'
t.geometry 'geom', limit: { srid: 3857, type: 'geometry' }
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
Expand Down
1 change: 1 addition & 0 deletions lib/state_district_exporter.rb
Expand Up @@ -48,6 +48,7 @@ def load_geoms_into_database
record.geometry.projection.each do |poly|
StateDistrictGeom.create(
full_code: full_code(record),
chamber: chamber,
geom: poly
)
end
Expand Down

0 comments on commit 103ade5

Please sign in to comment.