1

I have a few hundred road segments, described as lines, which I am trying to write to a single shape file using MATLAB's mapping toolbox. However I can only manage to get one feature per shapefile, which isn't very useful. I have developed a work around which calls GDAL from within MATLAB using system(), but the company I work for can be sensitive about us using software that they don't specifically support, so if I can find an all MATLAB option that would be ideal.

I assume that either it's not possible, or more likely that it's really simple and I'm missing something obvious. The shapewrite() function requires a 'mapstruct' structure item, but the documentation on how to write such a thing is not very clear, I've only succeded in writing single featured mapstructs. The shapewrite documentation only shows how to write a multi-feature shapefile from a mapstruct that was just read using shaperead(), but not how to write your own multi-feature mapstruct structure.

Here's what I have so far...

% Lines is a structure containing all of the individual features
LineIDs = fieldnames(Lines);    
First = 1;
for LineIndex = 1:numel(LineIDs)  
    LineID = LineIDs{LineIndex};
    Line = Lines.(LineID);   
    % So now Line is a structure for that individual line, it contains
    % such fields as the X and Y vertices, the BoundingBox, the Geometry 
    % type (line in this case), and other attributes. Line is my single 
    % feature 'mapstruct'. It can be written to a shapefile using shapewrite.
    LineFileName = sprintf('TempFiles\%s.shp', LineID);
    shapewrite(Line, LineFileName)
    % Now use GDAL to merge this file with all the others into one large file.
    % These next lines are the ones I would like to replace.
    if First
        First = 0;
        MergedFileName = 'MergedFile'
        MergedFileNameF = 'MergedFile.shp'
        % If the file already exists, remove it.
        if exist(MergedFileNameF, 'file') == 2
            delete(MergedFileNameF)
        end
        % Write the file to be merged to.
        [status, ~] = system(sprintf('ogr2ogr %s %s', MergedFileNameF, LineFileName));
    else
        % Merge with the file.
        [status, ~] = system(sprintf('ogr2ogr -update -append %s %s -nln %s', ...
                      MergedFileNameF, TempFile, MergedFileName));
    end
    if status == 0
        % Merge was succesful
        % Delete temp files, etc
    end
end

My single feature mapstruct structures look like this:

>> Line
ans = 

          Class: 'A Road'
             ID: 28841
       Geometry: 'Line'
           Name: 'Willowbank Road'
              X: [3.9323e+05 3.9335e+05 NaN]
              Y: [8.0548e+05 8.0547e+05 NaN]
    BoundingBox: [2x2 double]
      DftNumber: 'A93'
       FEATCODE: 25729
             CP: 80026

And they are stored within Lines, which looks like this...

>> Lines
ans =

    A: [1x1 struct]
    B: [1x1 struct]
    C: [1x1 struct]
    etc etc
  • Eddie, No doubt this comment will be removed - but many thanks for posting the update with your solution. It has helped me with a similar problem. Regards, 10B. – 10B Sep 22 '16 at 13:14
2

Well it turns out that it is very simple, to write multiple features you give shapewrite an array of structs, rather than a single struct. It's a shame that the shapewrite documentation doesn't describe that option, but perhaps they assume that it is obvious...

Anyway, editing the code above so that it works without using GDAL:

% Lines is a structure containing all of the individual features
LineIDs = fieldnames(Lines);    
First = 1;
i = 1;
for LineIndex = 1:numel(LineIDs)  
    LineID = LineIDs{LineIndex};
    Line = Lines.(LineID);   
    % This time, rather than write it to an individual file, add Line
    % to an array of structs.
    if First
       % Preallocate an array of structs with the appropriate fields.
       LinesArray = repmat(Line, numel(LineIDs), 1 );
       First = 0;
    else
        LinesArray(i) = Line;
    end
    i = i + 1;
end
% And now the multiple feature shape file can be written in one line.
shapewrite(LinesArray, 'MergedFile.shp')

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.