# editions Systeme D / Richard Fairhurst 2004-2008
#
# All in/out parameters are floats unless explicitly stated.
- # Note that in getway/getway_old, SWF object name and way id are
- #ĂŠidentical and one could probably be eliminated.
#
# to trap errors (getway_old,putway,putpoi,deleteway only):
# return(-1,"message") <-- just puts up a dialogue
when 'getway_old'; results[index]=putdata(index,getway_old(args))
when 'getway_history'; results[index]=putdata(index,getway_history(args))
when 'putway'; r=putway(args,renumberednodes)
- renumberednodes=r[3]
- results[index]=putdata(index,r)
+ renumberednodes=r[3]
+ results[index]=putdata(index,r)
when 'deleteway'; results[index]=putdata(index,deleteway(args))
when 'putpoi'; results[index]=putdata(index,putpoi(args))
when 'getpoi'; results[index]=putdata(index,getpoi(args))
private
+ # Return presets (default tags and crap) to potlatch
+ # Global is set up in config/environment.rb on startup, code is in lib/osm.rb
+ def getpresets
+ return POTLATCH_PRESETS
+ end
# ====================================================================
# Remote calls
- # ----- getpresets
- # in: none
- # does: reads tag preset menus, colours, and autocomplete config files
- # out: [0] presets, [1] presetmenus, [2] presetnames,
- # [3] colours, [4] casing, [5] areas, [6] autotags
- # (all hashes)
-
- def getpresets
- RAILS_DEFAULT_LOGGER.info(" Message: getpresets")
-
- # Read preset menus
- presets={}
- presetmenus={}; presetmenus['point']=[]; presetmenus['way']=[]; presetmenus['POI']=[]
- presetnames={}; presetnames['point']={}; presetnames['way']={}; presetnames['POI']={}
- presettype=''
- presetcategory=''
- # StringIO.open(txt) do |file|
- File.open("#{RAILS_ROOT}/config/potlatch/presets.txt") do |file|
- file.each_line {|line|
- t=line.chomp
- if (t=~/(\w+)\/(\w+)/) then
- presettype=$1
- presetcategory=$2
- presetmenus[presettype].push(presetcategory)
- presetnames[presettype][presetcategory]=["(no preset)"]
- elsif (t=~/^(.+):\s?(.+)$/) then
- pre=$1; kv=$2
- presetnames[presettype][presetcategory].push(pre)
- presets[pre]={}
- kv.split(',').each {|a|
- if (a=~/^(.+)=(.*)$/) then presets[pre][$1]=$2 end
- }
- end
- }
- end
-
- # Read colours/styling
- colours={}; casing={}; areas={}
- File.open("#{RAILS_ROOT}/config/potlatch/colours.txt") do |file|
- file.each_line {|line|
- t=line.chomp
- if (t=~/(\w+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)/) then
- tag=$1
- if ($2!='-') then colours[tag]=$2.hex end
- if ($3!='-') then casing[tag]=$3.hex end
- if ($4!='-') then areas[tag]=$4.hex end
- end
- }
- end
-
- # Read auto-complete
- autotags={}; autotags['point']={}; autotags['way']={}; autotags['POI']={};
- File.open("#{RAILS_ROOT}/config/potlatch/autocomplete.txt") do |file|
- file.each_line {|line|
- t=line.chomp
- if (t=~/^(\w+)\/(\w+)\s+(.+)$/) then
- tag=$1; type=$2; values=$3
- if values=='-' then autotags[type][tag]=[]
- else autotags[type][tag]=values.split(',').sort.reverse end
- end
- }
- end
- [presets,presetmenus,presetnames,colours,casing,areas,autotags]
- end
# ----- whichways
- # return array of ways in current bounding box
-
- # in: [0] xmin, [1] ymin, [2] xmax, [3] ymax (bbox in degrees)
- # [4] baselong (longitude of SWF map origin),
- # [5] basey (projected latitude of SWF map origin),
- # [6] masterscale (SWF map scale)
- # does: finds all ways and POI nodes in bounding box
- # at present, instead of using correct (=more complex) SQL to find
- # corner-crossing ways, it simply enlarges the bounding box
- # out: [0] array of way ids,
- # [1] array of POIs
- # (where each POI is an array containing:
- # [0] id, [1] projected long, [2] projected lat, [3] hash of tags)
+
+ # Find all the way ids and nodes (including tags and projected lat/lng) which aren't part of those ways in an are
+ #
+ # The argument is an array containing the following, in order:
+ # 0. minimum longitude
+ # 1. minimum latitude
+ # 2. maximum longitude
+ # 3. maximum latitude
+ # 4. baselong, 5. basey, 6. masterscale as above
def whichways(args)
xmin = args[0].to_f-0.01
RAILS_DEFAULT_LOGGER.info(" Message: whichways, bbox=#{xmin},#{ymin},#{xmax},#{ymax}")
- waylist = ActiveRecord::Base.connection.select_all("SELECT DISTINCT current_way_nodes.id AS wayid"+
- " FROM current_way_nodes,current_nodes,current_ways "+
- " WHERE current_nodes.id=current_way_nodes.node_id "+
- " AND current_nodes.visible=1 "+
- " AND current_ways.id=current_way_nodes.id "+
- " AND current_ways.visible=1 "+
- " AND "+OSM.sql_for_area(ymin, xmin, ymax, xmax, "current_nodes."))
-
- ways = waylist.collect {|a| a['wayid'].to_i } # get an array of way IDs
+ # find the way ids in an area
+ nodes_in_area = Node.find_by_area(ymin, xmin, ymax, xmax,:conditions => "visible = 1", :include => :way_nodes)
+ waynodes_in_area = nodes_in_area.collect {|node| node.way_nodes }.flatten
+ ways = waynodes_in_area.collect {|way_node| way_node.id[0]}.uniq
- pointlist = ActiveRecord::Base.connection.select_all("SELECT current_nodes.id,current_nodes.latitude*0.0000001 AS lat,current_nodes.longitude*0.0000001 AS lng,current_nodes.tags "+
- " FROM current_nodes "+
- " LEFT OUTER JOIN current_way_nodes cwn ON cwn.node_id=current_nodes.id "+
- " WHERE "+OSM.sql_for_area(ymin, xmin, ymax, xmax, "current_nodes.")+
- " AND cwn.id IS NULL "+
- " AND current_nodes.visible=1")
-
- points = pointlist.collect {|a| [a['id'],long2coord(a['lng'].to_f,baselong,masterscale),lat2coord(a['lat'].to_f,basey,masterscale),tag2array(a['tags'])] } # get a list of node ids and their tags
+ # find the node ids in an area that aren't part of ways
+ node_ids_in_area = nodes_in_area.collect {|node| node.id}.uniq
+ node_ids_used_in_ways = waynodes_in_area.collect {|way_node| way_node.node_id}.uniq
+ node_ids_not_used_in_area = node_ids_in_area - node_ids_used_in_ways
+ nodes_not_used_in_area = Node.find(node_ids_not_used_in_area)
+ points = nodes_not_used_in_area.collect {|n| [n.id, n.lon_potlatch(baselong,masterscale), n.lat_potlatch(basey,masterscale), n.tags_as_hash] }
[ways,points]
end
# ----- whichways_deleted
# return array of deleted ways in current bounding box
-
# in: as whichways
# does: finds all deleted ways with a deleted node in bounding box
# out: [0] array of way ids
-
+
def whichways_deleted(args)
xmin = args[0].to_f-0.01
ymin = args[1].to_f-0.01
basey = args[5]
masterscale = args[6]
+
+
sql=<<-EOF
SELECT DISTINCT current_ways.id
FROM current_nodes,way_nodes,current_ways
[ways]
end
+
# ----- getway
- # in: [0] SWF object name,
- # [1] way id, [2] baselong, [3] basey, [4] masterscale
- # does: gets way and all nodes
- # out: [0] SWF object name (unchanged),
- # [1] array of points
- # (where each point is an array containing
- # [0] projected long, [1] projected lat, [2] node id,
- # [3] null, [4] hash of node tags),
- # [2] xmin, [3] xmax, [4] ymin, [5] ymax (unprojected bbox)
+
+ # Get a way with all of it's nodes and tags
+ # The input is an array with the following components, in order:
+ # 0. wayid - the ID of the way to get
+ # 1. baselong - origin of SWF map (longitude)
+ # 2. basey - origin of SWF map (latitude)
+ # 3. masterscale - SWF map scale
+ #
+ # The output is an array which contains all the nodes (with projected
+ # latitude and longitude) and tags for a way (and all the nodes tags).
+ # It also has the way's unprojected (WGS84) bbox.
+ #
+ # FIXME: The server really shouldn't be figuring out a ways bounding box and doing projection for potlatch
+ # FIXME: the argument splitting should be done in the 'talk' method, not here
def getway(args)
- objname,wayid,baselong,basey,masterscale=args
+ wayid,baselong,basey,masterscale = args
wayid = wayid.to_i
- points = []
- xmin = ymin = 999999
- xmax = ymax = -999999
RAILS_DEFAULT_LOGGER.info(" Message: getway, id=#{wayid}")
- readwayquery(wayid,true).each {|row|
- points<<[long2coord(row['longitude'].to_f,baselong,masterscale),lat2coord(row['latitude'].to_f,basey,masterscale),row['id'].to_i,nil,tag2array(row['tags'])]
- xmin = [xmin,row['longitude'].to_f].min
- xmax = [xmax,row['longitude'].to_f].max
- ymin = [ymin,row['latitude'].to_f].min
- ymax = [ymax,row['latitude'].to_f].max
- }
+ way = Way.find_eager(wayid)
+ long_array = []
+ lat_array = []
+ points = []
- attributes={}
- attrlist=ActiveRecord::Base.connection.select_all "SELECT k,v FROM current_way_tags WHERE id=#{wayid}"
- attrlist.each {|a| attributes[a['k'].gsub(':','|')]=a['v'] }
+ way.way_nodes.each do |way_node|
+ node = way_node.node # get the node record
+ projected_longitude = node.lon_potlatch(baselong,masterscale) # do projection for potlatch
+ projected_latitude = node.lat_potlatch(basey,masterscale)
+ id = node.id
+ tags_hash = node.tags_as_hash
+
+ points << [projected_longitude, projected_latitude, id, nil, tags_hash]
+ long_array << projected_longitude
+ lat_array << projected_latitude
+ end
- [objname,points,attributes,xmin,xmax,ymin,ymax]
+ [wayid,points,way.tags,long_array.min,long_array.max,lat_array.min,lat_array.max]
end
# ----- getway_old
# returns old version of way
- # in: [0] SWF object name, [1] way id,
- # [2] way version to get (or -1 for "last deleted version")
- # [3] baselong, [4] basey, [5] masterscale
+ # in: [0] way id,
+ # [1] way version to get (or -1 for "last deleted version")
+ # [2] baselong, [3] basey, [4] masterscale
# does: gets old version of way and all constituent nodes
# for undelete, always uses the most recent version of each node
# (even if it's moved)
RAILS_DEFAULT_LOGGER.info(" Message: getway_old (server is #{SERVER_URL})")
# if SERVER_URL=="www.openstreetmap.org" then return -1,"Revert is not currently enabled on the OpenStreetMap server." end
- objname,wayid,version,baselong,basey,masterscale=args
+ wayid,version,baselong,basey,masterscale=args
wayid = wayid.to_i
version = version.to_i
xmin = ymin = 999999
attrlist.each {|a| attributes[a['k'].gsub(':','|')]=a['v'] }
attributes['history']="Retrieved from v"+version.to_s
- [0,objname,points,attributes,xmin,xmax,ymin,ymax,version]
+ [0,wayid,points,attributes,xmin,xmax,ymin,ymax,version]
end
# ----- getway_history
ActiveRecord::Base.connection.execute("DELETE FROM current_relation_members WHERE id=#{relation} AND member_type='#{type}' AND member_id=#{objid}")
end
-
def sqlescape(a)
a.gsub(/[\000-\037]/,"").gsub("'","''").gsub(92.chr) {92.chr+92.chr}
end