473,569 Members | 2,782 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

12 New Member
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
7 1465
SpecialKay
109 New Member
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
4,059 Recognized Expert Specialist
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
hutch75
12 New Member
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
hutch75
12 New Member
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
4,059 Recognized Expert Specialist
look into the sprintf function if you need to pad numbers with zeros.
Mar 28 '08 #6
SpecialKay
109 New Member
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
hutch75
12 New Member
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

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

Similar topics

9
4502
by: Wm | last post by:
As an amateur wannabe-pro programmer, I am trying to learn not only how to use PHP but how to do it *efficiently*. (Trust me, you don't wanna see some of my stuff!!!) I'm noticing a number of my pages have a mixture of HTML and PHP, very interspersed. Example: A form with 20 fields where I echo a variable in each field to show existing data....
136
9251
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their code was littered with document.all and eval, for example, and I wanted to create a practical list of best practices that they could easily put to...
8
2130
by: Ian Davies | last post by:
Hello I am trying to run a few INSERT queries only if the user clicks 'continue' in a <a href> The queries takes variables created from SELECT queries. I have tried to acheive this by putting The INSERT queries into a function at the top of my php file and having the SELECT queries below it. I have a message that pops up that includes a link...
0
4208
by: Anonieko Ramos | last post by:
ASP.NET Forms Authentication Best Practices Dr. Dobb's Journal February 2004 Protecting user information is critical By Douglas Reilly Douglas is the author of Designing Microsoft ASP.NET Applications and owner of Access Microsystems. Doug can be reached at doug@accessmicrosystems.com....
16
2783
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and efficient navigating within Visual Studio 2005. Let's say your project (or solution) has dozens of forms and hundreds or even thousands of routines. ...
13
2553
by: G | last post by:
Hello, Looking for opinions on a fairly simple task, new to ASP.net (C#) and want to make sure I do this as efficiently as possible. I have a web based form, and I need to run some SQL before submit, which determines exactly where to send the form contents. The table of "receipients" could contain in the region of 3,500 recipients but is...
41
2835
by: Jim | last post by:
Hi guys, I have an object which represents an "item" in a CMS "component" where an "item" in the most basic form just a field, and a "component" is effectively a table. "item" objects can be created and then added to "component" objects to build up the component definition. My dilemma comes in deciding how to read/write data to the...
14
2021
by: Patrick A | last post by:
All, I have an Access DB. On a nightly basis, I want to look at an Other DB (not Access, but SQL) and: + Add any new records from Other.Clients into Access.Clients Is this something I should use a "tool" (SQL Data Compare, SqlSync, etc.) to do, or could I pull this off reliably every night just using
12
25077
by: =?ISO-8859-1?Q?Ren=E9?= | last post by:
Hi, is there a rule of thumb what is better/faster/more performant in SQL Server 2005? a) SELECT * FROM A INNER JOIN B ON B.ID = A.ID AND B.Cond1 = 1 AND B.Cond2 = 2 b) SELECT * FROM A INNER JOIN B ON B.ID = A.ID WHERE B.Cond1 = 1 AND B.Cond2 = 2
0
7697
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8120
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6283
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.