Posted on 2006-06-04
Last Modified: 2008-03-10
I want a program in perl which would read NMEA data from a text file and would write longtitude,latitude and elevation in a database which is set up with Postgresql. I lack in programming so I would also like the source code.
Thank You
Question by:geomvigla
    LVL 3

    Expert Comment

    Hmmm, perhaps you need a programmer for this? If you can post sample data and table structures, I might be able to help you.

    Author Comment

    I have a file with gps data in nmea format the format is like this
    I have a parser in perl and I want to read this file and write it in a table in Postgresql. I don't have a ready database where i will store it. Right now I need a simple code to read the file parse the data and store it to a database. I know it is not difficult but I don't have the programming experience and enoygh time to study more on it.
    The parser source code is:

    my @f = split ',', $t;
    if ($f[0] eq 'GPRMC') {
          shift @f;

          my ($latdir,$longdir, $magvardir);
          ($gps{rmc}{time}, $gps{rmc}{warning},
           $gps{rmc}{latitude}, $latdir, $gps{rmc}{longtitude}, $longdir,
           $gps{rmc}{sog}, $gps{rmc}{cmg}, $gps{rmc}{date},
           $gps{rmc}{magvar}, $magvardir ) = @f;

          $magvardir ||= '';
          $gps{rmc}{latitude}   *= -1 if $latdir eq 'S';
          $gps{rmc}{longtitude} *= -1 if $longdir eq 'W';
          $gps{rmc}{magvar}     *= -1 if $magvardir eq 'W';
          $gps{rmc}{latitude} /= 100;
          $gps{rmc}{longtitude} /= 100;

    I just need to store longtitude and latitude
    I hope I can find some help and some source code
    Thank you


    Author Comment

    Ok I solve the problem of reading the file and parsing the data.
    Now I want to add $x,$y,$z to a tabl of postgresql.
    Can you help me on how I make the connection with my database?
    LVL 3

    Expert Comment

    Argh! Sorry, I had typed a script, and then my browser blew up.


    use strict;
    use DBI;

    my $dsn = "dbi:Pg:dbname=test";
    # or "dbi:Pg:dbname=test;host=;port=5432" to connect to a remote box.

    my $dbh = DBI->connect($dsn, "user", "password" {
                           AutoCommit => 0,
                           RaiseError => 1,
                           PrintError => 1,

    # Assumes a table mytables with columns latitude, longitude and elevation.

    my $sql = "INSERT INTO mytable (latitude, longitude, elevation) VALUES (?, ?, ?)";

    my $sth = $dbh->prepare($sql);
    eval {
             $sth->execute($x, $y, $z);

    if ($@) {
             print "Transaction failed: $@\n";
    else {


    # end script

    If you are using PostgreSQL 8.x, just use the COPY option in psql and import the original CSV.

    Author Comment

    Thank you!
    Can you please comment the meaning of eval{ and if($@)
    LVL 3

    Accepted Solution

    eval {}; is the Perl way of doing a try {} catch{} block.

    If the statement fails/errors out for any reason, putting it in an eval block will allow you to handle it gracefuilly.

    If there is an error, eval{}; puts the error in the variable $@. Otherwise this variable is not defined.
    If you are sure your data is clean, I would implement something like the following pseudocode:

    open database connection
    prepare statement
    while <valid data>
        eval {
            execute statement
        if (error) {

    # all the data has been successfully inserted
    disconnect from database

    Author Comment

    Thank you very much!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
    Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now