From 40d276cc4cc8fb37231e7ca3b9d020ef1fa832ab Mon Sep 17 00:00:00 2001 From: Andrew W Hill Date: Fri, 13 Apr 2012 12:56:14 -0400 Subject: [PATCH] added reprojection and santization jobs to the utils for reuse across importers --- .../lib/cartodb-importer/lib/utils.rb | 17 ++++++++++++++ .../lib/cartodb-importer/loaders/shp.rb | 22 ++++++++----------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/lib/importer/lib/cartodb-importer/lib/utils.rb b/lib/importer/lib/cartodb-importer/lib/utils.rb index 9ed39b987..5ad0bed45 100644 --- a/lib/importer/lib/cartodb-importer/lib/utils.rb +++ b/lib/importer/lib/cartodb-importer/lib/utils.rb @@ -86,6 +86,23 @@ def log str #puts str # if @@debug end + def reproject_import random_table_name + @db_connection.run("ALTER TABLE #{random_table_name} RENAME COLUMN the_geom TO the_geom_orig;") + geom_type = @db_connection["SELECT GeometryType(the_geom_orig) as type from #{random_table_name} WHERE the_geom_orig IS NOT NULL LIMIT 1"].first[:type] + @db_connection.run("SELECT AddGeometryColumn('#{random_table_name}','the_geom',4326, '#{geom_type}', 2);") + @db_connection.run("UPDATE \"#{random_table_name}\" SET the_geom = ST_Force_2D(ST_Transform(the_geom_orig, 4326)) WHERE the_geom_orig IS NOT NULL") + @db_connection.run("ALTER TABLE #{random_table_name} DROP COLUMN the_geom_orig") + @db_connection.run("CREATE INDEX \"#{random_table_name}_the_geom_gist\" ON \"#{random_table_name}\" USING GIST (the_geom)") + end + def sanitize_table_columns random_table_name + # Sanitize column names where needed + column_names = @db_connection.schema(random_table_name).map{ |s| s[0].to_s } + need_sanitizing = column_names.each do |column_name| + if column_name != column_name.sanitize_column_name + @db_connection.run("ALTER TABLE #{random_table_name} RENAME COLUMN \"#{column_name}\" TO #{column_name.sanitize_column_name}") + end + end + end end end end \ No newline at end of file diff --git a/lib/importer/lib/cartodb-importer/loaders/shp.rb b/lib/importer/lib/cartodb-importer/loaders/shp.rb index e7250756b..84b7cfc66 100644 --- a/lib/importer/lib/cartodb-importer/loaders/shp.rb +++ b/lib/importer/lib/cartodb-importer/loaders/shp.rb @@ -75,12 +75,7 @@ def process! if shp_args_command[0] != '4326' @data_import.log_update("reprojecting the_geom column from #{shp_args_command[0]} to 4326") begin - @db_connection.run("ALTER TABLE #{random_table_name} RENAME COLUMN the_geom TO the_geom_orig;") - geom_type = @db_connection["SELECT GeometryType(the_geom_orig) as type from #{random_table_name} WHERE the_geom_orig IS NOT NULL LIMIT 1"].first[:type] - @db_connection.run("SELECT AddGeometryColumn('#{random_table_name}','the_geom',4326, '#{geom_type}', 2);") - @db_connection.run("UPDATE \"#{random_table_name}\" SET the_geom = ST_Force_2D(ST_Transform(the_geom_orig, 4326)) WHERE the_geom_orig IS NOT NULL") - @db_connection.run("ALTER TABLE #{random_table_name} DROP COLUMN the_geom_orig") - @db_connection.run("CREATE INDEX \"#{random_table_name}_the_geom_gist\" ON \"#{random_table_name}\" USING GIST (the_geom)") + reproject_import random_table_name rescue Exception => msg @runlog.err << msg @data_import.set_error_code(2000) @@ -91,14 +86,15 @@ def process! end begin # Sanitize column names where needed + sanitize_table_columns random_table_name # TODO: We need to move this out of all loader methods and into the importer.rb method - column_names = @db_connection.schema(random_table_name).map{ |s| s[0].to_s } - need_sanitizing = column_names.each do |column_name| - - if column_name != column_name.sanitize_column_name - @db_connection.run("ALTER TABLE #{random_table_name} RENAME COLUMN \"#{column_name}\" TO #{column_name.sanitize_column_name}") - end - end + # column_names = @db_connection.schema(random_table_name).map{ |s| s[0].to_s } + # need_sanitizing = column_names.each do |column_name| + # + # if column_name != column_name.sanitize_column_name + # @db_connection.run("ALTER TABLE #{random_table_name} RENAME COLUMN \"#{column_name}\" TO #{column_name.sanitize_column_name}") + # end + # end rescue @runlog.err << msg @data_import.log_update("ERROR: Failed to sanitize some column names")