How to calculate the bounding box for a given lat/lng location?
Date : March 29 2020, 07:55 AM
Hope that helps I suggest to approximate locally the Earth surface as a sphere with radius given by the WGS84 ellipsoid at the given latitude. I suspect that the exact computation of latMin and latMax would require elliptic functions and would not yield an appreciable increase in accuracy (WGS84 is itself an approximation). My implementation follows (It's written in Python; I have not tested it): # degrees to radians
def deg2rad(degrees):
return math.pi*degrees/180.0
# radians to degrees
def rad2deg(radians):
return 180.0*radians/math.pi
# Semi-axes of WGS-84 geoidal reference
WGS84_a = 6378137.0 # Major semiaxis [m]
WGS84_b = 6356752.3 # Minor semiaxis [m]
# Earth radius at a given latitude, according to the WGS-84 ellipsoid [m]
def WGS84EarthRadius(lat):
# http://en.wikipedia.org/wiki/Earth_radius
An = WGS84_a*WGS84_a * math.cos(lat)
Bn = WGS84_b*WGS84_b * math.sin(lat)
Ad = WGS84_a * math.cos(lat)
Bd = WGS84_b * math.sin(lat)
return math.sqrt( (An*An + Bn*Bn)/(Ad*Ad + Bd*Bd) )
# Bounding box surrounding the point at given coordinates,
# assuming local approximation of Earth surface as a sphere
# of radius given by WGS84
def boundingBox(latitudeInDegrees, longitudeInDegrees, halfSideInKm):
lat = deg2rad(latitudeInDegrees)
lon = deg2rad(longitudeInDegrees)
halfSide = 1000*halfSideInKm
# Radius of Earth at given latitude
radius = WGS84EarthRadius(lat)
# Radius of the parallel at given latitude
pradius = radius*math.cos(lat)
latMin = lat - halfSide/radius
latMax = lat + halfSide/radius
lonMin = lon - halfSide/pradius
lonMax = lon + halfSide/pradius
return (rad2deg(latMin), rad2deg(lonMin), rad2deg(latMax), rad2deg(lonMax))
def dps2deg(degrees, primes, seconds):
return degrees + primes/60.0 + seconds/3600.0
def deg2dps(degrees):
intdeg = math.floor(degrees)
primes = (degrees - intdeg)*60.0
intpri = math.floor(primes)
seconds = (primes - intpri)*60.0
intsec = round(seconds)
return (int(intdeg), int(intpri), int(intsec))
|
Calculate bounding box for OpenStreetMap
Date : March 29 2020, 07:55 AM
wish of those help Yes, it is. I've had a similar problem, so I've written it up. Like this: use Math::Trig;
sub getTileNumber {
my ($lat,$lon,$zoom) = @_;
my $xtile = int( ($lon+180)/360 * 2**$zoom ) ;
my $ytile = int( (1 - log(tan(deg2rad($lat)) + sec(deg2rad($lat)))/pi)/2 * 2**$zoom ) ;
return ($xtile, $ytile);
}
sub getLonLat {
my ($xtile, $ytile, $zoom) = @_;
my $n = 2 ** $zoom;
my $lon_deg = $xtile / $n * 360.0 - 180.0;
my $lat_deg = rad2deg(atan(sinh(pi * (1 - 2 * $ytile / $n))));
return ($lon_deg, $lat_deg);
}
# convert from permalink OSM format like:
# http://www.openstreetmap.org/?lat=43.731049999999996&lon=15.79375&zoom=13&layers=M
# to OSM "Export" iframe embedded bbox format like:
# http://www.openstreetmap.org/export/embed.html?bbox=15.7444,43.708,15.8431,43.7541&layer=mapnik
sub LonLat_to_bbox {
my ($lat, $lon, $zoom) = @_;
my $width = 425; my $height = 350; # note: must modify this to match your embed map width/height in pixels
my $tile_size = 256;
my ($xtile, $ytile) = getTileNumber ($lat, $lon, $zoom);
my $xtile_s = ($xtile * $tile_size - $width/2) / $tile_size;
my $ytile_s = ($ytile * $tile_size - $height/2) / $tile_size;
my $xtile_e = ($xtile * $tile_size + $width/2) / $tile_size;
my $ytile_e = ($ytile * $tile_size + $height/2) / $tile_size;
my ($lon_s, $lat_s) = getLonLat($xtile_s, $ytile_s, $zoom);
my ($lon_e, $lat_e) = getLonLat($xtile_e, $ytile_e, $zoom);
my $bbox = "$lon_s,$lat_s,$lon_e,$lat_e";
return $bbox;
}
|
How do I calculate a bounding polygon?
Date : March 29 2020, 07:55 AM
|
How to calculate bounding box area?
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further The first two are the left most x and y coordinates respectively. The next two values are the lengths in the x and y direction respectively (starting from the x and y coordinates). So for area you need to multiply the last two values (e.g., 287*165). For more information see the documentation for regionprops('BoundingBox').
|
How to calculate the center of the bounding box?
Date : March 29 2020, 07:55 AM
|