Ejemplo n.º 1
0
    def test_san_francisco_save_method_invalid_item_type(self):
        """
        """
        starting_record_count = Occurrence.objects.count()
        new_occurrence = Occurrence()
        new_occurrence.item_type = "Fake"
        new_occurrence.basis_of_record = "HumanObservation"
        new_occurrence.collecting_method = "Surface Standard"
        new_occurrence.field_number = datetime.now()
        new_occurrence.geom = "POINT (40.8352906016 11.5303732536)"
        new_occurrence.save()

        now = datetime.now()
        self.assertEqual(Occurrence.objects.count(), starting_record_count +
                         1)  # test that one record has been added
        self.assertEqual(new_occurrence.date_last_modified.day,
                         now.day)  # test date last modified is correct
        self.assertEqual(new_occurrence.point_x(), 40.8352906016)
        self.assertEqual(new_occurrence.item_type, "Fake")
Ejemplo n.º 2
0
    def test_san_francisco_save_method_invalid_item_type(self):
        """
        """
        starting_record_count = Occurrence.objects.count()
        new_occurrence = Occurrence()
        new_occurrence.item_type = "Fake"
        new_occurrence.basis_of_record = "HumanObservation"
        new_occurrence.collecting_method = "Surface Standard"
        new_occurrence.field_number = datetime.now()
        new_occurrence.geom = "POINT (40.8352906016 11.5303732536)"
        new_occurrence.save()

        now = datetime.now()
        self.assertEqual(Occurrence.objects.count(), starting_record_count+1)  # test that one record has been added
        self.assertEqual(new_occurrence.date_last_modified.day, now.day)  # test date last modified is correct
        self.assertEqual(new_occurrence.point_x(), 40.8352906016)
        self.assertEqual(new_occurrence.item_type, "Fake")
Ejemplo n.º 3
0
 def test_san_francisco_occurrence_save_simple(self):
     """
     Test san_francisco_occurrence instance save method with the simplest possible attributes.
     """
     starting_record_count = Occurrence.objects.count()  # get current number of occurrence records
     # The simplest occurrence instance we can create needs six bits of data.
     # Using the instance creation and then save methods
     new_occurrence = Occurrence(id=1, item_type="Faunal", basis_of_record="HumanObservation",
                                 collecting_method="Surface Standard", field_number=datetime.now(),
                                 geom="POINT (40.8352906016 11.5303732536)")
     new_occurrence.save()
     now = datetime.now()
     self.assertEqual(Occurrence.objects.count(), starting_record_count+1)  # test that one record has been added
     self.assertEqual(new_occurrence.date_last_modified.day, now.day)  # test date last modified is correct
     self.assertEqual(new_occurrence.point_x(), 40.8352906016)
     self.assertEqual(new_occurrence.point_y(), 11.5303732536)
     self.assertEqual(new_occurrence.easting(), 700158.9412020452)
     self.assertEqual(new_occurrence.northing(), 1275265.0366469107)
Ejemplo n.º 4
0
 def test_san_francisco_occurrence_save_simple(self):
     """
     Test san_francisco_occurrence instance save method with the simplest possible attributes.
     """
     starting_record_count = Occurrence.objects.count(
     )  # get current number of occurrence records
     # The simplest occurrence instance we can create needs six bits of data.
     # Using the instance creation and then save methods
     new_occurrence = Occurrence(id=1,
                                 item_type="Faunal",
                                 basis_of_record="HumanObservation",
                                 collecting_method="Surface Standard",
                                 field_number=datetime.now(),
                                 geom="POINT (40.8352906016 11.5303732536)")
     new_occurrence.save()
     now = datetime.now()
     self.assertEqual(Occurrence.objects.count(), starting_record_count +
                      1)  # test that one record has been added
     self.assertEqual(new_occurrence.date_last_modified.day,
                      now.day)  # test date last modified is correct
     self.assertEqual(new_occurrence.point_x(), 40.8352906016)
     self.assertEqual(new_occurrence.point_y(), 11.5303732536)
     self.assertEqual(new_occurrence.easting(), 700158.9412020452)
     self.assertEqual(new_occurrence.northing(), 1275265.0366469107)
