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
|
class ApiController < ApplicationController
|
||||||
|
|
||||||
def map
|
def map
|
||||||
|
response.headers["Content-Type"] = 'application/xml'
|
||||||
# Figure out the bbox
|
# Figure out the bbox
|
||||||
bbox = params['bbox']
|
bbox = params['bbox']
|
||||||
unless bbox and bbox.count(',') == 3
|
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])
|
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 }
|
node_ids = nodes.collect {|node| node.id }
|
||||||
node_ids_sql = "(#{node_ids.join(',')})"
|
segments = Array.new
|
||||||
|
if node_ids.length > 0
|
||||||
# get the referenced segments
|
node_ids_sql = "(#{node_ids.join(',')})"
|
||||||
segments = Segment.find_by_sql "select * from segments where node_a in #{node_ids_sql} or node_b in #{node_ids_sql}"
|
# 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
|
# see if we have nay missing nodes
|
||||||
segments_nodes = segments.collect {|segment| segment.node_a }
|
segments_nodes = segments.collect {|segment| segment.node_a }
|
||||||
segments_nodes += segments.collect {|segment| segment.node_b }
|
segments_nodes += segments.collect {|segment| segment.node_b }
|
||||||
|
@ -38,10 +40,21 @@ class ApiController < ApplicationController
|
||||||
doc = XML::Document.new
|
doc = XML::Document.new
|
||||||
doc.encoding = 'UTF-8'
|
doc.encoding = 'UTF-8'
|
||||||
root = XML::Node.new 'osm'
|
root = XML::Node.new 'osm'
|
||||||
root['version'] = '0.4'
|
root['version'] = API_VERSION
|
||||||
root['generator'] = 'OpenStreetMap server'
|
root['generator'] = 'OpenStreetMap server'
|
||||||
doc.root = root
|
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|
|
nodes.each do |node|
|
||||||
root << node.to_xml_node()
|
root << node.to_xml_node()
|
||||||
end
|
end
|
||||||
|
@ -50,6 +63,10 @@ class ApiController < ApplicationController
|
||||||
root << segment.to_xml_node()
|
root << segment.to_xml_node()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
ways.each do |way|
|
||||||
|
root << way.to_xml_node()
|
||||||
|
end
|
||||||
|
|
||||||
render :text => doc.to_s
|
render :text => doc.to_s
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,6 +4,7 @@ class NodeController < ApplicationController
|
||||||
before_filter :authorize
|
before_filter :authorize
|
||||||
|
|
||||||
def create
|
def create
|
||||||
|
response.headers["Content-Type"] = 'application/xml'
|
||||||
if request.put?
|
if request.put?
|
||||||
node = nil
|
node = nil
|
||||||
begin
|
begin
|
||||||
|
@ -33,6 +34,7 @@ class NodeController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def rest
|
def rest
|
||||||
|
response.headers["Content-Type"] = 'application/xml'
|
||||||
unless Node.exists?(params[:id])
|
unless Node.exists?(params[:id])
|
||||||
render :nothing => true, :status => 404
|
render :nothing => true, :status => 404
|
||||||
return
|
return
|
||||||
|
@ -86,6 +88,7 @@ class NodeController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def history
|
def history
|
||||||
|
response.headers["Content-Type"] = 'application/xml'
|
||||||
node = Node.find(params[:id])
|
node = Node.find(params[:id])
|
||||||
|
|
||||||
unless node
|
unless node
|
||||||
|
|
|
@ -4,6 +4,7 @@ class SegmentController < ApplicationController
|
||||||
before_filter :authorize
|
before_filter :authorize
|
||||||
|
|
||||||
def create
|
def create
|
||||||
|
response.headers["Content-Type"] = 'application/xml'
|
||||||
if request.put?
|
if request.put?
|
||||||
segment = Segment.from_xml(request.raw_post, true)
|
segment = Segment.from_xml(request.raw_post, true)
|
||||||
|
|
||||||
|
@ -35,6 +36,7 @@ class SegmentController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def rest
|
def rest
|
||||||
|
response.headers["Content-Type"] = 'application/xml'
|
||||||
unless Segment.exists?(params[:id])
|
unless Segment.exists?(params[:id])
|
||||||
render :nothing => true, :status => 404
|
render :nothing => true, :status => 404
|
||||||
return
|
return
|
||||||
|
@ -79,6 +81,7 @@ class SegmentController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def history
|
def history
|
||||||
|
response.headers["Content-Type"] = 'application/xml'
|
||||||
segment = Segment.find(params[:id])
|
segment = Segment.find(params[:id])
|
||||||
|
|
||||||
unless segment
|
unless segment
|
||||||
|
|
|
@ -75,7 +75,7 @@ class Node < ActiveRecord::Base
|
||||||
doc = XML::Document.new
|
doc = XML::Document.new
|
||||||
doc.encoding = 'UTF-8'
|
doc.encoding = 'UTF-8'
|
||||||
root = XML::Node.new 'osm'
|
root = XML::Node.new 'osm'
|
||||||
root['version'] = '0.4'
|
root['version'] = API_VERSION
|
||||||
root['generator'] = 'OpenStreetMap server'
|
root['generator'] = 'OpenStreetMap server'
|
||||||
doc.root = root
|
doc.root = root
|
||||||
root << to_xml_node()
|
root << to_xml_node()
|
||||||
|
|
|
@ -68,7 +68,7 @@ class Segment < ActiveRecord::Base
|
||||||
doc = XML::Document.new
|
doc = XML::Document.new
|
||||||
doc.encoding = 'UTF-8'
|
doc.encoding = 'UTF-8'
|
||||||
root = XML::Node.new 'osm'
|
root = XML::Node.new 'osm'
|
||||||
root['version'] = '0.4'
|
root['version'] = API_VERSION
|
||||||
root['generator'] = 'OpenStreetMap server'
|
root['generator'] = 'OpenStreetMap server'
|
||||||
doc.root = root
|
doc.root = root
|
||||||
root << to_xml_node()
|
root << to_xml_node()
|
||||||
|
|
|
@ -50,7 +50,12 @@ class Way < ActiveRecord::Base
|
||||||
root['generator'] = 'OpenStreetMap server'
|
root['generator'] = 'OpenStreetMap server'
|
||||||
|
|
||||||
doc.root = root
|
doc.root = root
|
||||||
|
|
||||||
|
root << to_xml_node()
|
||||||
|
return doc
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_xml_node
|
||||||
el1 = XML::Node.new 'way'
|
el1 = XML::Node.new 'way'
|
||||||
el1['id'] = self.id.to_s
|
el1['id'] = self.id.to_s
|
||||||
el1['visible'] = self.visible.to_s
|
el1['visible'] = self.visible.to_s
|
||||||
|
@ -68,10 +73,9 @@ class Way < ActiveRecord::Base
|
||||||
e['v'] = tag.v
|
e['v'] = tag.v
|
||||||
el1 << e
|
el1 << e
|
||||||
end
|
end
|
||||||
|
return el1
|
||||||
root << el1
|
end
|
||||||
return doc
|
|
||||||
end
|
|
||||||
|
|
||||||
def segs
|
def segs
|
||||||
@segs = Array.new unless @segs
|
@segs = Array.new unless @segs
|
||||||
|
|
|
@ -52,6 +52,7 @@ end
|
||||||
|
|
||||||
# Include your application configuration below
|
# Include your application configuration below
|
||||||
|
|
||||||
|
API_VERSION = '0.4'
|
||||||
|
|
||||||
ActionMailer::Base.server_settings = {
|
ActionMailer::Base.server_settings = {
|
||||||
:address => "localhost",
|
:address => "localhost",
|
||||||
|
|
|
@ -1,20 +1,20 @@
|
||||||
ActionController::Routing::Routes.draw do |map|
|
ActionController::Routing::Routes.draw do |map|
|
||||||
|
|
||||||
# API
|
# 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/#{API_VERSION}/segment/create", :controller => 'segment', :action => 'create'
|
||||||
map.connect 'api/0.4/node/:id/history', :controller => 'node', :action => 'history', :id => nil
|
map.connect "api/#{API_VERSION}/segment/:id/history", :controller => 'segment', :action => 'history'
|
||||||
map.connect 'api/0.4/node/:id', :controller => 'node', :action => 'rest', :id => nil
|
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/#{API_VERSION}/way/create", :controller => 'way', :action => 'create'
|
||||||
map.connect 'api/0.4/segment/:id/history', :controller => 'segment', :action => 'history'
|
map.connect "api/#{API_VERSION}/way/:id/history", :controller => 'way', :action => 'history'
|
||||||
map.connect 'api/0.4/segment/:id', :controller => 'segment', :action => 'rest'
|
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/#{API_VERSION}/map", :controller => 'api', :action => 'map'
|
||||||
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'
|
|
||||||
|
|
||||||
# web site
|
# web site
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue