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]
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]
Comment