Пример #1
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]))

                    mlp_occ = Occurrence()

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

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

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

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

                    #utmPoint = utm.from_latlon(o.geometry.y, o.geometry.x)

                    # 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
                    mlp_occ.geom = pnt

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


                    # Validate Collecting Method
                    collection_method = attributes_dict.get("Collection Method")
                    if collection_method in ("Surface Standard", "Standard"):
                        mlp_occ.collecting_method = "Surface Standard"
                    elif collection_method in ("Surface Intensive", "Intensive"):
                        mlp_occ.collecting_method = "Surface Intensive"
                    elif collection_method in ("Surface Complete", "Complete"):
                        mlp_occ.collecting_method = "Surface Complete"
                    elif collection_method in ("Exploratory Survey", "Exploratory"):
                        mlp_occ.collecting_method = "Exploratory Survey"
                    elif collection_method in ("Dry Screen 5mm", "Dry Screen 5 Mm", "Dry Screen 5 mm"):
                        mlp_occ.collecting_method = "Dry Screen 5mm"
                    elif collection_method in ("Dry Screen 2mm", "Dry Screen 2 Mm", "Dry Screen 2 mm"):
                        mlp_occ.collecting_method = "Dry Screen 2mm"
                    elif collection_method in ("Dry Screen 1mm", "Dry Screen 1 Mm", "Dry Screen 1 mm"):
                        mlp_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"

                    mlp_occ.collecting_method = attributes_dict.get("Collection Method")
                    mlp_occ.collector = attributes_dict.get("Collector")
                    mlp_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'):
                        mlp_occ.in_situ = False
                    elif attributes_dict.get("In Situ") in ('Yes', "YES", 'yes'):
                        mlp_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/mlp")
                                    image_added = True
                                    break
                        except IndexError:
                            pass

                    mlp_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(mlp_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
                        mlp_occ.image = new_file_name[new_file_name.find(os.sep)+1:]
                        mlp_occ.save()
                    feature_count += 1

                elif type(o) is not Placemark:
                    raise IOError("KML File is badly formatted")
Пример #2
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]))

                    mlp_occ = Occurrence()

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

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

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

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

                    #utmPoint = utm.from_latlon(o.geometry.y, o.geometry.x)

                    # 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
                    mlp_occ.geom = pnt

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


                    # Validate Collecting Method
                    collection_method = attributes_dict.get("Collection Method")
                    if collection_method in ("Surface Standard", "Standard"):
                        mlp_occ.collecting_method = "Surface Standard"
                    elif collection_method in ("Surface Intensive", "Intensive"):
                        mlp_occ.collecting_method = "Surface Intensive"
                    elif collection_method in ("Surface Complete", "Complete"):
                        mlp_occ.collecting_method = "Surface Complete"
                    elif collection_method in ("Exploratory Survey", "Exploratory"):
                        mlp_occ.collecting_method = "Exploratory Survey"
                    elif collection_method in ("Dry Screen 5mm", "Dry Screen 5 Mm", "Dry Screen 5 mm"):
                        mlp_occ.collecting_method = "Dry Screen 5mm"
                    elif collection_method in ("Dry Screen 2mm", "Dry Screen 2 Mm", "Dry Screen 2 mm"):
                        mlp_occ.collecting_method = "Dry Screen 2mm"
                    elif collection_method in ("Dry Screen 1mm", "Dry Screen 1 Mm", "Dry Screen 1 mm"):
                        mlp_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"

                    mlp_occ.collecting_method = attributes_dict.get("Collection Method")
                    mlp_occ.collector = attributes_dict.get("Collector")
                    mlp_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'):
                        mlp_occ.in_situ = False
                    elif attributes_dict.get("In Situ") in ('Yes', "YES", 'yes'):
                        mlp_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/mlp")
                                    image_added = True
                                    break
                        except IndexError:
                            pass

                    mlp_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(mlp_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
                        mlp_occ.image = new_file_name[new_file_name.find(os.sep)+1:]
                        mlp_occ.save()
                    feature_count += 1

                elif type(o) is not Placemark:
                    raise IOError("KML File is badly formatted")
Пример #3
0
        def import_placemarks(placemark_list):
            feature_count = 0

            for o in placemark_list:  # iterate through all the placemarks in the KML/KMZ file and process each one

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

                    # save the placemark attribute data located in the description element as an element tree
                    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]))

                    # Create a new, empty occurrence instance
                    omo_mursi_occ = Occurrence()

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

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

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

                    # Field Number and Year Collected
                    try:
                        # parse field number
                        omo_mursi_occ.field_number = datetime.strptime(attributes_dict.get("Time"),
                                                                       "%b %d, %Y, %I:%M %p")
                        # set the year collected from field number
                        omo_mursi_occ.year_collected = omo_mursi_occ.field_number.year
                    except ValueError:
                        omo_mursi_occ.field_number = datetime.now()
                        omo_mursi_occ.problem = True
                        try:
                            error_string = "Upload error, missing field number, using current date and time instead."
                            omo_mursi_occ.problem_comment = omo_mursi_occ.problem_comment + " " +error_string
                        except TypeError:
                            omo_mursi_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
                    omo_mursi_occ.geom = pnt

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

                    # Validate Collecting Method
                    collection_method = attributes_dict.get("Collection Method")
                    if collection_method in ("Surface Standard", "Standard"):
                        omo_mursi_occ.collecting_method = "Surface Standard"
                    elif collection_method in ("Surface Intensive", "Intensive"):
                        omo_mursi_occ.collecting_method = "Surface Intensive"
                    elif collection_method in ("Surface Complete", "Complete"):
                        omo_mursi_occ.collecting_method = "Surface Complete"
                    elif collection_method in ("Exploratory Survey", "Exploratory"):
                        omo_mursi_occ.collecting_method = "Exploratory Survey"
                    elif collection_method in ("Dry Screen 5mm", "Dry Screen 5 Mm", "Dry Screen 5 mm"):
                        omo_mursi_occ.collecting_method = "Dry Screen 5mm"
                    elif collection_method in ("Dry Screen 2mm", "Dry Screen 2 Mm", "Dry Screen 2 mm"):
                        omo_mursi_occ.collecting_method = "Dry Screen 2mm"
                    elif collection_method in ("Dry Screen 1mm", "Dry Screen 1 Mm", "Dry Screen 1 mm"):
                        omo_mursi_occ.collecting_method = "Dry Screen 1mm"

                    omo_mursi_occ.collecting_method = attributes_dict.get("Collection Method")
                    omo_mursi_occ.collector = attributes_dict.get("Collector")
                    omo_mursi_occ.individual_count = attributes_dict.get("Count")

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

                    ##############
                    # Save Image #
                    ##############
                    omo_mursi_occ.save()  # need to save record before adding image in order to obtain the DB ID

                    # Now look for images if this is a KMZ
                    if kml_file_extension.lower() == "kmz":
                        # grab image names from XML element tree
                        image_file_name_list = table.xpath("//img/@src")
                        # grab the name of the first image
                        try:
                            image_file_name = image_file_name_list[0]
                            # grab the file info from the zip list
                            for file_info in kmz_file.filelist:
                                if image_file_name == file_info.orig_filename:
                                    # grab the image file itself
                                    image = kmz_file.open(image_file_name)  # get a handle on the image in the kmz file
                                    image_stream = BytesIO(image.read()) # read the image without saving to disk
                                    save_file_name = str(omo_mursi_occ.id) + "_" + image_file_name  # rename
                                    omo_mursi_occ.image.save(save_file_name, File(image_stream))  # save the image
                                    break
                        except IndexError:
                            pass

                    feature_count += 1

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