def import_osm_file(db_name, feature_type, file_path): """Import the OSM xml file into a postgis database. :param db_name: The database to use. :type db_name: str :param feature_type: The feature to import. :type feature_type: str :param file_path: Path to the OSM file. :type file_path: str """ overpass_resource_path = overpass_resource_base_path(feature_type) style_file = '%s.style' % overpass_resource_path # Used to standarise types while data is in pg still transform_path = '%s.sql' % overpass_resource_path createdb_executable = which('createdb')[0] createdb_command = '%s -T template_postgis %s' % (createdb_executable, db_name) osm2pgsql_executable = which('osm2pgsql')[0] osm2pgsql_options = config.OSM2PGSQL_OPTIONS osm2pgsql_command = '%s -S %s -d %s %s %s' % (osm2pgsql_executable, style_file, db_name, osm2pgsql_options, file_path) psql_executable = which('psql')[0] transform_command = '%s %s -f %s' % (psql_executable, db_name, transform_path) LOGGER.info(createdb_command) call(createdb_command, shell=True) LOGGER.info(osm2pgsql_command) call(osm2pgsql_command, shell=True) LOGGER.info(transform_command) call(transform_command, shell=True)
def drop_database(db_name): """Remove a database. :param db_name: The database :type db_name: str """ dropdb_executable = which('dropdb')[0] dropdb_command = '%s %s' % (dropdb_executable, db_name) LOGGER.info(dropdb_command) call(dropdb_command, shell=True)
def import_osm_file(db_name, feature_type, file_path): """Import the OSM xml file into a postgis database. :param db_name: The database to use. :type db_name: str :param feature_type: The feature to import. :type feature_type: str :param file_path: Path to the OSM file. :type file_path: str """ overpass_resource_path = overpass_resource_base_path(feature_type) style_file = '%s.style' % overpass_resource_path # Used to standarise types while data is in pg still transform_path = '%s.sql' % overpass_resource_path createdb_executable = which('createdb')[0] createdb_command = '%s -T template_postgis %s' % ( createdb_executable, db_name) osm2pgsql_executable = which('osm2pgsql')[0] osm2pgsql_options = config.OSM2PGSQL_OPTIONS osm2pgsql_command = '%s -S %s -d %s %s %s' % ( osm2pgsql_executable, style_file, db_name, osm2pgsql_options, file_path) psql_executable = which('psql')[0] transform_command = '%s %s -f %s' % ( psql_executable, db_name, transform_path) LOGGER.info(createdb_command) call(createdb_command, shell=True) LOGGER.info(osm2pgsql_command) call(osm2pgsql_command, shell=True) LOGGER.info(transform_command) call(transform_command, shell=True)
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
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