Utilisateur:Ikonact/Cartographie/wmt finit

Une page de Wikipédia, l'encyclopédie libre.
function wmt_finit(varargin)
%#WMT_FINIT finalise the map to plot. Plots the circles of latitude and the
%#lines of longitude as well as scales.
%#
%# WMT_FINIT('xticks', [x1 x2... xn],'yticks', [y1 y2... yn], 'name', gridname);
%# [x1 x2... xn] - lines of longitude to plot
%# [y1 y2... yn] - circles of latitude to plot
%# gridname - name of th elayer

%# The WMT tool is a collection of Octave/Matlab scripts able to generate geographic maps images.
%#
%# Copyright 2012 ikonact
%# http://commons.wikimedia.org/wiki/User:Ikonact
%# 
%# This file is part of the WMT tool.
%# 
%# WMT is free software: you can redistribute it and/or modify
%# it under the terms of the GNU General Public License as published by
%# the Free Software Foundation, either version 3 of the License, or
%# (at your option) any later version.
%# 
%# WMT is distributed in the hope that it will be useful,
%# but WITHOUT ANY WARRANTY; without even the implied warranty of
%# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%# GNU General Public License for more details.
%# 
%# You should have received a copy of the GNU General Public License
%# along with WMT.  If not, see <http://www.gnu.org/licenses/>.

global wmt_param; 
xticks=[];
yticks=[];

for i = 2:2:nargin
	switch(varargin{i-1})
		case 'xticks'
			xticks = varargin{i};
        case 'yticks'
			yticks = varargin{i};
        case 'name'
			name = varargin{i};
		otherwise
			disp(varargin{i});
	end
end

fprintf(wmt_param.fid, '<g id="gridlines" inkscape:groupmode="layer" inkscape:label="gridlines" clip-path="url(#IDmain)">\n');
for i = 1:length(xticks)
    if isfield(wmt_param, 'projection') && ~strcmp(wmt_param.projection,'')
        x = ones(1,50)*xticks(i);
        y = linspace(wmt_param.datalimits(1),wmt_param.datalimits(2), 50);
        h = m_plot(x, y); hold on;
        xlim(wmt_param.relxlim);
        ylim(wmt_param.relylim);
        plim = wmt_param.relylim(1);
        
    else
        h = plot([xticks(i) xticks(i)], [wmt_param.datalimits(1) wmt_param.datalimits(2)]); hold on;
        ylim([wmt_param.limits(1) wmt_param.limits(2)]);
        xlim([wmt_param.limits(3) wmt_param.limits(4)]);
        plim = wmt_param.limits(1);
        
    end
    plotpath(wmt_param.fid, h, wmt_param.pictsize, ['class="' name '" id="x' name int2str(i) '"']);
    [x1, y1] = reproject(gca, xticks(i), plim);
    fprintf(wmt_param.fid, '<text style="font-size:10px; font-family:sans-serif" x="%0.2f" y="%0.2f">%0.1f&#176;</text>\n',x1*wmt_param.pictsize(1), y1*wmt_param.pictsize(2), xticks(i));
    
end

for i = 1:length(yticks)
    if isfield(wmt_param, 'projection') && ~strcmp(wmt_param.projection,'')
        x = linspace(wmt_param.datalimits(3),wmt_param.datalimits(4), 50);
        y=ones(1,50)*yticks(i);
        h = m_plot(x, y); hold on;
        xlim(wmt_param.relxlim);
        ylim(wmt_param.relylim);
    else
        h = plot([wmt_param.limits(3) wmt_param.datalimits(4)], [yticks(i) yticks(i)]); hold on;
        ylim([wmt_param.limits(1) wmt_param.limits(2)]);
        xlim([wmt_param.limits(3) wmt_param.limits(4)]);
    end
     plotpath(wmt_param.fid, h, wmt_param.pictsize, ['class="' name '" id="y' name int2str(i) '"']);
     [x1, y1] = reproject(gca, wmt_param.limits(3), yticks(i));
     fprintf(wmt_param.fid, '<text style="font-size:10px; font-family:sans-serif" x="%0.2f" y="%0.2f">%0.1f&#176;</text>\n',x1*wmt_param.pictsize(1), y1*wmt_param.pictsize(2),yticks(i));
end

fprintf(wmt_param.fid, '</g>\n');
%# 
%# %# print scale line of 50 km
%# fprintf(wmt_param.fid, '<g id="scale" inkscape:groupmode="layer" inkscape:label="scale" clip-path="url(#IDmain)">\n');
%# [lon2,lat2,a21] = m_fdist(wmt_param.limits(4),wmt_param.limits(2),90,50000);
%# h = m_plot([wmt_param.limits(3) wmt_param.limits(4)], [wmt_param.limits(4) yticks(i)]); hold on;
%# plotpath(wmt_param.fid, h, wmt_param.pictsize, ['class="' name '" id="y' name int2str(i) '"']);
%# fprintf(wmt_param.fid, '</g>\n');

