Bounding Box aus Geokoordinaten

Begonnen von Prof. Dr. Peter Henning, 12 November 2026, 11:04:45

Vorheriges Thema - Nächstes Thema

Prof. Dr. Peter Henning

Mit dem nachfolgenden Code werden aus der Angabe einer geografischen Länge und Breite sowie eines Radius in km der linke untere und der rechte obere Eckpunkt der umschreibenden Bounding Box bestimmt und im Device $dev als Readings area_bottomLeft_latitude, area_bottomLeft_longitude, area_topRight_latitude und area_topRight_longitude gesetzt.

sub boundingBox($$$$) {
  my ($dev,$lon,$lat,$rad) = @_;

  return if !defined($defs{$dev});

  my $pi = 4 * atan2(1,1);
  my $earthRadius = 6371.0;  # km

  #-- Radius in Winkelabstände umrechnen
  my $dlat = ($rad / $earthRadius) * 180 / $pi;
  my $dlon = $dlat / cos($lat * $pi / 180);

  #-- Eckkoordinaten
  my $bottomLat = $lat - $dlat;
  my $topLat    = $lat + $dlat;
  my $leftLon   = $lon - $dlon;
  my $rightLon  = $lon + $dlon;

  #-- Readings setzen
  my $hash = $defs{$dev};

  readingsBeginUpdate($hash);
  readingsBulkUpdate($hash,"area_bottomLeft_latitude", sprintf("%.6f",$bottomLat));
  readingsBulkUpdate($hash,"area_bottomLeft_longitude",sprintf("%.6f",$leftLon));
  readingsBulkUpdate($hash,"area_topRight_latitude",   sprintf("%.6f",$topLat));
  readingsBulkUpdate($hash,"area_topRight_longitude",  sprintf("%.6f",$rightLon));
  readingsEndUpdate($hash,1);

  return;
}

LG

pah