-
Notifications
You must be signed in to change notification settings - Fork 2
Add EXIF photo importer #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Shup04
wants to merge
7
commits into
master
Choose a base branch
from
exif-photo-importer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ed85eef
Add EXIF photo point importer
Shup04 78be349
Include EXIF photo metadata
Shup04 d66a20f
Expose imported photo to image handlers
Shup04 99108de
Handle photos with incomplete EXIF data
Shup04 a265b65
Support .ZIP imports
Shup04 4482103
Handle malformed JPEG imports
Shup04 e7147d0
Preserve EXIF photos throughout import
Shup04 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| require 'exifr/jpeg' | ||
| require 'ostruct' | ||
| require 'fileutils' | ||
|
|
||
| module SpatialFeatures | ||
| module Importers | ||
| class ExifPhoto < Base | ||
| JPEG_PATTERN = /\.jpe?g\z/i.freeze | ||
| NO_PHOTOS = "This archive doesn't contain any JPEG photos.".freeze | ||
| UNREADABLE_PHOTO = "This photo couldn't be read. It may be damaged, or saved in a JPEG format we don't support.".freeze | ||
|
|
||
| def self.create_all(data, **options) | ||
| tmpdir = options.fetch(:tmpdir) | ||
| photos_dir = ::File.join(tmpdir, 'exif_photos') | ||
| FileUtils.mkdir_p(photos_dir) | ||
|
|
||
| files = Download.open_each(data, unzip: JPEG_PATTERN, tmpdir: tmpdir) | ||
| files.map.with_index do |file, index| | ||
| filename = ::File.basename(file.path) | ||
|
|
||
| # Separate directories prevent two photos with the same filename colliding, | ||
| # while keeping the original filename for feature names and warnings. | ||
| staged_path = ::File.join(photos_dir, index.to_s, filename) | ||
| FileUtils.mkdir_p(::File.dirname(staged_path)) | ||
|
|
||
| begin | ||
| file.rewind | ||
| ::File.open(staged_path, 'wb') do |staged_file| | ||
| IO.copy_stream(file, staged_file) | ||
| end | ||
| ensure | ||
| file.close | ||
| end | ||
|
|
||
| new(staged_path, **options) | ||
| end | ||
| rescue Unzip::PathNotFound | ||
| raise ImportError, NO_PHOTOS | ||
| ensure | ||
| Array(files).each {|file| file.close unless file.closed? } | ||
| end | ||
|
|
||
| def initialize(data, **options) | ||
| options[:source_identifier] ||= ::File.basename(data.to_s) | ||
| super(data, **options) | ||
| end | ||
|
|
||
| def cache_key | ||
| @cache_key ||= Digest::MD5.file(@data).hexdigest | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def each_record | ||
| photo = EXIFR::JPEG.new(@data) | ||
| gps = photo.gps | ||
| unless usable_gps?(gps) | ||
| @warnings << 'No usable GPS coordinates were found in this photo.' | ||
| return | ||
| end | ||
|
|
||
| yield OpenStruct.new( | ||
| name: ::File.basename(@data), | ||
| geog: "POINT(#{gps.longitude} #{gps.latitude})", | ||
| metadata: metadata_from(photo, gps), | ||
| importable_image_paths: [@data] | ||
| ) | ||
| rescue EXIFR::MalformedImage | ||
| raise ImportError, UNREADABLE_PHOTO | ||
| end | ||
|
|
||
| def usable_gps?(gps) | ||
| gps && gps.latitude.is_a?(Numeric) && gps.longitude.is_a?(Numeric) && | ||
| (-90..90).cover?(gps.latitude) && (-180..180).cover?(gps.longitude) | ||
| end | ||
|
|
||
| def metadata_from(photo, gps) | ||
| { | ||
| 'capture_time' => photo.date_time_original&.strftime('%Y-%m-%d %H:%M:%S'), | ||
| 'altitude' => gps.altitude&.to_s, | ||
| 'camera_model' => photo.model.presence | ||
| }.compact | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,240 @@ | ||
| require 'spec_helper' | ||
|
|
||
| describe SpatialFeatures::Importers::ExifPhoto do | ||
| let(:photo_path) { fixture_file_path('bc25_bt_0030.JPG') } | ||
|
|
||
| subject(:importer) { described_class.new(photo_path) } | ||
|
|
||
| let(:features) { importer.features } | ||
|
|
||
| describe '#features' do | ||
| it 'imports one feature from a geotagged photo' do | ||
| expect(features.count).to eq(1) | ||
| end | ||
|
|
||
| it 'places the feature at the EXIF GPS coordinates' do | ||
| expect(features.first.geog).to eq( | ||
| 'POINT(-125.12249 50.36145)' | ||
| ) | ||
| end | ||
|
|
||
| it 'includes EXIF metadata' do | ||
| expect(features.first.metadata).to eq( | ||
| 'capture_time' => '2025-08-08 16:48:16', | ||
| 'altitude' => '109.4', | ||
| 'camera_model' => 'NIKON D7500' | ||
| ) | ||
| end | ||
|
|
||
| it 'makes the photo available for attachment importing' do | ||
| expect(features.first.importable_image_paths).to eq([photo_path]) | ||
| end | ||
| end | ||
|
|
||
| describe '#cache_key' do | ||
| it 'is based on the photo contents rather than its path' do | ||
| expect(importer.cache_key).to eq(Digest::MD5.file(photo_path).hexdigest) | ||
| end | ||
| end | ||
|
|
||
| context 'when the photo has no GPS coordinates' do | ||
| before do | ||
| allow(EXIFR::JPEG).to receive(:new).with(photo_path).and_return(double(gps: nil)) | ||
| end | ||
|
|
||
| it 'does not import a feature' do | ||
| expect(features).to be_empty | ||
| end | ||
|
|
||
| it 'records a warning that identifies the problem' do | ||
| features | ||
|
|
||
| expect(importer.warnings).to include(a_string_matching(/GPS coordinates/i)) | ||
| end | ||
|
|
||
| it 'identifies the source photo by filename' do | ||
| expect(importer.source_identifier).to eq('bc25_bt_0030.JPG') | ||
| end | ||
| end | ||
|
|
||
| context 'when optional EXIF metadata is absent' do | ||
| let(:gps) { double(latitude: 50.36145, longitude: -125.12249, altitude: nil) } | ||
| let(:photo) { double(gps: gps, date_time_original: nil, model: nil) } | ||
|
|
||
| before do | ||
| allow(EXIFR::JPEG).to receive(:new).with(photo_path).and_return(photo) | ||
| end | ||
|
|
||
| it 'imports the point without blank metadata values' do | ||
| expect(features.first.metadata).to eq({}) | ||
| end | ||
| end | ||
|
|
||
| context 'when the JPEG is malformed' do | ||
| before do | ||
| allow(EXIFR::JPEG).to receive(:new).with(photo_path).and_raise(EXIFR::MalformedJPEG) | ||
| end | ||
|
|
||
| it 'raises an import error with a useful message' do | ||
| expect { features } | ||
| .to raise_error(SpatialFeatures::ImportError, /photo couldn't be read/i) | ||
| end | ||
| end | ||
|
|
||
| describe '.create_all' do | ||
| let(:tmpdir) { Dir.mktmpdir } | ||
|
|
||
| after do | ||
| FileUtils.remove_entry(tmpdir) if Dir.exist?(tmpdir) | ||
| end | ||
|
|
||
| context 'with an individual JPEG' do | ||
| subject(:created_importer) do | ||
| described_class.create_all(photo_path, tmpdir: tmpdir).first | ||
| end | ||
|
|
||
| it 'creates one importer from a staged copy' do | ||
| staged_path = created_importer.features.first.importable_image_paths.first | ||
|
|
||
| expect(created_importer.features.count).to eq(1) | ||
| expect(staged_path).to eq(::File.join(tmpdir, 'exif_photos', '0', 'bc25_bt_0030.JPG')) | ||
| expect(staged_path).not_to eq(photo_path) | ||
| expect(::File.binread(staged_path)).to eq(::File.binread(photo_path)) | ||
| end | ||
|
|
||
| it 'keeps the original filename for the feature and source identifier' do | ||
| expect(created_importer.source_identifier).to eq('bc25_bt_0030.JPG') | ||
| expect(created_importer.features.first.name).to eq('bc25_bt_0030.JPG') | ||
| end | ||
| end | ||
|
|
||
| context 'when the source path is unlinked while its file is still open' do | ||
| it 'stages the JPEG from the open file descriptor and closes it' do | ||
| owner = Tempfile.new(['remote-photo', '.JPG']) | ||
| owner.binmode | ||
| owner.write(::File.binread(photo_path)) | ||
| owner.flush | ||
| open_file = ::File.open(owner.path, 'rb') | ||
| owner.close! | ||
|
|
||
| expect(::File.exist?(open_file.path)).to be(false) | ||
| allow(SpatialFeatures::Download).to receive(:open_each).and_return([open_file]) | ||
|
|
||
| created_importer = described_class.create_all('https://example.test/photo.JPG', tmpdir: tmpdir).first | ||
| staged_path = created_importer.features.first.importable_image_paths.first | ||
|
|
||
| expect(open_file).to be_closed | ||
| expect(created_importer.cache_key).to eq(Digest::MD5.file(photo_path).hexdigest) | ||
| expect(created_importer.features.count).to eq(1) | ||
| expect(::File.binread(staged_path)).to eq(::File.binread(photo_path)) | ||
| ensure | ||
| open_file&.close unless open_file&.closed? | ||
| owner&.close! | ||
| end | ||
| end | ||
|
|
||
| context 'when staging the photo fails' do | ||
| it 'still closes every source file' do | ||
| source_files = Array.new(2) { ::File.open(photo_path, 'rb') } | ||
| allow(SpatialFeatures::Download).to receive(:open_each).and_return(source_files) | ||
| allow(IO).to receive(:copy_stream).and_raise(IOError, 'copy failed') | ||
|
|
||
| expect do | ||
| described_class.create_all(photo_path, tmpdir: tmpdir) | ||
| end.to raise_error(IOError, 'copy failed') | ||
| expect(source_files).to all(be_closed) | ||
| ensure | ||
| source_files&.each {|file| file.close unless file.closed? } | ||
| end | ||
| end | ||
|
|
||
| context 'with an in-memory remote JPEG' do | ||
| it 'remains available after the download temporary object is collected' do | ||
| bytes = ::File.binread(photo_path) | ||
| allow(URI).to receive(:open).and_return(StringIO.new(bytes)) | ||
|
|
||
| created_importer = described_class.create_all('https://example.test/photo.JPG', tmpdir: tmpdir).first | ||
| GC.start | ||
| staged_path = created_importer.features.first.importable_image_paths.first | ||
|
|
||
| expect(created_importer.cache_key).to eq(Digest::MD5.hexdigest(bytes)) | ||
| expect(created_importer.features.count).to eq(1) | ||
| expect(::File.file?(staged_path)).to be(true) | ||
| expect(::File.binread(staged_path)).to eq(bytes) | ||
| end | ||
| end | ||
|
|
||
| context 'with a ZIP of JPEGs' do | ||
| subject(:importers) do | ||
| described_class.create_all(fixture_file_path('sample_photos.zip'), tmpdir: tmpdir) | ||
| end | ||
|
|
||
| it 'creates one importer per photo' do | ||
| expect(importers.count).to eq(5) | ||
| end | ||
|
|
||
| it 'imports one distinct point per photo' do | ||
| features = importers.flat_map(&:features) | ||
|
|
||
| expect(features.count).to eq(5) | ||
| expect(features.map(&:geog).uniq.count).to eq(5) | ||
| end | ||
|
|
||
| it 'preserves each photo filename' do | ||
| expect(importers.map(&:source_identifier)).to contain_exactly( | ||
| 'bc25_bt_0030.JPG', | ||
| 'bc25_bt_0031.JPG', | ||
| 'bc25_bt_0032.JPG', | ||
| 'bc25_bt_0033.JPG', | ||
| 'bc25_bt_0034.JPG' | ||
| ) | ||
| end | ||
|
|
||
| it 'keeps every extracted photo available to image handlers' do | ||
| image_paths = importers.flat_map(&:features).flat_map(&:importable_image_paths) | ||
|
|
||
| expect(image_paths.count).to eq(5) | ||
| expect(image_paths.all? {|path| ::File.file?(path) }).to be(true) | ||
| end | ||
|
|
||
| it 'stages every photo below the managed temporary directory' do | ||
| image_paths = importers.flat_map(&:features).flat_map(&:importable_image_paths) | ||
|
|
||
| expect(image_paths).to all(start_with("#{tmpdir}/exif_photos/")) | ||
| end | ||
| end | ||
|
|
||
| context 'with duplicate filenames in different ZIP directories' do | ||
| let(:archive_path) do | ||
| ::File.join(tmpdir, 'duplicate_names.zip').tap do |path| | ||
| bytes = ::File.binread(photo_path) | ||
| Zip::OutputStream.open(path) do |zip| | ||
| zip.put_next_entry('first/repeated.JPG') | ||
| zip.write(bytes) | ||
| zip.put_next_entry('second/repeated.JPG') | ||
| zip.write(bytes) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| it 'keeps both photos without changing their displayed filename' do | ||
| importers = described_class.create_all(archive_path, tmpdir: tmpdir) | ||
| image_paths = importers.flat_map(&:features).flat_map(&:importable_image_paths) | ||
|
|
||
| expect(importers.map(&:source_identifier)).to eq(['repeated.JPG', 'repeated.JPG']) | ||
| expect(importers.flat_map(&:features).map(&:name)).to eq(['repeated.JPG', 'repeated.JPG']) | ||
| expect(image_paths.map {|path| ::File.basename(path) }).to eq(['repeated.JPG', 'repeated.JPG']) | ||
| expect(image_paths.uniq.count).to eq(2) | ||
| expect(image_paths.map {|path| ::File.binread(path) }).to all(eq(::File.binread(photo_path))) | ||
| end | ||
| end | ||
|
|
||
| context 'with a ZIP containing no JPEGs' do | ||
| it 'reports that the archive has no photos' do | ||
| expect do | ||
| described_class.create_all(fixture_file_path('archive_without_any_known_file.zip'), tmpdir: tmpdir) | ||
| end.to raise_error(SpatialFeatures::ImportError, /JPEG photos/i) | ||
| end | ||
| end | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I checked EXIFR, and tested with a negative altitude image and it works fine