Skip to content

Commit

Permalink
load_geographic_area_shapes still does not work.
Browse files Browse the repository at this point in the history
  • Loading branch information
TuckerJD committed Feb 24, 2014
1 parent 5968c3b commit 7cbea58
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 52 deletions.
5 changes: 4 additions & 1 deletion app/models/geographic_area.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ class GeographicArea < ActiveRecord::Base
belongs_to :level0, class_name: "GeographicArea", foreign_key: :level0_id
belongs_to :level1, class_name: "GeographicArea", foreign_key: :level1_id
belongs_to :level2, class_name: "GeographicArea", foreign_key: :level2_id

#belongs_to :geographic_item

belongs_to :gadm_geo_item, class_name: "GeographicItem", foreign_key: :gadm_geo_item_id
belongs_to :tdwg_geo_item, class_name: "GeographicItem", foreign_key: :id
belongs_to :tdwg_geo_item, class_name: "GeographicItem", foreign_key: :tdwg_geo_item_id
belongs_to :ne_geo_item, class_name: "GeographicItem", foreign_key: :ne_geo_item_id

validates_presence_of :data_origin
Expand Down
2 changes: 2 additions & 0 deletions app/models/geographic_area_type.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
class GeographicAreaType < ActiveRecord::Base
include Housekeeping::Users

has_many :geographic_areas, inverse_of: :geographic_area_type

validates_presence_of :name
end
5 changes: 2 additions & 3 deletions app/models/geographic_item.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
class GeographicItem < ActiveRecord::Base
include Housekeeping::Users

belongs_to :geographic_area
belongs_to :georeference
belongs_to :confidence

# Where would one put such code?
# RGeo::Geos.preferred_native_interface = :ffi
Expand All @@ -25,9 +27,6 @@ class GeographicItem < ActiveRecord::Base
set_rgeo_factory_for_column(t, column_factory)
end

belongs_to :geographic_area
belongs_to :confidence

validate :proper_data_is_provided

def object
Expand Down
110 changes: 62 additions & 48 deletions lib/tasks/initialization/geographic_areas.rake
Original file line number Diff line number Diff line change
Expand Up @@ -89,56 +89,70 @@ namespace :tw do

#'G:\Share\rails\shapes\gadm_v2_shp\gadm2.shp'
filename = ENV['table_name']
RGeo::Shapefile::Reader.open(filename, factory: Georeference::FACTORY) do |file|

file.each do |record|

# there are different ways of locating the record for this shape, depending on where the data came from.
finder = {}
placer = nil
case filename
when /ne_10m_admin_0_countries\.shp/i
finder = {:neID => record['iso_n3']}
placer = 'ne_geo_item'
when /ne_10m_admin_1_states_provinces_shp\.shp/i
finder = {:neID => record['adm1_code']}
placer = 'ne_geo_item'
when /level1/i
finder = {:tdwgID => record['LEVEL1_COD'].to_s + '----'}
placer = 'tdwg_geo_item'
when /level2/i
finder = {:tdwgID => record['LEVEL2_COD'].to_s + '---'}
placer = 'tdwg_geo_item'
when /level3/i
finder = {:tdwgID => record['LEVEL2_COD'].to_s + record['LEVEL3_COD']}
placer = 'tdwg_geo_item'
when /level4/i
finder = {:tdwgID => record['Level2_cod'].to_s + record['Level4_cod']}
placer = 'tdwg_geo_item'
when /gadm2/i
finder = {:gadmID => record['OBJECTID']}
placer = 'gadm_geo_item'
else
finder = nil
placer = nil
end
shapes = RGeo::Shapefile::Reader.open(filename, factory: Georeference::FACTORY)

shapes.each do |record|

# there are different ways of locating the record for this shape, depending on where the data came from.
finder = {}
placer = nil
case filename
when /ne_10m_admin_0_countries\.shp/i
finder = {:neID => record['iso_n3']}
placer = 'ne_geo_item'
when /ne_10m_admin_1_states_provinces_shp\.shp/i
finder = {:neID => record['adm1_code']}
placer = 'ne_geo_item'
when /level1/i
finder = {:tdwgID => record['LEVEL1_COD'].to_s + '----'}
placer = 'tdwg_geo_item'
when /level2/i
finder = {:tdwgID => record['LEVEL2_COD'].to_s + '---'}
placer = 'tdwg_geo_item'
when /level3/i
finder = {:tdwgID => record['LEVEL2_COD'].to_s + record['LEVEL3_COD']}
placer = 'tdwg_geo_item'
when /level4/i
finder = {:tdwgID => record['Level2_cod'].to_s + record['Level4_cod']}
placer = 'tdwg_geo_item'
when /gadm2/i
finder = {:gadmID => record['OBJECTID']}
placer = 'gadm_geo_item'
else
finder = nil
placer = nil
end

if finder.nil?
if finder.nil?
else
ga = GeographicArea.where(finder)
if ga.nil?
else
ga = GeographicArea.where(finder)
if ga.nil?
else
ga.each { |area|
gi = GeographicItem.new(creator: builder,
updater: builder,
multi_polygon: record.geometry)
gi.save!

area.send("#{placer}=".to_sym, gi)
area.save!
area
}
end

# create the shape in which we are interested.
gi = GeographicItem.new(creator: builder,
updater: builder,
multi_polygon: record.geometry)
gi.save!

ga.each { |area|
# placer = 'geographic_item'
area.send("#{placer}=".to_sym, gi)

#case placer
# when /tdwg/
# area.tdwg_geo_item = gi
# when /ne_/
# area.ne_geo_item = gi
# when /gadm/
# area.gadm_geo_item = gi
# else
# area
#end

area.save
area
}
end
end
end
Expand Down

0 comments on commit 7cbea58

Please sign in to comment.