Create Canon DSLR CR2 image statistics (exiftool & GNU Plot) and convert ML dual ISO CR2 files painlessly for post-processing
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
3.4 KiB

5 years ago
  1. #!/bin/bash
  2. # Geotag images
  3. # Copyright (C) 2017 Pekka Helenius
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. #
  19. ###############################################
  20. # TODO Coordinate threshold = Grid size is 30x30m?
  21. # TODO Find API for ASTER DEM v2 (better data)
  22. USERNAME=Fincer #For API (data request)
  23. ############################
  24. INTERNET_TEST=$(ping -c 3 nominatim.openstreetmap.org 2>&1 | grep -c "unknown host") #Ping Github three times and look for string 'unknown host'
  25. if [[ ! $INTERNET_TEST -eq 0 ]]; then #If 'unknown host' string is found, then
  26. echo -e "\nCan't connect to geoservice provider (Nominatim). Please check your internet connection and try again.\n"
  27. exit 1
  28. fi
  29. ############################
  30. for IMAGE in $(echo "${@}"); do
  31. IMAGE_BASENAME=$(basename "${IMAGE}" | cut -f 1 -d '.')
  32. IMAGE_EXTENSION=$(echo $(basename "${IMAGE}" | cut -f 2 -d '.' | sed '/^\s*$/d'))
  33. if [[ $(exiftool -n -p '$GPSLatitude,$GPSLongitude' "${IMAGE}" | awk -F ',' '{print NF}') != 2 ]]; then
  34. echo -e "$IMAGE_BASENAME: Missing coordinates. Are you sure you have geotagged the photo?\n"
  35. exit
  36. else
  37. LATITUDE=$(exiftool -n -p '$GPSLatitude' "${IMAGE}")
  38. LONGITUDE=$(exiftool -n -p '$GPSLongitude' "${IMAGE}")
  39. # Clear previous geonames information
  40. exiftool -Location= -LocationShownCity= -LocationShownCountryName= -LocationShownProvinceState= -LocationShownSublocation= -Country-PrimaryLocationName= -Sub-location= -Country= -City= -State= -Province-State= -GPSAltitude= "${IMAGE}" -overwrite_original
  41. #if [[ $(exiftool -p '$Country-PrimaryLocationName' "${IMAGE}" | awk '{print NF}') != 1 ]]; then
  42. echo -e "$IMAGE_BASENAME: retrieving country information.\n"
  43. reversegeo "${IMAGE}"
  44. #fi
  45. # Reference: http://www.geonames.org/export/web-services.html
  46. # Get elevation by retrieving a ASTER DEM value from GeoNames API server, grid size 30x30m
  47. # There are several error factors:
  48. # DEM Grid size
  49. # General inaccurancies in DEM model
  50. # Geoid model & projection errors
  51. # Coordinate errors
  52. # Variations in (estimated) height values
  53. # So the retrieved elevation value is just a rough estimation here
  54. ALTITUDE=$(curl -s "http://api.geonames.org/astergdem?lat=$LATITUDE&lng=$LONGITUDE&username=$USERNAME")
  55. # IF successful answer, then
  56. if [[ $ALTITUDE =~ '^[0-9]+$' ]]; then
  57. exiftool -GPSAltitude=$ALTITUDE "${IMAGE}" -overwrite_original
  58. echo -e "$IMAGE_BASENAME: Altitude value updated.\n"
  59. else
  60. # TODO IF NOT successful, try again for 2 times, then abort
  61. echo -e "$IMAGE_BASENAME: Couldn't retrieve altitude value.\n"
  62. done