def create_points(data): for id, values in data.iteritems(): print values new_point = TrapLocation() exp = Expedition.objects.get(id=values['exp_id']) if exp.grid_square_id is None: square_coords = values['square_number'] exp.grid_square_id = [ g.id for g in GridSquare.objects.all() if g.battleship_coords() == square_coords ][0] new_point.expedition = exp new_point.save() new_point.expedition.save() new_point.whether_a_trap_was_set_here = True new_point.transect_distance = values['distance'] # geography: # TODO some duplicates inside these model methods here.. can be # removed. new_point.set_transect_bearing_wrt_magnetic_north(values['bearing']) new_point.set_actual_lat_long([values['lat'], values['lon']]) new_point.set_suggested_lat_lon_from_mag_north(values['bearing'], values['distance']) new_point.understory = values['understory'] new_point.student_names = values['student_name'] new_point.team_letter = values['team_name'] new_point.team_number = values['trap_number'] new_point.bait_still_there = (values['bait_left'] == 't') if values['animal']: species = Species.objects.get( common_name__icontains=values['animal']) animal = Animal() animal.species = species animal.save() new_point.animal = animal animal.save() if values['habitat'] is not None: habitat = guess_habitat(values) new_point.habitat_id = habitat.id if values['bait'] is not None: bait = guess_bait(values) new_point.bait_id = bait.id new_point.save() test_points.append(new_point)
def create_points(data): for id, values in data.iteritems(): print values new_point = TrapLocation() exp = Expedition.objects.get(id=values['exp_id']) if exp.grid_square_id is None: square_coords = values['square_number'] exp.grid_square_id = [ g.id for g in GridSquare.objects.all() if g.battleship_coords() == square_coords][0] new_point.expedition = exp new_point.save() new_point.expedition.save() new_point.whether_a_trap_was_set_here = True new_point.transect_distance = values['distance'] # geography: # TODO some duplicates inside these model methods here.. can be # removed. new_point.set_transect_bearing_wrt_magnetic_north(values['bearing']) new_point.set_actual_lat_long([values['lat'], values['lon']]) new_point.set_suggested_lat_lon_from_mag_north( values['bearing'], values['distance']) new_point.understory = values['understory'] new_point.student_names = values['student_name'] new_point.team_letter = values['team_name'] new_point.team_number = values['trap_number'] new_point.bait_still_there = (values['bait_left'] == 't') if values['animal']: species = Species.objects.get( common_name__icontains=values['animal']) animal = Animal() animal.species = species animal.save() new_point.animal = animal animal.save() if values['habitat'] is not None: habitat = guess_habitat(values) new_point.habitat_id = habitat.id if values['bait'] is not None: bait = guess_bait(values) new_point.bait_id = bait.id new_point.save() test_points.append(new_point)
def deal_with_animals(point, rp): # Deal with animals: animal_key = 'animal_%d' % (point.id) if animal_key in rp and rp[animal_key] != 'None': species_id = int(rp[animal_key]) species = Species.objects.get(id=species_id) # TODO (icing ) here we assume that the animal has never been # trapped before. animal = Animal() animal.species = species animal.save() # Note, would be nice to have a foreign key to another Animal # denoting that this Animal is the same actual organism as the # other Animal, but recaptured at a later date and # aidentified by the same tag. # animal = find_already_tagged_animal_in_the_database_somehow() point.animal = animal point.save() animal.save()
def process_save_team_form(request): if request.method != 'POST': return HttpResponseRedirect('/mammals/all_expeditions/') rp = request.POST expedition_id = rp['expedition_id'] exp = Expedition.objects.get(id=expedition_id) team_letter = rp['team_letter'] the_team_points = exp.team_points(team_letter) form_map = { 'habitat': 'habitat_id', 'bait': 'bait_id', 'trap_type': 'trap_type_id' } form_map_booleans = { 'whether_a_trap_was_set_here': 'whether_a_trap_was_set_here', 'bait_still_there': 'bait_still_there' } for point in the_team_points: if 'student_names' in rp: point.student_names = rp['student_names'] point.save() for the_key, thing_to_update in form_map.iteritems(): rp_key = '%s_%d' % (the_key, point.id) if rp_key in rp and rp[rp_key] != 'None': setattr(point, '%s' % thing_to_update, rp[rp_key]) point.save() for the_key, thing_to_update in form_map_booleans.iteritems(): rp_key = '%s_%d' % (the_key, point.id) if rp_key in rp and rp[rp_key] != 'None': setattr(point, '%s' % thing_to_update, (rp[rp_key] == 'True')) point.save() rp_key = '%s_%d' % ('understory', point.id) if rp_key in rp and rp[rp_key] != 'None': point.understory = rp[rp_key] point.save() rp_key = '%s_%d' % ('notes_about_location', point.id) if rp_key in rp and rp[rp_key] != 'None': point.notes_about_location = rp[rp_key] point.save() # Deal with animals: animal_key = 'animal_%d' % (point.id) if animal_key in rp and rp[animal_key] != 'None': species_id = int(rp[animal_key]) species = Species.objects.get(id=species_id) # TODO (icing ) here we assume that the animal has never been # trapped before. animal_never_trapped_before = True if animal_never_trapped_before: animal = Animal() animal.species = species animal.save() else: # Note, would be nice to have a foreign key to another Animal # denoting that this Animal is the same actual organism as the # other Animal, but recaptured at a later date and # aidentified by the same tag. # animal = find_already_tagged_animal_in_the_database_somehow() pass point.animal = animal point.save() animal.save() # Deal with actual latitude and longitude: # The burden of providing good data here rests on the front end. # If the actual lat and lon don't meet our data standards, we're simply # not going to use them. # 1) Do they both exist? # 2) Are they both accurate to 5 decimals? # 3) Are they within a certain distance of the original lat and lon (no # interest in points in another country. correcting_lat_lon = False match_string = '(\-)?(\d){2}\.(\d){5}' # if your coordinate string doesn't match the above, you have a nice # day. lat_key = 'actual_lat_%d' % point.id lon_key = 'actual_lon_%d' % point.id max_diff = 250.0 # meters min_diff = 1.0 # meters correcting_lat_lon = True if correcting_lat_lon and lat_key not in rp: correcting_lat_lon = False if correcting_lat_lon and lon_key not in rp: correcting_lat_lon = False if correcting_lat_lon and match(match_string, rp[lat_key]) is None: correcting_lat_lon = False if correcting_lat_lon and match(match_string, rp[lon_key]) is None: correcting_lat_lon = False if correcting_lat_lon: diff_lat = point.actual_lat() - float(rp[lat_key]) diff_lon = point.actual_lon() - float(rp[lon_key]) distance_to_corrected_point_in_meters = hypotenuse( *to_meters(diff_lat, diff_lon)) if distance_to_corrected_point_in_meters > max_diff: correcting_lat_lon = False # distance_to_corrected_point_in_meters if (correcting_lat_lon and distance_to_corrected_point_in_meters < min_diff): correcting_lat_lon = False # distance_to_corrected_point_in_meters if correcting_lat_lon: point.set_actual_lat_long( [float(rp[lat_key]), float(rp[lon_key])]) point.save()