Created
October 21, 2010 20:59
-
-
Save timwaters/639346 to your computer and use it in GitHub Desktop.
batch import of control points into mapwarper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'fastercsv' | |
namespace :import do | |
desc "Imports GCP from a CSV file" | |
task :gcps_from_csv => :environment do | |
filename = "/path/to/lovely.csv" | |
puts "This imports a load of points from a csv file." | |
puts "WARNING: This may bugger up the system, especially if you have a lot of points!" | |
puts "Using File #{filename}" | |
print "Are you sure you want to continue ? [y/N] " | |
break unless STDIN.gets.match(/^y$/i) | |
data = open(filename) | |
points = FasterCSV.parse(data, :headers => true, :header_converters => :symbol, :col_sep => ",") | |
points.by_row! | |
p "Preparing to insert points! " | |
count = 0 | |
dup_count = 0 | |
points.each do | point | | |
if point.size > 0 | |
map_id = Map.find_by_upload_file_name(point[:filename]).id.to_i | |
gcp_conditions = {:x => point[:x].to_f, :y => point[:y].to_f, :lat => point[:lat].to_f, :lon => point[:lon].to_f, :map_id => map_id} | |
unless Gcp.exists?(gcp_conditions) | |
gcp = Gcp.new(gcp_conditions ) | |
gcp.save | |
print '.' | |
count+=1 | |
else | |
dup_count+=1 | |
print '-' | |
#Gcp.delete_all(gcp_conditions) | |
end | |
end | |
end | |
p "BOOM! Payload delivered! #{count} Points added. (#{dup_count} dups)." | |
end | |
end | |
filename,x,y,lat,lon | |
Image000.jpg,1,2,22.2,33.2 | |
Image000.jpg,3,4,232.22,323.2 | |
group.jpg,6,7,22.2,33.2 | |
group.jpg,6,7,232.22,323.2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment