Пример #1
0
def download_and_import(filename, database_type, database, schema, username,
                        password=None, port=None, host='localhost',
                        use_cache=False, download_dir=DEFAULT_DOWNLOAD_DIR,
                        language_code=DEFAULT_LANGUAGE_CODE,
                        keep_existing_data=False, recreate_tables=False):

    config.update(schema_name=schema, database_type=database_type,
                  database=database, username=username, password=password,
                  port=port, host=host)

    db_session = config.get_db_session()

    download_dir = normalize_path(download_dir)
    download_config = get_download_config(filename, language_code)
    local_filepaths = []
    for filename, opts in download_config.items():
        for local_filepath in download(opts, download_dir, use_cache):
            if opts.get('unzip') is True:
                local_filepath = unzip(
                    local_filepath, filename_to_extract=filename,
                    extract_dir=os.path.dirname(local_filepath))
            local_filepaths.append(local_filepath)

    create_geoname_tables(db_session, recreate_tables=recreate_tables)
    if not keep_existing_data:
        purge_geoname_tables(db_session)
    run_importers(db_session, download_dir, local_filepaths)
Пример #2
0
    def postal_codes_around(cls, postal_code, radius):
        """ List of postal codes within ``radius`` km around ``postal code``

        """

        session = config.get_db_session()
        conditions = []

        for center in session.query(cls).filter(
                        cls.postal_code == postal_code).all():
            conditions.append(
                func.ST_DWithin(cls.point, center.point, radius * 1000)
            )

        return session.query(cls).filter(or_(*conditions))
Пример #3
0
    def within_a_radius_of(self, radius):
        """ Find all other postal codes within the given radius.

        Args:
            self, km

        Returns:
            list of :class:`GeonamePostalCode`
        """

        session = config.get_db_session()

        return session.query(GeonamePostalCode).filter(
            func.ST_DWithin(GeonamePostalCode.point, self.point, radius * 1000)
        )