ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
So I guess the script needs to load the text file, or the two lines as seperate strings.
Then it should from the first line drop the brackets, read two numbers between commas into variables, put the values in an output file including the neccessary tagging.
Then read from the second line or string one number, and append it to the output with the neccessary tagging.
Then iterate from the points where it stopped reading in the lines last time.
I did some basic programming in Basic and Turbo Pascal, but even there I couldn't reproduce the necessary code in a fair amount of hours, I guess.
The reading from the two lines, as well as the output and tagging cold probably be done in two countered for loops (for processing each line: coordinate (possibly containing another sub-loop for: latitude, longitude), elevation) which would be nested in one primary loop, that refered to a complete "set" of output data (lat, lon; ele).
Is anybody willing to generate the code for me? Thank you.
So I guess the script needs to load the text file, or the two lines as seperate strings.
Then it should from the first line drop the brackets, read two numbers between commas into variables, put the values in an output file including the neccessary tagging.
Then read from the second line or string one number, and append it to the output with the neccessary tagging.
Then iterate from the points where it stopped reading in the lines last time.
I did some basic programming in Basic and Turbo Pascal, but even there I couldn't reproduce the necessary code in a fair amount of hours, I guess.
The reading from the two lines, as well as the output and tagging cold probably be done in two countered for loops (for processing each line: coordinate (possibly containing another sub-loop for: latitude, longitude), elevation) which would be nested in one primary loop, that refered to a complete "set" of output data (lat, lon; ele).
Is anybody willing to generate the code for me? Thank you.
No.
Read the Question Guidelines link. We will HELP you, but will NOT do your work/homework for you. Especially after saying that you're will to try to a 'limited extent' to try. Asking for handouts isn't a good thing, especially after saying you're not even willing to try.
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150
Rep:
No one is going to write a script for you, try yourself if you get stuck on a particular bit post and ask a apecific question and show what you have tried and where you are failing.
To get you started try getting hold of the advanced bash scripting guide ( abs ) it should be in your distros repo or you can do a quick google search, don't be put off by the 'advanced' in the title it has basic conceots as well.
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150
Rep:
Quote:
Originally Posted by TB0ne
No.
Read the Question Guidelines link. We will HELP you, but will NOT do your work/homework for you. Especially after saying that you're will to try to a 'limited extent' to try. Asking for handouts isn't a good thing, especially after saying you're not even willing to try.
I am active in a lot of forums, and I give handouts when I feel like it. Because I like to talk of, and apply the knowledge of the things that I am passionate about.
That's why I didn't mind to ask the people who know.
If someone seems unwilling to try that generally doesn't help, yes.
I didn't mean to in the first place... Anyway I know that I can probably do this coding on my own by the help of code snippets from google, it will be a nasty piece of code.
Now I'm set for it anyway.
Maybe it won't work out, then I'll ask you for your help again.
Well I already got closer as I thought I could in the beginning. The tagging structure comes out correct.
I still cannot get it to work though. To me it seems as it can't get the arrays defined correctly.
In each possible spot it fills in just values from the second line of data (second array, elevation data).
Can someone make anything of this error?
What I tried:
- Add a "declare -a" or "declare -A" to the definition of the array. That rather seems to worsen everything.
- Delete the elevation data line from the input file: Strangely the script fills every possible spot out with position data.
That I cannot understand, because it is my opinion that I programmed it to put specific data in very specific places. But it doesn't seem to mind at all.
(The brackets from the data input I deleted manually)
Please use [code][/code] tags around your code / data in the future to maintain formatting.
Personally I am not sure I understand the input data file structure? Are you saying there are only 2 lines in the file or multiple?
Looking at your current script:
1. Your while loop will fill both array and array2 with identical data
2. In addition to the above, only the last line read from the input file will be in both arrays
3. Deleting the elevation data and the square brackets will completely change the problem, the biggest issue I see is now you will not know which long / lat go together
4. Why the 500 limit in the for loop? What do you know about the input that you have not shared that would indicate this number?
5. How does the variable 'k' factor in to the solution??
6. Investigate here documents ... this will remove the need for multiple printf / echo statements, although I am not convinced all the data could not be in a single printf / echo
7. Instead of redirecting to the same file several times, either use the aforementioned here document or redirect the output from the for loop itself
I think maybe LibreOffice Calc and some text search & replace with geany would do. But I think shell scripting is a good option and it's a basic competence that would be good to know. My aim would be to easily repeat this processing from time to time.
Hello grail,
thanks for the detailed assitance.
The data is always present in two text lines:
[lat1],[lon1],[lat2],[lon2], ...
[ele1],[ele2], ...
I already had some doubt about 1. (/2.) and the function of the
Code:
while read line; do
I think that's the crux. I think I know how to solve it.
3. I deleted the elevation only for testing! It's supposed to be there.
latitude/longitude data is only recognised by the fact, that there is always a sequential pair of data! The code has no further robustness.
Manual deletion of the brackets is more efficient for me than finding out how to do in code. The same goes for the 4. manual iteration switch which I can manually fit to the maximum size of the raw GPX-Track data. I can find that later out if care.
5. One complete set of data consists of [lat1],[lon1],[ele1]
Variable "k" in the procedure serves to adress the second array to retrieve the according elevation data that belongs to a lat/lon pair (adressed in the first array by variable "start" distinct by even/odd number).
Thanks also for hints 6 and 7 concerning the quality/efficiency.
I could get the script written, and it works. Which is very nice!
Thanks a lot for your help. In wide parts I copied snippets. Many things I do not understand, like why (()), [[]] and ++, let alone RegExp and so forth.
It was not possible for me, to make the code much more elegant. I could not find out, how to concatenate strings and variables for use with printf. Also how to use the here document in that context. After over an hour of research I leave it be. Because it does what it should.
Code:
#!/bin/bash
FILE=data
k=0
l=0
# Add GPX file structure #
cat <<-EOF > file.gpx
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<gpx xmlns="http://www.topografix.com/GPX/1/1">
<trk>
<name>gps-track</name>
<trkseg>
EOF
###
# Read data #
while read line;do
((l++))
if [ $l = 1 ]; then
array=(`echo $line | sed 's/,/\n/g'`)
fi
if [ $l = 2 ]; then
array2=(`echo $line | sed 's/,/\n/g'`)
fi
done < $FILE
# Write latitude #
for (( start = 0; start < ${#array[@]}; start++ ))
do
if [[ $((start % 2)) -eq 0 ]];
then
printf '<trkpt lat="' >> file.gpx
printf ${array[start]} >> file.gpx
printf '" ' >> file.gpx
# Write longitude and elevation #
else
printf 'lon="' >> file.gpx
printf ${array[start]} >> file.gpx
printf '"> %s\n <ele>' >> file.gpx
printf ${array2[k]} >> file.gpx
printf '</ele> %s\n</trkpt> %s\n' >> file.gpx
((k++))
fi
done
# Add GPX file structure
cat <<-EOF2 >> file.gpx
</trkseg>
</trk>
</gpx>
EOF2
###
I think maybe LibreOffice Calc and some text search & replace with geany would do.
I wasn't thinking of using a scriptable text editor or spreadsheet, although that's a good idea. I was thinking of an actual programming language with JSON and XML libraries, as you're transforming (what looks like) JSON to XML.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.