[RUBY] Get location information quickly

I usually develop with Rails. At that time, I was addicted to an unexpected part in acquiring location information, so is it a memorandum of the created code? Heads up? I will leave it as. If you want to see only the situation where GPS information cannot be obtained, skip to GPS could not be obtained.

Immediately, I will try to confirm the location information in the terminal. You can also copy and paste the code.

environment: Ruby2.6.6 ImageMagick 7.0.10-34

Create a Ruby file

This time I created a main.rb file in a directory called gps_test. Then write the following code.

main.rb


require 'rmagick'

img = Magick::ImageList.new(File.expand_path('~/Downloads/Image data name'))
p img
p img.get_exif_by_entry('GPSLatitude')

#Latitude: Convert to decimal number and output
exif_lat = img.get_exif_by_entry('GPSLatitude')[0][1].split(',').map(&:strip)
latitude = (Rational(exif_lat[0]) + Rational(exif_lat[1])/60 + Rational(exif_lat[2])/3600).to_f
p latitude

#Longitude: Convert to decimal number and output
exif_lng = img.get_exif_by_entry('GPSLongitude')[0][1].split(',').map(&:strip)
longitude = (Rational(exif_lng[0]) + Rational(exif_lng[1])/60 + Rational(exif_lng[2])/3600).to_f
p longitude

Enter the image file you want to read (IMG_8765.jpg, etc.) in the image data name.

If you do Magick :: ImageList.new (File.expand_path ('~ / Desktop / image data name')), it will be read from Desktop. (As you see it ..)

In my case, the read permission of the download directory was not set, so I loaded it as Desktop.

Install RMagick

Execute the following from the terminal in the project

$ gem install rmagick

At this time, if an error occurs, this will be helpful. Installation failed with gem install rmagick Cannot install with bundle install "rmagick"

Run a Ruby program

$ ruby main.rb

Execution result

ruby main.rb
[/Users/tn/Desktop/Akihabara.jpeg JPEG 1200x1600 1200x1600+0+0 DirectClass 8-bit 535kb]
scene=0
[["GPSLatitude", "35/1, 41/1, 5920/100"]]
35.699777777777776
139.7717

Now you have the latitude and longitude of GPS!

GPS could not be obtained

To conclude, ** I couldn't get GPS because it was an HDR image when I took it with my iPhone. ** That means.

There is a function called "Auto HDR" in the iPhone settings.

Automatic HDR: A function that allows multiple photos to be taken continuously at high speed with various exposures and combined with a photo that clearly expresses bright areas and shadow details.

If you have an iPhone terminal, you can see the map by setting it with location information = GPS will also be acquired I thought, but I couldn't get it with the contents described at the beginning. (If you know how to get it, please let me know ...)

The program shouldn't be wrong, so why can't I get it .. You may want to make sure that automatic HDR is turned off. I wonder how many hours I wasted with this .. crying Turn off iPhone automatic HDR

I was doing a simple check from the preview to see if the location information was included. [Mac] How to quickly check the location information (geotag) of an image

** However, why can't GPS be read even though the location information is set? I tried to verify if there is a setting pattern. ** **

Pattern verification (location information setting is ON)

Pattern 1 both ON Turn on "automatic HDR (synthesis function)" Result: Turn on "Leave normal photo" → GPS cannot be read

Pattern 2 one side Turn on "automatic HDR (synthesis function)" Result: "Leave a normal photo" is turned off → GPS cannot be read

Pattern 3 one side "Automatic HDR (synthesis function)" is turned off. Result: "Leave a normal photo" is turned off → GPS cannot be read

Pattern 4 State of reference image "Automatic HDR (synthesis function)" is turned off. Result: Turn on "Leave normal photo" ** → GPS can be acquired **

reference image: スクリーンショット 2020-10-29 15.52.07.png

Summary

It's self-taught and inefficient, but this is another experience! !! I think I have grown a little this time as well, and I will continue to take on the challenge of development. It was a basic content, but I hope you can refer to the acquisition method and precautions.

reference

Use R Magick to convert image Exif location information to decimal value

Recommended Posts

Get location information quickly
Get Android location information
Get location information in Rails and sort in ascending order
Get EXIF information in Java
[Ethereum] Get block information using web3j
Acquisition of location information using FusedLocationProviderClient
Get caller information from stack trace (java)
[Java] Get tag information from music files
Get account information by accessing Strava's API