Changeset 15148


Ignore:
Timestamp:
05/29/13 15:50:31 (12 years ago)
Author:
Mathieu Morlighem
Message:

CHG: separated plot_googlemaps.m and googlemaps.m

Location:
issm/trunk-jpl/src/m/plot
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/m/plot/plot_googlemaps.m

    r15146 r15148  
    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);
     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');
     21else
     22        disp('Extracting image from Google maps...');
    2523
    26 %limits in lat/long
    27 ullat = max(latlist); ullon = min(lonlist);
    28 lrlat = min(latlist); lrlon = max(lonlist);
     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);
    2931
    30 %Find optimal zoom
    31 if exist(options,'zoom'),
    32         zoom = getfieldvalue(options,'zoom');
    33 else
    34         zoom    = optimalzoom(ullat,ullon,lrlat,lrlon);
    35         display(['Info: default zoom level ' num2str(zoom)]);
     32        %Image corners in lat/long
     33        ullat = max(latlist); ullon = min(lonlist);
     34        lrlat = min(latlist); lrlon = max(lonlist);
     35
     36        md=googlemaps(md,ullat,ullon,lrlat,lrlon,options);
    3637end
    37 scale   = 1;
    38 maxsize = 640;
     38
     39%Retrieve image from md
     40X = md.radaroverlay.x;
     41Y = md.radaroverlay.y;
     42final = md.radaroverlay.pwr;
     43
     44%Get some options
    3945transparency = getfieldvalue(options,'transparency',.3);
    4046
    41 %convert all these coordinates to pixels
    42 [ulx, uly]= latlontopixels(ullat, ullon, zoom);
    43 [lrx, lry]= latlontopixels(lrlat, lrlon, zoom);
    44 
    45 %calculate total pixel dimensions of final image
    46 dx = lrx - ulx;
    47 dy = uly - lry;
    48 
    49 %calculate rows and columns
    50 cols = ceil(dx/maxsize);
    51 rows = ceil(dy/maxsize);
    52 
    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;
    58 
    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
    85 
    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'),
     
    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);
     
    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
Note: See TracChangeset for help on using the changeset viewer.