Ejemplo n.º 5
0
        def import_placemarks(placemark_list):
            feature_count = 0

            for o in placemark_list:

                # Check to make sure that the object is a Placemark, filter out folder objects
                if type(o) is Placemark:

                    table = etree.fromstring(o.description)
                    attributes = table.xpath("//text()")
                    # TODO test attributes is even length
                    attributes_dict = dict(zip(attributes[0::2], attributes[1::2]))

                    san_francisco_occ = Occurrence()

                    ###################
                    # REQUIRED FIELDS #
                    ###################

                    # Validate Basis of Record
                    if attributes_dict.get("Basis Of Record") in ("Fossil", "FossilSpecimen", "Collection"):
                        san_francisco_occ.basis_of_record = "FossilSpecimen"
                    elif attributes_dict.get("Basis Of Record") in ("Observation", "HumanObservation"):
                        san_francisco_occ.basis_of_record = "HumanObservation"

                    # Validate Item Type
                    item_type = attributes_dict.get("Item Type")
                    if item_type in ("Artifact", "Artifactual", "Archeology", "Archaeological"):
                        san_francisco_occ.item_type = "Artifactual"
                    elif item_type in ("Faunal", "Fauna"):
                        san_francisco_occ.item_type = "Faunal"
                    elif item_type in ("Floral", "Flora"):
                        san_francisco_occ.item_type = "Floral"
                    elif item_type in ("Geological", "Geology"):
                        san_francisco_occ.item_type = "Geological"

                    # Field Number
                    try:
                        san_francisco_occ.field_number = datetime.strptime(attributes_dict.get("Time"), "%b %d, %Y, %I:%M %p")  # parse field nubmer
                        san_francisco_occ.year_collected = san_francisco_occ.field_number.year  # set the year collected form field number
                    except ValueError:
                        san_francisco_occ.field_number = datetime.now()
                        san_francisco_occ.problem = True
                        try:
                            error_string = "Upload error, missing field number, using current date and time instead."
                            san_francisco_occ.problem_comment = san_francisco_occ.problem_comment + " " +error_string
                        except TypeError:
                            san_francisco_occ.problem_comment = error_string

                    # Process point, comes in as well known text string
                    # Assuming point is in GCS WGS84 datum = SRID 4326
                    pnt = GEOSGeometry("POINT (" + str(o.geometry.x) + " " + str(o.geometry.y) + ")", 4326)  # WKT
                    san_francisco_occ.geom = pnt

                    #######################
                    # NON-REQUIRED FIELDS #
                    #######################
                    san_francisco_occ.barcode = attributes_dict.get("Barcode")
                    san_francisco_occ.item_number = san_francisco_occ.barcode
                    san_francisco_occ.catalog_number = "San Francisco-" + str(san_francisco_occ.item_number)
                    san_francisco_occ.remarks = attributes_dict.get("Remarks")
                    san_francisco_occ.item_scientific_name = attributes_dict.get("Scientific Name")
                    san_francisco_occ.item_description = attributes_dict.get("Description")


                    # Validate Collecting Method
                    collection_method = attributes_dict.get("Collection Method")
                    if collection_method in ("Surface Standard", "Standard"):
                        san_francisco_occ.collecting_method = "Surface Standard"
                    elif collection_method in ("Surface Intensive", "Intensive"):
                        san_francisco_occ.collecting_method = "Surface Intensive"
                    elif collection_method in ("Surface Complete", "Complete"):
                        san_francisco_occ.collecting_method = "Surface Complete"
                    elif collection_method in ("Exploratory Survey", "Exploratory"):
                        san_francisco_occ.collecting_method = "Exploratory Survey"
                    elif collection_method in ("Dry Screen 5mm", "Dry Screen 5 Mm", "Dry Screen 5 mm"):
                        san_francisco_occ.collecting_method = "Dry Screen 5mm"
                    elif collection_method in ("Dry Screen 2mm", "Dry Screen 2 Mm", "Dry Screen 2 mm"):
                        san_francisco_occ.collecting_method = "Dry Screen 2mm"
                    elif collection_method in ("Dry Screen 1mm", "Dry Screen 1 Mm", "Dry Screen 1 mm"):
                        san_francisco_occ.collecting_method = "Dry Screen 1mm"
                    # else:
                    #     mlp_occ.collecting_method = None
                    #     mlp_occ.problem = True
                    #     mlp_occ.problem_comment = mlp_occ.problem_comment + " problem importing collecting method"

                    san_francisco_occ.collecting_method = attributes_dict.get("Collection Method")
                    san_francisco_occ.collector = attributes_dict.get("Collector")
                    san_francisco_occ.individual_count = attributes_dict.get("Count")
                    #if mlp_occ:
                    #    mlp_occ.year_collected = mlp_occ.field_number.year

                    if attributes_dict.get("In Situ") in ('No', "NO", 'no'):
                        san_francisco_occ.in_situ = False
                    elif attributes_dict.get("In Situ") in ('Yes', "YES", 'yes'):
                        san_francisco_occ.in_situ = True

                    ##############
                    # Save Image #
                    ##############
                    image_file = ""
                    image_added = False
                    # Now look for images if this is a KMZ
                    if KML_file_extension.lower() == "kmz":
                        # grab image names from XML
                        image_tags = table.xpath("//img/@src")
                        # grab the name of the first image
                        try:
                            image_tag = image_tags[0]
                            # grab the file info from the zip list
                            for file_info in KMZ_file.filelist:
                                if image_tag == file_info.orig_filename:
                                    # grab the image file itself
                                    image_file = KMZ_file.extract(file_info, "media/uploads/images/san_francisco")
                                    image_added = True
                                    break
                        except IndexError:
                            pass

                    san_francisco_occ.save()
                    # need to save record before adding image in order to obtain the DB ID
                    if image_added:
                        # strip off the file name from the path
                        image_path = image_file[:image_file.rfind(os.sep)]
                        # construct new file name
                        new_file_name = image_path + os.sep + str(san_francisco_occ.id) + "_" + image_tag
                        # rename extracted file with DB record ID
                        os.rename(image_file, new_file_name)
                        # need to strip off "media" folder from relative path saved to DB
                        san_francisco_occ.image = new_file_name[new_file_name.find(os.sep)+1:]
                        san_francisco_occ.save()
                    feature_count += 1

                elif type(o) is not Placemark:
                    raise IOError("KML File is badly formatted")
