Search code examples
matlabqgisgeodesic-sphere

Matlab: Shape file in vector (X,Y) to raster matrix converter


I am having problem with converting Shape file which is a border of Germany with their X,Y coordinates to raster matrix format of same shape. I simply don't know which method to use for this case. I would be thankful if somebody help me to find the right way of thinking for this application.


Solution

  • It can be very much with ease obtained in MATLAB. YOu need to use function inpolygon. This function gives you indices of the points lying inside and putside the given polygon. Once you know the indices, you can get what you want. YOu may check the below demo:

    x = [1 2 3 4 3 2]';
    y = [4 5 5 4 3 3]';
    k = boundary(x,y);
    x0 = min(x) ; x1 = max(x) ;
    y0 = min(y) ; y1 = max(y) ;
    [X,Y] = meshgrid(linspace(x0,x1),linspace(y0,y1)) ;
    idx = inpolygon(X(:),Y(:),x(k),y(k)) ;
    X(~idx) = NaN ; 
    Y(~idx) = NaN ;
    plot(x,y,'.r')
    hold on
    patch(x(k),y(k),'k') ;
    plot(X(idx),Y(idx),'.r')
    

    Just replace (x,y) above with your shape file coordinates, you will get what you want.