Class: CartoDB::Exporter

Inherits:
Object
  • Object
show all
Defined in:
lib/cartodb-exporter/exporter.rb

Constant Summary collapse

SUPPORTED_FORMATS =
%W{ .csv .shp .kml }
OUTPUT_FILE_LOCATION =
"/tmp"
@@debug =
true

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Exporter

Returns a new instance of Exporter.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cartodb-exporter/exporter.rb', line 17

def initialize(options = {})
  log "options: #{options}"
  @@debug = options[:debug] if options[:debug]
  @table_created = nil
  @export_to_file = options[:export_to_file]
  @type = options[:type]
  raise "export_to_file value can't be nil" if @export_to_file.nil?

  @db_configuration = options.slice(:database, :username, :password, :host, :port)
  @db_configuration[:port] ||= 5432
  @db_configuration[:host] ||= '127.0.0.1'
  @db_connection = Sequel.connect("postgres://#{@db_configuration[:username]}:#{@db_configuration[:password]}@#{@db_configuration[:host]}:#{@db_configuration[:port]}/#{@db_configuration[:database]}")

  unless options[:suggested_name].nil? || options[:suggested_name].blank?
    @force_name = true
    @suggested_name = get_valid_name(options[:suggested_name])
  else
    @force_name = false
  end
  
rescue => e
  log $!
  log e.backtrace
  raise e
end

Class Attribute Details

.debugObject

Returns the value of attribute debug.



8
9
10
# File 'lib/cartodb-exporter/exporter.rb', line 8

def debug
  @debug
end

Instance Attribute Details

#db_configurationObject

Returns the value of attribute db_configuration.



12
13
14
# File 'lib/cartodb-exporter/exporter.rb', line 12

def db_configuration
  @db_configuration
end

#db_connectionObject

Returns the value of attribute db_connection.



12
13
14
# File 'lib/cartodb-exporter/exporter.rb', line 12

def db_connection
  @db_connection
end

#export_to_fileObject

Returns the value of attribute export_to_file.



12
13
14
# File 'lib/cartodb-exporter/exporter.rb', line 12

def export_to_file
  @export_to_file
end

#extObject

Returns the value of attribute ext.



12
13
14
# File 'lib/cartodb-exporter/exporter.rb', line 12

def ext
  @ext
end

#force_nameObject (readonly)

Returns the value of attribute force_name.



15
16
17
# File 'lib/cartodb-exporter/exporter.rb', line 15

def force_name
  @force_name
end

#suggested_nameObject

Returns the value of attribute suggested_name.



12
13
14
# File 'lib/cartodb-exporter/exporter.rb', line 12

def suggested_name
  @suggested_name
end

#table_createdObject (readonly)

Returns the value of attribute table_created.



15
16
17
# File 'lib/cartodb-exporter/exporter.rb', line 15

def table_created
  @table_created
end

#typeObject

Returns the value of attribute type.



12
13
14
# File 'lib/cartodb-exporter/exporter.rb', line 12

def type
  @type
end

Instance Method Details

#export!Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/cartodb-exporter/exporter.rb', line 43

def export!
  path = "#{OUTPUT_FILE_LOCATION}/exporting_#{Time.now.to_i}_#{@export_to_file}"
  
  python_bin_path = `which python`.strip
  psql_bin_path = `which psql`.strip
  
  entries = []
  
  export_type = ".#{@type}"

  if @type == 'csv'
    
    ogr2ogr_bin_path = `which ogr2ogr`.strip
    ogr2ogr_command = %Q{#{ogr2ogr_bin_path} -f "CSV" #{path} PG:"host=#{@db_configuration[:host]} port=#{@db_configuration[:port]} user=#{@db_configuration[:username]} dbname=#{@db_configuration[:database]}" #{@export_to_file}}

    output = `#{ogr2ogr_command} &> /dev/null`
    
    Zip::ZipOutputStream.open("#{path}.zip") do |zia|
      zia.put_next_entry("#{@export_to_file}.#{type}")
      zia.print IO.read("#{path}/#{@export_to_file}.#{type}")
    end
    FileUtils.rm_rf(path)
    
    log "path: #{path}"
    return OpenStruct.new({
      :name => @export_to_file, 
      :import_type => export_type,
      :path => "#{path}.#{type}"
      })
    
  end
  if @type == 'kml'

    ogr2ogr_bin_path = `which ogr2ogr`.strip
    ogr2ogr_command = %Q{#{ogr2ogr_bin_path} -f "KML" #{path}.kml PG:"host=#{@db_configuration[:host]} port=#{@db_configuration[:port]} user=#{@db_configuration[:username]} dbname=#{@db_configuration[:database]}" #{@export_to_file}}

    output = `#{ogr2ogr_command} &> /dev/null`

    Zip::ZipOutputStream.open("#{path}.kmz") do |zia|
      zia.put_next_entry("doc.kml")
      zia.print IO.read("#{path}.kml")
    end
    FileUtils.rm_rf("#{path}.kml")

    log "path: #{path}"
    return OpenStruct.new({
      :name => @export_to_file, 
      :import_type => export_type,
      :path => "#{path}.#{type}"
      })

  end
  if @type == 'shp'

    ogr2ogr_bin_path = `which ogr2ogr`.strip
    ogr2ogr_command = %Q{#{ogr2ogr_bin_path} -f "ESRI Shapefile" #{path}.shp PG:"host=#{@db_configuration[:host]} port=#{@db_configuration[:port]} user=#{@db_configuration[:username]} dbname=#{@db_configuration[:database]}" #{@export_to_file}}

    output = `#{ogr2ogr_command} &> /dev/null`
    
    Zip::ZipOutputStream.open("#{path}.zip") do |zia|
      
      begin
        zia.put_next_entry("#{export_to_file}.shp")
        zia.print IO.read("#{path}.shp")
        FileUtils.rm_rf("#{path}.shp")
      rescue Exception=>e
        # handle e
        log "info #{e}"
      end
    

      begin
        zia.put_next_entry("#{export_to_file}.shx")
        zia.print IO.read("#{path}.shx")
        FileUtils.rm_rf("#{path}.shx")
      rescue Exception=>e
        # handle e
        log "info #{e}"
      end


      begin
        zia.put_next_entry("#{export_to_file}.dbf")
        zia.print IO.read("#{path}.dbf")
        FileUtils.rm_rf("#{path}.dbf")
      rescue Exception=>e
        # handle e
        log "info #{e}"
      end


      begin
        zia.put_next_entry("#{export_to_file}.prj")
        zia.print IO.read("#{path}.prj")
        FileUtils.rm_rf("#{path}.prj")
      rescue Exception=>e
        # handle e
        log "info #{e}"
      end


      begin
        zia.put_next_entry("#{export_to_file}.sbn")
        zia.print IO.read("#{path}.sbn")
        FileUtils.rm_rf("#{path}.sbn")
      rescue Exception=>e
        # handle e
        log "info #{e}"
      end

    end
    
    return OpenStruct.new({
      :name => @export_to_file, 
      :import_type => export_type,
      :path => "#{path}.#{type}"
      })

  end
rescue => e
  log "====================="
  log $!
  log e.backtrace
  log "====================="
  if !@table_created.nil?
    @db_connection.drop_table(@suggested_name)
  end
  raise e
ensure
  @db_connection.disconnect
end