CCExifGeo.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // CCExifGeo.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 03/02/16.
  6. // Copyright (c) 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "CCExifGeo.h"
  24. #import "NCBridgeSwift.h"
  25. @implementation CCExifGeo
  26. + (CCExifGeo *)sharedInstance {
  27. static CCExifGeo *sharedInstance;
  28. @synchronized(self)
  29. {
  30. if (!sharedInstance) {
  31. sharedInstance = [CCExifGeo new];
  32. }
  33. return sharedInstance;
  34. }
  35. }
  36. - (void)setExifLocalTableEtag:(tableMetadata *)metadata directoryUser:(NSString *)directoryUser activeAccount:(NSString *)activeAccount
  37. {
  38. NSString *dateTime;
  39. NSString *latitudeRef;
  40. NSString *longitudeRef;
  41. NSString *stringLatitude = @"0";
  42. NSString *stringLongitude = @"0";
  43. double latitude = 0;
  44. double longitude = 0;
  45. NSDate *date = [NSDate new];
  46. if (![[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", directoryUser, metadata.fileID]])
  47. return;
  48. NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", directoryUser, metadata.fileID]];
  49. CGImageSourceRef originalSource = CGImageSourceCreateWithURL((CFURLRef) url, NULL);
  50. if (!originalSource)
  51. return;
  52. CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(originalSource, 0, NULL);
  53. if (!imageProperties)
  54. return;
  55. CFDictionaryRef tiff = CFDictionaryGetValue(imageProperties, kCGImagePropertyTIFFDictionary);
  56. CFDictionaryRef gps = CFDictionaryGetValue(imageProperties, kCGImagePropertyGPSDictionary);
  57. if (tiff) {
  58. dateTime = (NSString *)CFDictionaryGetValue(tiff, kCGImagePropertyTIFFDateTime);
  59. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  60. [dateFormatter setDateFormat:@"yyyy:MM:dd HH:mm:ss"];
  61. date = [dateFormatter dateFromString:dateTime];
  62. if (!date) date = metadata.date;
  63. }
  64. if (gps) {
  65. latitude = [(NSString *)CFDictionaryGetValue(gps, kCGImagePropertyGPSLatitude) doubleValue];
  66. longitude = [(NSString *)CFDictionaryGetValue(gps, kCGImagePropertyGPSLongitude) doubleValue];
  67. latitudeRef = (NSString *)CFDictionaryGetValue(gps, kCGImagePropertyGPSLatitudeRef);
  68. longitudeRef = (NSString *)CFDictionaryGetValue(gps, kCGImagePropertyGPSLongitudeRef);
  69. // conversion 4 decimal +N -S
  70. // The latitude in degrees. Positive values indicate latitudes north of the equator. Negative values indicate latitudes south of the equator.
  71. if ([latitudeRef isEqualToString:@"N"])
  72. stringLatitude = [NSString stringWithFormat:@"+%.4f", latitude];
  73. else
  74. stringLatitude = [NSString stringWithFormat:@"-%.4f", latitude];
  75. // conversion 4 decimal +E -W
  76. // The longitude in degrees. Measurements are relative to the zero meridian, with positive values extending east of the meridian
  77. // and negative values extending west of the meridian.
  78. if ([longitudeRef isEqualToString:@"E"])
  79. stringLongitude = [NSString stringWithFormat:@"+%.4f", longitude];
  80. else
  81. stringLongitude = [NSString stringWithFormat:@"-%.4f", longitude];
  82. if (latitude == 0 || longitude == 0){
  83. stringLatitude = @"0";
  84. stringLongitude = @"0";
  85. }
  86. }
  87. // Wite data EXIF in TableLocalFile
  88. if (tiff || gps)
  89. [[NCManageDatabase sharedInstance] setLocalFileWithFileID:metadata.fileID date:nil exifDate:date exifLatitude:stringLatitude exifLongitude:stringLongitude fileName:nil etag:nil etagFPE:nil];
  90. CFRelease(originalSource);
  91. CFRelease(imageProperties);
  92. }
  93. - (void)setGeocoderEtag:(NSString *)fileID exifDate:(NSDate *)exifDate latitude:(NSString*)latitude longitude:(NSString*)longitude
  94. {
  95. // If exists already geocoder data in TableGPS exit
  96. if ([[NCManageDatabase sharedInstance] getLocationFromGeoLatitude:latitude longitude:longitude])
  97. return;
  98. CLGeocoder *geocoder = [[CLGeocoder alloc] init];
  99. CLLocation *location = [[CLLocation alloc] initWithLatitude:[latitude doubleValue] longitude:[longitude doubleValue]];
  100. [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
  101. //DDLogInfo(@"[LOG] Found placemarks: %@, error: %@", placemarks, error);
  102. if (error == nil && [placemarks count] > 0) {
  103. CLPlacemark *placemark = [placemarks lastObject];
  104. NSString *thoroughfare = @"";
  105. NSString *postalCode = @"";
  106. NSString *locality = @"";
  107. NSString *administrativeArea = @"";
  108. NSString *country = @"";
  109. if (placemark.thoroughfare) thoroughfare = placemark.thoroughfare;
  110. if (placemark.postalCode) postalCode = placemark.postalCode;
  111. if (placemark.locality) locality = placemark.locality;
  112. if (placemark.administrativeArea) administrativeArea = placemark.administrativeArea;
  113. if (placemark.country) country = placemark.country;
  114. NSString *location = [NSString stringWithFormat:@"%@ %@ %@ %@ %@", thoroughfare, postalCode, locality, administrativeArea, country];
  115. location = [location stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
  116. // GPS
  117. if ([location length] > 0) {
  118. [[NCManageDatabase sharedInstance] addGeocoderLocation:location placemarkAdministrativeArea:placemark.administrativeArea placemarkCountry:placemark.country placemarkLocality:placemark.locality placemarkPostalCode:placemark.postalCode placemarkThoroughfare:placemark.thoroughfare latitude:latitude longitude:longitude];
  119. NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:exifDate, fileID, nil];
  120. // Notify for CCDetail
  121. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"insertGeocoderLocation" object:nil userInfo:dictionary];
  122. }
  123. } else {
  124. //NSLog(@"[LOG] setGeocoderFileID : %@", error.debugDescription);
  125. }
  126. }];
  127. }
  128. @end