Exemple #1
0
def metadata_file(extension, version, lang, feature):
    """Get the best metadata file.

    :param extension: The extension 'xml' or 'keywords' we expect.
    :rtype extension: str

    :param version: The InaSAFE version.
    :rtype version: float

    :param lang: The language of the user.
    :rtype lang: str

    :param feature: The feature type:
        buildings, building-points, roads, potential-idp, boundary-[1,11]
    :rtype feature: str

    :return: The filename.
    :rtype: str
    """
    base_path = shapefile_resource_base_path(feature)

    if extension == 'keywords':
        # We check for only the localised file.
        prefix = '-%s.keywords' % lang
        source_path = '%s%s' % (base_path, prefix)
        if not os.path.isfile(source_path):
            # If not, we take the english version.
            prefix = '-en.keywords'

    else:
        # Extension is xml.
        # We check first for the perfect file (version and lang).
        prefix = '-%s-%s.xml' % (version, lang)
        source_path = '%s%s' % (base_path, prefix)

        if not os.path.isfile(source_path):
            # If not, we check for the same version, but in english.
            prefix = '-%s-en.xml' % version
            source_path = '%s%s' % (base_path, prefix)

            if not os.path.isfile(source_path):
                # We check for the maximum version available and localised.
                latest = latest_xml_metadata_file(feature)

                prefix = '-%s-%s.xml' % (latest, lang)
                source_path = '%s%s' % (base_path, prefix)
                if not os.path.isfile(source_path):
                    # We take the maximum version available in english.
                    prefix = '-%s-en.xml' % latest
    return prefix
def metadata_file(extension, version, lang, feature):
    """Get the best metadata file.

    :param extension: The extension 'xml' or 'keywords' we expect.
    :rtype extension: str

    :param version: The InaSAFE version.
    :rtype version: float

    :param lang: The language of the user.
    :rtype lang: str

    :param feature: The feature type:
        buildings, building-points, roads, potential-idp, boundary-[1,11]
    :rtype feature: str

    :return: The filename.
    :rtype: str
    """
    base_path = shapefile_resource_base_path(feature)

    if extension == 'keywords':
        # We check for only the localised file.
        prefix = '-%s.keywords' % lang
        source_path = '%s%s' % (base_path, prefix)
        if not os.path.isfile(source_path):
            # If not, we take the english version.
            prefix = '-en.keywords'

    else:
        # Extension is xml.
        # We check first for the perfect file (version and lang).
        prefix = '-%s-%s.xml' % (version, lang)
        source_path = '%s%s' % (base_path, prefix)

        if not os.path.isfile(source_path):
            # If not, we check for the same version, but in english.
            prefix = '-%s-en.xml' % version
            source_path = '%s%s' % (base_path, prefix)

            if not os.path.isfile(source_path):
                # We check for the maximum version available and localised.
                latest = latest_xml_metadata_file(feature)

                prefix = '-%s-%s.xml' % (latest, lang)
                source_path = '%s%s' % (base_path, prefix)
                if not os.path.isfile(source_path):
                    # We take the maximum version available in english.
                    prefix = '-%s-en.xml' % latest
    return prefix
Exemple #3
0
def latest_xml_metadata_file(feature):
    """Get the latest version available of the XML metadata for the feature.

    :param feature: The type of feature:
        buildings, building-points, roads, potential-idp, boundary-[1,11]
    :type feature: str

    :return The latest version available.
    :rtype float
    """
    base_path = shapefile_resource_base_path(feature)
    directory = os.path.dirname(os.path.abspath(base_path))
    files = os.listdir(directory)
    resource = os.path.basename(base_path)
    regexp = r'^%s-(\d.\d)-en.xml' % resource

    max_version = None
    for one_file in files:
        r = re.search(regexp, one_file)
        if r:
            version = float(r.group(1))
            if not max_version or max_version < version:
                max_version = version
    return max_version
def latest_xml_metadata_file(feature):
    """Get the latest version available of the XML metadata for the feature.

    :param feature: The type of feature:
        buildings, building-points, roads, potential-idp, boundary-[1,11]
    :type feature: str

    :return The latest version available.
    :rtype float
    """
    base_path = shapefile_resource_base_path(feature)
    directory = os.path.dirname(os.path.abspath(base_path))
    files = os.listdir(directory)
    resource = os.path.basename(base_path)
    regexp = r'^%s-(\d.\d)-en.xml' % resource

    max_version = None
    for one_file in files:
        r = re.search(regexp, one_file)
        if r:
            version = float(r.group(1))
            if not max_version or max_version < version:
                max_version = version
    return max_version
def extract_shapefile(
        feature_type,
        db_name,
        directory_name,
        qgis_version=2,
        output_prefix='',
        inasafe_version=None,
        lang='en'):
    """Extract a database to a shapefile.

    This is a multi-step process:
        * Create a temporary postgis database
        * Load the osm dataset into POSTGIS with osm2pgsql and our custom
             style file.
        * Save the data out again to a shapefile
        * Zip the shapefile ready for user to download

    :param feature_type: The feature to extract.
    :type feature_type: str

    :param db_name: The database to extract.
    :type db_name: str

    :param directory_name: The directory to use for the extract.
    :type directory_name: str

    :param qgis_version: Get the QGIS version. Currently 1,
        2 are accepted, default to 2. A different qml style file will be
        returned depending on the version
    :type qgis_version: int

    :param output_prefix: Base name for the shape file. Defaults to ''
        which will result in an output file of feature_type + '.shp'. Adding a
        prefix of e.g. 'test-' would result in a downloaded file name of
        'test-buildings.shp'. Allowed characters are [a-zA-Z-_0-9].
    :type output_prefix: str

    :param inasafe_version: The InaSAFE version, to get correct metadata.
    :type inasafe_version: str

    :param lang: The language desired for the labels in the legend.
        Example : 'en', 'fr', etc. Default is 'en'.
    :type lang: str

    :returns: Path to zipfile that was created.
    :rtype: str
    """
    # Extract
    os.makedirs(directory_name)
    shapefile_resource_path = shapefile_resource_base_path(feature_type)

    shape_path = os.path.join(directory_name, '%s.shp' % output_prefix)

    if qgis_version > 1:
        qml_source_path = '%s-%s.qml' % (shapefile_resource_path, lang)
        if not os.path.isfile(qml_source_path):
            qml_source_path = '%s-en.qml' % shapefile_resource_path
    else:
        qml_source_path = '%s-qgis1.qml' % shapefile_resource_path

    qml_dest_path = os.path.join(directory_name, '%s.qml' % output_prefix)

    license_source_path = '%s.license' % generic_shapefile_base_path()
    license_dest_path = os.path.join(
        directory_name, '%s.license' % output_prefix)
    prj_source_path = '%s.prj' % generic_shapefile_base_path()
    prj_dest_path = os.path.join(
        directory_name, '%s.prj' % output_prefix)

    pgsql2shp_executable = which('pgsql2shp')[0]
    pgsql2shp_command = '%s -f %s %s %s' % (
        pgsql2shp_executable, shape_path, db_name, SQL_QUERY_MAP[feature_type])

    # Now run the commands in sequence:
    LOGGER.info(pgsql2shp_command)
    call(pgsql2shp_command, shell=True)
    copyfile(qml_source_path, qml_dest_path)

    metadata = metadata_files(
        inasafe_version, lang, feature_type, output_prefix)

    for destination, source in metadata.items():
        source_path = '%s%s' % (shapefile_resource_path, source)
        destination_path = os.path.join(directory_name, destination)
        copyfile(source_path, destination_path)
        add_metadata_timestamp(destination_path)

    # Generic files
    copyfile(prj_source_path, prj_dest_path)
    copyfile(license_source_path, license_dest_path)

    # Now zip it up and return the path to the zip, removing the original shp
    zipfile = zip_shp(
        shape_path,
        extra_ext=['.qml', '.keywords', '.license', '.xml'],
        remove_file=True)
    LOGGER.info('Shape written to {path}'.format(path=shape_path))

    return zipfile
