将点数据列合并为纬度和经度到一个latlon

时间:2013-04-13 04:21:39

标签: ruby-on-rails ruby postgresql rake task

这是我当前从CSV导入的rake任务我可以重写它以导入和删除前两列/使用空格作为分隔符将数据连接成一个latlon列吗?

require 'csv'

desc "Import public library data from csv file"

task :import => [:environment] do
  namespace :import_incidents_csv do
    task :create_incidents => :environment do
      ActiveRecord::Base.connection.execute "COPY PublicLibraryBranchLocations          (DistrictNumber, DistrictName, LibrarySystem, Type, Branch, Phone, Email, Website, PhysicalAddress, City, Province, PostalCode, Latitude, Longitude) FROM '/home/mgmacri/data/PublicLibraryBranchLocations.csv' DELIMITER ',' CSV;"
    end
  end
end

1 个答案:

答案 0 :(得分:2)

COPY本身无法做到这一点,但您可以在导入之前更改CSV。

一种方法是COPY到临时表,然后使用这样的东西导入:

INSERT INTO PublicLibraryBranchLocations(DistrictNumber, DistrictName, LibrarySystem, Type, Branch, Phone, Email, Website, PhysicalAddress, City, Province, PostalCode, LatLon) 
SELECT DistrictNumber, DistrictName, LibrarySystem, Type, Branch, Phone, Email, Website, PhysicalAddress, City, Province, PostalCode, Latitude ||' '|| Longitude
FROM TEMP_PublicLibraryBranchLocations;

如您所见,最后Latitude ||' '|| Longitude有一个连接。

另一种方法是使用file_fdw创建FOREIGN TABLE,因此您可以直接将CSV文件作为表(可能称为PublicLibraryBranchLocations_FDW_CSV)读取,而无需导入该文件并将此数据中的数据导入{ {1}}使用类似的内容:

PublicLibraryBranchLocations