Skip to content

Commit

Permalink
added support for VRT files and filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
jsanz committed May 25, 2018
1 parent 96f1e76 commit 9141326
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions carto_cli/commands/dataset/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,22 @@ def get_cartodbfy_query(user,org,table_name):
else:
return 'SELECT CDB_CartoDBfyTable(\'{}\')'.format(table_name)

def get_vrt_type(postgis_type):
return postgis_type.replace('ST_','wkb')

@click.command(help="Display all your CARTO datasets")
@click.option('-f', '--format', default="json", help="Format of your results",
type=click.Choice(['json', 'csv', 'pretty']))
type=click.Choice(['json', 'csv', 'pretty','vrt']))
@click.option('-t', '--filter', help="Filter the datasets")
@click.help_option('-h', '--help')
@click.pass_context
def list(ctx, format):
def list(ctx, format, filter):
carto_obj = ctx.obj['carto']
manager = carto_obj.get_dataset_manager()
try:
datasets = manager.all()
all_datasets = manager.all()
datasets = [d for d in all_datasets if not filter or d.name.find(filter) > -1]

dataset_names = [dataset.name for dataset in datasets]

results = [
Expand Down Expand Up @@ -84,6 +89,24 @@ def list(ctx, format):
writer.writerow(row)

raw_result = csvfile.getvalue()
elif format == 'vrt':
with StringIO() as vrtfile:
vrtfile.write('<OGRVRTDataSource>')
for dataset in datasets:
types = dataset.table.geometry_types
if types and len(types) == 1:
geom_type = get_vrt_type(types[0])
template = '''
<OGRVRTLayer name="{layer}">
<LayerSRS>EPSG:4326</LayerSRS>
<SrcDataSource>Carto:{user}</SrcDataSource>
<GeometryType>{geomtype}</GeometryType>
<SrcLayer>{layer}</SrcLayer>
</OGRVRTLayer>'''.format(layer=dataset.name, geomtype=geom_type, user = carto_obj.user_name)

vrtfile.write(template)
vrtfile.write('\r\n</OGRVRTDataSource>\r\n')
raw_result = vrtfile.getvalue()
else:
table_general = PrettyTable(
['Table Name', 'Privacy', 'Locked', 'Likes', 'Size',
Expand Down Expand Up @@ -113,18 +136,19 @@ def list(ctx, format):

@click.command(help="List tables and their main Postgres statistics")
@click.option('-f', '--format', default="json", help="Format of your results",
type=click.Choice(['json', 'csv', 'pretty']))
type=click.Choice(['json', 'csv', 'pretty','vrt']))
@click.option('-t', '--filter', help="Filter the tables")
@click.help_option('-h', '--help')
@click.pass_context
def list_tables(ctx, format):
def list_tables(ctx, format, filter):
carto_obj = ctx.obj['carto']

if carto_obj.org_name:
schema_name = carto_obj.user_name
else:
schema_name = 'public'

sql = queries.LIST_TABLES.format(schema_name=schema_name,table_name='%')
sql = queries.LIST_TABLES.format(schema_name=schema_name,table_name='%{}%'.format(filter if filter else ''))
result = carto_obj.execute_sql(sql)

if format == 'json':
Expand Down Expand Up @@ -543,4 +567,4 @@ def cartodbfy(ctx, table_name):
table_name = table_name
))
click.echo("Batch SQL API job launched to cartodbfy the table")
click.echo(job_details['job_id'])
click.echo(job_details['job_id'])

0 comments on commit 9141326

Please sign in to comment.