source: issm/oecreview/Archive/14312-15392/ISSM-15147-15148.diff@ 15393

Last change on this file since 15393 was 15393, checked in by Mathieu Morlighem, 12 years ago

NEW: adding Archive/14312-15392 for oecreview

File size: 10.2 KB
  • ../trunk-jpl/src/m/plot/plot_googlemaps.m

     
    1515        error('buildgridded error message: gridded not supported for 3d meshes, project on a layer');
    1616end
    1717
    18 %Get xlim and ylim (used to extract radar image)
    19 xlim=getfieldvalue(options,'xlim',[min(x) max(x)]);
    20 ylim=getfieldvalue(options,'ylim',[min(y) max(y)]);
    21 [latlist lonlist]= xy2ll(...
    22         [linspace(xlim(1),xlim(2),100) linspace(xlim(2),xlim(2),100) linspace(xlim(2),xlim(1),100) linspace(xlim(1),xlim(1),100)],...
    23         [linspace(ylim(1),ylim(1),100) linspace(ylim(1),ylim(2),100) linspace(ylim(2),ylim(2),100) linspace(ylim(2),ylim(1),100)],...
    24         +1,45,70);
    25 
    26 %limits in lat/long
    27 ullat = max(latlist); ullon = min(lonlist);
    28 lrlat = min(latlist); lrlon = max(lonlist);
    29 
    30 %Find optimal zoom
    31 if exist(options,'zoom'),
    32         zoom = getfieldvalue(options,'zoom');
     18if ~any(isnan(md.radaroverlay.x)) & ~any(isnan(md.radaroverlay.y)) & ~any(isnan(md.radaroverlay.pwr)) &...
     19                size(md.radaroverlay.pwr,3)==3 & all(size(md.radaroverlay.x)==size(md.radaroverlay.pwr)),
     20        disp('plot_googlemaps info: the RGB image held by the model is being used');
    3321else
    34         zoom    = optimalzoom(ullat,ullon,lrlat,lrlon);
    35         display(['Info: default zoom level ' num2str(zoom)]);
    36 end
    37 scale   = 1;
    38 maxsize = 640;
    39 transparency = getfieldvalue(options,'transparency',.3);
     22        disp('Extracting image from Google maps...');
    4023
    41 %convert all these coordinates to pixels
    42 [ulx, uly]= latlontopixels(ullat, ullon, zoom);
    43 [lrx, lry]= latlontopixels(lrlat, lrlon, zoom);
     24        %Get xlim and ylim (used to extract radar image)
     25        xlim=getfieldvalue(options,'xlim',[min(x) max(x)]);
     26        ylim=getfieldvalue(options,'ylim',[min(y) max(y)]);
     27        [latlist lonlist]= xy2ll(...
     28                [linspace(xlim(1),xlim(2),100) linspace(xlim(2),xlim(2),100) linspace(xlim(2),xlim(1),100) linspace(xlim(1),xlim(1),100)],...
     29                [linspace(ylim(1),ylim(1),100) linspace(ylim(1),ylim(2),100) linspace(ylim(2),ylim(2),100) linspace(ylim(2),ylim(1),100)],...
     30                +1,45,70);
    4431
    45 %calculate total pixel dimensions of final image
    46 dx = lrx - ulx;
    47 dy = uly - lry;
     32        %Image corners in lat/long
     33        ullat = max(latlist); ullon = min(lonlist);
     34        lrlat = min(latlist); lrlon = max(lonlist);
    4835
    49 %calculate rows and columns
    50 cols = ceil(dx/maxsize);
    51 rows = ceil(dy/maxsize);
     36        md=googlemaps(md,ullat,ullon,lrlat,lrlon,options);
     37end
    5238
    53 %calculate pixel dimensions of each small image
    54 bottom = 120;
    55 largura = ceil(dx/cols);
    56 altura  = ceil(dy/rows);
    57 alturaplus = altura + bottom;
     39%Retrieve image from md
     40X = md.radaroverlay.x;
     41Y = md.radaroverlay.y;
     42final = md.radaroverlay.pwr;
    5843
    59 %Initialize final image
    60 final = zeros(floor(dy),floor(dx),3);%RGB image
    61 for x=0:cols-1,
    62         for y=0:rows-1,
    63                 dxn = largura * (0.5 + x);
    64                 dyn = altura * (0.5 + y);
    65                 [latn, lonn] = pixelstolatlon(ulx + dxn, uly - dyn - bottom/2, zoom);
    66                 position = [num2str(latn) ',' num2str(lonn)];
    67                 disp(['Google Earth tile: ' num2str(x) '/' num2str(cols-1) ' ' num2str(y) '/' num2str(rows-1) ' (center: ' position ')']);
    68                 params = [...
    69                         'center=' position ...
    70                         '&zoom=' num2str(zoom)...
    71                         '&size=' num2str(largura) 'x' num2str(alturaplus)...
    72                         '&maptype=satellite'...
    73                         '&sensor=false'...
    74                         '&scale=' num2str(scale)];
    75                 url = ['http://maps.google.com/maps/api/staticmap?' params];
    76                 [X, map]=imread(url,'png');
    77                 X=ind2rgb(X,map);
    78                 indx1 = floor(x*largura)+1;
    79                 indx2 = min(floor(dx),floor(x*largura)+size(X,2));
    80                 indy1 = floor(y*altura)+1;
    81                 indy2 = min(floor(dy),floor(y*altura)+size(X,1));
    82                 final(indy1:indy2,indx1:indx2,:)=X(1:indy2-indy1+1,1:indx2-indx1+1,:);
    83         end
    84 end
     44%Get some options
     45transparency = getfieldvalue(options,'transparency',.3);
    8546
    86 %Create model image
    87 [gX gY]=meshgrid(ulx:ulx+size(final,2)-1,uly:-1:uly-size(final,1)+1);
    88 [LAT LON]=pixelstolatlon(gX,gY, zoom);
    89 [X Y]=ll2xy(LAT,LON,+1,45,70);
     47%Prepare grid
    9048data_grid=InterpFromMeshToMesh2d(md.mesh.elements,md.mesh.x,md.mesh.y,data,X(:),Y(:),'default',NaN); data_grid=reshape(data_grid,size(X));
    91 
    92 %Process data_grid: add white in NaN and correct caxis accordingly
    9349data_nan=isnan(data_grid);
    9450if exist(options,'caxis'),
    9551        caxis_opt=getfieldvalue(options,'caxis');
     
    10157        data_min=min(data_grid(:));
    10258        data_max=max(data_grid(:));
    10359end
    104 
    10560colorm = getcolormap(options);
    10661image_rgb = ind2rgb(uint16((data_grid - data_min)*(length(colorm)/(data_max-data_min))),colorm);
    10762
     
    13186options=addfielddefault(options,'axis','xy equal off'); % default axis
    13287applyoptions(md,data,options);
    13388end
    134 
    135 function [px py]=latlontopixels(lat, lon, zoom),
    136 
    137         EARTH_RADIUS = 6378137;
    138         EQUATOR_CIRCUMFERENCE = 2 * pi * EARTH_RADIUS;
    139         INITIAL_RESOLUTION = EQUATOR_CIRCUMFERENCE / 256.0;
    140         ORIGIN_SHIFT = EQUATOR_CIRCUMFERENCE / 2.0;
    141 
    142     mx = (lon * ORIGIN_SHIFT) / 180.0;
    143     my = log(tan((90 + lat) * pi/360.0))/(pi/180.0);
    144     my = (my * ORIGIN_SHIFT) /180.0;
    145     res = INITIAL_RESOLUTION / (2^zoom);
    146     px = (mx + ORIGIN_SHIFT) / res;
    147     py = (my + ORIGIN_SHIFT) / res;
    148  end
    149 
    150 function [lat lon]=pixelstolatlon(px, py, zoom),
    151 
    152         EARTH_RADIUS = 6378137;
    153         EQUATOR_CIRCUMFERENCE = 2 * pi * EARTH_RADIUS;
    154         INITIAL_RESOLUTION = EQUATOR_CIRCUMFERENCE / 256.0;
    155         ORIGIN_SHIFT = EQUATOR_CIRCUMFERENCE / 2.0;
    156 
    157     res = INITIAL_RESOLUTION / (2^zoom);
    158     mx = px * res - ORIGIN_SHIFT;
    159     my = py * res - ORIGIN_SHIFT;
    160     lat = (my / ORIGIN_SHIFT) * 180.0;
    161     lat = 180 / pi * (2*atan(exp(lat*pi/180.0)) - pi/2.0);
    162     lon = (mx / ORIGIN_SHIFT) * 180.0;
    163  end
    164 function  zoom = optimalzoom(ullat,ullon,lrlat,lrlon)
    165 
    166         EARTH_RADIUS = 6378137;
    167         EQUATOR_CIRCUMFERENCE = 2 * pi * EARTH_RADIUS;
    168         INITIAL_RESOLUTION = EQUATOR_CIRCUMFERENCE / 256.0;
    169 
    170         optimalsize = 1000; %Number of pixels in final image
    171 
    172         [ulmx ulmy]=ll2mercator(ullat,ullon);
    173         [lrmx lrmy]=ll2mercator(lrlat,lrlon);
    174         distance = sqrt((lrmx-ulmx)^2 + (lrmy-ulmy)^2);
    175 
    176         zoom1 = floor(log(INITIAL_RESOLUTION*optimalsize/(lrmx-ulmx))/log(2));
    177         zoom2 = floor(log(INITIAL_RESOLUTION*optimalsize/(ulmy-lrmy))/log(2));
    178 
    179         zoom=max(zoom1,zoom2);
    180 
    181         zoom = min(max(1,zoom),21);
    182 end
  • ../trunk-jpl/src/m/plot/googlemaps.m

     
     1function md = googlemaps(md,ullat,ullon,lrlat,lrlon,varargin)
     2%GOOGLEMAPS - Extract image from Google maps for given region
     3%
     4%   Usage:
     5%       md = googlemaps(md,ullat,ullon,lrlat,lrlon)
     6%       md = googlemaps(md,ullat,ullon,lrlat,lrlon,options)
     7%
     8%   - ullat,ullon: Upper Left corner latitude and longitude
     9%   - lrlat,lrlon: Lower Right corner latitude and longitude
     10%
     11%   Available options:
     12%      - zoom: zoom level, between 1 and 21 (default dynamically calculated)
     13
     14%Parse inputs
     15if nargin<5,
     16        help googlemaps
     17        error('Wrong usage');
     18elseif nargin==5,
     19        options=pairoptions;
     20else
     21        options=varargin{:};
     22        if ~isa(options,'pairoptions'),
     23                options=pairoptions(varargin{:});
     24        end
     25end
     26
     27%Find optimal zoom
     28if exist(options,'zoom'),
     29        zoom = getfieldvalue(options,'zoom');
     30else
     31        zoom    = optimalzoom(ullat,ullon,lrlat,lrlon);
     32        display(['googlemaps info: default zoom level ' num2str(zoom)]);
     33end
     34scale   = 1;
     35maxsize = 640;
     36
     37%convert all these coordinates to pixels
     38[ulx, uly]= latlontopixels(ullat, ullon, zoom);
     39[lrx, lry]= latlontopixels(lrlat, lrlon, zoom);
     40
     41%calculate total pixel dimensions of final image
     42dx = lrx - ulx;
     43dy = uly - lry;
     44
     45%calculate rows and columns
     46cols = ceil(dx/maxsize);
     47rows = ceil(dy/maxsize);
     48
     49%calculate pixel dimensions of each small image
     50bottom = 120;
     51largura = ceil(dx/cols);
     52altura  = ceil(dy/rows);
     53alturaplus = altura + bottom;
     54
     55%Initialize final image
     56final = zeros(floor(dy),floor(dx),3);%RGB image
     57for x=0:cols-1,
     58        for y=0:rows-1,
     59                dxn = largura * (0.5 + x);
     60                dyn = altura * (0.5 + y);
     61                [latn, lonn] = pixelstolatlon(ulx + dxn, uly - dyn - bottom/2, zoom);
     62                position = [num2str(latn) ',' num2str(lonn)];
     63                disp(['Google Earth tile: ' num2str(x) '/' num2str(cols-1) ' ' num2str(y) '/' num2str(rows-1) ' (center: ' position ')']);
     64                params = [...
     65                        'center=' position ...
     66                        '&zoom=' num2str(zoom)...
     67                        '&size=' num2str(largura) 'x' num2str(alturaplus)...
     68                        '&maptype=satellite'...
     69                        '&sensor=false'...
     70                        '&scale=' num2str(scale)];
     71                url = ['http://maps.google.com/maps/api/staticmap?' params];
     72                [X, map]=imread(url,'png');
     73                X=ind2rgb(X,map);
     74                indx1 = floor(x*largura)+1;
     75                indx2 = min(floor(dx),floor(x*largura)+size(X,2));
     76                indy1 = floor(y*altura)+1;
     77                indy2 = min(floor(dy),floor(y*altura)+size(X,1));
     78                final(indy1:indy2,indx1:indx2,:)=X(1:indy2-indy1+1,1:indx2-indx1+1,:);
     79        end
     80end
     81
     82%Create coordinates grids
     83[gX gY]=meshgrid(ulx:ulx+size(final,2)-1,uly:-1:uly-size(final,1)+1);
     84[LAT LON]=pixelstolatlon(gX,gY, zoom);
     85[X Y]=ll2xy(LAT,LON,+1,45,70);
     86
     87md.radaroverlay.pwr=final;
     88md.radaroverlay.x=X;
     89md.radaroverlay.y=Y;
     90
     91end
     92function [px py]=latlontopixels(lat, lon, zoom),
     93        EARTH_RADIUS = 6378137;
     94        EQUATOR_CIRCUMFERENCE = 2 * pi * EARTH_RADIUS;
     95        INITIAL_RESOLUTION = EQUATOR_CIRCUMFERENCE / 256.0;
     96        ORIGIN_SHIFT = EQUATOR_CIRCUMFERENCE / 2.0;
     97
     98        [mx,my]=ll2mercator(lat,lon);
     99        res = INITIAL_RESOLUTION / (2^zoom);
     100        px = (mx + ORIGIN_SHIFT) / res;
     101        py = (my + ORIGIN_SHIFT) / res;
     102end
     103
     104function [lat lon]=pixelstolatlon(px, py, zoom),
     105        EARTH_RADIUS = 6378137;
     106        EQUATOR_CIRCUMFERENCE = 2 * pi * EARTH_RADIUS;
     107        INITIAL_RESOLUTION = EQUATOR_CIRCUMFERENCE / 256.0;
     108        ORIGIN_SHIFT = EQUATOR_CIRCUMFERENCE / 2.0;
     109
     110        res = INITIAL_RESOLUTION / (2^zoom);
     111        mx = px * res - ORIGIN_SHIFT;
     112        my = py * res - ORIGIN_SHIFT;
     113        [lat lon] = mercator2ll(mx,my);
     114end
     115function  zoom = optimalzoom(ullat,ullon,lrlat,lrlon)
     116
     117        EARTH_RADIUS = 6378137;
     118        EQUATOR_CIRCUMFERENCE = 2 * pi * EARTH_RADIUS;
     119        INITIAL_RESOLUTION = EQUATOR_CIRCUMFERENCE / 256.0;
     120
     121        optimalsize = 1000; %Number of pixels in final image
     122
     123        [ulmx ulmy]=ll2mercator(ullat,ullon);
     124        [lrmx lrmy]=ll2mercator(lrlat,lrlon);
     125        distance = sqrt((lrmx-ulmx)^2 + (lrmy-ulmy)^2);
     126
     127        zoom1 = floor(log(INITIAL_RESOLUTION*optimalsize/(lrmx-ulmx))/log(2));
     128        zoom2 = floor(log(INITIAL_RESOLUTION*optimalsize/(ulmy-lrmy))/log(2));
     129
     130        zoom=max(zoom1,zoom2);
     131
     132        zoom = min(max(1,zoom),21);
     133end
Note: See TracBrowser for help on using the repository browser.