| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | our $VERSION = '0.03'; | ||
| 4 | package Geo::GoogleMaps::OffsetCenter; | ||
| 5 | # ABSTRACT: Offset a Lat/Long in Google Maps | ||
| 6 | |||
| 7 | |||
| 8 | use Params::Validate; | ||
| 9 | use Math::Trig qw/ pi /; | ||
| 10 | use Regexp::Common; | ||
| 11 | use Exporter::Easy ( | ||
| 12 | OK => [ qw/ offset_center_by_occlusion offset_center_by_pixel / ], | ||
| 13 | ); | ||
| 14 | |||
| 15 | use constant RADIUS_OF_EARTH => 6_378_100; | ||
| 16 | |||
| 17 | |||
| 18 | sub offset_center_by_occlusion { | ||
| 19 | validate_pos( | ||
| 20 | @_, | ||
| 21 | { regex => qr/$RE{num}{real}/, optional => 0 }, # latitude | ||
| 22 | { regex => qr/$RE{num}{real}/, optional => 0 }, # longitude | ||
| 23 | { regex => qr/$RE{num}{int}/, optional => 0 }, # width | ||
| 24 | { regex => qr/$RE{num}{int}/, optional => 0 }, # height | ||
| 25 | { regex => qr/$RE{num}{int}/, optional => 0 }, # zoom level | ||
| 26 | { regex => qr/$RE{num}{int}/, optional => 0 }, # occlusion | ||
| 27 | ); | ||
| 28 | |||
| 29 | my( | ||
| 30 | $latitude_geo_entity, | ||
| 31 | $longitude_geo_entity, | ||
| 32 | $width_total, | ||
| 33 | $height_total, | ||
| 34 | $zoom_level, | ||
| 35 | $width_occlusion_from_left | ||
| 36 | ) = @_; | ||
| 37 | |||
| 38 | # we will need these | ||
| 39 | my $number_of_pixels = 256 * 2**$zoom_level; | ||
| 40 | my $meters_per_pixel = ( 2 * pi * RADIUS_OF_EARTH ) / $number_of_pixels; | ||
| 41 | my $meters_per_degree = ( 2 * pi * RADIUS_OF_EARTH ) / 360; | ||
| 42 | |||
| 43 | # find the number of pixels we need to move the center | ||
| 44 | my $pixels_offset = _get_pixels_offset( $width_total, $height_total, $width_occlusion_from_left ); | ||
| 45 | |||
| 46 | # find the number of meters we need to move | ||
| 47 | my $meters_offset = $pixels_offset * $meters_per_pixel; | ||
| 48 | |||
| 49 | # now find the number of degrees we need to move | ||
| 50 | my $degrees_offset = $meters_offset / $meters_per_degree; | ||
| 51 | |||
| 52 | $longitude_geo_entity -= $degrees_offset; | ||
| 53 | |||
| 54 | return { | ||
| 55 | latitude => $latitude_geo_entity, | ||
| 56 | longitude => $longitude_geo_entity | ||
| 57 | }; | ||
| 58 | } | ||
| 59 | |||
| 60 | |||
| 61 | sub offset_center_by_pixel { | ||
| 62 | validate_pos( | ||
| 63 | @_, | ||
| 64 | { regex => qr/$RE{num}{real}/, optional => 0 }, # latitude | ||
| 65 | { regex => qr/$RE{num}{real}/, optional => 0 }, # longitude | ||
| 66 | { regex => qr/$RE{num}{int}/, optional => 0 }, # width of the whole image | ||
| 67 | { regex => qr/$RE{num}{int}/, optional => 0 }, # height of the whole image | ||
| 68 | { regex => qr/$RE{num}{int}/, optional => 0 }, # x coordinate of where it should be; x_final | ||
| 69 | { regex => qr/$RE{num}{int}/, optional => 0 }, # y coordinate of where it should be; y_final | ||
| 70 | { regex => qr/$RE{num}{int}/, optional => 0 }, # zoom level | ||
| 71 | ); | ||
| 72 | |||
| 73 | my( | ||
| 74 | $latitude_geo_entity, | ||
| 75 | $longitude_geo_entity, | ||
| 76 | $width_total, | ||
| 77 | $height_total, | ||
| 78 | $x_final, | ||
| 79 | $y_final, | ||
| 80 | $zoom_level, | ||
| 81 | ) = @_; | ||
| 82 | |||
| 83 | # important for both vertical and horizontal offset | ||
| 84 | my $number_of_pixels = 256 * 2**$zoom_level; | ||
| 85 | my $x_initial = int( $width_total / 2 ); | ||
| 86 | my $y_initial = int( $height_total / 2 ); | ||
| 87 | |||
| 88 | # important for vertical offset | ||
| 89 | my $meters_per_pixel_vertical = ( pi * RADIUS_OF_EARTH ) / $number_of_pixels; # pi * r = length of longitude | ||
| 90 | my $meters_per_degree_vertical = ( pi * RADIUS_OF_EARTH ) / 180; | ||
| 91 | |||
| 92 | # important for horizontal offset | ||
| 93 | my $meters_per_pixel_horizontal = ( 2 * pi * RADIUS_OF_EARTH ) / $number_of_pixels; | ||
| 94 | my $meters_per_degree_horizontal = ( 2 * pi * RADIUS_OF_EARTH ) / 360; | ||
| 95 | |||
| 96 | ##################### | ||
| 97 | # horizontal offset # | ||
| 98 | ##################### | ||
| 99 | |||
| 100 | my $horizontal_offset_in_degrees; | ||
| 101 | { | ||
| 102 | my $pixels_offset = -1 * ($x_final - $x_initial); | ||
| 103 | |||
| 104 | # find the number of meters we need to move | ||
| 105 | my $meters_offset = $pixels_offset * $meters_per_pixel_horizontal; | ||
| 106 | |||
| 107 | # now find the number of degrees we need to move | ||
| 108 | $horizontal_offset_in_degrees = $meters_offset / $meters_per_degree_horizontal; | ||
| 109 | } | ||
| 110 | |||
| 111 | ################### | ||
| 112 | # vertical offset # | ||
| 113 | ################### | ||
| 114 | |||
| 115 | my $vertical_offset_in_degrees; | ||
| 116 | { | ||
| 117 | my $pixels_offset = -1 * ($y_final - $y_initial); | ||
| 118 | |||
| 119 | # find the number of meters we need to move | ||
| 120 | my $meters_offset = $pixels_offset * $meters_per_pixel_vertical; | ||
| 121 | |||
| 122 | # now find the number of degrees we need to move | ||
| 123 | $vertical_offset_in_degrees = $meters_offset / $meters_per_degree_vertical; | ||
| 124 | } | ||
| 125 | |||
| 126 | $latitude_geo_entity += $vertical_offset_in_degrees; | ||
| 127 | $longitude_geo_entity += $horizontal_offset_in_degrees; | ||
| 128 | |||
| 129 | return { | ||
| 130 | latitude => $latitude_geo_entity, | ||
| 131 | longitude => $longitude_geo_entity | ||
| 132 | }; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub _get_pixels_offset { | ||
| 136 | my( $width_total, $height_total, $width_occlusion_from_left ) = @_; | ||
| 137 | |||
| 138 | # actually we don't care about the height, heh. | ||
| 139 | |||
| 140 | my $current_center = int( $width_total / 2 ); | ||
| 141 | my $center_of_effective_area = int( $width_total - $width_occlusion_from_left ) / 2; | ||
| 142 | |||
| 143 | return abs( $current_center - $center_of_effective_area ); | ||
| 144 | } | ||
| 145 | |||
| 146 | 1; | ||
| 147 | |||
| 148 | __END__ |