Class: GeoController

Inherits:
ApplicationController show all
Defined in:
app/controllers/geo_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



32
33
34
35
36
37
38
# File 'app/controllers/geo_controller.rb', line 32

def create
  if params[:editv2]
    create_v2
  else
    create_v1
  end
end

#create_v1Object



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
# File 'app/controllers/geo_controller.rb', line 180

def create_v1
  @features = []
  feature_collection = geo_model.geojson_decode(request.raw_post)
  if feature_collection.nil? || !geo_model.can_edit?(current_ability)
    head :bad_request
    return
  end

  feature_collection.each do |feature|
    if feature.feature_id.is_a? Integer
      new_feature = geo_model.find(feature.feature_id)
    end
    if new_feature.nil?
      new_feature = geo_model.new
    end

    logger.info "#{geo_model.table_name}.update_attributes_from_feature: #{feature.inspect}"
    if new_feature.update_attributes_from_geojson_feature(feature, current_user)
      # transform geometry to SRID from GeoJSON feature
      client_srid = feature.geometry.nil? ? geo_model.default_client_srid : feature.geometry.srid
      new_feature = geo_model.json_filter.select_geojson_geom(client_srid).find(new_feature.id)

      @features << new_feature
    else
      head :unprocessable_entity
      return
    end
  end

  render :json => @features.to_geojson, :status => :created
end

#create_v2Object



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
# File 'app/controllers/geo_controller.rb', line 56

def create_v2
  unless geo_model.can_edit?(current_ability)
    render :json => {
      :error => "Forbidden"
    }, :status => :forbidden
    return
  end

  geojson_data = request.raw_post
  begin
    feature_collection = geo_model.geojson_decode(geojson_data)
  rescue => err
    # JSON parse error
    render :json => {
      :error => "Invalid JSON: #{err.message}"
    }, :status => :bad_request
    return
  end
  error = geo_model.validate_feature_collection(feature_collection, geojson_data)
  unless error.nil?
    render :json => error, :status => :bad_request
    return
  end

  @features = []
  feature_collection.each do |feature|
    if feature.feature_id.is_a? Integer
      begin
        new_feature = geo_model.find(feature.feature_id)
      rescue ActiveRecord::RecordNotFound => err
        render :json => {
          :error => "Feature ID not found"
        }, :status => :not_found
        return
      end
    end
    if new_feature.nil?
      new_feature = geo_model.new
    end

    logger.info "#{geo_model.table_name}.update_attributes_from_feature: #{feature.inspect}"
    if new_feature.update_attributes_from_geojson_feature(feature, current_user)
      # transform geometry to SRID from GeoJSON feature
      client_srid = feature.geometry.nil? ? geo_model.default_client_srid : feature.geometry.srid
      new_feature = geo_model.json_filter.select_geojson_geom(client_srid).find(new_feature.id)

      @features << new_feature
    else
      render :json => {
        :error => "Feature validation failed",
        :validation_errors => new_feature.errors.full_messages
      }, :status => :unprocessable_entity
      return
    end
  end

  render :json => @features.to_geojson, :status => :created
end

#destroyObject



48
49
50
51
52
53
54
# File 'app/controllers/geo_controller.rb', line 48

def destroy
  if params[:editv2]
    destroy_v2
  else
    destroy_v1
  end
end

#destroy_v1Object



236
237
238
239
240
# File 'app/controllers/geo_controller.rb', line 236

def destroy_v1
  @feature = geo_model.user_filter(current_ability).find(params[:id])
  @feature.destroy
  head :no_content
end

#destroy_v2Object



165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'app/controllers/geo_controller.rb', line 165

def destroy_v2
  begin
    @feature = geo_model.user_filter(current_ability).find(params[:id])
  rescue ActiveRecord::RecordNotFound => err
    render :json => {
      :error => "Feature ID not found"
    }, :status => :not_found
    return
  end
  @feature.destroy
  render :json => {
    :message => "Feature removed"
  }
end

#geo_modelObject



5
6
7
# File 'app/controllers/geo_controller.rb', line 5

def geo_model
  throw NotImplementedError
end

#indexObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/geo_controller.rb', line 9

def index
  client_srid = params[:srid].blank? ? geo_model.default_client_srid : params[:srid].to_i

  @features = geo_model.bbox_filter(params)
  @features = @features.user_filter(current_ability)
  respond_to do |format|
    # NOTE: return GeoJSON by default (OpenLayers.Protocol.HTTP PUT does not work with '.json' format selection)
    format.html {
      render :json => @features.json_filter.select_geojson_geom(client_srid).to_geojson
    }
    format.csv {
      send_csv_excel(@features.csv_filter)
    }
  end
end

#showObject



25
26
27
28
29
30
# File 'app/controllers/geo_controller.rb', line 25

def show
  client_srid = params[:srid].blank? ? geo_model.default_client_srid : params[:srid].to_i

  @feature = geo_model.user_filter(current_ability).json_filter.select_geojson_geom(client_srid).find(params[:id])
  render :json => [@feature].to_geojson
end

#updateObject



40
41
42
43
44
45
46
# File 'app/controllers/geo_controller.rb', line 40

def update
  if params[:editv2]
    update_v2
  else
    update_v1
  end
end

#update_v1Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'app/controllers/geo_controller.rb', line 212

def update_v1
  # NOTE: user_filter checks if model is editable
  feature = geo_model.user_filter(current_ability).geojson_decode(request.raw_post)
  if feature.nil?
    head :bad_request
    return
  end

  if feature.feature_id.is_a? Integer
    # NOTE: user_filter may limit editable features; find raises RecordNotFound if feature cannot be found
    @feature = geo_model.user_filter(current_ability).find(feature.feature_id)
  end

  if @feature.update_attributes_from_geojson_feature(feature, current_user)
    # transform geometry to SRID from GeoJSON feature
    client_srid = feature.geometry.nil? ? geo_model.default_client_srid : feature.geometry.srid
    @feature = geo_model.json_filter.select_geojson_geom(client_srid).find(@feature.id)

    render :json => @feature.to_geojson, :status => :created
  else
    head :unprocessable_entity
  end
end

#update_v2Object



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
# File 'app/controllers/geo_controller.rb', line 115

def update_v2
  unless geo_model.can_edit?(current_ability)
    render :json => {
      :error => "Forbidden"
    }, :status => :forbidden
    return
  end

  geojson_data = request.raw_post
  begin
    feature = geo_model.geojson_decode(geojson_data)
  rescue => err
    # JSON parse error
    render :json => {
      :error => "Invalid JSON: #{err.message}"
    }, :status => :bad_request
    return
  end
  error = geo_model.validate_feature(feature, geojson_data)
  unless error.nil?
    render :json => error, :status => :bad_request
    return
  end

  if feature.feature_id.is_a? Integer
    begin
      # NOTE: user_filter may limit editable features; find raises RecordNotFound if feature cannot be found
      @feature = geo_model.user_filter(current_ability).find(feature.feature_id)
    rescue ActiveRecord::RecordNotFound => err
      render :json => {
        :error => "Feature ID not found"
      }, :status => :not_found
      return
    end
  end

  if @feature.update_attributes_from_geojson_feature(feature, current_user)
    # transform geometry to SRID from GeoJSON feature
    client_srid = feature.geometry.nil? ? geo_model.default_client_srid : feature.geometry.srid
    @feature = geo_model.json_filter.select_geojson_geom(client_srid).find(@feature.id)

    render :json => @feature.to_geojson, :status => :created
  else
    render :json => {
      :error => "Feature validation failed",
      :validation_errors => @feature.errors.full_messages
    }, :status => :unprocessable_entity
  end
end