Class: Concensus::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/concensus/resource.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, geometry, state) ⇒ Resource

Returns a new instance of Resource.


23
24
25
26
27
28
# File 'lib/concensus/resource.rb', line 23

def initialize(name, geometry, state)
  @name = name
  @geometry = geometry
  @state = state
  @year = Concensus::configuration.year
end

Instance Attribute Details

#geometryMultiPolygon

Returns georuby geometry object.

Returns:

  • (MultiPolygon)

    georuby geometry object


12
13
14
# File 'lib/concensus/resource.rb', line 12

def geometry
  @geometry
end

#nameString

Returns this resource's name in the shpfile metadata.

Returns:

  • (String)

    this resource's name in the shpfile metadata


15
16
17
# File 'lib/concensus/resource.rb', line 15

def name
  @name
end

#stateString

Returns the state this resource belongs to, if applicable.

Returns:

  • (String)

    the state this resource belongs to, if applicable


18
19
20
# File 'lib/concensus/resource.rb', line 18

def state
  @state
end

#yearFixnum

Returns this resource's census year.

Returns:

  • (Fixnum)

    this resource's census year


21
22
23
# File 'lib/concensus/resource.rb', line 21

def year
  @year
end

Class Method Details

.already_unzipped?(zipped_file_path) ⇒ Boolean

Returns true if the zip's files are found in temporary storage.

Returns:

  • (Boolean)

    true if the zip's files are found in temporary storage


99
100
101
102
103
104
# File 'lib/concensus/resource.rb', line 99

def self.already_unzipped?(zipped_file_path)
  file_path_without_extension = filename_without_extension(zipped_file_path)
  File.exists?("#{file_path_without_extension}.shp") &&
  File.exists?("#{file_path_without_extension}.dbf") &&
  File.exists?("#{file_path_without_extension}.shx")
end

.file_extension(filename) ⇒ String

Returns file's extension.

Parameters:

  • filename (String)

Returns:

  • (String)

    file's extension.


114
115
116
# File 'lib/concensus/resource.rb', line 114

def self.file_extension(filename)
  filename[/\.[a-z]*$/]
end

.filename_without_extension(filename) ⇒ String

Returns filename with the extension stripped.

Parameters:

  • filename (String)

Returns:

  • (String)

    filename with the extension stripped.


108
109
110
# File 'lib/concensus/resource.rb', line 108

def self.filename_without_extension(filename)
  filename.gsub(/\.[a-z]*$/, "")
end

.get_and_unzip(uri) ⇒ String

Checks for census zipfile in temporary storage. If not there, downloads it. Then, checks for unzipped files in temporary storage. If not there, unzips the archive.

Returns:

  • (String)

    path to shpfile in temporary storage


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/concensus/resource.rb', line 34

def self.get_and_unzip(uri)
  safe_filename = uri.gsub("/", "_").gsub(".zip", "")
  zipped_file_path = "#{Concensus::configuration.tmp_dir}#{safe_filename}.zip"
  
  if !File.exists?(zipped_file_path)
    zipped_file = File.new(zipped_file_path, "wb")
    ext_file = open(Concensus::configuration.root_url + uri)
    zipped_file.write(ext_file.read)
  end
  
  if !already_unzipped?(zipped_file_path)
    unzipped_files = Zip::ZipFile.open(zipped_file_path)
  
    unzipped_files.each do |x|
      file = File.new(Concensus::configuration.tmp_dir + safe_filename + file_extension(x.to_s), "wb")
      file.write(x.get_input_stream.read)
      file.close
    end
  end
  
  return "#{Concensus::configuration.tmp_dir}#{safe_filename}.shp"
end

.process_find(class_name, shp_file_path, identifier, state, name = nil) ⇒ Array, Resource

Converts the shpfile to a georuby object.

If we're searching for a specific region in the shpfile, we find it by iterating over each region and matching by metadata. We return the first one we find.

Returns:

  • (Array)

    returns array if we're looking for mulitple regions

  • (Resource)

    returns resource if we're looking for a specific region


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/concensus/resource.rb', line 65

def self.process_find(class_name, shp_file_path, identifier, state, name = nil)
  
  # Prevent annoying georuby error messages
  previous_stderr, $stderr = $stderr, StringIO.new
  
  if name
    GeoRuby::Shp4r::ShpFile.open(shp_file_path) do |shp|
      matched_shape = shp.find {|x| x.data[identifier].match(name) }
      raise ShapeNotFound if !matched_shape
      return class_name.split('::').reduce(Concensus){|cls, c| cls.const_get(c) }.new(matched_shape.data[identifier], matched_shape.geometry, state)
    end
  else
    places = []
    GeoRuby::Shp4r::ShpFile.open(shp_file_path).each do |shp|
      places << class_name.split('::').reduce(Concensus){|cls, c| cls.const_get(c) }.new(shp.data[identifier], shp.geometry, state)
    end
    return places
  end
  
  # Restore previous value of stderr
  $stderr.string
  $stderr = previous_stderr
  
end

.state_code_to_id(state_code) ⇒ Fixnum

Converts a state code, like “CA”, to the US Census Bureau's numerical identifier.

Parameters:

  • two-letter (String)

    state code

Returns:

  • (Fixnum)

    numerical census state identifier


93
94
95
# File 'lib/concensus/resource.rb', line 93

def self.state_code_to_id(state_code)
  Concensus::configuration.census_state_ids[state_code]
end