Best to use If and conditions? or some other form of logic for this example

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hutch75
    New Member
    • Feb 2008
    • 12

    Best to use If and conditions? or some other form of logic for this example

    Hi Folks,

    Wondering if my logic is bringing me down the right path..was hoping for some feedback before I spend too much time creating the source document / database to test it out.

    At a high level, what I'm trying to do is read through a database (CSV file) of GPS coordinates and general descriptions for devices that are deployed throughout a national geography. Rather than have every device in the database formatted into XML (KML) and overlayed on a google map. (too big-irrelevant) I want a more narrow field of geography. So the idea is to use a web-based form to enter an address, send that value to google's geocode API, and use the returning latitude/longitude values as a reference point for the perl logic.

    The perl logic would then build an XML file with any device that encompasses an area approximately 60 kilometers x 60 kilometers from the starting address.

    I've broken this project into several areas, my question here is regarding the best the approach for using perl logic to create the XML file that contains all devices within a 60km x 60km area.

    The following is a rough draft of my untested code, does it look like the right path to go down? (EG, will the IF conditions do what I want it to? or is there a better approach)

    Regards,

    Hutch


    [CODE=perl]

    # Way for Perl Script to only build XML file for a specific sized area based on starting coordinates.
    #
    # Idea is to only add the devices to the KML (XML) file instead of every single one in the source database (csv file)
    #
    # Estimations for how many kilometers a numerical increase or decrease to latitude and longitude is:
    #
    # Start Lat = -37.96 Start Long = 145.05
    #
    # Changing Lat by .2 is approximately 37.85 km -- changing long by .2 is approximately 30 km
    #
    #
    # field [1] = X coordinates (latitude column) of devices
    # field [2] = y coordinates (longitude column) of devices
    # $ARGV[1] = Latitude of starting location returned from google's geocoder API
    # $ARGV[2] = longitude of starting location returned from google's geocoder API

    # open CSV file to Read From
    open(CSV_FILE, "/dir1/dir2/csv-file.csv") ||
    die "Can't open file: $!";

    # open/create an XML file to write to
    open(XML_FILE, ">/dir1/dir2/xml-file.xml") ||
    die "Can't open file: $!";

    # Open the while loop to read csv file
    while (<CSV_FILE>) {

    # Delete the new line char for each line
    chomp;

    # Split each field, on the comma delimiter, into an array
    my @fields = split(/,/);

    #
    # Want the following to print xml info for elements in an approximate coverage size of 75km north/south x 60km east/west
    #
    if (($fields[1] <= $ARGV[1] + .20 && $fields[1] >= $ARGV[1] - .20) && ($fields[2] <= $ARGV[2] + .20 && $fields[2] >= $ARGV[1] - .20)) {


    print XML_FILE<<"EOF" ;
    <Header>
    <Heading1>$fiel ds[1]</Heading1>
    <Heading2>$fiel ds[xx]</Heading2>
    <Heading3>[xxTBDxx]</Heading3>
    EOF

    } # close if bracket

    } # close while loop


    # Close all open files

    close XML_FILE;
    close CSV_FILE;

    [/CODE]
  • SpecialKay
    New Member
    • Mar 2008
    • 109

    #2
    Unfortunatly, i dont have any suggestions for you.
    I just wanted to say Good luck, your project looks like a lot of fun.

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      Before investing too much time in writing code I suggest your search CPAN for modules that already do this. Search for goolge or googlemaps or whatever search criteria is relevant and then search through the list of modules.

      Comment

      • hutch75
        New Member
        • Feb 2008
        • 12

        #4
        Thanks for the feedback folks.

        Quick Update on progress and a different question at bottom:

        Update:

        Eventually got the syntax right, and here is the current working view:

        [code=perl]

        #!/usr/bin/perl -w
        use strict;
        use warnings;

        use CGI qw(:standard);

        # GPS value derived from web-based javascript query to google's geocoder api. # java variable for google's response is passed through as a hidden input type # eg., input type=hidden, name=location, value=js variable
        my $GPS = param('location ');

        # retain GPS variable to represent GPS coordinates as a whole (-xx.873698, xxx.223633)
        # extract individual lat & long coordinates -xx.xxxxxx, xxx.xxxxxx
        # and assign each their own variable

        my $lat = substr($GPS, 1, 10);
        my $long = substr($GPS, 13, 10);

        # open/create an XML file to write to
        open(XML_FILE, ">/home/www/dir/netmap/overlay/test-overlay.xml") ||
        die "Can't open file: $!";

        # Print the initial XML header and the root element
        # Take care to leave indentations intact
        print XML_FILE qq {<?xml version=\"1.0\" encoding="UTF-8"?>
        <test stuff>
        <Groups>
        <Sub>
        <Name>Test</Name>
        <Symbol_Type>Bl inker</Symbol_Type>

        }; #close print

        # open CSV file to Read From
        open(CSV_FILE, "/home/www/dir/netmap/sites/sitelist.csv") ||
        die "Can't open file: $!";

        # Open the while loop to read csv file
        # data starts on line 4
        # 1st column (0) is latitude - 2nd column (1) is longitude
        while (<CSV_FILE>) {
        next if $. < 4;

        # Delete the new line char for each line
        chomp;

        # Split each field, on the comma delimiter, into an array
        my @fields = split(/,/);
        #
        # Want the following to print xml info for elements in an approximate coverage size of 75km north/south x 60km east/west
        #
        #
        if (($fields[0] <= $lat + .20 || $fields[0] >= $lat - .20) && ($fields[1] <= $long + .20 || $fields[1] >= $long - .20)) {


        print XML_FILE<<"EOF" ;
        <Header>
        <Heading1>$fiel ds[0]</Heading1>
        <Heading2>$fiel ds[1]</Heading2>
        <Heading3>[xxTBDxx]</Heading3>
        EOF

        } # close if bracket

        } # close while loop


        # Close all open files

        close XML_FILE;
        close CSV_FILE;

        # print for debugging
        print "Content-type: text/html\n\n";
        print "Done. Received $GPS\n";
        print "Done. Received $lat\n";
        print "Done. Received $long\n";
        [/code]


        My question is:

        What can I do to overcome the eventual issue of dealing with incoming GPS coordinates that have a varying character length.

        Example. Normally the GPS coordinates are received in following format.

        $GPS = (-23.700358, 133.880889)

        When that happens, the following works great to extract the latitude and longitude as individual values.

        my $lat = substr($GPS, 1, 10);
        my $long = substr($GPS, 13, 10);

        But when one of the coordinate sets ends in 0 (eg, 133.880880) the value is passed through as 133.88089 -- changing the character count for the substr command.

        Any ideas? I reckon there must be a way to do screening logic for the total character count, yet I don't think that would help identify if it were the lat, or long, or both that changed.

        Regards,

        Hutch

        Comment

        • hutch75
          New Member
          • Feb 2008
          • 12

          #5
          Hi all,

          Correction to the last, the correct IF logic is as follows:

          [CODE=perl]if (($fields[0] - $lat <= 0.20 && $fields[0] - $lat >= -0.20) && ($fields[1] - $long <= 0.20 && $fields[1] - $long >= -0.20)) {
          Action, print, etc.
          } # close if bracket[/CODE]

          ---
          Would be of much great assist if anyone can lend an idea to my last query in the above post re: how to dis-assemble a string of unknown character lengths igroring the irrelevant and re-assemble into 2 distinct variables.

          I'll do the heavy lifting if you can give me ideas to work with.

          Cheers,

          Hutch
          Last edited by eWish; Apr 7 '08, 03:32 AM. Reason: Please use code tags

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            look into the sprintf function if you need to pad numbers with zeros.

            Comment

            • SpecialKay
              New Member
              • Mar 2008
              • 109

              #7
              Could you splits it by a Delimiter rather then character count. For example the comma. if your GPS cor is (0, 27.485697) or whatever. Take all the digits before the comma in $lat and all digits after comma in $long.

              Comment

              • hutch75
                New Member
                • Feb 2008
                • 12

                #8
                Thanks KevinADC and SpecialKay.

                Kay, your idea re; using the commas is what sparked the solution.


                In essence...
                [CODE=perl]
                # GPS variable name represents pair of GPS coordinates of unknown and uncontrollable lengths
                $GPS = (-xx.xxxx, xxx.xxxxxxxx)

                # retain GPS variable for use elsewhere, use newGPS variable
                # original GPS format has parantheses ( ) and blank space substituted for a comma ,
                # the $newGPS variable value looks like ,-xx.xxxx,,xxx.xx xxx,
                (my $newGPS = $GPS) =~ s/[()\s]/,/g;

                # now we are turning the variable string into an array
                my @coord = split(/,/,$newGPS);

                # extract the comma seperated values of the array where needed
                # use $coord[1] for latitude of any length
                # use $coord[3] for longitude of any length[/CODE]


                Thanks again,

                Hutch
                Last edited by eWish; Apr 7 '08, 03:32 AM. Reason: Please use code tags

                Comment

                Working...