Exemple #6
0
def extract_shapefile(feature_type,
                      db_name,
                      directory_name,
                      qgis_version=2,
                      output_prefix='',
                      inasafe_version=None,
                      lang='en'):
    """Extract a database to a shapefile.

    This is a multi-step process:
        * Create a temporary postgis database
        * Load the osm dataset into POSTGIS with osm2pgsql and our custom
             style file.
        * Save the data out again to a shapefile
        * Zip the shapefile ready for user to download

    :param feature_type: The feature to extract.
    :type feature_type: str

    :param db_name: The database to extract.
    :type db_name: str

    :param directory_name: The directory to use for the extract.
    :type directory_name: str

    :param qgis_version: Get the QGIS version. Currently 1,
        2 are accepted, default to 2. A different qml style file will be
        returned depending on the version
    :type qgis_version: int

    :param output_prefix: Base name for the shape file. Defaults to ''
        which will result in an output file of feature_type + '.shp'. Adding a
        prefix of e.g. 'test-' would result in a downloaded file name of
        'test-buildings.shp'. Allowed characters are [a-zA-Z-_0-9].
    :type output_prefix: str

    :param inasafe_version: The InaSAFE version, to get correct metadata.
    :type inasafe_version: str

    :param lang: The language desired for the labels in the legend.
        Example : 'en', 'fr', etc. Default is 'en'.
    :type lang: str

    :returns: Path to zipfile that was created.
    :rtype: str
    """
    # Extract
    os.makedirs(directory_name)
    shapefile_resource_path = shapefile_resource_base_path(feature_type)

    shape_path = os.path.join(directory_name, '%s.shp' % output_prefix)

    if qgis_version > 1:
        qml_source_path = '%s-%s.qml' % (shapefile_resource_path, lang)
        if not os.path.isfile(qml_source_path):
            qml_source_path = '%s-en.qml' % shapefile_resource_path
    else:
        qml_source_path = '%s-qgis1.qml' % shapefile_resource_path

    qml_dest_path = os.path.join(directory_name, '%s.qml' % output_prefix)

    license_source_path = '%s.license' % generic_shapefile_base_path()
    license_dest_path = os.path.join(directory_name,
                                     '%s.license' % output_prefix)
    prj_source_path = '%s.prj' % generic_shapefile_base_path()
    prj_dest_path = os.path.join(directory_name, '%s.prj' % output_prefix)

    pgsql2shp_executable = which('pgsql2shp')[0]
    pgsql2shp_command = '%s -f %s %s %s' % (
        pgsql2shp_executable, shape_path, db_name, SQL_QUERY_MAP[feature_type])

    # Now run the commands in sequence:
    LOGGER.info(pgsql2shp_command)
    call(pgsql2shp_command, shell=True)
    copyfile(qml_source_path, qml_dest_path)

    metadata = metadata_files(inasafe_version, lang, feature_type,
                              output_prefix)

    for destination, source in metadata.items():
        source_path = '%s%s' % (shapefile_resource_path, source)
        destination_path = os.path.join(directory_name, destination)
        copyfile(source_path, destination_path)
        add_metadata_timestamp(destination_path)

    # Generic files
    copyfile(prj_source_path, prj_dest_path)
    copyfile(license_source_path, license_dest_path)

    # Now zip it up and return the path to the zip, removing the original shp
    zipfile = zip_shp(shape_path,
                      extra_ext=['.qml', '.keywords', '.license', '.xml'],
                      remove_file=True)
    LOGGER.info('Shape written to {path}'.format(path=shape_path))

    return zipfile