Voilà la source pour faire le boulot demandé.
Pour traiter 20Mo de données, ça prend 1 seconde :
Je détaillerai les sources dans le wiki et préviendrai quand ce sera fait
Pour traiter 20Mo de données, ça prend 1 seconde :
Code :
$ time ruby extract_path_from_svg.rb
Extraction started...
real 0m1.023s
user 0m0.857s
sys 0m0.137s
Je détaillerai les sources dans le wiki et préviendrai quand ce sera fait
#!/usr/bin/env ruby
require 'nokogiri'
if __FILE__ == $0
@doc = Nokogiri::XML(File.open("World_Map.svg"))
puts "Extraction started..."
@flights = [
{
:name => 'Kwala',
:color => '#898123',
:id => 24
},
{
:name => 'Bouh',
:color => '#156783',
:id => 25
},
{
:name => 'Victeam',
:color => '#FEFEDE',
:id => 26
},
{
:name => 'Teamon & Pumba',
:color => '#edafce',
:id => 27
}
]
@territory_id = 0
@percentages = [16, 20, 35, 29]
@data_file = File.open('data.js', 'w')
@data_file.write 'var territories = [' + "\n"
@i = 0
@territories_names = @doc.search('d')
@doc.search("path").each do |path|
@territory_id = @territory_id + 1
@percentages.sort_by {rand}
@data_file.write "{\n"
@data_file.write '"id": ' + @territory_id.to_s + ",\n"
@data_file.write '"path": "' + path.attributes['d'].value + '"' + ",\n"
if !@territories_names[@territory_id - 1].nil?
@data_file.write '"name": ' + @territories_names[@territory_id - 1].attributes['id'].value + ",\n"
end
@data_file.write '"owning_informations": [' + "\n"
@data_file.write '{ "owning_percentage": ' + @percentages[0].to_s + ', "flight": {"id": ' + @flights[0][:id].to_s + ', "name": "' + @flights[0][:name] + '", "color": "' + @flights[0][:color] + '"}},' + "\n"
@data_file.write '{ "owning_percentage": ' + @percentages[1].to_s + ', "flight": {"id": ' + @flights[1][:id].to_s + ', "name": "' + @flights[1][:name] + '", "color": "' + @flights[1][:color] + '"}},' + "\n"
@data_file.write '{ "owning_percentage": ' + @percentages[2].to_s + ', "flight": {"id": ' + @flights[2][:id].to_s + ', "name": "' + @flights[2][:name] + '", "color": "' + @flights[2][:color] + '"}},' + "\n"
@data_file.write '{ "owning_percentage": ' + @percentages[3].to_s + ', "flight": {"id": ' + @flights[3][:id].to_s + ', "name": "' + @flights[3][:name] + '", "color": "' + @flights[3][:color] + '"}}' + "\n"
@data_file.write "]\n"
@data_file.write "},\n"
end
@data_file.write ']'
@data_file.close
end