Module: MapfishCoreExtensions::ActiveRecord::Base::ClassMethods

Defined in:
lib/mapfish_core_extensions/active_record/base.rb

Instance Method Summary collapse

Instance Method Details

#find_by_mapfish_filter(params, args = {}) ⇒ Object

Rails 2 backwards compatible finder



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
# File 'lib/mapfish_core_extensions/active_record/base.rb', line 115

def find_by_mapfish_filter(params, args = {})
  filter = {}
  #Create geometry filter
  srid = geometry_column.srid
  pnt = nil
  pnt = Point.from_x_y(params['lon'].to_f, params['lat'].to_f, srid) if params['lon'] && params['lat']
  box = nil
  if pnt && params['tolerance']
    rad = params['tolerance'].to_f/2
    box = [[pnt.x+rad, pnt.y+rad], [pnt.x-rad, pnt.y-rad], srid]
  end
  if params['bbox']
    x1, y1, x2, y2 = params['bbox'].split(',').collect(&:to_f)
    box = [[x1, y1], [x2, y2], srid]
  end
  if box
    box3d = "'BOX3D(#{box[0].join(" ")},#{box[1].join(" ")})'::box3d"
    filter = "#{table_name}.#{connection.quote_column_name(geometry_column.name)} && SetSRID(#{box3d}, #{box[2] || DEFAULT_SRID} ) "
  end
  conditions = sanitize_sql_for_conditions(filter)

  #Add attribute filter
  attrfilter = []
  params.each do |key, value|
    next if value.nil? || !value.respond_to?(:empty?) || value.empty?
    field, op = key.split('__')
    case op
    when 'eq'
      attrfilter << ["#{field} = ?", value]
    when 'ne'
      attrfilter << ["#{field} <> ?", value]
    when 'lt'
      attrfilter << ["#{field} < ?", value]
    when 'lte'
      attrfilter << ["#{field} <= ?", value]
    when 'gt'
      attrfilter << ["#{field} > ?", value]
    when 'gte'
      attrfilter << ["#{field} >= ?", value]
    when 'ilike'
      #TODO: support wildcarded ILIKE: https://trac.mapfish.org/trac/mapfish/ticket/495
      attrfilter << ["#{field} ILIKE ?", value]
    end
  end
  unless attrfilter.empty?
    sql, sqlparams = attrfilter.transpose
    if conditions.nil?
      conditions = ''
    else
      conditions << " AND "
    end
    conditions << sanitize_sql_for_conditions([sql.join(' AND ')] + sqlparams)
  end

  #Create finder arguments
  args.merge!(:conditions => conditions)
  args.merge!({:limit => params['maxfeatures'] }) if params['maxfeatures']
  args.merge!({:limit => params['limit'] }) if params['limit']
  args.merge!(:offset => params['offset']) if params['offset']
  find(:all, args)
end

#geometry_columnObject



53
54
55
56
# File 'lib/mapfish_core_extensions/active_record/base.rb', line 53

def geometry_column
  #Returns first geometry column found. TODO: override by configuration (set_geometry_column)?
  @geometry_column ||= columns.detect {|col| col.respond_to?(:geometry_type) }
end

#mapfish_filter(params) ⇒ Object



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
# File 'lib/mapfish_core_extensions/active_record/base.rb', line 66

def mapfish_filter(params)
  filter = scoped

  #Create geometry filter
  srid = geometry_column.srid
  pnt = nil
  pnt = Point.from_x_y(params['lon'].to_f, params['lat'].to_f, srid) if params['lon'] && params['lat']
  box = nil
  if pnt && params['tolerance']
    rad = params['tolerance'].to_f/2
    box = [[pnt.x+rad, pnt.y+rad], [pnt.x-rad, pnt.y-rad], srid]
  end
  if params['bbox']
    x1, y1, x2, y2 = params['bbox'].split(',').collect(&:to_f)
    box = [[x1, y1], [x2, y2], srid]
  end
  if box
    box3d = "'BOX3D(#{box[0].join(" ")},#{box[1].join(" ")})'::box3d"
    filter = filter.where("#{table_name}.#{connection.quote_column_name(geometry_column.name)} && SetSRID(#{box3d}, #{box[2] || DEFAULT_SRID} ) ")
  end

  #Add attribute filter
  params.each do |key, value|
    next if value.nil? || !value.respond_to?(:empty?) || value.empty?
    field, op = key.split('__')
    col = "#{table_name}.#{connection.quote_column_name(field)}"
    case op
    when 'eq'
      filter = filter.where("#{col} = ?", value)
    when 'ne'
      filter = filter.where("#{col} <> ?", value)
    when 'lt'
      filter = filter.where("#{col} < ?", value)
    when 'lte'
      filter = filter.where("#{col} <= ?", value)
    when 'gt'
      filter = filter.where("#{col} > ?", value)
    when 'gte'
      filter = filter.where("#{col} >= ?", value)
    when 'ilike'
      #TODO: support wildcarded ILIKE: https://trac.mapfish.org/trac/mapfish/ticket/495
      filter = filter.where("#{col} ILIKE ?", value)
    end
  end

  filter
end

#params_from_geojson(geoson) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/mapfish_core_extensions/active_record/base.rb', line 58

def params_from_geojson(geoson)
  name = self.to_s.downcase #unless geoson.is_a?(Array)
  data = geoson.delete(:properties) || {}
  data[geometry_column.name] = Geometry.from_geojson(geoson.delete(:geometry), geometry_column.srid)
  geoson.delete(:type)
  {name => data, primary_key => geoson.delete(:id)}
end