CCExifGeo.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // CCExifGeo.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 03/02/16.
  6. // Copyright (c) 2016 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  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 "CCUtility.h"
  25. #import "NCBridgeSwift.h"
  26. @implementation CCExifGeo
  27. + (CCExifGeo *)sharedInstance {
  28. static CCExifGeo *sharedInstance;
  29. @synchronized(self)
  30. {
  31. if (!sharedInstance) {
  32. sharedInstance = [CCExifGeo new];
  33. }
  34. return sharedInstance;
  35. }
  36. }
  37. - (void)setExifLocalTableEtag:(tableMetadata *)metadata
  38. {
  39. NSString *dateTime;
  40. NSString *latitudeRef;
  41. NSString *longitudeRef;
  42. NSString *stringLatitude = @"0";
  43. NSString *stringLongitude = @"0";
  44. double latitude = 0;
  45. double longitude = 0;
  46. NSDate *date = [NSDate new];
  47. if (![CCUtility fileProviderStorageExists:metadata.ocId fileNameView:metadata.fileNameView])
  48. return;
  49. NSURL *url = [NSURL fileURLWithPath:[CCUtility getDirectoryProviderStorageOcId:metadata.ocId fileNameView:metadata.fileNameView]];
  50. CGImageSourceRef originalSource = CGImageSourceCreateWithURL((CFURLRef) url, NULL);
  51. if (!originalSource)
  52. return;
  53. CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(originalSource, 0, NULL);
  54. if (!imageProperties)
  55. return;
  56. CFDictionaryRef tiff = CFDictionaryGetValue(imageProperties, kCGImagePropertyTIFFDictionary);
  57. CFDictionaryRef gps = CFDictionaryGetValue(imageProperties, kCGImagePropertyGPSDictionary);
  58. if (tiff) {
  59. dateTime = (NSString *)CFDictionaryGetValue(tiff, kCGImagePropertyTIFFDateTime);
  60. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  61. [dateFormatter setDateFormat:@"yyyy:MM:dd HH:mm:ss"];
  62. date = [dateFormatter dateFromString:dateTime];
  63. if (!date) date = metadata.date;
  64. }
  65. if (gps) {
  66. latitude = [(NSString *)CFDictionaryGetValue(gps, kCGImagePropertyGPSLatitude) doubleValue];
  67. longitude = [(NSString *)CFDictionaryGetValue(gps, kCGImagePropertyGPSLongitude) doubleValue];
  68. latitudeRef = (NSString *)CFDictionaryGetValue(gps, kCGImagePropertyGPSLatitudeRef);
  69. longitudeRef = (NSString *)CFDictionaryGetValue(gps, kCGImagePropertyGPSLongitudeRef);
  70. // conversion 4 decimal +N -S
  71. // The latitude in degrees. Positive values indicate latitudes north of the equator. Negative values indicate latitudes south of the equator.
  72. if ([latitudeRef isEqualToString:@"N"])
  73. stringLatitude = [NSString stringWithFormat:@"+%.4f", latitude];
  74. else
  75. stringLatitude = [NSString stringWithFormat:@"-%.4f", latitude];
  76. // conversion 4 decimal +E -W
  77. // The longitude in degrees. Measurements are relative to the zero meridian, with positive values extending east of the meridian
  78. // and negative values extending west of the meridian.
  79. if ([longitudeRef isEqualToString:@"E"])
  80. stringLongitude = [NSString stringWithFormat:@"+%.4f", longitude];
  81. else
  82. stringLongitude = [NSString stringWithFormat:@"-%.4f", longitude];
  83. if (latitude == 0 || longitude == 0){
  84. stringLatitude = @"0";
  85. stringLongitude = @"0";
  86. }
  87. }
  88. // Wite data EXIF in TableLocalFile
  89. if (tiff || gps)
  90. [[NCManageDatabase sharedInstance] setLocalFileWithOcId:metadata.ocId date:nil exifDate:date exifLatitude:stringLatitude exifLongitude:stringLongitude fileName:nil etag:nil];
  91. CFRelease(originalSource);
  92. CFRelease(imageProperties);
  93. }
  94. - (void)setGeocoderEtag:(NSString *)ocId exifDate:(NSDate *)exifDate latitude:(NSString*)latitude longitude:(NSString*)longitude
  95. {
  96. // If exists already geocoder data in TableGPS exit
  97. if ([[NCManageDatabase sharedInstance] getLocationFromGeoLatitude:latitude longitude:longitude])
  98. return;
  99. CLGeocoder *geocoder = [[CLGeocoder alloc] init];
  100. CLLocation *location = [[CLLocation alloc] initWithLatitude:[latitude doubleValue] longitude:[longitude doubleValue]];
  101. [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
  102. //DDLogInfo(@"[LOG] Found placemarks: %@, error: %@", placemarks, error);
  103. if (error == nil && [placemarks count] > 0) {
  104. CLPlacemark *placemark = [placemarks lastObject];
  105. NSString *thoroughfare = @"";
  106. NSString *postalCode = @"";
  107. NSString *locality = @"";
  108. NSString *administrativeArea = @"";
  109. NSString *country = @"";
  110. if (placemark.thoroughfare) thoroughfare = placemark.thoroughfare;
  111. if (placemark.postalCode) postalCode = placemark.postalCode;
  112. if (placemark.locality) locality = placemark.locality;
  113. if (placemark.administrativeArea) administrativeArea = placemark.administrativeArea;
  114. if (placemark.country) country = placemark.country;
  115. NSString *location = [NSString stringWithFormat:@"%@ %@ %@ %@ %@", thoroughfare, postalCode, locality, administrativeArea, country];
  116. location = [location stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
  117. // GPS
  118. if ([location length] > 0) {
  119. [[NCManageDatabase sharedInstance] addGeocoderLocation:location placemarkAdministrativeArea:placemark.administrativeArea placemarkCountry:placemark.country placemarkLocality:placemark.locality placemarkPostalCode:placemark.postalCode placemarkThoroughfare:placemark.thoroughfare latitude:latitude longitude:longitude];
  120. NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:exifDate, ocId, nil];
  121. // Notify for CCDetail
  122. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"insertGeocoderLocation" object:nil userInfo:dictionary];
  123. }
  124. } else {
  125. //NSLog(@"[LOG] setGeocoderocId : %@", error.debugDescription);
  126. }
  127. }];
  128. }
  129. @end