Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single changeset query #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions rails/app/controllers/changeset_api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 (
Expand Down
4 changes: 4 additions & 0 deletions rails/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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