readers/tfl_common.rb
# Find subscript for a new node in a given way
Find subscript for a new node in a given way
Find subscript for a new node in a given way
def way_subscript(lat,lon,way_id,prop=-1)
		pr = prop
		if pr==-1 then _,_,pr=snap(lat,lon,way_id) end
		if pr==0 || pr==1 then raise "Found proportion of #{pr} in way #{way_id}, at #{lat},#{lon}" end
		sql = <<-SQL
		SELECT n.id,n.lat,n.lon,
		       ST_LineLocatePoint(ST_Transform(l.way,4326),ST_SetSRID(ST_MakePoint(n.lon/10000000.0,n.lat/10000000.0),4326)) AS prop
		  FROM planet_osm_nodes n 
		  JOIN planet_osm_ways w ON n.id=any(w.nodes) AND w.id=#{way_id}
		  JOIN planet_osm_line l ON l.osm_id=w.id
	  ORDER BY array_position(w.nodes,n.id);
		SQL
		results = $conn.exec(sql).to_a
		results.each_with_index do |res,i|
			return i if i==results.length-1
			return i if pr>=res['prop'].to_f && pr<=results[i+1]['prop'].to_f
		end
		raise "Couldn't find index for node in way"
	end
def way_subscript ( lat , lon , way_id , prop = - 1 ) pr = prop if pr == - 1 then _ , _ , pr = snap ( lat , lon , way_id ) end if pr == 0 || pr == 1 then raise "Found proportion of #{pr} in way #{way_id}, at #{lat},#{lon}" end sql = <<-SQL 
		SELECT n.id,n.lat,n.lon,
		       ST_LineLocatePoint(ST_Transform(l.way,4326),ST_SetSRID(ST_MakePoint(n.lon/10000000.0,n.lat/10000000.0),4326)) AS prop
		  FROM planet_osm_nodes n 
		  JOIN planet_osm_ways w ON n.id=any(w.nodes) AND w.id= #{ way_id } 
		  JOIN planet_osm_line l ON l.osm_id=w.id
	  ORDER BY array_position(w.nodes,n.id);
		 SQL results = $conn . exec ( sql ) . to_a results . each_with_index do | res , i | return i if i == results . length - 1 return i if pr >= res [ 'prop' ] . to_f && pr <= results [ i + 1 ] [ 'prop' ] . to_f end raise "Couldn't find index for node in way" end
Find subscript for a new node in a given way
Find subscript for a new node in a given way
# First, calculate the 0-1 proportion along the way if we don't already have it # Then find this for each node # Finally, return the index after which we insert the new node # note edge case for self-intersecting ways like 578030062 (because array_position finds the first one)
lat
<not_specific>
lon
<not_specific>
way_id
<not_specific>
prop
<not_specific>