| 1 | #!/usr/local/bin/perl | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Getopt::Long qw{GetOptions}; | ||
| 5 | use Geo::H3 0.04; | ||
| 6 | require #hide from rpmbuild | ||
| 7 | Geo::GoogleEarth::Pluggable; | ||
| 8 | require #hide from rpmbuild | ||
| 9 | Geo::GoogleEarth::Pluggable::Plugin::Styles; | ||
| 10 | require #hide from rpmbuild | ||
| 11 | Path::Class; | ||
| 12 | |||
| 13 | my $lat = 38.889480654699476; | ||
| 14 | my $lon = -77.03523875953579; | ||
| 15 | my $resoultion = 8; | ||
| 16 | my $output = "output.kmz"; | ||
| 17 | GetOptions( | ||
| 18 | 'lat=s' => \$lat, | ||
| 19 | 'lon=s' => \$lon, | ||
| 20 | 'resolution|res|r=s' => \$resoultion, | ||
| 21 | 'output|out=s' => \$output, | ||
| 22 | ); | ||
| 23 | |||
| 24 | my $format = $output =~ m/\.kml\Z/i ? "kml" : "kmz"; | ||
| 25 | |||
| 26 | my $gh3 = Geo::H3->new or die("Error: Geo::H3 object could not be constructed"); | ||
| 27 | my $geo = $gh3->geo(lat=>$lat, lon=>$lon) or die("Error: Geo object could not be constructed"); | ||
| 28 | my $h3 = $geo->h3($resoultion) or die("Error: H3 object could not be constructed"); | ||
| 29 | my $center = $h3->geo or die("Error: Centroid object could not be constructed"); | ||
| 30 | |||
| 31 | printf "Lat: %s\n", $lat; | ||
| 32 | printf "Lon: %s\n", $lon; | ||
| 33 | printf "Resolution: %s\n", $resoultion; | ||
| 34 | printf "Output: %s\n", $output; | ||
| 35 | printf "Format: %s\n", $format; | ||
| 36 | |||
| 37 | my $document = Geo::GoogleEarth::Pluggable->new(name=>"Geo::H3") or die("Error: Geo::GoogleEarth::Pluggable object could not be constructed"); | ||
| 38 | my $style_index = $document->Style(PolyStyle=>$document->AreaStyleBlue(alpha=>"20%") , LineStyle=>$document->LineStyleBlue(width=>4)); | ||
| 39 | my $style_child = $document->Style(PolyStyle=>$document->AreaStyleBlack(alpha=>"20%"), LineStyle=>$document->LineStyleBlack(width=>2)); | ||
| 40 | my $style_parent = $document->Style(PolyStyle=>$document->AreaStyleWhite(alpha=>"20%"), LineStyle=>$document->LineStyleWhite(width=>2)); | ||
| 41 | my $style_hex_ring = $document->Style(PolyStyle=>$document->AreaStyleGreen(alpha=>"20%"), LineStyle=>$document->LineStyleGreen(width=>2)); | ||
| 42 | $document->Point(name=>"Input" , lat=>$lat, lon=>$lon); | ||
| 43 | $document->Point(name=>"Center", lat=>$center->lat, lon=>$center->lon); | ||
| 44 | |||
| 45 | if ($h3->resolution > 0) { | ||
| 46 | my $folder = $document->Folder(name=>"Parent"); | ||
| 47 | my $parent = $h3->parent; | ||
| 48 | $folder->LinearRing(name=>$parent->string, coordinates=>$parent->geoBoundary->coordinates, style=>$style_parent); | ||
| 49 | } | ||
| 50 | |||
| 51 | { | ||
| 52 | my $folder = $document->Folder(name=>"Hex Ring"); | ||
| 53 | foreach my $hex (@{$h3->hexRing}) { | ||
| 54 | $folder->LinearRing(name=>$hex->string, coordinates=>$hex->geoBoundary->coordinates, style=>$style_hex_ring); | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | { | ||
| 59 | my $folder = $document->Folder(name=>"Children"); | ||
| 60 | foreach my $child (@{$h3->children}) { | ||
| 61 | $folder->LinearRing(name=>$child->string, coordinates=>$child->geoBoundary->coordinates, style=>$style_child); | ||
| 62 | } | ||
| 63 | } | ||
| 64 | |||
| 65 | $document->LinearRing(name=>$h3->string, coordinates=>$h3->geoBoundary->coordinates, style=>$style_index); | ||
| 66 | |||
| 67 | my $outfile = Path::Class::file($output); | ||
| 68 | $outfile->spew($format eq "kml" ? $document->render : $document->archive); | ||
| 69 | |||
| 70 | __END__ |