Ejemplo n.º 6
0
        def import_placemarks(placemark_list):
            feature_count = 0

            for o in placemark_list:

                # Check to make sure that the object is a Placemark, filter out folder objects
                if type(o) is Placemark:

                    table = etree.fromstring(o.description)
                    attributes = table.xpath("//text()")
                    # TODO test attributes is even length
                    attributes_dict = dict(
                        zip(attributes[0::2], attributes[1::2]))

                    san_francisco_occ = Occurrence()

                    ###################
                    # REQUIRED FIELDS #
                    ###################

                    # Validate Basis of Record
                    if attributes_dict.get("Basis Of Record") in (
                            "Fossil", "FossilSpecimen", "Collection"):
                        san_francisco_occ.basis_of_record = "FossilSpecimen"
                    elif attributes_dict.get("Basis Of Record") in (
                            "Observation", "HumanObservation"):
                        san_francisco_occ.basis_of_record = "HumanObservation"

                    # Validate Item Type
                    item_type = attributes_dict.get("Item Type")
                    if item_type in ("Artifact", "Artifactual", "Archeology",
                                     "Archaeological"):
                        san_francisco_occ.item_type = "Artifactual"
                    elif item_type in ("Faunal", "Fauna"):
                        san_francisco_occ.item_type = "Faunal"
                    elif item_type in ("Floral", "Flora"):
                        san_francisco_occ.item_type = "Floral"
                    elif item_type in ("Geological", "Geology"):
                        san_francisco_occ.item_type = "Geological"

                    # Field Number
                    try:
                        san_francisco_occ.field_number = datetime.strptime(
                            attributes_dict.get("Time"),
                            "%b %d, %Y, %I:%M %p")  # parse field nubmer
                        san_francisco_occ.year_collected = san_francisco_occ.field_number.year  # set the year collected form field number
                    except ValueError:
                        san_francisco_occ.field_number = datetime.now()
                        san_francisco_occ.problem = True
                        try:
                            error_string = "Upload error, missing field number, using current date and time instead."
                            san_francisco_occ.problem_comment = san_francisco_occ.problem_comment + " " + error_string
                        except TypeError:
                            san_francisco_occ.problem_comment = error_string

                    # Process point, comes in as well known text string
                    # Assuming point is in GCS WGS84 datum = SRID 4326
                    pnt = GEOSGeometry("POINT (" + str(o.geometry.x) + " " +
                                       str(o.geometry.y) + ")", 4326)  # WKT
                    san_francisco_occ.geom = pnt

                    #######################
                    # NON-REQUIRED FIELDS #
                    #######################
                    san_francisco_occ.barcode = attributes_dict.get("Barcode")
                    san_francisco_occ.item_number = san_francisco_occ.barcode
                    san_francisco_occ.catalog_number = "San Francisco-" + str(
                        san_francisco_occ.item_number)
                    san_francisco_occ.remarks = attributes_dict.get("Remarks")
                    san_francisco_occ.item_scientific_name = attributes_dict.get(
                        "Scientific Name")
                    san_francisco_occ.item_description = attributes_dict.get(
                        "Description")

                    # Validate Collecting Method
                    collection_method = attributes_dict.get(
                        "Collection Method")
                    if collection_method in ("Surface Standard", "Standard"):
                        san_francisco_occ.collecting_method = "Surface Standard"
                    elif collection_method in ("Surface Intensive",
                                               "Intensive"):
                        san_francisco_occ.collecting_method = "Surface Intensive"
                    elif collection_method in ("Surface Complete", "Complete"):
                        san_francisco_occ.collecting_method = "Surface Complete"
                    elif collection_method in ("Exploratory Survey",
                                               "Exploratory"):
                        san_francisco_occ.collecting_method = "Exploratory Survey"
                    elif collection_method in ("Dry Screen 5mm",
                                               "Dry Screen 5 Mm",
                                               "Dry Screen 5 mm"):
                        san_francisco_occ.collecting_method = "Dry Screen 5mm"
                    elif collection_method in ("Dry Screen 2mm",
                                               "Dry Screen 2 Mm",
                                               "Dry Screen 2 mm"):
                        san_francisco_occ.collecting_method = "Dry Screen 2mm"
                    elif collection_method in ("Dry Screen 1mm",
                                               "Dry Screen 1 Mm",
                                               "Dry Screen 1 mm"):
                        san_francisco_occ.collecting_method = "Dry Screen 1mm"
                    # else:
                    #     mlp_occ.collecting_method = None
                    #     mlp_occ.problem = True
                    #     mlp_occ.problem_comment = mlp_occ.problem_comment + " problem importing collecting method"

                    san_francisco_occ.collecting_method = attributes_dict.get(
                        "Collection Method")
                    san_francisco_occ.collector = attributes_dict.get(
                        "Collector")
                    san_francisco_occ.individual_count = attributes_dict.get(
                        "Count")
                    #if mlp_occ:
                    #    mlp_occ.year_collected = mlp_occ.field_number.year

                    if attributes_dict.get("In Situ") in ('No', "NO", 'no'):
                        san_francisco_occ.in_situ = False
                    elif attributes_dict.get("In Situ") in ('Yes', "YES",
                                                            'yes'):
                        san_francisco_occ.in_situ = True

                    ##############
                    # Save Image #
                    ##############
                    image_file = ""
                    image_added = False
                    # Now look for images if this is a KMZ
                    if KML_file_extension.lower() == "kmz":
                        # grab image names from XML
                        image_tags = table.xpath("//img/@src")
                        # grab the name of the first image
                        try:
                            image_tag = image_tags[0]
                            # grab the file info from the zip list
                            for file_info in KMZ_file.filelist:
                                if image_tag == file_info.orig_filename:
                                    # grab the image file itself
                                    image_file = KMZ_file.extract(
                                        file_info,
                                        "media/uploads/images/san_francisco")
                                    image_added = True
                                    break
                        except IndexError:
                            pass

                    san_francisco_occ.save()
                    # need to save record before adding image in order to obtain the DB ID
                    if image_added:
                        # strip off the file name from the path
                        image_path = image_file[:image_file.rfind(os.sep)]
                        # construct new file name
                        new_file_name = image_path + os.sep + str(
                            san_francisco_occ.id) + "_" + image_tag
                        # rename extracted file with DB record ID
                        os.rename(image_file, new_file_name)
                        # need to strip off "media" folder from relative path saved to DB
                        san_francisco_occ.image = new_file_name[new_file_name.
                                                                find(os.sep) +
                                                                1:]
                        san_francisco_occ.save()
                    feature_count += 1

                elif type(o) is not Placemark:
                    raise IOError("KML File is badly formatted")