Class: CitySDK::Importer

Inherits:
Object show all
Defined in:
lib/citysdk/importer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pars, fr = nil) ⇒ Importer

Returns a new instance of Importer.

Raises:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/citysdk/importer.rb', line 7

def initialize(pars, fr = nil)
  @params = pars

  raise Exception.new("Missing :host in Importer parameters.") if @params[:host].nil?
  raise Exception.new("Missing :layer in Importer parameters.") if @params[:layer].nil?
  raise Exception.new("Missing :file_path in Importer parameters.") if @params[:file_path].nil?

  @api = CitySDK::API.new(@params[:host])
  if @params[:name]
    raise Exception.new("Missing :password in Importer parameters.") if @params[:password].nil?
    raise Exception.new("Failure to authenticate '#{@params[:name]}' with api.") if not @api.authenticate(@params[:name],@params[:password])
    @api.release
  end

  @params[:addresslayer] = 'bag.vbo' if @params[:addressleyer].nil?
  @params[:addressfield] = 'postcode_huisnummer' if @params[:addressfield].nil?

  @filereader = fr || FileReader.new(@params)
end

Instance Attribute Details

#apiObject (readonly)

Returns the value of attribute api.



5
6
7
# File 'lib/citysdk/importer.rb', line 5

def api
  @api
end

#filereaderObject (readonly)

Returns the value of attribute filereader.



5
6
7
# File 'lib/citysdk/importer.rb', line 5

def filereader
  @filereader
end

#paramsObject (readonly)

Returns the value of attribute params.



5
6
7
# File 'lib/citysdk/importer.rb', line 5

def params
  @params
end

Instance Method Details

#add_to_addressObject

Raises:



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
# File 'lib/citysdk/importer.rb', line 135

def add_to_address()
  failed = []
  if @params[:postcode] and @params[:housenumber]
    begin
      
      @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[:layer]
          data = filter_fields(row)
          yield({addto: qres[:results][0][:cdk_id], data: data}) if block_given?
          n = @api.put(url,{'data'=>data})
        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

#do_import(&block) ⇒ Object



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
# File 'lib/citysdk/importer.rb', line 70

def do_import(&block)
  result = {
    created: 0,
    not_added: 0
  }

  failed = nil

  # TODO: add possibility to add node to postal code
  # if @params[:hasaddress] == 'certain'
  #   failed = add_to_address(&block)
  #  end

  if failed == []
    result[:updated] += @filereader.content.length
    return result
  end

  if failed
    result[:updated] += (@filereader.content.length - failed.length)
  end

  objects = failed || @filereader.content
  count = objects.length

  begin
    

    if @params[:hasgeometry]
      begin
        objects.each do |record|

          node = {
            type: 'Feature',
            properties: record[:properties],
            geometry: record[:geometry],
          }

          node[:properties][:data] = filter_fields(record[:properties][:data])
          node[:crs] = { type: 'EPSG', properties: { code: @params[:srid]  } } if @params[:srid] != 4326

          node[:properties][:title] = node[:properties][:data][@params[:title]] if @params[:title]

          yield(node[:properties]) if block_given?

          @api.create_object(node) if node[:properties][:data] != nil
          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.message)
  ensure
    a = sign_out
    result[:created] += a[:created]
    result[:not_added] = count
  end
  return result
end

#filter_fields(h) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/citysdk/importer.rb', line 60

def filter_fields(h)
  data = {}
  h.each_key do |k|
    k = (k.to_sym rescue k) || k
    j = @params[:alternate_fields][k]
    data[j] = h[k] if @params[:fields].include?(k) and j
  end
  data
end

#set_parameterr(k, v) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/citysdk/importer.rb', line 32

def set_parameterr(k,v)
  begin
    @params[(k.to_sym rescue k) || k] = v
    return true
  rescue
  end
  nil
end

#sign_inObject



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/citysdk/importer.rb', line 41

def 
  begin
    sign_out if @signed_in
    @api.set_host(@params[:host])
    @api.set_layer(@params[:layer])
    @api.authenticate(@params[:name],@params[:password])
    @signed_in = true
  rescue => e
    @api.release
    raise e
  ensure
  end
end

#sign_outObject



55
56
57
58
# File 'lib/citysdk/importer.rb', line 55

def sign_out
  @signed_in = false
  return @api.release
end

#write(path) ⇒ Object



28
29
30
# File 'lib/citysdk/importer.rb', line 28

def write(path)
  return @filereader.write(path)
end