MATLAB Answers

# How to transform multiple sets of coordinates in a csv file into a single shapefile

30 views (last 30 days)
Blue on 21 Jan 2021
Answered: Rohit Pappu on 25 Jan 2021
Hi,
I was wondering what was the easiest way to transform the coordinates of multiple polygons in a csv file into a single shapefile ? In the attached file I have multiple polygons with their corresponding latitudee and longitude coordinates and I would like to transform these into a single shapefile consisting of four polygons. I would also like to set the projection to mercator.
How do I do that ?
Thank you,
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Answers (1)

Rohit Pappu on 25 Jan 2021
A possible workaround is as follows
% Read the data from the csv file (exclude the header row)
data = readcell("test.csv","NumHeaderLines",1);
% Create an empty struct array with required field names
d = struct('Geometry','','Lat',[],'Lon',[],'Name','');
% Since there are 4 polygons, iterate four times
for i =1:4
% Define the starting and ending row for each polygon (Every 6th row is filled with NaNs)
first = 6*(i-1)+1;
last = 6*i-1;
% Set the fields for a dummy struct
Data.Geometry='Polygon';
Data.Lat = cell2mat(data(first:last,2));
Data.Lon = cell2mat(data(first:last,3));
Data.Name = cell2mat(data(first,1));
% Store the data in the struct array
d(i) = Data;
end
% Write struct array to shapefile
shapewrite(d,'myfile.shp');
% Verify if data has been written correctly or not
P = shaperead('myfile.shp')
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by