Skip to content

Commit

Permalink
Comparing nw110 to ne10 & ne50.
Browse files Browse the repository at this point in the history
  • Loading branch information
TuckerJD committed Jan 14, 2014
1 parent ff30e84 commit 99ef533
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 27 deletions.
8 changes: 8 additions & 0 deletions db/migrate/20140113215233_tweak_geographic_areas6.rb
@@ -0,0 +1,8 @@
class TweakGeographicAreas6 < ActiveRecord::Migration
def change

add_column :geographic_areas, :ne_geo_item_id, :integer
add_column :geographic_areas, :data_origin, :string

end
end
44 changes: 24 additions & 20 deletions lib/tasks/build_geographic_area.rake
Expand Up @@ -9,9 +9,9 @@

ISO_YEAR_1_2 = 'ISO 3166-1-alpha-2' # ISO 3166-1:2006 two-letter country abbreviations
ISO_YEAR_1_3 = 'ISO 3166-1-alpha-3' # ISO 3166-1:2006 three-letter country abbreviations
# Other current choices are:
ISO_YEAR_3 = 'ISO 3166-3:1999' # Country names which have been deleted from -1 since 1974
ISO_YEAR_2 = 'ISO 3166-2:2007' # State or Province level for country codes in 3166-1
# Other current choices are:
ISO_YEAR_3 = 'ISO 3166-3:1999' # Country names which have been deleted from -1 since 1974
ISO_YEAR_2 = 'ISO 3166-2:2007' # State or Province level for country codes in 3166-1

TDWG2_L1 = 'TDWG2 Level 1'
TDWG2_L2 = 'TDWG2 Level 2'
Expand All @@ -20,6 +20,10 @@ TDWG2_L4 = 'TDWG2 Level 4'

GADM2 = 'GADM2'

NE_10 = 'NaturalEarth (10m)'
NE_50 = 'NaturalEarth (50m)'
NE_110 = 'NaturalEarth (110m)'

#
namespace :tw do
namespace :init do
Expand Down Expand Up @@ -319,7 +323,7 @@ def read_dbf(filenames)

@lvl0_items, @lvl1_items, @lvl2_items, @lvl3_items, @lvl4_items, @lvl5_items = {}, {}, {}, {}, {}, {}

# @global will be filled such that ga.name is the key for easier location later
# @global will be filled such that ga.name is the key for easier location later
iso_items, @global = {}, {}
gat5 = GeographicAreaType.where(name: 'Country').first

