Skip to content

Commit

Permalink
Merge pull request #70 from mholthausen/database-config
Browse files Browse the repository at this point in the history
Adds optional support for database host and port
  • Loading branch information
delhomer committed Jan 31, 2020
2 parents 537fd19 + 9ec2034 commit 5c60fe8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions docs/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ correctly placed. Usage example:
The database export requires a user name, a database name, the name of the table
and its column that contains the geometry and (optionaly) the name of the column
that contains the object's ID. Usage example:
that contains the object's ID and the host and port. Usage example:

.. code-block:: shell
$ py3dtiles export -t table -D database -c geometry_column
$ py3dtiles export -t table -D database -c geometry_column -i id -u oslandia -H localhost -P 5432
14 changes: 10 additions & 4 deletions py3dtiles/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,14 @@ def wkbs2tileset(wkbs, ids, transform):
arrays2tileset(positions, normals, bboxes, transform, ids)


def from_db(db_name, table_name, column_name, id_column_name, user_name):
def from_db(db_name, table_name, column_name, id_column_name, user_name, host=None, port=None):
user = getpass.getuser() if user_name is None else user_name

try:
connection = psycopg2.connect(dbname=db_name, user=user)
connection = psycopg2.connect(dbname=db_name, user=user, host=host, port=port)
except psycopg2.OperationalError:
pw = getpass.getpass("Postgres password for user {}\n".format(user))
connection = psycopg2.connect(dbname=db_name, user=user, password=pw)
connection = psycopg2.connect(dbname=db_name, user=user, password=pw, host=host, port=port)

cur = connection.cursor()

Expand Down Expand Up @@ -318,14 +318,20 @@ def init_parser(subparser, str2bool):
u_help = 'database user name'
parser.add_argument('-u', metavar='USER', type=str, help=u_help)

H_help = 'database host'
parser.add_argument('-H', metavar='HOST', type=str, help=H_help)

P_help = 'database port'
parser.add_argument('-P', metavar='PORT', type=int, help=P_help)


def main(args):
if args.D is not None:
if args.t is None or args.c is None:
print('Error: please define a table (-t) and column (-c)')
exit()

from_db(args.D, args.t, args.c, args.i, args.u)
from_db(args.D, args.t, args.c, args.i, args.u, args.H, args.P)
elif args.d is not None:
from_directory(args.d, args.o)
else:
Expand Down

0 comments on commit 5c60fe8

Please sign in to comment.