with bline_vertex as (
	
	select	
	b_rcvr_path_uuid as unique_path_id,
	bldg_id as bldg_number,
	bldg_long,
	bldg_lat,
	bldg_meanelevation*3.28 as bldg_mean_elev,
	bldg_maxheight*3.28 as bldg_maxheight,
	bldg_address,
	b_rcvr_call_sign,
	b_rcvr_long,
	b_rcvr_lat,
	b_rcvr_meanelev as b_rcvr_grd_elev_me,
	b_rcvr_raat,
	
	-- delete the end points from the set of vertices
   ST_PointN(
	  st_segmentize(b_line_4326::geography, 80)::geometry,
	  generate_series(2, ST_NPoints(st_segmentize(b_line_4326::geography, 80)::geometry)-1)
   ) as bline_vertex,
   
    bldg_b_rcvr_distance_km as path_distance_km,
	a_rcvr_call_sign,
	b_rcvr_point_4326
	

	from WQQM959_WQQM961_DAT_b_1)
	
	select
	concat (bldg_number,'.' ,b_rcvr_call_sign,'.',a_rcvr_call_sign) as unique_path_id,
	bldg_number,
	bldg_long,
	bldg_lat,
	bldg_mean_elev*3.28 bldg_mean_elev_ft,
	bldg_maxheight*3.28 bldg_maxheight_ft,
	bldg_address,
	b_rcvr_call_sign,
	b_rcvr_long,
	b_rcvr_lat,
	b_rcvr_grd_elev_me,
	b_rcvr_raat b_rcvr_raat_me,
	round (st_x(bline_vertex)::decimal,6) incr_long,
	round (st_y(bline_vertex)::decimal,6) incr_lat,
    round (st_value(WQQM959_WQQM961_dem.rast,bline_vertex)::numeric,4) incr_elev_me,
    round(( st_distance(bline_vertex.bline_vertex::geography,bline_vertex.b_rcvr_point_4326::geography)/1000.00)::numeric,6) incr_dist_km,
	round (path_distance_km::decimal,6) path_distance_km
	
	
	
	

from bline_vertex, WQQM959_WQQM961_dem

where st_intersects(WQQM959_WQQM961_dem.rast,bline_vertex)



order by bldg_number asc,
incr_dist_km desc