various map API rails stuff
This commit is contained in:
parent
6b73747271
commit
2803612d9d
8 changed files with 53 additions and 25 deletions
|
@ -1,6 +1,7 @@
|
|||
class ApiController < ApplicationController
|
||||
|
||||
def map
|
||||
response.headers["Content-Type"] = 'application/xml'
|
||||
# Figure out the bbox
|
||||
bbox = params['bbox']
|
||||
unless bbox and bbox.count(',') == 3
|
||||
|
@ -19,11 +20,12 @@ class ApiController < ApplicationController
|
|||
nodes = Node.find(:all, :conditions => ['latitude > ? AND longitude > ? AND latitude < ? AND longitude < ? AND visible = 1', min_lat, min_lon, max_lat, max_lon])
|
||||
|
||||
node_ids = nodes.collect {|node| node.id }
|
||||
segments = Array.new
|
||||
if node_ids.length > 0
|
||||
node_ids_sql = "(#{node_ids.join(',')})"
|
||||
|
||||
# get the referenced segments
|
||||
segments = Segment.find_by_sql "select * from segments where node_a in #{node_ids_sql} or node_b in #{node_ids_sql}"
|
||||
|
||||
end
|
||||
# see if we have nay missing nodes
|
||||
segments_nodes = segments.collect {|segment| segment.node_a }
|
||||
segments_nodes += segments.collect {|segment| segment.node_b }
|
||||
|
@ -38,10 +40,21 @@ class ApiController < ApplicationController
|
|||
doc = XML::Document.new
|
||||
doc.encoding = 'UTF-8'
|
||||
root = XML::Node.new 'osm'
|
||||
root['version'] = '0.4'
|
||||
root['version'] = API_VERSION
|
||||
root['generator'] = 'OpenStreetMap server'
|
||||
doc.root = root
|
||||
|
||||
# get ways
|
||||
# find which ways are needed
|
||||
segment_ids = segments.collect {|segment| segment.id }
|
||||
ways = Array.new
|
||||
if segment_ids.length > 0
|
||||
way_segments = WaySegment.find_by_segment_id(segment_ids)
|
||||
way_ids = way_segments.collect {|way_segment| way_segment.id }
|
||||
|
||||
ways = Way.find(segment_ids)
|
||||
end
|
||||
|
||||
nodes.each do |node|
|
||||
root << node.to_xml_node()
|
||||
end
|
||||
|
@ -50,6 +63,10 @@ class ApiController < ApplicationController
|
|||
root << segment.to_xml_node()
|
||||
end
|
||||
|
||||
ways.each do |way|
|
||||
root << way.to_xml_node()
|
||||
end
|
||||
|
||||
render :text => doc.to_s
|
||||
|
||||
end
|
||||
|
|
|
@ -4,6 +4,7 @@ class NodeController < ApplicationController
|
|||
before_filter :authorize
|
||||
|
||||
def create
|
||||
response.headers["Content-Type"] = 'application/xml'
|
||||
if request.put?
|
||||
node = nil
|
||||
begin
|
||||
|
@ -33,6 +34,7 @@ class NodeController < ApplicationController
|
|||
end
|
||||
|
||||
def rest
|
||||
response.headers["Content-Type"] = 'application/xml'
|
||||
unless Node.exists?(params[:id])
|
||||
render :nothing => true, :status => 404
|
||||
return
|
||||
|
@ -86,6 +88,7 @@ class NodeController < ApplicationController
|
|||
end
|
||||
|
||||
def history
|
||||
response.headers["Content-Type"] = 'application/xml'
|
||||
node = Node.find(params[:id])
|
||||
|
||||
unless node
|
||||
|
|
|
@ -4,6 +4,7 @@ class SegmentController < ApplicationController
|
|||
before_filter :authorize
|
||||
|
||||
def create
|
||||
response.headers["Content-Type"] = 'application/xml'
|
||||
if request.put?
|
||||
segment = Segment.from_xml(request.raw_post, true)
|
||||
|
||||
|
@ -35,6 +36,7 @@ class SegmentController < ApplicationController
|
|||
end
|
||||
|
||||
def rest
|
||||
response.headers["Content-Type"] = 'application/xml'
|
||||
unless Segment.exists?(params[:id])
|
||||
render :nothing => true, :status => 404
|
||||
return
|
||||
|
@ -79,6 +81,7 @@ class SegmentController < ApplicationController
|
|||
end
|
||||
|
||||
def history
|
||||
response.headers["Content-Type"] = 'application/xml'
|
||||
segment = Segment.find(params[:id])
|
||||
|
||||
unless segment
|
||||
|
|
|
@ -75,7 +75,7 @@ class Node < ActiveRecord::Base
|
|||
doc = XML::Document.new
|
||||
doc.encoding = 'UTF-8'
|
||||
root = XML::Node.new 'osm'
|
||||
root['version'] = '0.4'
|
||||
root['version'] = API_VERSION
|
||||
root['generator'] = 'OpenStreetMap server'
|
||||
doc.root = root
|
||||
root << to_xml_node()
|
||||
|
|
|
@ -68,7 +68,7 @@ class Segment < ActiveRecord::Base
|
|||
doc = XML::Document.new
|
||||
doc.encoding = 'UTF-8'
|
||||
root = XML::Node.new 'osm'
|
||||
root['version'] = '0.4'
|
||||
root['version'] = API_VERSION
|
||||
root['generator'] = 'OpenStreetMap server'
|
||||
doc.root = root
|
||||
root << to_xml_node()
|
||||
|
|
|
@ -51,6 +51,11 @@ class Way < ActiveRecord::Base
|
|||
|
||||
doc.root = root
|
||||
|
||||
root << to_xml_node()
|
||||
return doc
|
||||
end
|
||||
|
||||
def to_xml_node
|
||||
el1 = XML::Node.new 'way'
|
||||
el1['id'] = self.id.to_s
|
||||
el1['visible'] = self.visible.to_s
|
||||
|
@ -68,11 +73,10 @@ class Way < ActiveRecord::Base
|
|||
e['v'] = tag.v
|
||||
el1 << e
|
||||
end
|
||||
|
||||
root << el1
|
||||
return doc
|
||||
return el1
|
||||
end
|
||||
|
||||
|
||||
def segs
|
||||
@segs = Array.new unless @segs
|
||||
@segs
|
||||
|
|
|
@ -52,6 +52,7 @@ end
|
|||
|
||||
# Include your application configuration below
|
||||
|
||||
API_VERSION = '0.4'
|
||||
|
||||
ActionMailer::Base.server_settings = {
|
||||
:address => "localhost",
|
||||
|
|
|
@ -1,20 +1,20 @@
|
|||
ActionController::Routing::Routes.draw do |map|
|
||||
|
||||
# API
|
||||
API_VERSION = '0.4' # change this in envronment.rb too
|
||||
map.connect "api/#{API_VERSION}/node/create", :controller => 'node', :action => 'create'
|
||||
map.connect "api/#{API_VERSION}/node/:id/history", :controller => 'node', :action => 'history', :id => nil
|
||||
map.connect "api/#{API_VERSION}/node/:id", :controller => 'node', :action => 'rest', :id => nil
|
||||
|
||||
map.connect 'api/0.4/node/create', :controller => 'node', :action => 'create'
|
||||
map.connect 'api/0.4/node/:id/history', :controller => 'node', :action => 'history', :id => nil
|
||||
map.connect 'api/0.4/node/:id', :controller => 'node', :action => 'rest', :id => nil
|
||||
map.connect "api/#{API_VERSION}/segment/create", :controller => 'segment', :action => 'create'
|
||||
map.connect "api/#{API_VERSION}/segment/:id/history", :controller => 'segment', :action => 'history'
|
||||
map.connect "api/#{API_VERSION}/segment/:id", :controller => 'segment', :action => 'rest'
|
||||
|
||||
map.connect 'api/0.4/segment/create', :controller => 'segment', :action => 'create'
|
||||
map.connect 'api/0.4/segment/:id/history', :controller => 'segment', :action => 'history'
|
||||
map.connect 'api/0.4/segment/:id', :controller => 'segment', :action => 'rest'
|
||||
map.connect "api/#{API_VERSION}/way/create", :controller => 'way', :action => 'create'
|
||||
map.connect "api/#{API_VERSION}/way/:id/history", :controller => 'way', :action => 'history'
|
||||
map.connect "api/#{API_VERSION}/way/:id", :controller => 'way', :action => 'rest'
|
||||
|
||||
map.connect 'api/0.4/way/create', :controller => 'way', :action => 'create'
|
||||
map.connect 'api/0.4/way/:id/history', :controller => 'way', :action => 'history'
|
||||
map.connect 'api/0.4/way/:id', :controller => 'way', :action => 'rest'
|
||||
|
||||
map.connect 'api/0.4/map', :controller => 'api', :action => 'map'
|
||||
map.connect "api/#{API_VERSION}/map", :controller => 'api', :action => 'map'
|
||||
|
||||
# web site
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue