Net-API-Nominatim

 view release on metacpan or  search on metacpan

lib/Net/API/Nominatim/Model/BoundingBox.pm  view on Meta::CPAN

package Net::API::Nominatim::Model::BoundingBox;

use strict;
use warnings;

our $VERSION = '0.03';

use Data::Roundtrip qw/json2perl no-unicode-escape-permanently/;

# export nothing otherwise we need to adjust our sub names
# to avoid clashes, e.g. fromHash, use these like
#   Net::API::Nominatim::Model::BoundingBox::fromHash()

#use Exporter;
#our (@EXPORT_OK, %EXPORT_TAGS);
#BEGIN {
#	@EXPORT_OK = qw/ fromHash fromArrayOfArrays fromArray fromJSONArray fromRandom /;
#	%EXPORT_TAGS = ( all => [@EXPORT_OK] );
#}

### Constructor, all fields are initialised to 0.0
### (whereas in Address.pm they are initialised to '', even lat/lon)
sub new {
	my ($class, $params) = @_;

	my $self = {
		lat1 => 0.0,
		lat2 => 0.0,
		lon1 => 0.0,
		lon2 => 0.0,
	};
	bless $self => $class;
	return $self unless defined $params;

	if( ref($params)eq'HASH' ){
		fromHash($params, $self);
	} elsif( ref($params)eq'' ){
		if( ! defined fromJSONArray($params, $self) ){ print STDERR __PACKAGE__."->new(), line ".__LINE__." : error, input JSON string was malformed, failed.\n"; return undef }
	} elsif( ref($params) eq __PACKAGE__ ){
		fromArray($params->toArray(), $self);
	} elsif( ref($params)eq'ARRAY' && scalar(@$params) && ref($params->[0])eq'ARRAY'){
		fromArrayOfArrays($params, $self);
	} elsif( ref($params)eq'ARRAY' && scalar(@$params) && ref($params->[0])eq''){
		fromArray($params, $self);
	}
	return $self;
}

###########################################
# getters and setters at the same time
#
sub fields { return sort keys %{$_[0]} }
sub lat1 { return $_[1] ? $_[0]->{lat1} = $_[1] : $_[0]->{lat1} }
sub lon1 { return $_[1] ? $_[0]->{lon1} = $_[1] : $_[0]->{lon1} }
sub lat2 { return $_[1] ? $_[0]->{lat2} = $_[1] : $_[0]->{lat2} }
sub lon2 { return $_[1] ? $_[0]->{lon2} = $_[1] : $_[0]->{lon2} }

# randomise all fields of CURRENT object to random
# numbers specific for lat/lon, 
# (see randomLat(), randomLon() on how this is done).
# By default empty and undef fields will not be randomised.
# Unless optional 2nd parameter is set to 1. Default is 0.
# It will/can also randomise the boundingbox object.
sub randomise { 
	for ('lat1', 'lat2'){
		$_[0]->{$_} = randomLat();
	}
	for ('lon1', 'lon2'){
		$_[0]->{$_} = randomLon();
	}
}

# Clone current object and return the new one.
# It can return undef on failure.
sub clone { return Net::API::Nominatim::Model::BoundingBox->new($_[0]->toArray()) }

# It checks equality between our current object and the
# second object passed as the input parameter.
# It returns 1 if equal, 0 if not,
# it first checks if the types of objects are the same.
sub equals {
	my ($x, $y) = @_;
	return 0 unless ref($x) eq ref($y); # not same object type
	return 1 if "$x" eq "$y"; # same pointer

	for(keys %$x){
		return 0 unless $x->{$_} == $y->{$_};
	}
	return 1 # equal
}

# return a 1D array adhering to the Nominatim notation: [lat1,lat2,lon1,lon2]



( run in 0.424 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )