def create_from_file(self, file_path): photo = Photo() with open(file_path, 'rb') as f: tags = exifread.process_file(f) if 'GPS GPSLongitude' in tags: # We have gps... lon = convert_gps_ratio(tags['GPS GPSLongitude'].values) if tags['GPS GPSLongitudeRef'].values == 'W': lon *= -1 lat = convert_gps_ratio(tags['GPS GPSLatitude'].values) if tags['GPS GPSLatitudeRef'].values == 'S': lat *= -1 photo.latitude = lat photo.longitude = lon if 'Image DateTime' in tags: # We have a date... date_string = tags['Image DateTime'].values date_fmt = '%Y:%m:%d %H:%M:%S' timestamp_naiive = datetime.strptime(date_string, date_fmt) tz = timezone.get_default_timezone() if photo.latitude and photo.longitude: tz = get_timezone(photo.latitude, photo.longitude) photo.timestamp = timestamp_naiive.replace(tzinfo=tz) # Now let's see if there's a matching ride! rides = Ride.objects.filter( start__lte=photo.timestamp, end__gte=photo.timestamp ) if rides.count() == 1: photo.ride = rides[0] photo.save() photo.src.save(os.path.basename(file_path), File(f)) return photo
def create_from_cyclemeter(self, file_path): ride = Ride(name='Ride') map_bounds = { 'lat_min': 0, 'lat_max': 0, 'lon_min': 0, 'lon_max': 0 } with open(file_path, 'r') as csvfile: reader = csv.DictReader(csvfile) tz = pytz.utc for index, row in enumerate(reader): this_coord = [float(row['Longitude']), float(row['Latitude'])] date_fmt = '%Y-%m-%d %H:%M:%S' if index == 0: tz = get_timezone(*this_coord) # Now we record the ride start time ride.start = datetime.strptime(row['Time'], date_fmt).replace(tzinfo=tz) map_bounds = { 'lat_min': this_coord[1], 'lat_max': this_coord[1], 'lon_min': this_coord[0], 'lon_max': this_coord[0] } else: map_bounds = { 'lat_min': min(this_coord[1], map_bounds['lat_min']), 'lat_max': max(this_coord[1], map_bounds['lat_max']), 'lon_min': min(this_coord[0], map_bounds['lon_min']), 'lon_max': max(this_coord[0], map_bounds['lon_max']) } else: """Let's save the "ratio" of the map. We'll use this to figure out which template to use... """ width = map_bounds['lon_max'] - map_bounds['lon_min'] height = map_bounds['lat_max'] - map_bounds['lat_min'] ride.map_ratio = width / float(height) # Here, 'row' is the last row in the file... ride.end = datetime.strptime(row['Time'], date_fmt).replace(tzinfo=tz) ride.distance = float(row['Distance (miles)']) ride.average_speed = float(row['Average Speed (mph)']) ride_time = list(map(int, row['Ride Time'].split(':'))) ride.ride_time = timedelta( hours=ride_time[0], minutes=ride_time[1], seconds=ride_time[2]) stopped_time = list(map(int, row['Stopped Time'].split(':'))) ride.stopped_time = timedelta( hours=stopped_time[0], minutes=stopped_time[1], seconds=stopped_time[2]) ride.ride_file.save(os.path.basename(file_path), File(csvfile)) ride.save() return ride