Class: Nhgis::ShapeFile

Inherits:
NhgisActiveRecord::Base
  • Object
show all
Defined in:
app/models/nhgis/shape_file.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.ids_from_filters(ds_list, gl_list, ti_list, tp_list, tp_as_bv_list, kw_list, tp_operator, gl_operator, ti_operator) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/nhgis/shape_file.rb', line 39

def self.ids_from_filters(ds_list, gl_list, ti_list, tp_list, tp_as_bv_list, kw_list, tp_operator, gl_operator, ti_operator)
  # shortcut 2 (like the one in paginated_rows_from_filters below)   -- TODO: refactor opportunity
  if tp_list.size > 0 || tp_as_bv_list.size > 0 || kw_list.size > 0
    ds_list = Dataset.get_istads_ids_for_keywords_topics(ds_list, kw_list, tp_list, tp_as_bv_list, tp_operator, ti_operator)
    return [] if ds_list.size == 0
  end

  # FIXME - is this a correct assumption, ongoing, w/future integration work??
  # shortcut 1: we 'know' with multiple years AND'd there will be no boundary files
  if ti_list.size > 1 && ti_operator == "AND"
    return []
  end

  sqlbld = sql_from_filters(ds_list, gl_list, ti_list, ti_operator, nil, nil)
  sql = sqlbld.sql
  RunSqlRun.return_array(sql).map{|r| r['id']}
end

.paginated_rows_from_filters(filter_selections, page, per_page, sort_index, sort_order, ignore1, ignore2 = nil, ignore3 = nil) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/models/nhgis/shape_file.rb', line 58

def self.paginated_rows_from_filters(filter_selections, page, per_page, sort_index, sort_order, ignore1, ignore2=nil, ignore3=nil)
  ds_list = filter_selections.filtered_datasets
  gl_list = filter_selections.filtered_geog_levels
  ti_list = filter_selections.filtered_years
  tp_list = filter_selections.filtered_topics
  tp_as_bv_list = filter_selections.filtered_topics_as_breakdowns
  kw_list = filter_selections.filtered_keywords
  tp_operator = filter_selections.filtered_topics_operator
  ti_operator = filter_selections.filtered_years_operator

  # FIXME - is this a correct assumption, ongoing, w/future integration work??
  # shortcut 1: we 'know' with multiple years AND'd there will be no boundary files
  if ti_list.size > 1 && ti_operator == "AND"
    # TODO: include a max-pop-score entry, possibly w/1.0 or some default - value doesn't matter
    return GridPaginate.new([], 0, per_page)
  end
  # shortcut 2:
  if tp_list.size > 0 || tp_as_bv_list.size > 0 || kw_list.size > 0
    ds_list = Dataset.get_istads_ids_for_keywords_topics(ds_list, kw_list, tp_list, tp_as_bv_list, tp_operator, ti_operator)
    # TOOD: in this case, max-pop-score comes from sister method, paginated_rows_from_istads_ids
    return paginated_rows_from_istads_ids([], page, per_page, sort_index, sort_order) if ds_list.size == 0
  end
  sqlbld = sql_from_filters(ds_list, gl_list, ti_list, ti_operator, sort_index, sort_order)

  # Part I - rowcount
  sql = sqlbld.sql
  query = sanitize_sql(sql.dup)
  rowcount, dummy = find_by_sql(query).size.to_i  # max_pop_score not being set here (yet!)

  # Part II - max population score
  sql = sqlbld.sql_override_selects_no_order_by(["COUNT(sf.id) AS count", "MAX(sf.popularity_score) AS maxpop"])
  query = sanitize_sql(sql.dup)
  answer =  RunSqlRun.return_array(query)[0]
  dummy, max_pop_score = answer["count"], answer["maxpop"]

  rows = paginated_grid_rows_query(page, per_page) do
    sqlbld.selects << "#{max_pop_score} AS `max_popularity_score`" if !max_pop_score.nil?
    sqlbld.sql
  end
  GridPaginate.new(rows, rowcount, per_page)
end

.paginated_rows_from_istads_ids(istads_ids, page, per_page, sort_index, sort_order) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/models/nhgis/shape_file.rb', line 100

def self.paginated_rows_from_istads_ids(istads_ids, page, per_page, sort_index, sort_order)
  return GridPaginate.new([], 0, per_page) if istads_ids.empty?
  sqlbld = sql_from_istads_ids(istads_ids, sort_index, sort_order)

  # Part I - rowcount
  rowcount, dummy = total_row_count_query { sqlbld.sql }

  # Part II - max population score
  sql = sqlbld.sql_override_selects_no_order_by(["COUNT(sf.id) AS count", "MAX(sf.popularity_score) AS maxpop"])
  query = sanitize_sql(sql.dup)
  answer =  RunSqlRun.return_array(query)[0]
  dummy, max_pop_score = answer["count"], answer["maxpop"]

  rows = paginated_grid_rows_query(page, per_page) do
    sqlbld.selects << "#{max_pop_score} AS `max_popularity_score`" if !max_pop_score.nil?
    sqlbld.sql
  end
  GridPaginate.new(rows, rowcount, per_page)
end

.popscore_csv_file_nameObject



35
36
37
# File 'app/models/nhgis/shape_file.rb', line 35

def self.popscore_csv_file_name
  "popscores_shape_files.csv"
end

.popscore_temp_table_nameObject



31
32
33
# File 'app/models/nhgis/shape_file.rb', line 31

def self.popscore_temp_table_name
  "TEMP_pop_shp"
end

.shape_file_information(relative_pathname, filename) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/models/nhgis/shape_file.rb', line 12

def self.shape_file_information(relative_pathname, filename)

  sql = <<-SQL_O_MATIC
select sf.geog_label, sf.cached_year_label, st.directory_name as source_dir, sf.id, sf.filename, sf.relative_pathname, sf.description, sf.*,
gt.time_instance_id, gt.geog_level_id, gl.istads_id
from shape_files sf
join geotimes gt on gt.id = sf.geotime_id
join geog_levels gl on gl.id = gt.geog_level_id and gl.istads_id in ("nation", "state", "county")
join source_types st on st.id = sf.source_type_id
WHERE sf.relative_pathname = ? AND sf.filename = ?
order by geog_label, cached_year_label, st.directory_name
  SQL_O_MATIC

  sql = ShapeFile.send(:sanitize_sql_array, [sql, relative_pathname, filename])

  connection.execute(sql).to_a
end

Instance Method Details

#extract_output_filenameObject



120
121
122
123
124
# File 'app/models/nhgis/shape_file.rb', line 120

def extract_output_filename
  #TODO add base vs. integ when integrated shapefiles are deployed (and added to the metadata) krh 2/19/20131
  #TODO add generalizations (or nongen) when generalized shapefiles are deployed (and added to the metadata) krh 2/19/20131
  "#{source_type.directory_name}_#{filename}"
end