readers/tfl_common.rb
# Find junctions between a set of ways
Find junctions between a set of ways
Find junctions between a set of ways
def find_junctions(lat,lon,way_ids)
		return [] if way_ids.empty?
		node_count    = {}
		node_highways = {}
		node_ways     = {}
		sql = "SELECT id,nodes,tags FROM planet_osm_ways WHERE id IN (#{way_ids.join(',')})"
		$conn.exec(sql).each do |res|
			tag_list = parse_pg_array(res['tags'] || '{}')
			highway  = Hash[*tag_list]['highway']
			nids     = parse_pg_array(res['nodes']).map(&:to_i)
			nids.each_with_index do |nid,i|
				end_point = (i==0 || i==nids.length-1)
				node_count[nid] = (node_count[nid] || 0) + (end_point ? 1 : 2)
				node_highways[nid] = {} unless node_highways[nid]
				node_highways[nid][highway] = true
				node_ways[nid] = {} unless node_ways[nid]
				node_ways[nid][res['id'].to_i] = true
			end
		end
		junction_nids = node_count.keys.select { |nid| node_count[nid]>2 }
		return [] if junction_nids.empty?
		sql = "SELECT id,lat,lon FROM planet_osm_nodes WHERE id IN (#{junction_nids.join(',')})"
		$conn.exec(sql).collect { |res|
			nlat = res['lat'].to_f/10000000.0
			nlon = res['lon'].to_f/10000000.0
			id  = res['id'].to_i
			{ id: id, lat: nlat, lon: nlon,
			  highways: node_highways[id].keys,
			  way_ids: node_ways[id].keys,
			  dist: FasterHaversine.distance(lat,lon,nlat,nlon)*1000
			}
		}.sort_by { |n| n[:dist] }
	end
def find_junctions ( lat , lon , way_ids ) return [ ] if way_ids . empty? node_count = { } node_highways = { } node_ways = { } sql = "SELECT id,nodes,tags FROM planet_osm_ways WHERE id IN (#{way_ids.join(',')})" $conn . exec ( sql ) . each do | res | tag_list = parse_pg_array ( res [ 'tags' ] || '{}' ) highway = Hash [ * tag_list ] [ 'highway' ] nids = parse_pg_array ( res [ 'nodes' ] ) . map ( & :to_i ) nids . each_with_index do | nid , i | end_point = ( i == 0 || i == nids . length - 1 ) node_count [ nid ] = ( node_count [ nid ] || 0 ) + ( end_point ? 1 : 2 ) node_highways [ nid ] = { } unless node_highways [ nid ] node_highways [ nid ] [ highway ] = true node_ways [ nid ] = { } unless node_ways [ nid ] node_ways [ nid ] [ res [ 'id' ] . to_i ] = true end end junction_nids = node_count . keys . select { | nid | node_count [ nid ] > 2 } return [ ] if junction_nids . empty? sql = "SELECT id,lat,lon FROM planet_osm_nodes WHERE id IN (#{junction_nids.join(',')})" $conn . exec ( sql ) . collect { | res | nlat = res [ 'lat' ] . to_f / 10000000.0 nlon = res [ 'lon' ] . to_f / 10000000.0 id = res [ 'id' ] . to_i { id : id , lat : nlat , lon : nlon , highways : node_highways [ id ] . keys , way_ids : node_ways [ id ] . keys , dist : FasterHaversine . distance ( lat , lon , nlat , nlon ) * 1000 } } . sort_by { | n | n [ :dist ] } end
Find junctions between a set of ways
Find junctions between a set of ways
#nids = res['nodes'].delete('{}').split(',').map(&:to_i)
lat
<not_specific>
lon
<not_specific>
way_ids
<not_specific>