コード例 #1
0
    def test_get_database_connection(self, connect):
        from sqlite3 import Row

        cursor_mock = Mock()
        cursor_mock.fetchone.return_value = "test"
        connect().__enter__().cursor.return_value = cursor_mock

        # Test that a connection object is returned
        with get_database_connection(self.path) as conn:
            self.assertEqual(conn.cursor().fetchone(), "test")

        # Test that the row_factory property is correctly set to sqlite3.Row
        self.assertEqual(get_database_connection(self.path).row_factory, Row)
コード例 #2
0
def add_geopackage_metadata(filepath, job, provider):
    """Add metadata information to the geopackage at the specified location."""
    bbox = job.extents
    job_description = job.description
    provider_description = provider.service_description
    # Read layers from the geopackage, these all need to be added to the metadata
    # Layers are the entries in `gpkg_contents` -- each entry has a corresponding table with data.
    with get_database_connection(filepath) as conn:
        cursor = conn.cursor()
        layers = Geopackage.get_layers(cursor)

    # Create a Generator object that will handle adding the necessary XML.
    generator = Generator(filepath, logger)
    for _layer in layers:
        # Add a layer identity to the meta data. If already present, the identity will be updated.
        generator.add_layer_identity(
            table_name=_layer["table_name"],
            abstract_msg=build_abstract(job_description, provider_description,
                                        _layer["description"]),
            bbox=dict(
                min_x=_layer["min_x"] or bbox[0],
                min_y=_layer["min_y"] or bbox[1],
                max_x=_layer["max_x"] or bbox[2],
                max_y=_layer["max_y"] or bbox[3],
            ),
            srs_id=_layer["srs_id"],
            srs_organization="EPSG",
            organization_name="EventKit",
        )
    generator.write_metadata()
コード例 #3
0
    def __load_metadata(self):
        """
        Finds any existing metadata and returns it, otherwise returns an empty string.

        This will grab all the entries in the gpkg_metadata Table
        :return: a list of Metadata in the GeoPackage
        """
        with get_database_connection(self.geopackage_path) as db:
            cursor = db.cursor()
            Metadata.ensure_metadata_tables(cursor)
            db.commit()
            return Metadata.get_all_metadata(cursor=cursor)
コード例 #4
0
    def write_metadata(self):
        """
        Generates the metadata tables and adds the NSG metadata to the extensions table.
        Also for any calls to add_layer_identity it adds that data as well. Be sure to
        add layer identity information before calling this method, since this writes to the
        GeoPackage
        """
        with get_database_connection(self.geopackage_path) as db:
            # Add entry to specify that this GeoPackage supports the Metadata extension
            try:
                cursor = db.cursor()
                Metadata.ensure_metadata_tables(cursor)
                db.commit()
            except sqlite3.OperationalError:
                if self.log:
                    self.log.error(
                        "Failed to add the metadata entry to the extension table"
                    )
                    raise
            except sqlite3.IntegrityError:
                if self.log:
                    self.log.error(
                        "Failed to add the metadata entry to the extension table"
                    )

            # Add metadata entries
            try:
                root = self.tree
                # remove the extra whitespace to prep to make the xml pretty printed
                Generator.strip(root)
                # encode the xml in utf-8
                xml_tostring = tostring(root, encoding="UTF-8", method="xml")
                # pretty print the xml
                reparsed = minidom.parseString(xml_tostring)
                pretty_xml = reparsed.toprettyxml()

                # insert xml in gpkg
                nsg_metadata_entry = Metadata.nsg_entry_template(
                    id=1)  # NSG metadata must have id of 1
                nsg_metadata_entry["metadata"] = pretty_xml
                Metadata.insert_or_update_metadata_row(
                    cursor=cursor, metadata=nsg_metadata_entry)
                db.commit()
            except sqlite3.IntegrityError:
                if self.log:
                    self.log.error(
                        "Failed to add NSG profile metadata to the metadata tables"
                    )