CCExifGeo.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // CCExifGeo.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 03/02/16.
  6. // Copyright (c) 2014 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. #ifndef EXTENSION
  25. #ifdef CUSTOM_BUILD
  26. #import "CustomSwift.h"
  27. #else
  28. #import "Nextcloud-Swift.h"
  29. #endif
  30. #else
  31. #ifdef EXTENSION_SHARE
  32. #ifdef CUSTOM_BUILD
  33. #import "CustomSwiftShare.h"
  34. #else
  35. #import "Share-Swift.h"
  36. #endif
  37. #endif
  38. #ifdef EXTENSION_PICKER
  39. #ifdef CUSTOM_BUILD
  40. #import "CustomSwiftPick.h"
  41. #else
  42. #import "Picker-Swift.h"
  43. #endif
  44. #endif
  45. #endif
  46. @implementation CCExifGeo
  47. + (void)setExifLocalTableFileID:(CCMetadata *)metadata directoryUser:(NSString *)directoryUser activeAccount:(NSString *)activeAccount
  48. {
  49. NSString *stringLatitude;
  50. NSString *stringLongitude;
  51. NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", directoryUser, metadata.fileID]];
  52. CGImageSourceRef originalSource = CGImageSourceCreateWithURL((CFURLRef) url, NULL);
  53. NSDictionary *structExif =(__bridge NSDictionary*) CGImageSourceCopyPropertiesAtIndex(originalSource, 0, NULL);
  54. NSDictionary *tiff = [structExif objectForKey:@"{TIFF}"];
  55. NSString *dateTime = [tiff objectForKey:@"DateTime"];
  56. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  57. [dateFormatter setDateFormat:@"yyyy:MM:dd HH:mm:ss"];
  58. NSDate *date = [[NSDate alloc] init];
  59. date = [dateFormatter dateFromString:dateTime];
  60. if (!date) date = metadata.date;
  61. NSDictionary *gps = [structExif objectForKey:@"{GPS}"];
  62. double latitude = [[gps objectForKey:@"Latitude"] doubleValue];
  63. double longitude = [[gps objectForKey:@"Longitude"] doubleValue];
  64. NSString *latitudeRef = [gps objectForKey:@"LatitudeRef"];
  65. NSString *longitudeRef = [gps objectForKey:@"LongitudeRef"];
  66. // conversion 4 decimal +N -S
  67. // The latitude in degrees. Positive values indicate latitudes north of the equator. Negative values indicate latitudes south of the equator.
  68. if ([latitudeRef isEqualToString:@"N"])
  69. stringLatitude = [NSString stringWithFormat:@"+%.4f", latitude];
  70. else
  71. stringLatitude = [NSString stringWithFormat:@"-%.4f", latitude];
  72. // conversion 4 decimal +E -W
  73. // The longitude in degrees. Measurements are relative to the zero meridian, with positive values extending east of the meridian
  74. // and negative values extending west of the meridian.
  75. if ([longitudeRef isEqualToString:@"E"])
  76. stringLongitude = [NSString stringWithFormat:@"+%.4f", longitude];
  77. else
  78. stringLongitude = [NSString stringWithFormat:@"-%.4f", longitude];
  79. if (latitude == 0 || longitude == 0){
  80. stringLatitude = @"0";
  81. stringLongitude = @"0";
  82. }
  83. // Wite data EXIF in TableLocalFile
  84. [CCCoreData setGeoInformationLocalFromFileID:metadata.fileID exifDate:date exifLatitude:stringLatitude exifLongitude:stringLongitude activeAccount:activeAccount];
  85. }
  86. + (void)setGeocoderFileID:(NSString *)fileID exifDate:(NSDate *)exifDate latitude:(NSString*)latitude longitude:(NSString*)longitude
  87. {
  88. // If exists already geocoder data in TableGPS exit
  89. if ([[NCManageDatabase sharedInstance] getLocationFromGeoLatitude:latitude longitude:longitude])
  90. return;
  91. CLGeocoder *geocoder = [[CLGeocoder alloc] init];
  92. CLLocation *location = [[CLLocation alloc] initWithLatitude:[latitude doubleValue] longitude:[longitude doubleValue]];
  93. [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
  94. //DDLogInfo(@"[LOG] Found placemarks: %@, error: %@", placemarks, error);
  95. if (error == nil && [placemarks count] > 0) {
  96. CLPlacemark *placemark = [placemarks lastObject];
  97. NSString *thoroughfare = @"";
  98. NSString *postalCode = @"";
  99. NSString *locality = @"";
  100. NSString *administrativeArea = @"";
  101. NSString *country = @"";
  102. if (placemark.thoroughfare) thoroughfare = placemark.thoroughfare;
  103. if (placemark.postalCode) postalCode = placemark.postalCode;
  104. if (placemark.locality) locality = placemark.locality;
  105. if (placemark.administrativeArea) administrativeArea = placemark.administrativeArea;
  106. if (placemark.country) country = placemark.country;
  107. NSString *location = [NSString stringWithFormat:@"%@ %@ %@ %@ %@", thoroughfare, postalCode, locality, administrativeArea, country];
  108. location = [location stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
  109. // GPS
  110. if ([location length] > 0) {
  111. [[NCManageDatabase sharedInstance] addGeocoderLocation:location placemarkAdministrativeArea:placemark.administrativeArea placemarkCountry:placemark.country placemarkLocality:placemark.locality placemarkPostalCode:placemark.postalCode placemarkThoroughfare:placemark.thoroughfare latitude:latitude longitude:longitude];
  112. NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:exifDate, fileID, nil];
  113. // Notify for CCDetail
  114. [[NSNotificationCenter defaultCenter] postNotificationName:@"insertGeocoderLocation" object:nil userInfo:dictionary];
  115. }
  116. } else {
  117. //NSLog(@"[LOG] setGeocoderFileID : %@", error.debugDescription);
  118. }
  119. }];
  120. }
  121. @end