Reading shapefiles in MATLAB

Viewed 174

I don't know if I'm using a wonky shapefile or if I'm misinterpreting the documentation, but I'm a bit lost using shapefiles in matlab.

This is the shapefile I'm using: http://gis-txdot.opendata.arcgis.com/datasets/8b2f979d4ef24388a6a893019322e71c_0 All you have to do to download is go to the "download" tab and click on "shapefile"

The variables when you use

tx = 'TxDOT_National_Highway_System.shp'
S = shaperead(tx)

end up being:

Geometry

BoundingBox

X

Y

I can't seem to separate the X and Y components, or create an array of X and Y components because the data is stored as a structure in Matlab. Could anyone help me with how I might properly read this sort of file?

Ultimately, I'm trying to use this code alongside my lat/lon grid to find the distance from each grid cell to the nearest road.

Thank you for your help!

1 Answers

First of all, S is a structure array composed of 3707 elements and 22 fields:

>> S

S = 

  3707×1 struct array with fields:

    Geometry
    BoundingBox
    X
    Y
    FID
    OBJECTID
    RTE_NM
    RTE_PRFX
    RTE_NBR
    RTE_SFX
    RDBD_TYPE
    GID
    BEGIN_DFO
    END_DFO
    ASSET_NM
    ASSET_ID
    SYSTEM
    NHS
    NHS_TYPE
    SHAPE_STLe
    GlobalID
    SHAPE_Leng

To get access to each field, please use the dot notation. Doing so you, can create an array for both the X and Y components.

For example, to get access to the X and Y coordinates of element 1550 and create two arrays, one for X and another for Y, just write

xcoord = S(1550).X;
ycoord = S(1550).Y;

Obviously the variable names xcoord and ycoord are just fantasy names and you can choose whatever you prefer, e.g. Xcoordinate and Ycoordinate, etc...

You can either concatenate the X and Y coordinates of one element of S, as the element 1550 of the previous example:

xy = [xcoord' ycoord'];

which gives you (see the Command Window):

xy =

              -96.76991556          32.7821172500001
         -96.7688313799999          32.7818193100001
         -96.7684402199999               32.78172727
         -96.7677338999999          32.7815250100001
         -96.7675673799999          32.7814278500001
               -96.7673988                32.7813114
         -96.7673016699999               32.78120284
              -96.76717806          32.7810746500001
         -96.7670568199999               32.78082692
               -96.7670496               32.78078513
         -96.7670363399999               32.78070841
              -96.76700863               32.78054445
              -96.76701293          32.7803245900001
              -96.76705201          32.7800722200001
              -96.76720117               32.77939115
              -96.76719042               32.77931733
                       NaN                       NaN

You can also extract the X and Y coordinates for all the elements of S and store them in two cell arrays, one for X and one for Y:

for i = 1 : size(S,1)
    xcoord_all{i} = S(i).X;
    ycoord_all{i} = S(i).Y;
end

and get this (I just show one of the two cell arrays):

>> xcoord_all'

ans =

  3707×1 cell array

    {[   -96.69759709 -96.6967296 -96.6963104599999 -96.69589959 -96.69569547 -96.69549246 -96.69509046 -96.69469021 … ]}
    {[                                                        -95.4938972909999 -95.4942444599999 -95.4944808399999 NaN]}
    {[  -97.68040307 -97.67991297 -97.67653147 -97.6762490899999 -97.67597196 -97.67569659 -97.67542134 -97.67514984 … ]}
    {[   -95.15539546 -95.1553812 -95.15539139 -95.15536392 -95.15526412 -95.15522901 -95.15516918 -95.1551221299999 … ]}
    {[-95.1225811099999 -95.1224503399999 -95.1221235899999 -95.1212964699999 -95.12087209 -95.12040859 -95.11992872 … ]}
    {[     -95.1557963399999 -95.15576384 -95.1557369399999 -95.15572797 -95.15570559 -95.15565359 -95.1555110899999 … ]}
    {[                 -95.15512864 -95.15513321 -95.1551099599999 -95.1551072999999 -95.15509859 -95.1550880899999 NaN]}
    {[                           -95.4084804 -95.40847286 -95.4084479699999 -95.40844047 -95.40846146 -95.408463926 NaN]}
    {[          -95.24882372 -95.2489598399999 -95.24900459 -95.24920084 -95.2492689599999 -95.25023221 -95.25191684 … ]}
    {[                                                                               -95.0470135799999 -95.04685736 NaN]
...etc..
...etc..
...etc..

Here, again, xcoord_all and xcoord_all are just fantasy names and you can use what you prefer.

In addition, why to store the X and Y coordinates inside cell arrays ? Just because the elements of S have different size, and cell arrays can contain data of varying types and sizes!

Another thing you might be interested in is the visualization of the X and Y coordinates in Matlab:

hold on
for i = 1 : size(S,1)
    plot(S(i).X, S(i).Y)
end
hold off

which just gives this: enter image description here

Related