blib/lib/Geo/Geocalc.pm

filter: Statementstmt 9.0% 13 / 143 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 26.6% 4 / 15 Podpod 100% 11 / 11 totaltotal 14.6% 28 / 191 CC 14 SCAR 25.4
CC
14
Cov
8%
generateGridCoordinates
30.0
generateKMLPlacemarks
20.0
generateKMLPolygons
12.0
CRAP 12.6
↑ Summary
1 package Geo::Geocalc;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Math::Trig;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Geo::Geocalc - operations on geographical coordinates.
11  
12 =head1 VERSION
13  
14 Version 0.01
15  
16 =cut
17  
18 1 our $VERSION = '0.01';
19  
20  
21 =head1 SYNOPSIS
22  
23 Geo::Geocalc simple Perl module for coordinates processing. The basic assumptions of this module are: simple, light and clean as possible.
24  
25 Some of the main functions (code snippets):
26  
27 use Geo::Geocalc;
28  
29 $distanceInKm = &getDistance($lat1,$lng1,$lat2,$lng2,$R) # calculate distance (km) between two points on Earth.
30  
31 $latitudeInDegree = &getLat($lat1,$lng1,$lng2,$distance,$R) # calculate latitude (deg) of the point based on other point and distance on Earth between them.
32  
33 $longitudeInDegree = &getLng($lat1,$lng1,$lat2,$distance,$R) # calculate longitude (deg) of the point based on other point and distance on Earth between them.
34  
35 &generateKMLPlacemarks($outputKmlFilename, @coordinates); # generate outputKmlFilename.kml with Placemarks based on an array of coordinates (deg) ($lat1, $lng1, $lat2, $lng2, ...)
36  
37 &generateKMLPolygons($outputKmlFilename, \%hashOfCoords); # generate outputKmlFilename.kml with Polygons based on an reference to hash of 0 -> @ of coordinates of corners (deg - $lat1, $lng1, $lat2, $lng2 ...) , recommended to use hash generated with generateGridCoordinates function
38  
39 &generateGridCoordinates($lat1, $lng1, $lat2, $lng2, $width, \%gridCoordinates, $R); # coordinates of opposite corners on Earth to be covered by the grid, size of grid in km, reference to hash of grid output coordinates and optional, Earth radius.
40  
41 ($qLat, $qLng) = &Geo::Geocalc::randomPlacemarkInsidePolygon(@{$gridCoordinates{$sector}}); # generate random place (latitude and longitude values) inside provided Polygon - array of 4 corners coordinates ($lat1, $lng1, $lat2, $lng2, ...)
42  
43 ...
44  
45 =head1 SUBROUTINES/METHODS
46  
47 =head2 generateKMLPolygons
48  
49 =cut
50  
51 0 ▶sub generateKMLPolygons {
Subroutine generateKMLPolygons: not called CC=3 SCAR=24.8
Pod documented
52 0 my $fileName = shift;
53 0 my $polygonsHashRef = shift;
54  
55 0 ▶ open(FILE, ">$fileName") || die "Can not open file $fileName!";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, ">$fileName" or die "Can not open file $fileName!" 0 0 0
56 0 print FILE &getKMLHeader;
57 0 print FILE " <Folder>";
58 0 print FILE " <name>Polygons</name>";
59 0 print FILE " <description>Generated Polygons with generateKMLPolygons function</description>";
60  
61 0 foreach my $key (keys %{$polygonsHashRef}) {
62 0 print FILE " <Placemark>";
63 0 print FILE " <Polygon>";
64 0 print FILE " <altitudeMode>clampToGround</altitudeMode>";
65 0 print FILE " <outerBoundaryIs>";
66 0 print FILE " <LinearRing>";
67 0 print FILE " <coordinates>";
68  
69 0 print FILE " ${$polygonsHashRef}{$key}[1]," . ${$polygonsHashRef}{$key}[0];
70 0 print FILE " ${$polygonsHashRef}{$key}[3]," . ${$polygonsHashRef}{$key}[2];
71 0 print FILE " ${$polygonsHashRef}{$key}[5]," . ${$polygonsHashRef}{$key}[4];
72 0 print FILE " ${$polygonsHashRef}{$key}[7]," . ${$polygonsHashRef}{$key}[6];
73  
74 0 print FILE " </coordinates>";
75 0 print FILE " </LinearRing>";
76 0 print FILE " </outerBoundaryIs>";
77 0 print FILE " </Polygon>";
78 0 print FILE " </Placemark>";
79 }
80  
81 0 print FILE " </Folder>";
82 0 print FILE &getKMLClose;
83 0 close FILE;
84  
85 };
86  
87 =head2 generateKMLPlacemarks
88  
89 =cut
90  
91 0 ▶sub generateKMLPlacemarks {
Subroutine generateKMLPlacemarks: not called CC=4 SCAR=30.0
Pod documented
92 0 my $fileName = shift;
93 0 my @coordinates = @_;
94  
95 0 ▶ if(scalar(@coordinates)%2 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(@coordinates) % 2 == 0) { } 0 0 0
96  
97 0 ▶ open(FILE, ">$fileName") || die "Can not open file $fileName!";
Branch: 1 branch
BranchTrueFalseTotal
open FILE, ">$fileName" or die "Can not open file $fileName!" 0 0 0
98 0 print FILE &getKMLHeader;
99 0 print FILE " <Folder>";
100 0 print FILE " <name>Placemarks</name>";
101 0 print FILE " <description>Generated Placemarks with generateKMLPlacemarks function</description>";
102  
103 0 ▶ for(my $i = 0; $i < scalar(@coordinates); $i+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @coordinates) 0 0 0
104 0 print FILE " <Placemark>";
105 0 print FILE " <Point>";
106 0 print FILE " <coordinates>" . $coordinates[$i+1] . ", " . $coordinates[$i] . "</coordinates>";
107 0 print FILE " </Point>";
108 0 print FILE " </Placemark>";
109 }
110 0 print FILE " </Folder>";
111 0 print FILE &getKMLClose;
112 0 close FILE;
113 } else {
114 0 print "ERROR. Array must contain pairs of coords. Length of array is not an even number (".scalar(@coordinates).")."
115 }
116 };
117  
118 =head2 randomPlacemarkInsidePolygon
119  
120 =cut
121  
122 0 ▶sub randomPlacemarkInsidePolygon {
Subroutine randomPlacemarkInsidePolygon: not called CC=1 SCAR=6.9
Pod documented
123 0 my @polygon = @_;
124 0 my ($maxLat, $maxLng, $minLat, $minLng) = 0;
125 0 my ($randLat, $randLng) = 0;
126  
127 # check if PMR is belong to rectangle
128 #
129 #
130 # D ---------- C
131 # | |
132 # | |
133 # | |
134 # | |
135 # A ---------- B
136 #
137 # A(0,1) B(2,3) C(4,5) D(6,7)
138 # lat lower than lat of D and greater than lat of A
139 # lng lower than lng of C and greater than lng of D
140  
141 0 $maxLat = $polygon[6];
142 0 $minLat = $polygon[0];
143 0 $maxLng = $polygon[5];
144 0 $minLng = $polygon[7];
145  
146 0 $randLat = $minLat + rand($maxLat - $minLat);
147 0 $randLng = $minLng + rand($maxLng - $minLng);
148  
149 0 ($randLat, $randLng);
150 };
151  
152 =head2 generateGridCoordinates
153  
154 =cut
155  
156 0 ▶sub generateGridCoordinates {
Subroutine generateGridCoordinates: not called CC=5 SCAR=34.0
Pod documented
157 0 my ($startLat, $startLng, $stopLat, $stopLng, $width, $tmpLat, $tmpLng, $gridHashRef, $counter, $R, $startSectorId) = 0;
158 0 ($startLat, $startLng, $stopLat, $stopLng, $width, $gridHashRef, $R, $startSectorId) = @_;
159  
160 0 ▶ $R //= 6371; #//
Condition0%0/2
$R //= 6371
l!l
00
MC/DC0%0/1
$R //= 6371
$R
Truth table0%0/2
A
A= $R
Aresult
11
00
161 0 ▶ $startSectorId //= 1; #//
Condition0%0/2
$startSectorId //= 1
l!l
00
MC/DC0%0/1
$startSectorId //= 1
$startSectorId
Truth table0%0/2
A
A= $startSectorId
Aresult
11
00
162  
163 0 my $maxLat = &getMax(($startLat, $stopLat));
164 0 my $maxLng = &getMax(($startLng, $stopLng));
165 0 my $minLat = &getMin(($startLat, $stopLat));
166 0 my $minLng = &getMin(($startLng, $stopLng));
167  
168 0 $tmpLng = $minLng;
169  
170 0 $counter = $startSectorId;
171  
172 0 ▶ while($tmpLng < $maxLng) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmpLng < $maxLng) 0 0 0
173  
174 0 $tmpLat = $minLat;
175  
176 0 ▶ while($tmpLat < $maxLat) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmpLat < $maxLat) 0 0 0
177 #A
178 0 ${$gridHashRef}{$counter}[0] = $tmpLat;
179 0 ${$gridHashRef}{$counter}[1] = $tmpLng;
180 #B
181 0 ${$gridHashRef}{$counter}[2] = ${$gridHashRef}{$counter}[0];
182 0 ${$gridHashRef}{$counter}[3] = &getLng(${$gridHashRef}{$counter}[0], ${$gridHashRef}{$counter}[1], ${$gridHashRef}{$counter}[0], $width, $R);
183 #C
184 0 ${$gridHashRef}{$counter}[4] = &getLat(${$gridHashRef}{$counter}[2], ${$gridHashRef}{$counter}[3], ${$gridHashRef}{$counter}[3], $width, $R);
185 0 ${$gridHashRef}{$counter}[5] = ${$gridHashRef}{$counter}[3];
186 #D
187 0 ${$gridHashRef}{$counter}[6] = &getLat(${$gridHashRef}{$counter}[0], ${$gridHashRef}{$counter}[1], ${$gridHashRef}{$counter}[0], $width, $R);
188 0 ${$gridHashRef}{$counter}[7] = ${$gridHashRef}{$counter}[1];
189  
190 0 $tmpLat = ${$gridHashRef}{$counter}[4];
191 0 $counter++;
192 }
193 0 $tmpLng = ${$gridHashRef}{($counter-1)}[3];
194 }
195 };
196  
197  
198 =head2 getDistance
199  
200 =cut
201  
202 0 ▶sub getDistance {
Subroutine getDistance: not called CC=1 SCAR=6.9
Pod documented
203 0 my ($lat1, $lng1, $lat2, $lng2, $R) = 0;
204 0 ($lat1, $lng1, $lat2, $lng2, $R) = @_;
205  
206 0 return acos(sin(deg2rad($lat1))*sin(deg2rad($lat2))+cos(deg2rad($lat1))*cos(deg2rad($lat2))*cos(deg2rad($lng1)-deg2rad($lng2)))*$R;
207 };
208  
209 =head2 getLat
210  
211 =cut
212  
213 0 ▶sub getLat {
Subroutine getLat: not called CC=1 SCAR=6.9
Pod documented
214 0 my ($lat1, $lng1, $lng2, $dist, $R) = 0;
215 0 ($lat1, $lng1, $lng2, $dist, $R) = @_;
216
217 0 return (rad2deg(asin(sin(deg2rad($lat1))*cos($dist/$R)+cos(deg2rad($lat1))*sin($dist/$R)*cos(0))));
218 };
219  
220 =head2 getLng
221  
222 =cut
223  
224 0 ▶sub getLng {
Subroutine getLng: not called CC=1 SCAR=6.9
Pod documented
225 0 my ($lat1, $lng1, $lat2, $dist, $R) = 0;
226 0 ($lat1, $lng1, $lat2, $dist, $R) = @_;
227  
228 0 return (rad2deg(deg2rad($lng1) + atan2(sin(90)*sin($dist/$R)*cos(deg2rad($lat1)), cos($dist/$R)-sin(deg2rad($lat1))*sin(deg2rad($lat2)))));
229 };
230  
231  
232 =head2 getKMLHeader
233  
234 =cut
235  
236 0 ▶sub getKMLHeader {
Subroutine getKMLHeader: not called CC=1 SCAR=6.9
Pod documented
237 0 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
238 <kml xmlns=\"http://www.opengis.net/kml/2.2\">
239 <Document>"
240 };
241  
242 =head2 getKMLClose
243  
244 =cut
245  
246 0 ▶sub getKMLClose {
Subroutine getKMLClose: not called CC=1 SCAR=6.9
Pod documented
247 0 " </Document>
248 </kml>"
249 };
250  
251 =head2 getMax
252  
253 =cut
254  
255 0 ▶sub getMax {
Subroutine getMax: not called CC=3 SCAR=24.8
Pod documented
256 0 my $max = $_[0];
257  
258 0 foreach my $tmp (@_) {
259 0 ▶ if($max < $tmp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max < $tmp) 0 0 0
260 0 $max = $tmp;
261 }
262 }
263 0 $max;
264 };
265  
266 =head2 getMin
267  
268 =cut
269  
270 0 ▶sub getMin {
Subroutine getMin: not called CC=3 SCAR=24.8
Pod documented
271 0 my $min = $_[0];
272  
273 0 foreach my $tmp (@_) {
274 0 ▶ if($min > $tmp) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min > $tmp) 0 0 0
275 0 $min = $tmp;
276 }
277 }
278 0 $min;
279 };
280  
281 =head1 AUTHOR
282  
283 Mecin, C<< <mecin at cpan.org> >>
284  
285 =head1 BUGS
286  
287 Please report any bugs or feature requests to C<bug-geo-geocalc at rt.cpan.org>, or through
288 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Geo-Geocalc>. I will be notified, and then you'll
289 automatically be notified of progress on your bug as I make changes.
290  
291  
292 =head1 SUPPORT
293  
294 You can find documentation for this module with the perldoc command.
295  
296 perldoc Geo::Geocalc
297  
298  
299 You can also look for information at:
300  
301 https://github.com/Mecin/geoQGen
302  
303 and
304  
305 https://github.com/Mecin/geoQGame
306  
307 or contact me directly.
308  
309  
310 =over 4
311  
312 =item * RT: CPAN's request tracker (report bugs here)
313  
314 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Geo-Geocalc>
315  
316 =item * AnnoCPAN: Annotated CPAN documentation
317  
318 L<http://annocpan.org/dist/Geo-Geocalc>
319  
320 =item * CPAN Ratings
321  
322 L<http://cpanratings.perl.org/d/Geo-Geocalc>
323  
324 =item * Search CPAN
325  
326 L<http://search.cpan.org/dist/Geo-Geocalc/>
327  
328 =back
329  
330  
331 =head1 ACKNOWLEDGEMENTS
332  
333  
334 =head1 LICENSE AND COPYRIGHT
335  
336 Copyright 2017 Mecin.
337  
338 This program is free software; you can redistribute it and/or modify it
339 under the terms of the the Artistic License (2.0). You may obtain a
340 copy of the full license at:
341  
342 L<http://www.perlfoundation.org/artistic_license_2_0>
343  
344 Any use, modification, and distribution of the Standard or Modified
345 Versions is governed by this Artistic License. By using, modifying or
346 distributing the Package, you accept this license. Do not use, modify,
347 or distribute the Package, if you do not accept this license.
348  
349 If your Modified Version has been derived from a Modified Version made
350 by someone other than you, you are nevertheless required to ensure that
351 your Modified Version complies with the requirements of this license.
352  
353 This license does not grant you the right to use any trademark, service
354 mark, tradename, or logo of the Copyright Holder.
355  
356 This license includes the non-exclusive, worldwide, free-of-charge
357 patent license to make, have made, use, offer to sell, sell, import and
358 otherwise transfer the Package with respect to any patent claims
359 licensable by the Copyright Holder that are necessarily infringed by the
360 Package. If you institute patent litigation (including a cross-claim or
361 counterclaim) against any party alleging that the Package constitutes
362 direct or contributory patent infringement, then this Artistic License
363 to you shall terminate on the date that such litigation is filed.
364  
365 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
366 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
367 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
368 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
369 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
370 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
371 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
372 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
373  
374  
375 =cut
376  
377 1 1; # End of Geo::Geocalc
↑ Summary