1

I am trying to use the KML toolbox for Matlab in http://www.mathworks.com/matlabcentral/fileexchange/34694-kml-toolbox-v2-7 to create a KML file from a Matlab array to be shown in Google Earth. However, I cannot get it. How could I get the KML file from a matlab array?

More in detail, the array has values of the altitude in an area, knowing also the latitude and longitud of the area (stored in two vectors). I would like to show each cell of the array with a different colour.

1

I had similar problems before. You can use folling MATLAB function to plot your grid. It's far from being perfect, but that's what I've figured out so far.

You probably hve to adjust it to your needs!!

NOTE: you need to habe the KML-Toolbox and the MAPPING-Toolbox installed.

            function [ ok ] = grid2kmloverlay( grd, R, varargin )
            % grid2kmloverlay takes a georeferenced grid and produces a KMZ file out of it
            % INPUTS:
            % grd: grid
            % R: reference vector for the grid
            %
            %OPTINAL INPUTS:
            % 'saveas', 'path/to/file'
            % 'datasetname', 'nameOfDatasetAsShownInGE'
            %
            % 'clim', [min max]
            % 'cmap', colormap (valid string or matrix);
            % 'opacity', 0...1 (not working properly);
            % 'debug', boolean (if set to 1 then show directly in GoogleEarth )


            %% Set defaults
            %color
            cLim=[min(grd(:)) max(grd(:))];
            cmap=jet(2048);
            opacity=1;

            % OutFile
            saveflag=0;
            OutFile=['C:\tmp\', 'grd_overlay.kml'];
            DataSetName='test';

            %
            colorbarflag=1;

            %show directly in GoogleEarth?
            debug=0;
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            %% Parse Varargs
            if (~isempty(varargin))
                for c=1:2:length(varargin)
                    switch varargin{c}
                        case 'clim'
                            cLim=varargin{c+1};

                        case 'cmap'
                            cmap=varargin{c+1};

                        case 'opacity'
                            opacity=varargin{c+1};


                        case 'saveas'
                            OutFile=varargin{c+1};
                            saveflag=1;

                        case 'datasetname' % e.g. the date
                            DataSetName=varargin{c+1};

                        case 'debug'
                            debug=varargin{c+1};

                        case 'nocolorbar'
                            colorbarflag=0;
                            % (continued)

                        otherwise
                            error(['Invalid optional argument, ', ...
                                varargin{c}]);
                    end % switch
                end % for
            end % if
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


            %% determine the limits of the grid, use setltln() for that
            % We start with the upperLeft edge of the grid
            row=1;
            col=1;
            [maxlat, minlon]=setltln(grd,R,row,col);

            %then the bottomRight edge of the grid
            row=size(grd,1);
            col=size(grd,2);
            [minlat, maxlon]=setltln(grd,R,row,col);

            lonlim=[minlon maxlon];
            latlim=[minlat maxlat];
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

            %% Prepare the plot
            %set NaN values to be transparent
            alphamask=repmat(opacity,size(grd));
            alphamask(isnan(grd))=0;

            f1=figure;
            h1=imagesc([min(lonlim),max(lonlim)]...
                ,[min(latlim),max(latlim)]...
                ,grd);
            set(gca, 'YDir', 'normal');
            colormap(cmap);
            set(gca, 'CLim', cLim);
            set(h1,'AlphaData', alphamask);



            %% Make a KMZ/KML and run in GoogleEarth
            k = kml(DataSetName); %create an kmltoolbox object
            f = k.createFolder('image.overlay');
            f.transfer(gca,'keepAxis',false...
                ,'transparentBG',true...
                ,'altitudeMode','clampToGround'...
                ,'altitude',1);

            if saveflag
                k.save(OutFile);
                disp(['Saved KMZ to:', char(10), OutFile])
            end

            if debug
                k.run
            end

            close(f1)


            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            % EXPERIMENTAL make Colorbar
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            if colorbarflag
                %% Make a colorbar
                fig2=figure;
                left=100; bottom=100 ; width=10 ; height=500;
                pos=[left bottom width height];
                axis off
                cbh=colorbar([0.1 0.1  0.7  0.8],...
                    'FontSize', 30,...
                    'FontWeight', 'bold');

                caxis(cLim)
                %  title('NO2 VCD')
                set(fig2,'OuterPosition',pos)
                % print(fig2,'-dpng','-r150','colorbar.png')


                %         colorbarwithtitle('NO_2 VCD')
                print(fig2,'-dpng','-r150','colorbar.png')


                % kml.overlay(west, east, south, north, 'file','image.jpg')
                west=max(lonlim)+0.2;
                east=max(lonlim)+0.4;
                north= max(latlim);
                south=min(latlim);
                % add colorbar to KML
                cb=f.createFolder('Legend');
                cb.overlay(west, east, south, north, 'file','colorbar.png')

                if saveflag
                    k.save(OutFile);
                    disp(['Saved KMZ to:', char(10), OutFile])
                end

                if debug
                    k.run
                end


            end
            end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.