By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,843 Members | 1,420 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,843 IT Pros & Developers. It's quick & easy.

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

P: 12
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


Expand|Select|Wrap|Line Numbers
  1.  
  2. # Way for Perl Script to only build XML file for a specific sized area based on starting coordinates. 
  3. #
  4. # Idea is to only add the devices to the KML (XML) file instead of every single one in the source database (csv file)
  5. #
  6. # Estimations for how many kilometers a numerical increase or decrease to latitude and longitude is:
  7. #
  8. # Start Lat = -37.96  Start Long = 145.05
  9. #   
  10. # Changing Lat by .2 is approximately 37.85 km -- changing long by .2 is approximately 30 km
  11. # field [1] = X coordinates (latitude column)  of devices
  12. # field [2] = y coordinates (longitude column) of devices
  13. # $ARGV[1] = Latitude of starting location returned from google's geocoder API
  14. # $ARGV[2] = longitude of starting location returned from google's geocoder API
  15.  
  16. # open CSV file to Read From
  17. open(CSV_FILE, "/dir1/dir2/csv-file.csv") || 
  18. die "Can't open file: $!";
  19.  
  20. # open/create an XML file to write to
  21. open(XML_FILE, ">/dir1/dir2/xml-file.xml") || 
  22.      die "Can't open file: $!";
  23.  
  24. # Open the while loop to read csv file
  25. while (<CSV_FILE>) {
  26.  
  27. # Delete the new line char for each line
  28. chomp; 
  29.  
  30. # Split each field, on the comma delimiter, into an array
  31. my @fields = split(/,/);
  32.  
  33. #
  34. # Want the following to print xml info for elements in an approximate coverage size of 75km north/south x 60km east/west
  35. #
  36. if (($fields[1] <= $ARGV[1] + .20 && $fields[1] >= $ARGV[1] - .20) && ($fields[2] <= $ARGV[2] + .20 && $fields[2] >= $ARGV[1] - .20)) {
  37.  
  38.  
  39. print XML_FILE<<"EOF";
  40.       <Header>
  41.         <Heading1>$fields[1]</Heading1>
  42.         <Heading2>$fields[xx]</Heading2>
  43.         <Heading3>[xxTBDxx]</Heading3>
  44. EOF
  45.  
  46. }  # close if bracket
  47.  
  48. }  # close while loop
  49.  
  50.  
  51. # Close all open files
  52.  
  53. close XML_FILE;
  54. close CSV_FILE;
  55.  
  56.  
Mar 25 '08 #1
Share this Question
Share on Google+
7 Replies


100+
P: 109
Unfortunatly, i dont have any suggestions for you.
I just wanted to say Good luck, your project looks like a lot of fun.
Mar 26 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
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.
Mar 26 '08 #3

P: 12
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #!/usr/bin/perl -w
  3. use strict;
  4. use warnings;
  5.  
  6. use CGI qw(:standard);
  7.  
  8. # 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
  9. my $GPS = param('location');
  10.  
  11. # retain GPS variable to represent GPS coordinates as a whole (-xx.873698, xxx.223633) 
  12. # extract individual lat & long coordinates -xx.xxxxxx, xxx.xxxxxx 
  13. # and assign each their own variable
  14.  
  15. my $lat = substr($GPS, 1, 10); 
  16. my $long = substr($GPS, 13, 10);
  17.  
  18. # open/create an XML file to write to
  19. open(XML_FILE, ">/home/www/dir/netmap/overlay/test-overlay.xml") || 
  20.      die "Can't open file: $!";
  21.  
  22. # Print the initial XML header and the root element
  23. # Take care to leave indentations intact
  24. print XML_FILE qq {<?xml version=\"1.0\" encoding="UTF-8"?>
  25.   <test stuff>
  26.   <Groups>
  27.     <Sub>
  28.       <Name>Test</Name>
  29.       <Symbol_Type>Blinker</Symbol_Type>
  30.  
  31.     }; #close print
  32.  
  33. # open CSV file to Read From
  34. open(CSV_FILE, "/home/www/dir/netmap/sites/sitelist.csv") || 
  35. die "Can't open file: $!";
  36.  
  37. # Open the while loop to read csv file
  38. # data starts on line 4
  39. # 1st column (0) is latitude - 2nd column (1) is longitude
  40. while (<CSV_FILE>) {
  41.     next if $. < 4;
  42.  
  43. # Delete the new line char for each line
  44. chomp;
  45.  
  46. # Split each field, on the comma delimiter, into an array
  47. my @fields = split(/,/);
  48. #
  49. # Want the following to print xml info for elements in an approximate coverage size of 75km north/south x 60km east/west
  50. #
  51.     if (($fields[0] <= $lat + .20 || $fields[0] >= $lat - .20) && ($fields[1] <= $long + .20 || $fields[1] >= $long - .20)) {
  52.  
  53.  
  54. print XML_FILE<<"EOF";
  55.       <Header>
  56.         <Heading1>$fields[0]</Heading1>
  57.         <Heading2>$fields[1]</Heading2>
  58.         <Heading3>[xxTBDxx]</Heading3>
  59. EOF
  60.  
  61.     }  # close if bracket
  62.  
  63. }  # close while loop
  64.  
  65.  
  66. # Close all open files
  67.  
  68. close XML_FILE;
  69. close CSV_FILE;
  70.  
  71. # print for debugging
  72. print "Content-type: text/html\n\n"; 
  73. print "Done. Received $GPS\n"; 
  74. print "Done. Received $lat\n";
  75. print "Done. Received $long\n";
  76.  

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
Mar 28 '08 #4

P: 12
Hi all,

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

Expand|Select|Wrap|Line Numbers
  1. if (($fields[0] - $lat <= 0.20 && $fields[0] - $lat >= -0.20) && ($fields[1] - $long <= 0.20 && $fields[1] - $long >= -0.20)) {
  2. Action, print, etc.
  3. }   # close if bracket
---
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
Mar 28 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
look into the sprintf function if you need to pad numbers with zeros.
Mar 28 '08 #6

100+
P: 109
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.
Mar 31 '08 #7

P: 12
Thanks KevinADC and SpecialKay.

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


In essence...
Expand|Select|Wrap|Line Numbers
  1. # GPS variable name represents pair of GPS coordinates of unknown and uncontrollable lengths
  2. $GPS = (-xx.xxxx, xxx.xxxxxxxx) 
  3.  
  4. # retain GPS variable for use elsewhere, use newGPS variable
  5. # original GPS format has parantheses ( ) and blank space substituted for a comma ,
  6. # the $newGPS variable value looks like ,-xx.xxxx,,xxx.xxxxx,
  7. (my $newGPS = $GPS) =~ s/[()\s]/,/g;
  8.  
  9. # now we are turning the variable string into an array
  10. my @coord = split(/,/,$newGPS);
  11.  
  12. # extract the comma seperated values of the array where needed
  13. # use $coord[1] for latitude of any length 
  14. # use $coord[3] for longitude of any length

Thanks again,

Hutch
Apr 6 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.