From f86e4d33113f462092fe23263721cccd599c3303 Mon Sep 17 00:00:00 2001 From: Ilya Zverev Date: Mon, 9 Sep 2013 23:48:40 +0100 Subject: [PATCH] Single changeset query --- .../controllers/changeset_api_controller.rb | 28 +++++++++++++++++++ rails/config/routes.rb | 4 +++ 2 files changed, 32 insertions(+) diff --git a/rails/app/controllers/changeset_api_controller.rb b/rails/app/controllers/changeset_api_controller.rb index a36c995..94ce4ff 100644 --- a/rails/app/controllers/changeset_api_controller.rb +++ b/rails/app/controllers/changeset_api_controller.rb @@ -47,6 +47,16 @@ def summary_tilerange render :json => @summary_list.as_json, :callback => params[:callback] end + def changeset_json + @changesets = find_changeset('json') + render :json => JSON[@changesets.map(&:generate_json)], :callback => params[:callback] + end + + def changeset_geojson + @changesets = find_changeset('geojson') + render :json => changesets_to_geojson(@changesets, @x, @y, @zoom), :callback => params[:callback] + end + private def find_changesets_by_tile(format) @x, @y, @zoom = get_xyz(params) @@ -97,6 +107,24 @@ def find_changesets_by_range(format) changesets end + def find_changeset(format) + @id = params[:id].to_i + changesets = ActiveRecord::Base.connection.select_all(" + SELECT cs.*, + #{format == 'geojson' ? '(SELECT array_agg(ST_AsGeoJSON(g)) FROM unnest(t.geom) AS g) AS geom_geojson,' : ''} + #{format == 'geojson' ? '(SELECT array_agg(ST_AsGeoJSON(g)) FROM unnest(t.prev_geom) AS g) AS prev_geom_geojson,' : ''} + (SELECT ST_Extent(g) FROM unnest(t.geom) AS g)::text AS bboxes, + cs.bbox::box2d::text AS total_bbox, + (SELECT array_agg(g) FROM unnest(t.changes) AS g) AS change_ids + FROM changeset_tiles t + INNER JOIN changesets cs ON (cs.id = t.changeset_id) + WHERE cs.id = #{@id} + GROUP BY t.geom, t.prev_geom, t.changes + ").collect {|row| Changeset.new(row)} + load_changes(changesets) + changesets + end + def generate_summary_tile @x, @y, @zoom = get_xyz(params) rows = ActiveRecord::Base.connection.select_all("WITH agg AS ( diff --git a/rails/config/routes.rb b/rails/config/routes.rb index 4b619ef..c4cb313 100644 --- a/rails/config/routes.rb +++ b/rails/config/routes.rb @@ -18,6 +18,10 @@ match 'api/0.1/summary/:zoom/:x/:y' => 'changeset_api#summary_tile', :constraints => @xyz_constrains match 'api/0.1/summary/:zoom/:x1/:y1/:x2/:y2' => 'changeset_api#summary_tilerange', :constraints => @range_constrains, :format => 'json' + # Single changeset operations + match 'api/0.1/changeset/:id.geojson' => 'changeset_api#changeset_geojson', :constraints => { :id => /\d+/ }, :format => 'json' + match 'api/0.1/changeset/:id.json' => 'changeset_api#changeset_json', :constraints => { :id => /\d+/ }, :format => 'json' + # Map API match 'api/0.1/kothic/:zoom/:x/:y.js' => 'map_api#kothic', :constraints => @xyz_constrains end