Skip to content

Commit

Permalink
Added template support to scripts
Browse files Browse the repository at this point in the history
Added createtemplates script

Added template support to creategdb script
  • Loading branch information
JeffJacobson committed Aug 4, 2017
1 parent a624794 commit b1b895e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 28 deletions.
3 changes: 3 additions & 0 deletions .vscode/cSpell.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
"arcpy",
"creategdb",
"createwsdottrafficgdb",
"createwsdottraffictemplategdb",
"docstrings",
"dumpjson",
"dumpwsdottrafficjson",
"fgdb",
"mkdir",
"multipointtopoint",
"nargs",
"pylint",
"rtype",
"unittest"
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

setup(
name="wsdot.traffic",
version="1.2.1",
version="1.3.0",
description="Retrieves data from WSDOT Traffic API",
long_description=LONG_DESC,
url="https://github.com/WSDOT-GIS/wsdot-traffic-gp",
Expand Down Expand Up @@ -42,6 +42,7 @@
entry_points={
'console_scripts': [
'createwsdottrafficgdb = wsdot.traffic.gp.creategdb:main',
'createwsdottraffictemplategdb = wsdot.traffic.gp.createtemplates:main',
'dumpwsdottrafficjson = wsdot.traffic.dumpjson:main',
'multipointtopoint = wsdot.traffic.gp.multipointtopoint:main'
]
Expand Down
16 changes: 9 additions & 7 deletions src/wsdot/traffic/gp/creategdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def main():
parser.add_argument("--gdb-path", type=str, default=default_gdb_path,
help='Path to where the GDB will be created. Defaults to "%s".' % default_gdb_path,
nargs="?")
p_help = "WSDOT Traffic API code. Defaults to value of %s environment variable if available. If this environment variable does not exist, than this parameter is required." % api_code_var_name
parser.add_argument("--templates-gdb", help="Path to GDB with template feature classes. (Creating feature classes with templates is faster than using the Add Field tool.)")
p_help = "WSDOT Traffic API code. Defaults to value of %s environment variable if available. If this environment variable does not exist, then this parameter is required." % api_code_var_name
parser.add_argument("--code", "-c", type=str,
required=api_code is None, default=api_code,
help=p_help)
Expand All @@ -59,7 +60,9 @@ def main():
names = default_names
else:
names = args.names
create_gdb(args.gdb_path, args.code, None, names)

templates_gdb = args.templates_gdb
create_gdb(args.gdb_path, args.code, templates_gdb, names)


def create_gdb(out_gdb_path="./TravelerInfo.gdb", access_code=None,
Expand All @@ -82,7 +85,7 @@ def create_gdb(out_gdb_path="./TravelerInfo.gdb", access_code=None,

# Download each of the REST endpoints.
for name in names:
_LOGGER.info("Contacting %(url)s...", {"url": URLS[name]})
print("Contacting %s..." % URLS[name])
# If user provided access code, use it.
# Otherwise don't provide to function, which will use default from
# environment or text file.`
Expand All @@ -92,15 +95,14 @@ def create_gdb(out_gdb_path="./TravelerInfo.gdb", access_code=None,
data = get_traveler_info(name)
out_table = os.path.join(out_gdb_path, name)
create_table(out_table, None, data, templates_gdb)
_LOGGER.info("Compressing data in %(out_gdb_path)s...",
{"out_gdb_path": out_gdb_path})
print("Compressing data in %s..." % out_gdb_path)

zip_path = "%s.zip" % out_gdb_path
_LOGGER.info("Creating %(zip_path)s...", {"zip_path", zip_path})
print("Creating %s..." % zip_path)
if os.path.exists(zip_path):
os.remove(zip_path)
with zipfile.ZipFile(zip_path, "w") as out_zip:
_LOGGER.info("Adding files to zip...")
print("Adding files to zip...")
for dirpath, dirnames, filenames in os.walk(out_gdb_path):
del dirnames
for file_name in filenames:
Expand Down
50 changes: 30 additions & 20 deletions src/wsdot/traffic/gp/createtemplates.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from __future__ import (absolute_import, division, print_function,
unicode_literals)

import argparse
import os
from os import path
import logging
Expand All @@ -13,27 +14,36 @@

_LOGGER = logging.getLogger(__name__)

if __name__ == '__main__':
# Get the .. directory
DATA_DIR = path.abspath("Data")

# Create the data dir if it does not exist already.
if not path.exists(DATA_DIR):
_LOGGER.info("Creating directory, %(dir)s...", {"dir": DATA_DIR})
os.mkdir(DATA_DIR)
else:
_LOGGER.info("%(dir)s already exists. Skipping creation step.",
{"dir": DATA_DIR})
def main():

parser = argparse.ArgumentParser("Create templates feature class", None,
"Create template feature classes")
default_path = "Templates.gdb"
parser.add_argument("template_gdb", help="Path where template file GDB will be created. Defaults to %s" % default_path,
default=default_path, nargs='?')
parser.add_argument("--overwrite", action='store_true',
help='Overwrite existing filegeodatabase by deleting and recreating it.')

args = parser.parse_args()

# Create the Templates GDB
arcpy.env.overwriteOutput = True
GDB_NAME = "Templates.gdb"
GDB_PATH = path.join(DATA_DIR, GDB_NAME)
MSG_DICT = {"gdb_path": GDB_PATH}
_LOGGER.info("Creating %(gdb_path)s...", MSG_DICT)
arcpy.management.CreateFileGDB(DATA_DIR, GDB_NAME)
for key in TABLE_DEFS_DICT_DICT:
MSG_DICT["table_name"] = key
_LOGGER.info("Creating %(table_name)s in %(gdb_path)s...", MSG_DICT)
create_table(path.join(GDB_PATH, key))
gdb_path = args.template_gdb
workspace, name = os.path.split(args.template_gdb)
if not workspace:
workspace = "."
arcpy.env.overwriteOutput = args.overwrite

if arcpy.env.overwriteOutput or not os.path.exists(gdb_path):
print("Creating %s..." % gdb_path)
arcpy.management.CreateFileGDB(workspace, name)
for key in TABLE_DEFS_DICT_DICT:
print("Creating %s in %s..." % (key, gdb_path))
create_table(path.join(gdb_path, key))
else:
print('"%s" already exists and will not be recreated.' % gdb_path)
_LOGGER.info("Completed")


if __name__ == '__main__':
main()

0 comments on commit b1b895e

Please sign in to comment.