function show_maps(db_file)
    % show_maps(db_file)
    conn=sqlite(db_file)
    sql="SELECT * FROM images;";
    res=fetch(conn,sql);
    close(conn);
    tr=[];
    resF.db=db_file;
    resF.data=res;
    set(groot,'UserData',resF);
    for idx=1:2811
        if ~ismember(get_ref(idx),[14,15,16])
            continue
        end
        lat=str2num(res.lat(idx));
        lon=str2num(res.lon(idx));
        yaw=str2num(res.yaw(idx));
        altitude=str2num(res.altitude(idx));
        range=res.range(idx);
        hull=str2num(res.hull(idx));
        [La,Lo]=get_circumference(lat,lon,yaw,altitude,range);
        points=[La',Lo'];
        points=points([hull],:);
        geoplot(points(:,1),points(:,2),'ButtonDownFcn',{@a1,idx});
        hold on;
        tr=[tr;lat',lon'];

    end
    h=geoplot(tr(:,1),tr(:,2));
    hold off;
end

function a1(src,eventdata,idx,~)
   resF=get(groot,"UserData");
   res=table2struct(resF.data);
   res=res(idx);
   lat=str2num(res.lat);
   lon=str2num(res.lon);
   ref=get_ref(idx);
   N=length(lat);
   idx1=fix(N/3);
   idx2=fix(2*N/3);
   fstring=['SELECT * FROM images WHERE images.cnt_id IN (SELECT cnt_id FROM rtr WHERE xmin<=%15.10f ',...
       ' AND %15.10f <= xmax AND ymin<= %15.10f ',...
       ' AND  %15.10f <= ymax AND ref_nr <> %10d) AND abs(yawc - %15.10f) < 0.1;'];
   fstring=['SELECT * FROM images WHERE images.cnt_id IN (SELECT cnt_id FROM rtr WHERE xmin<=%15.10f ',...
       ' AND %15.10f <= xmax AND ymin<= %15.10f ',...
       ' AND  %15.10f <= ymax ) AND abs(yawc - %15.10f) < 0.1;'];
   if lat(idx1) < lat(idx2)
       [lati,lata]=deal(lat(idx1),lat(idx2));
   else
       [lata,lati]=deal(lat(idx1),lat(idx2));
   end   
   if lon(idx1) < lon(idx2)
       [loni,lona]=deal(lon(idx1),lon(idx2));
   else
       [lona,loni]=deal(lon(idx1),lon(idx2));
   end         
   %sql=string(sprintf(fstring,lati,lata,loni,lona,res.ref_nr,res.yawc));
   sql=string(sprintf(fstring,lati,lata,loni,lona,res.yawc));
   conn=sqlite(resF.db);
   other=fetch(conn,sql);
   close(conn);
   if isempty(conn)
       return
   end
   unzul=[];
   for i=1:size(other,1)
       if ~ismember(get_ref(other.cnt_id(i)),[7,8,9])
            unzul=[unzul,i];
       end
   end
   other([unzul],:)=[];
   di=[];
   for i=1:size(other,1)
       di=[di,norm([res.latc,res.lonc]-[other.latc(i),other.lonc(i)])];
   end
   [~,iss]=sort(di);
   other_image=other(iss(1),:);
   
   
   figure(9);
   first=imread(fullfile(res.path,res.id+".png"));
   imshow(first);
   title("Ref: "+convert_to_title(res.id), 'Interpreter', 'none');
   figure(10);
   second=imread(fullfile(other_image.path,other_image.id+".png"));
   imshow(second);
   title(convert_to_title(other_image.id), 'Interpreter', 'none');
   show_corr_diff(first,second,11);
end

function res=substr(stri,pos,len)
    res=extractBetween(stri,pos,pos+len-1);
end

function titel_str=convert_to_title(id)
    ids=split(id,'_');
    da=ids(2);
    dat=substr(da,7,2)+"."+substr(da,5,2)+"."+substr(da,1,4);
    t1=ids(3);
    ti1=substr(t1,1,2)+":"+substr(t1,3,2)+":"+substr(t1,5,2);
    t2=ids(5);
    ti2=substr(t2,1,2)+":"+substr(t2,3,2)+":"+substr(t2,5,2);
    titel_str=dat+" "+ti1+"-"+ti2;
end

function show_corr_diff(first,second,id)
first_p=first(:,1:200,:);
second_p=second(:,1:200,:);
c = normxcorr2(second_p(:,:,1),first_p(:,:,1));
[max_c,imax] = max(abs(c(:)));
%figure(12);
%surf(c) 
%shading flat
[ypeak,xpeak] = ind2sub(size(c),imax(1));
xoffset = (xpeak-size(second_p,2)) ;
yoffset = (ypeak-size(second_p,1));
%display([xoffset,yoffset])
if (yoffset<0) || (xoffset<0)
    [first,second]=deal(second,first);
    xoffset=abs(xoffset);
    yoffset=abs(yoffset);
end
xbegin = round(xoffset + 1);
xend   = round(xoffset + size(second,2));
ybegin = round(yoffset + 1);
yend   = round(yoffset + size(second,1));

r_second = uint8(zeros(size(first)));
r_second(ybegin:yend,xbegin:xend,:) = second;

figure(id);
imshowpair(first,r_second,"diff")

end