How to make more segments of polyline. For example , X1, Y1 ( one node), x2, Y2( second node)

6 views (last 30 days)
Hi everyone!
I have a dataset of 4000 plus polylines with only follwoing columns ; X, Y, and segment ID in a different formate i.e., txt, SVG and CSV. I collected this datset by digitizing the lines in google earth pro, which gave me file in Kmz and kml formates. However, the softare that I am using doesnot accept any file in this formate. The software accept txt or svg with the following formate; X1, Y1 ( One node), X2, Y2 ( second node), these two nodes represent one polyline ( Please also see the attached snapshot as well).
I have tried several conversion tools to convert the Kmz/Kml formate into csv, txt or svg but not able to get the require formate ( X1, y1, X2, Y2). Please also find the file in an attachment.
I am looking a way to make a different nodes of polylines. I will really appreciate any fruitful suggestions!
Best!

Answers (1)

Paras Gupta
Paras Gupta on 7 Sep 2023
Hello,
I understand that you want to create polylines with multiple segments using the nodes given in the .csv file and output the results to a tab limited .txt file. Please find the code below to achieve the same.
% Read the CSV file
data = readmatrix('Polylines.csv');
% Extract the X and Y coordinates from the data
x = data(:, 1);
y = data(:, 2);
% Determine the number of fracture traces in the dataset
numTraces = size(x, 1);
%%%%% Set the desired number of random fracture traces to select
numRandomTraces = 100; % Change this to your desired number
% Create a tab-delimited text file
outputFile = 'random_fracture_traces.txt';
fileID = fopen(outputFile, 'w');
for i = 1:numRandomTraces
%%%%% Change the upper bound to set the maximum number of nodes in a
% polyline
randomIndex = randi([2, 5]);
% Generate random indices for selecting fracture traces
randomIndices = randperm(numTraces, randomIndex);
trace_x = x(randomIndices);
trace_y = y(randomIndices);
% print the first node of the polyline
fprintf(fileID, '%.7f\t%.7f', trace_x(1), trace_y(1));
% print the rest of the nodes of the polyline
for j = 2:randomIndex
fprintf(fileID, '\t%.7f\t%.7f', trace_x(j), trace_y(j));
end
fprintf(fileID, '\n');
end
fclose(fileID);
The above code creates numRandomTraces number of random polylines such that each polyline has randomIndex number of nodes. You can change these variables as per your requirements.
You can refer to the following documents for more information on the code:
Hope it helps.

Community Treasure Hunt

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

Start Hunting!