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.

102 lines
3.4 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. #!/bin/env bash
  2. # Geotag images
  3. # Copyright (C) 2017,2023 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 (provides better data)
  22. # Username for REST API data request
  23. USERNAME=<PUT_YOUR_USERNAME_HERE>
  24. ############################
  25. # Ping openstreetmap.org three times.
  26. PING_ADDRESS="nominatim.openstreetmap.org"
  27. INTERNET_TEST=$(ping -c 3 "${PING_ADDRESS}" 2>&1 | grep -c "unknown host")
  28. if [[ ! "${INTERNET_TEST}" -eq 0 ]]
  29. then
  30. echo -e "\nCan't connect to the Geo Service provider '${PING_ADDRESS}'. Please check your internet connection and try again.\n"
  31. exit 1
  32. fi
  33. ############################
  34. for IMAGE in $(echo "${@}")
  35. do
  36. IMAGE_BASENAME=$(basename "${IMAGE}" | cut -f 1 -d '.')
  37. IMAGE_EXTENSION=$(echo $(basename "${IMAGE}" | cut -f 2 -d '.' | sed '/^\s*$/d'))
  38. if [[ $(exiftool -n -p '$GPSLatitude,$GPSLongitude' "${IMAGE}" | awk -F ',' '{print NF}') != 2 ]]
  39. then
  40. echo -e "$IMAGE_BASENAME: Missing coordinates. Are you sure you have geotagged the photo?\n"
  41. exit 0
  42. else
  43. LATITUDE=$(exiftool -n -p '$GPSLatitude' "${IMAGE}")
  44. LONGITUDE=$(exiftool -n -p '$GPSLongitude' "${IMAGE}")
  45. # Clear previous geonames information.
  46. exiftool \
  47. -Location= \
  48. -LocationShownCity= \
  49. -LocationShownCountryName= \
  50. -LocationShownProvinceState= \
  51. -LocationShownSublocation= \
  52. -Country-PrimaryLocationName= \
  53. -Sub-location= \
  54. -Country= \
  55. -City= \
  56. -State= \
  57. -Province-State= \
  58. -GPSAltitude= \
  59. "${IMAGE}" \
  60. -overwrite_original
  61. echo -e "$IMAGE_BASENAME: retrieving country information.\n"
  62. reversegeo "${IMAGE}"
  63. # Reference: http://www.geonames.org/export/web-services.html
  64. # Get elevation by retrieving a ASTER DEM value from GeoNames API server, grid size 30x30m
  65. # There are several error factors:
  66. # DEM Grid size
  67. # General inaccurancies in DEM model
  68. # Geoid model & projection errors
  69. # Coordinate errors
  70. # Variations in (estimated) height values
  71. # So the retrieved elevation value is just a rough estimation here.
  72. ALTITUDE=$(curl -s "http://api.geonames.org/astergdem?lat=$LATITUDE&lng=$LONGITUDE&username=$USERNAME")
  73. # If we have a successful answer, then
  74. if [[ "${ALTITUDE}" =~ '^[0-9]+$' ]]
  75. then
  76. exiftool -GPSAltitude="${ALTITUDE}" "${IMAGE}" -overwrite_original
  77. echo -e "${IMAGE_BASENAME}: Altitude value updated.\n"
  78. else
  79. # TODO: If not successful, try again for 2 times, then abort.
  80. echo -e "${IMAGE_BASENAME}: Couldn't retrieve altitude value.\n"
  81. fi
  82. fi
  83. done