| 1 | package FWS::V2::Geo; | ||
| 2 | |||
| 3 | use 5.006; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | FWS::V2::Geo - Framework Sites version 2 geo location methods | ||
| 10 | |||
| 11 | =head1 VERSION | ||
| 12 | |||
| 13 | Version 1.13091122 | ||
| 14 | |||
| 15 | =cut | ||
| 16 | |||
| 17 | our $VERSION = '1.13091122'; | ||
| 18 | |||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | use FWS::V2; | ||
| 23 | |||
| 24 | my $fws = FWS::V2->new(); | ||
| 25 | |||
| 26 | # | ||
| 27 | # Return the center of grand rapids michigan zip codes | ||
| 28 | # | ||
| 29 | my %locationCenterHash = $fws->locationCenterHash( city => 'Grand Rapids', state => 'mi' ); | ||
| 30 | |||
| 31 | |||
| 32 | |||
| 33 | =head1 DESCRIPTION | ||
| 34 | |||
| 35 | Framework Sites version 2 geographic methods for use with zip code and city searching. | ||
| 36 | |||
| 37 | =head1 METHODS | ||
| 38 | |||
| 39 | |||
| 40 | =head2 countryArray | ||
| 41 | |||
| 42 | Return the array of countries from the DB with name, twoCharacter, and threeCharacter keys. | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | sub countryArray { | ||
| 47 | my ( $self, %paramHash ) = @_; | ||
| 48 | my %countryHash; | ||
| 49 | my @countryArray; | ||
| 50 | my @countryPull; | ||
| 51 | |||
| 52 | if ( !$paramHash{countryCode} ) { @countryPull = @{$self->runSQL( SQL => "select name,twoCharacter,threeCharacter from country" )} } | ||
| 53 | else { @countryPull = @{$self->runSQL( SQL => "select name,twoCharacter,threeCharacter from country where twoCharacter like '" . $self->safeSQL( $paramHash{countryCode} ) . "' LIMIT 1" )} } | ||
| 54 | |||
| 55 | if ( !@countryPull ) { | ||
| 56 | $countryHash{name} = 'United States'; | ||
| 57 | $countryHash{twoCharacter} = 'US'; | ||
| 58 | $countryHash{threeCharacter} = 'USA'; | ||
| 59 | push( @countryArray, {%countryHash} ); | ||
| 60 | } | ||
| 61 | else { | ||
| 62 | while ( @countryPull ) { | ||
| 63 | $countryHash{name} = shift( @countryPull ); | ||
| 64 | $countryHash{twoCharacter} = shift( @countryPull ); | ||
| 65 | $countryHash{threeCharacter} = shift( @countryPull ); | ||
| 66 | push( @countryArray, {%countryHash} ); | ||
| 67 | } | ||
| 68 | } | ||
| 69 | return @countryArray; | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | |||
| 74 | =head2 locationCenterHash | ||
| 75 | |||
| 76 | Find the lat and long of a city based on its zip codes located in it. | ||
| 77 | |||
| 78 | =cut | ||
| 79 | |||
| 80 | sub locationCenterHash { | ||
| 81 | my ( $self, %paramHash ) = @_; | ||
| 82 | |||
| 83 | my $whereStatement = '1=1'; | ||
| 84 | |||
| 85 | # | ||
| 86 | # if state is passed constrain to that | ||
| 87 | # | ||
| 88 | if ( $paramHash{state} && $paramHash{city} ) { $whereStatement .= " and city like '" . $self->safeSQL( $paramHash{city} ) . "' and stateAbbr like '" . $self->safeSQL( $paramHash{state} ) . "'" } | ||
| 89 | |||
| 90 | # | ||
| 91 | # if zip is passed constrain to that | ||
| 92 | # | ||
| 93 | if ( $paramHash{zip} ) { $whereStatement .= " and zipCode like '" . $self->safeSQL( $paramHash{zip} ) . "'" } | ||
| 94 | |||
| 95 | my @zipArray = @{$self->runSQL( SQL => "select latitude,longitude from zipcode where " . $whereStatement )}; | ||
| 96 | |||
| 97 | my $dirCount; | ||
| 98 | my $totalLat; | ||
| 99 | my $totalLong; | ||
| 100 | while ( @zipArray ) { | ||
| 101 | $dirCount++; | ||
| 102 | my $lat = shift( @zipArray ); | ||
| 103 | my $long = shift( @zipArray ); | ||
| 104 | $totalLat += $lat; | ||
| 105 | $totalLong += $long; | ||
| 106 | } | ||
| 107 | $paramHash{locationCount} = $dirCount; | ||
| 108 | if ( $dirCount > 0 ) { | ||
| 109 | $paramHash{latitude} = $totalLat / $dirCount; | ||
| 110 | $paramHash{longitude} = $totalLong / $dirCount; | ||
| 111 | } | ||
| 112 | return %paramHash; | ||
| 113 | } | ||
| 114 | |||
| 115 | |||
| 116 | =head2 cityArray | ||
| 117 | |||
| 118 | return a list of cities based on keywords, state, or autoComplete style which will search based on an uncomplete query text. | ||
| 119 | |||
| 120 | =cut | ||
| 121 | |||
| 122 | sub cityArray { | ||
| 123 | my ( $self, %paramHash ) = @_; | ||
| 124 | my $city = $self->safeSQL( $paramHash{city} ); | ||
| 125 | my $keywords = $self->safeSQL( $paramHash{keywords} ); | ||
| 126 | my $state = $self->safeSQL( $paramHash{state} ); | ||
| 127 | my $autoComplete = $self->safeSQL( $paramHash{autoComplete} ); | ||
| 128 | |||
| 129 | |||
| 130 | my $whereStatement = '1=1'; | ||
| 131 | my $score = '1 as score'; | ||
| 132 | |||
| 133 | # | ||
| 134 | # if state is passed constrain to that | ||
| 135 | # | ||
| 136 | if ( $autoComplete ) { $whereStatement .= " and city like '" . $autoComplete . "%'" } | ||
| 137 | |||
| 138 | # | ||
| 139 | # if state is passed constrain to that | ||
| 140 | # | ||
| 141 | if ( $state ) { $whereStatement .= " and stateAbbr like '" . $state . "'" } | ||
| 142 | |||
| 143 | # | ||
| 144 | # if city is passed constrain to that | ||
| 145 | # | ||
| 146 | if ( $city ) { $whereStatement .= " and city like '" . $city . "'" } | ||
| 147 | |||
| 148 | # | ||
| 149 | # if keywords is passed constrain to that | ||
| 150 | # | ||
| 151 | if ( $keywords ) { | ||
| 152 | $score = "match(city) against('" . $keywords . "' IN NATURAL LANGUAGE MODE) as score"; | ||
| 153 | $whereStatement .= " and match(city) against('" . $keywords . "' IN NATURAL LANGUAGE MODE)"; | ||
| 154 | } | ||
| 155 | |||
| 156 | my @zipcodes = @{$self->runSQL( SQL => "select city, " . $score . ",zipCode, stateAbbr,areaCode,UTC,latitude,longitude from zipcode where " . $whereStatement . " order by score desc" )}; | ||
| 157 | |||
| 158 | my @returnArray; | ||
| 159 | while ( @zipcodes ) { | ||
| 160 | my %zipHash; | ||
| 161 | $zipHash{city} = shift( @zipcodes ); | ||
| 162 | $zipHash{score} = shift( @zipcodes ); | ||
| 163 | $zipHash{zip} = shift( @zipcodes ); | ||
| 164 | $zipHash{state} = shift( @zipcodes ); | ||
| 165 | $zipHash{areaCode} = shift( @zipcodes ); | ||
| 166 | $zipHash{UTC} = shift( @zipcodes ); | ||
| 167 | $zipHash{latitude} = shift( @zipcodes ); | ||
| 168 | $zipHash{longitude} = shift( @zipcodes ); | ||
| 169 | push( @returnArray, {%zipHash} ); | ||
| 170 | } | ||
| 171 | |||
| 172 | return @returnArray; | ||
| 173 | } | ||
| 174 | |||
| 175 | |||
| 176 | =head2 updateZipArray | ||
| 177 | |||
| 178 | Recompile a zip code array adding extra keys that relate to the passed zip code. | ||
| 179 | |||
| 180 | The following keys will be added: | ||
| 181 | zipState | ||
| 182 | zipStateAbbr | ||
| 183 | zipAreaCode | ||
| 184 | zipUTC | ||
| 185 | zipLatitude | ||
| 186 | zipLongitude | ||
| 187 | zipCounty | ||
| 188 | zipDistance | ||
| 189 | |||
| 190 | =cut | ||
| 191 | |||
| 192 | sub updateZipArray { | ||
| 193 | my ( $self, $fromZip, @dataArray ) = @_; | ||
| 194 | # | ||
| 195 | # clean the from zip | ||
| 196 | # | ||
| 197 | $fromZip = $self->safeSQL( $fromZip ); | ||
| 198 | |||
| 199 | # | ||
| 200 | # get the zipArray and create a , delemented string or the sql | ||
| 201 | # | ||
| 202 | my @zipArray; | ||
| 203 | for my $i ( 0 .. $#dataArray ) { | ||
| 204 | if( $dataArray[$i]{zip} =~ /^\d{5}$/ ) {push( @zipArray,$dataArray[$i]{zip} ) } | ||
| 205 | } | ||
| 206 | |||
| 207 | # | ||
| 208 | # convert the zip into an array | ||
| 209 | # | ||
| 210 | my $destIn = join( ',', @zipArray ); | ||
| 211 | |||
| 212 | # | ||
| 213 | # trap to make sure its not blank | ||
| 214 | # | ||
| 215 | $destIn ||= '0'; | ||
| 216 | |||
| 217 | # | ||
| 218 | # build the SQL | ||
| 219 | # | ||
| 220 | my $SQL = "select distinct destination.zipCode,destination.stateAbbr,destination.areaCode,destination.UTC,"; | ||
| 221 | $SQL .= "destination.latitude,destination.longitude,"; | ||
| 222 | $SQL .= " round(3956 * 2 * ASIN(SQRT( "; | ||
| 223 | $SQL .= " POWER(SIN((origin.latitude - destination.latitude) * 0.0174532925 / 2), 2) +"; | ||
| 224 | $SQL .= " COS(origin.latitude * 0.0174532925) * "; | ||
| 225 | $SQL .= " COS(destination.latitude * 0.0174532925) * "; | ||
| 226 | $SQL .= " POWER(SIN((origin.longitude - destination.longitude) * 0.0174532925 / 2), 2) "; | ||
| 227 | $SQL .= " ))) as distance "; | ||
| 228 | $SQL .= " from zipcode origin, zipcode destination "; | ||
| 229 | $SQL .= " where origin.zipCode = '" . $self->safeSQL( $fromZip ) . "' and destination.zipCode in (" . $self->safeSQL( $destIn ) .") "; | ||
| 230 | |||
| 231 | |||
| 232 | |||
| 233 | # | ||
| 234 | # execute the array | ||
| 235 | # | ||
| 236 | my @distanceArray = @{$self->runSQL( SQL => $SQL )}; | ||
| 237 | |||
| 238 | # | ||
| 239 | # loop though the array creating an hash array | ||
| 240 | # | ||
| 241 | my %zipHash; | ||
| 242 | while ( @distanceArray ) { | ||
| 243 | my $zip = shift( @distanceArray ); | ||
| 244 | $zipHash{$zip}{state} = shift( @distanceArray ); | ||
| 245 | $zipHash{$zip}{areaCode} = shift( @distanceArray ); | ||
| 246 | $zipHash{$zip}{UTC} = shift( @distanceArray ); | ||
| 247 | $zipHash{$zip}{latitude} = shift( @distanceArray ); | ||
| 248 | $zipHash{$zip}{longitude} = shift( @distanceArray ); | ||
| 249 | $zipHash{$zip}{distance} = shift( @distanceArray ); | ||
| 250 | |||
| 251 | if ( $zip eq $fromZip ) { $zipHash{$zip}{distance} = 2 } | ||
| 252 | |||
| 253 | } | ||
| 254 | |||
| 255 | # | ||
| 256 | # recycle the dataArray we started with and add the goods | ||
| 257 | # | ||
| 258 | for my $i ( 0 .. $#dataArray ) { | ||
| 259 | my $zip = $dataArray[$i]{zip}; | ||
| 260 | $dataArray[$i]{zipState} = $zipHash{$zip}{state}; | ||
| 261 | $dataArray[$i]{zipStateAbbr} = $zipHash{$zip}{state}; | ||
| 262 | $dataArray[$i]{zipAreaCode} = $zipHash{$zip}{areaCode}; | ||
| 263 | $dataArray[$i]{zipUTC} = $zipHash{$zip}{UTC}; | ||
| 264 | $dataArray[$i]{zipLatitude} = $zipHash{$zip}{latitude}; | ||
| 265 | $dataArray[$i]{zipLongitude} = $zipHash{$zip}{longitude}; | ||
| 266 | $dataArray[$i]{zipCounty} = $zipHash{$zip}{county}; | ||
| 267 | $dataArray[$i]{zipDistance} = $zipHash{$zip}{distance}; | ||
| 268 | } | ||
| 269 | |||
| 270 | return @dataArray; | ||
| 271 | } | ||
| 272 | |||
| 273 | |||
| 274 | =head2 zipHash | ||
| 275 | |||
| 276 | Return information about a zip code by passing zip | ||
| 277 | |||
| 278 | =cut | ||
| 279 | |||
| 280 | sub zipHash { | ||
| 281 | my ( $self, %paramHash ) = @_; | ||
| 282 | my @zipArray; | ||
| 283 | if ( $paramHash{zip} ) { | ||
| 284 | @zipArray = @{$self->runSQL( SQL => "select zipCode,stateAbbr,city,areaCode,UTC,latitude,longitude from zipcode where zipCode = '" . $self->safeSQL( $paramHash{zip} ) . "' limit 1" )}; | ||
| 285 | } | ||
| 286 | if ( $paramHash{ip} ) { | ||
| 287 | my @ipSplit = split( /\./, $paramHash{ip} ); | ||
| 288 | my $ipNumber = ( 16777216 * $ipSplit[0] ) + ( 65536 * $ipSplit[1] ) + ( 256 * $ipSplit[2] ) + $ipSplit[3]; | ||
| 289 | @zipArray = @{$self->runSQL( SQL => "select zipcode.zipCode,zipcode.stateAbbr,zipcode.city,zipcode.areaCode,zipcode.UTC,zipcode.latitude,zipcode.longitude from geo_block left join zipcode on geo_block.loc_id = zipcode.loc_id where " . $ipNumber . " > start_ip and " . $ipNumber . " < end_ip" )}; | ||
| 290 | } | ||
| 291 | my %zipHash; | ||
| 292 | $zipHash{zip} = shift( @zipArray ); | ||
| 293 | $zipHash{state} = shift( @zipArray ); | ||
| 294 | $zipHash{city} = shift( @zipArray ); | ||
| 295 | $zipHash{areaCode} = shift( @zipArray ); | ||
| 296 | $zipHash{UTC} = shift( @zipArray ); | ||
| 297 | $zipHash{latitude} = shift( @zipArray ); | ||
| 298 | $zipHash{longitude} = shift( @zipArray ); | ||
| 299 | return %zipHash; | ||
| 300 | } | ||
| 301 | |||
| 302 | |||
| 303 | =head1 AUTHOR | ||
| 304 | |||
| 305 | Nate Lewis, C<< <nlewis at gnetworks.com> >> | ||
| 306 | |||
| 307 | =head1 BUGS | ||
| 308 | |||
| 309 | Please report any bugs or feature requests to C<bug-fws-v2 at rt.cpan.org>, or through | ||
| 310 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=FWS-V2>. I will be notified, and then you'll | ||
| 311 | automatically be notified of progress on your bug as I make changes. | ||
| 312 | |||
| 313 | |||
| 314 | |||
| 315 | |||
| 316 | =head1 SUPPORT | ||
| 317 | |||
| 318 | You can find documentation for this module with the perldoc command. | ||
| 319 | |||
| 320 | perldoc FWS::V2::Geo | ||
| 321 | |||
| 322 | |||
| 323 | You can also look for information at: | ||
| 324 | |||
| 325 | =over 4 | ||
| 326 | |||
| 327 | =item * RT: CPAN's request tracker (report bugs here) | ||
| 328 | |||
| 329 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=FWS-V2> | ||
| 330 | |||
| 331 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 332 | |||
| 333 | L<http://annocpan.org/dist/FWS-V2> | ||
| 334 | |||
| 335 | =item * CPAN Ratings | ||
| 336 | |||
| 337 | L<http://cpanratings.perl.org/d/FWS-V2> | ||
| 338 | |||
| 339 | =item * Search CPAN | ||
| 340 | |||
| 341 | L<http://search.cpan.org/dist/FWS-V2/> | ||
| 342 | |||
| 343 | =back | ||
| 344 | |||
| 345 | |||
| 346 | =head1 LICENSE AND COPYRIGHT | ||
| 347 | |||
| 348 | Copyright 2013 Nate Lewis. | ||
| 349 | |||
| 350 | This program is free software; you can redistribute it and/or modify it | ||
| 351 | under the terms of either: the GNU General Public License as published | ||
| 352 | by the Free Software Foundation; or the Artistic License. | ||
| 353 | |||
| 354 | See http://dev.perl.org/licenses/ for more information. | ||
| 355 | |||
| 356 | |||
| 357 | =cut | ||
| 358 | |||
| 359 | 1; # End of FWS::V2::Geo |