CCExifGeo.m 5.8 KB

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