Class: CitySDK::Importer
- Inherits:
-
Object
- Object
- CitySDK::Importer
- Defined in:
- lib/citysdk/importer.rb
Instance Attribute Summary collapse
-
#api ⇒ Object
readonly
Returns the value of attribute api.
-
#filereader ⇒ Object
readonly
Returns the value of attribute filereader.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Instance Method Summary collapse
- #addToAddress(dryrun = false) ⇒ Object
- #doImport(dryrun = false) ⇒ Object
- #filterFields(h) ⇒ Object
-
#initialize(pars) ⇒ Importer
constructor
A new instance of Importer.
- #setLayerStatus(m) ⇒ Object
- #setMatchParameter(l, f, v) ⇒ Object
- #setParameter(k, v) ⇒ Object
- #sign_in ⇒ Object
- #sign_out ⇒ Object
- #write(path) ⇒ Object
Constructor Details
#initialize(pars) ⇒ Importer
Returns a new instance of Importer.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/citysdk/importer.rb', line 6 def initialize(pars) @params = pars raise Exception.new("Missing :host in Importer parameters.") if @params[:host].nil? raise Exception.new("Missing :layername in Importer parameters.") if @params[:layername].nil? raise Exception.new("Missing :file_path in Importer parameters.") if @params[:file_path].nil? @api = CitySDK::API.new(@params[:host]) if @params[:email] raise Exception.new("Missing :passw in Importer parameters.") if @params[:passw].nil? raise Exception.new("Failure to authenticate '#{@params[:email]}' with api.") if not @api.authenticate(@params[:email],@params[:passw]) @api.release end @params[:addresslayer] = 'bag.vbo' if @params[:addressleyer].nil? @params[:addressfield] = 'postcode_huisnummer' if @params[:addressfield].nil? @params[:create_type] = 'create' if @params[:create_type].nil? # CREATE_TYPE_UPDATE = 'update' # CREATE_TYPE_ROUTES = 'routes' # CREATE_TYPE_CREATE = 'create' @filereader = FileReader.new(@params) end |
Instance Attribute Details
#api ⇒ Object (readonly)
Returns the value of attribute api.
4 5 6 |
# File 'lib/citysdk/importer.rb', line 4 def api @api end |
#filereader ⇒ Object (readonly)
Returns the value of attribute filereader.
4 5 6 |
# File 'lib/citysdk/importer.rb', line 4 def filereader @filereader end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
4 5 6 |
# File 'lib/citysdk/importer.rb', line 4 def params @params end |
Instance Method Details
#addToAddress(dryrun = false) ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/citysdk/importer.rb', line 220 def addToAddress(dryrun=false) failed = [] if @params[:postcode] and @params[:housenumber] begin sign_in @filereader.content.each do |rec| row = rec[:properties] pc = row[@params[:postcode]].to_s hn = row[@params[:housenumber]].to_s qres = {} if not (pc.empty? or hn.empty?) pc = pc.downcase.gsub(/[^a-z0-9]/,'') hn.scan(/\d+/).reverse.each { |n| # puts "/nodes?#{@params[:addresslayer]}::#{@params[:addressfield]}=#{pc + n}" qres = @api.get("/nodes?#{@params[:addresslayer]}::#{@params[:addressfield]}=#{pc + n}") break if qres[:status]=='success' and qres[:record_count].to_i >= 1 } else qres[:status]='nix' end if qres[:status]=='success' and qres[:results] and qres[:results][0] url = '/' + qres[:results][0][:cdk_id] + '/' + @params[:layername] n = @api.put(url,{'data'=>filterFields(row)}) if not dryrun else failed << rec end end rescue => e raise e ensure sign_out end return failed end raise Exception.new("Addresses not well defined in dataset.") end |
#doImport(dryrun = false) ⇒ Object
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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/citysdk/importer.rb', line 105 def doImport(dryrun=false) result = { :updated => 0, :created => 0, :not_added => 0 } failed = nil if @params[:hasaddress] == 'certain' failed = addToAddress(dryrun) # elsif @params[:hasaddress] == 'maybe' # failed = addToAddress(dryrun) end if failed == [] result[:updated] += @filereader.content.length return result end if failed result[:updated] += (@filereader.content.length - failed.length) end nodes = failed || @filereader.content count = nodes.length @api.set_createTemplate( { :create => { :params => { #TODO other create types!! :create_type => @params[:create_type], :srid => @params[:srid] || 4326 } } } ) match_tpl = { :match => { :params => { :debug => true, :layers => {} } } } begin sign_in if @params[:unique_id] and @params[:match] and (@params[:hasgeometry] != 'unknown') match_tpl[:match][:params][:radius] = @params[:radius] || 200 match_tpl[:match][:params][:geometry_type] = @params[:geometry_type] || :point @params[:match].each do |a| match_tpl[:match][:params][:layers][a[0]] = {} if match_tpl[:match][:params][:layers][a[0]].nil? match_tpl[:match][:params][:layers][a[0]][a[1]] = a[2] end @api.set_matchTemplate(match_tpl) nodes.each do |rec| node = { :geom => rec[:geometry], :id => rec[:id] } node[:name] = rec[:properties][@params[:name]] if @params[:name] node[:data] = filterFields(rec[:properties]) # puts JSON.pretty_generate(node) yield(node) if block_given? @api.match_create_node(node) if not dryrun count -= 1 end @api.match_create_flush elsif @params[:unique_id] and (@params[:hasgeometry] != 'unknown') # puts "" # puts "doImport... 2" # puts "" begin nodes.each do |rec| node = { :geom => rec[:geometry], :id => rec[:id] } node[:name] = rec[:properties][@params[:name].to_sym] if @params[:name] node[:data] = filterFields(rec[:properties]) yield(node) if block_given? @api.create_node(node) if not dryrun count -= 1 end @api.create_flush rescue => e raise e end else raise Exception.new("Cannot import. Geometry or uniuque id is not known for records.") if failed.nil? end rescue => e raise Exception.new(e.) ensure a = sign_out result[:updated] += a[0] result[:created] += a[1] result[:not_added] = count end return result end |
#filterFields(h) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/citysdk/importer.rb', line 91 def filterFields(h) # puts "h: \n#{h}" # puts "@params[:fields]: \n#{@params[:fields]}" data = {} h.each_key do |k| k = (k.to_sym rescue k) || k data[k] = h[k] if @params[:fields].include?(k) end # puts "data: \n#{data}" data end |
#setLayerStatus(m) ⇒ Object
37 38 39 40 41 42 43 44 45 |
# File 'lib/citysdk/importer.rb', line 37 def setLayerStatus(m) begin sign_in @api.set_layer_status(m.gsub("\n","<br/>")) sign_out rescue Exception => e puts "File Importer setLayerStatus Exception #{e.}" end end |
#setMatchParameter(l, f, v) ⇒ Object
56 57 58 59 60 61 62 63 64 |
# File 'lib/citysdk/importer.rb', line 56 def setMatchParameter(l,f,v) begin @params[:match] = [] if @params[:match].nil? @params[:match] << [l,f,v] return true rescue end nil end |
#setParameter(k, v) ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/citysdk/importer.rb', line 47 def setParameter(k,v) begin @params[(k.to_sym rescue k) || k] = v return true rescue end nil end |
#sign_in ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/citysdk/importer.rb', line 66 def sign_in if @params[:email].nil? raise Exception.new("No credentials provided..") end begin sign_out if @signed_in @api.set_host(@params[:host]) @api.set_layer(@params[:layername]) @api.set_matchTemplate(@params[:match_tpl]) if @params[:match_tpl] @api.set_createTemplate(@params[:create_tpl]) if @params[:create_tpl] @api.authenticate(@params[:email],@params[:passw]) @signed_in = true rescue => e @api.release raise e ensure end end |
#sign_out ⇒ Object
86 87 88 89 |
# File 'lib/citysdk/importer.rb', line 86 def sign_out @signed_in = false return @api.release end |
#write(path) ⇒ Object
33 34 35 |
# File 'lib/citysdk/importer.rb', line 33 def write(path) return @filereader.write(path) end |