Expand Down Expand Up @@ -609,7 +613,7 @@ def read_dbf(filenames)
ga.iso_3166_a2 = nation_code
ga.geographic_area_type = gat5
# the following may seem a bit redundant, but it is indicated the end of a national hierarchy
ga.level0 = ga
ga.level0 = ga
end
iso_items.merge!(ga.name => ga) if !(ga.nil?)
end
Expand All @@ -624,7 +628,7 @@ def read_dbf(filenames)
gadm2.each { |item|

l0_name = item['NAME_0'].titlecase
l0_iso = item['ISO']
l0_iso = item['ISO']
l0_id = item['ID_0']

gadm_id = item['OBJECTID']
Expand Down Expand Up @@ -660,9 +664,9 @@ def read_dbf(filenames)
# build lvl0 key value from lvl0 data

l0_key = {
'ID_0' => l0_id,
'ISO' => l0_iso,
'NAME_0' => l0_name
'ID_0' => l0_id,
'ISO' => l0_iso,
'NAME_0' => l0_name
}

# look in iso_items for an existing record by name
Expand All @@ -674,9 +678,9 @@ def read_dbf(filenames)

if ga.nil?
# create a record for the zero level, and the @global list
ga = GeographicArea.new(parent: earth,
ga = GeographicArea.new(parent: earth,
iso_3166_a3: l0_iso,
name: l0_name)
name: l0_name)
else
# l0 is the object we want
l0 = ga
Expand All @@ -702,7 +706,7 @@ def read_dbf(filenames)
@global.merge!(place)
# yes, this *is* the same as parent
ga.level0 = ga
l0 = ga
l0 = ga

if l1_name.empty?
# nothing to do
Expand Down Expand Up @@ -772,7 +776,7 @@ def read_dbf(filenames)
ga.level0 = ga.parent
ga.level1 = ga
end
l1 = ga
l1 = ga

if l2_name.empty?
# nothing to do
Expand All @@ -798,18 +802,18 @@ def read_dbf(filenames)
l2 = @lvl2_items[l2_key]
if l2.nil?
# create a record for level 2, and the @global list
ga = GeographicArea.new(parent: l1,
name: l2_name,
geographic_area_type: add_gat(item['ENGTYPE_2']))
ga = GeographicArea.new(parent: l1,
name: l2_name,
geographic_area_type: add_gat(item['ENGTYPE_2']))

ga.gadm_valid_from = item['VALIDFR_2']
ga.gadm_valid_to = item['VALIDTO_2']
ga.level0 = ga.parent.parent
ga.level1 = ga.parent
ga.level2 = ga
ga.level0 = ga.parent.parent
ga.level1 = ga.parent
ga.level2 = ga

# put the item in the lvl2 list
place = {l2_key => ga}
place = {l2_key => ga}
@lvl2_items.merge!(place)
# and the @global list
@global.merge!(place)
Expand Down
48 changes: 41 additions & 7 deletions lib/tasks/compare_ne10_to_gadm.rake
Expand Up @@ -8,23 +8,57 @@ namespace :tw do

matched = 0
unmatched = 0
a3_only = 0
index = 0
result = ''

ne10 = RGeo::Shapefile::Reader.open('G:\Share\Downloads\PostgreSQL\PostGIS\10m_cultural\10m_cultural\ne_10m_admin_0_countries.shp', factory: Georeference::FACTORY)
# ne0_10 = RGeo::Shapefile::Reader.open('G:\Share\Downloads\PostgreSQL\PostGIS\10m_cultural\10m_cultural\ne_10m_admin_0_countries.shp', factory: Georeference::FACTORY)
ne0_10 = DBF::Table.new('G:\Share\Downloads\PostgreSQL\PostGIS\10m_cultural\10m_cultural\ne_10m_admin_0_countries.dbf')
ne1_10 = DBF::Table.new('G:\Share\Downloads\PostgreSQL\PostGIS\10m_cultural\10m_cultural\ne_10m_admin_1_states_provinces_lines_shp.dbf')
ne0_110 = DBF::Table.new('G:\Share\rails\shapes\NaturalEarth\ne_110m_admin_0_countries\ne_110m_admin_0_countries.dbf')
ne0_10 = DBF::Table.new('G:\Share\Downloads\PostgreSQL\PostGIS\10m_cultural\10m_cultural\ne_10m_admin_0_countries.dbf')
# gadm = RGeo::Shapefile::Reader.open('G:\Share\rails\shapes\gadm_v2_shp\gadm2.shp', factory: Georeference::FACTORY)

ne10.each {|item|
ga = GeographicArea.where(name: item.attributes['name'])
=begin
ne1_10.each {|item|
ne1_name = item.attributes['name']
if ne1_name =~ /US_/
puts ne1_name
end
}
=end

ne0_110.each {|item|
ne_a3 = item.attributes['adm0_a3']
ne_name = item.attributes['name'].titlecase
ga = GeographicArea.where(name: ne_name).first
if ga.nil?
unmatched += 1
result = "Unmatched (#{item.index}): #{item.attributes['name']}"
result = "Unmatched (#{index}): #{ne_name} (#{ne_a3})"
ga3 = GeographicArea.where(iso_3166_a3: ne_a3).first
if ga3.nil?
result += ' No A3 match found.'
puts result
else
result += " Found A3 match: (#{ga3.name})."
a3_only += 1
unmatched -= 1
matched += 1
puts result
end
else
result = "Matched (#{item.index}): #{ga.name}"
result = "Matched (#{index}): #{ga.name}"
if ne_a3 == ga.iso_3166_a3
result += " (#{ga.iso_3166_a3})"
else
result += " (A3 #{ne_a3} did not match.)"
end
matched += 1
puts result
end
puts result
index += 1
}
puts "Processed: #{matched + unmatched}, Matched: #{matched}, Unmatched: #{unmatched}."
puts "\nProcessed: #{matched + unmatched}: Unmatched: #{unmatched}, Matched: #{matched} (A3 Only: #{a3_only})."
end
end
end
Expand Down

0 comments on commit 99ef533

Please sign in to comment.