%# plot scale line
if isfield(wmt_param, 'projection') && strcmp(wmt_param.projection,'')
    %#1 deg = 112.12*cos(lat) => n deg = n*112.12*cos(lat)
    pictlngth = abs(abs(wmt_param.limits(4)) - abs(wmt_param.limits(3)));
    pictheight = abs(abs(wmt_param.limits(2)) - abs(wmt_param.limits(1)));
    lengthpictbtm = pictlngth*112.12*cos(wmt_param.limits(1)*pi/180); %# distance of the whole picture at the bottom
    lenghtpicttop = pictlngth*112.12*cos(wmt_param.limits(2)*pi/180); %# distance of the whole picture at the top
    
    t = floor((max(lengthpictbtm, lenghtpicttop)/10)/5); %# define how many times of 10 km line fits in 1/5 of the picture width at the top or bottom
    
    fprintf(wmt_param.fid, '<g id="scale" inkscape:groupmode="layer" inkscape:label="scale" clip-path="url(#IDmain)">\n');
    h = plot([wmt_param.limits(3) + pictlngth*0.02 wmt_param.limits(3) + (t*10/lenghtpicttop)*pictlngth + pictlngth*0.02], [wmt_param.limits(1) + pictheight*0.03 wmt_param.limits(1) + pictheight*0.03]);
    plotpath(wmt_param.fid, h, wmt_param.pictsize, ['style="fill:none;stroke:#000000;stroke-width:1;" class="scaleline" id="scaletop' name int2str(t*10) 'km"']);
    h = plot([wmt_param.limits(3) + pictlngth*0.02 wmt_param.limits(3) + (t*10/lengthpictbtm)*pictlngth + pictlngth*0.02], [wmt_param.limits(1) + pictheight*0.02 wmt_param.limits(1) + pictheight*0.02]);
    plotpath(wmt_param.fid, h, wmt_param.pictsize, ['style="fill:none;stroke:#000000;stroke-width:1;" class="scaleline" id="scalebottom' name int2str(t*10) 'km"']);
    
    [x1, y1] = reproject(gca, wmt_param.limits(3) + (t*10/lenghtpicttop)*pictlngth/2 + pictlngth*0.02, wmt_param.limits(1) + pictheight*0.035);
    fprintf(wmt_param.fid, '<text style="font-size:12px; font-family:sans-serif" x="%6.1f" y="%6.1f">%s km</text>\n',x1*wmt_param.pictsize(1), y1*wmt_param.pictsize(2), int2str(t*10));
    
    fprintf(wmt_param.fid, '</g>\n');
else
    xlm=get(gca,'xlim');
    ylm=get(gca,'ylim');

    %# Get into screen coords
    posx=xlm(1) + [0.05 0.30]*diff(xlm);
    posy=ylm(1) + [0.05 0.05]*diff(ylm);
    
    if diff(xlm)>10, %# we are probably already in meters, i.e. UTM
        scfac=1;
    else
        scfac=6378137; %#Earth radius in m (from WGS-84)
    end;
    
    predefdist = [1 2 5 10 20 50 100 200 500 1000 1500]*1000;
    d = diff(xlm)*scfac/4; %# compute distance for 1/4 of the picture's width
    
    tempd = abs(predefdist - d);
    dist = predefdist((tempd - min(tempd)) == 0);

    fprintf(wmt_param.fid, '<g id="scale" inkscape:groupmode="layer" inkscape:label="scale" clip-path="url(#IDmain)">\n');
    h= plot(posx(1)+[0 dist/scfac],posy(1)+[0 0]);
    plotpath(wmt_param.fid, h, wmt_param.pictsize, ['style="fill:none;stroke:#000000;stroke-width:1;" class="scaleline" id="scale' name int2str(dist) 'km"']);
    [x1, y1] = reproject(gca, posx(1)+ dist/scfac, posy(1));
    fprintf(wmt_param.fid, '<text style="font-size:12px; font-family:sans-serif" x="%6.1f" y="%6.1f">%s km</text>\n',x1*wmt_param.pictsize(1), y1*wmt_param.pictsize(2), int2str(dist/1000));
    
    fprintf(wmt_param.fid, '</g>\n');
end

fprintf(wmt_param.fid, '</svg>');
fclose (wmt_param.fid);