Package madrona :: Package raster_stats :: Package management :: Package commands :: Module update_rasterdatasets
[hide private]

Source Code for Module madrona.raster_stats.management.commands.update_rasterdatasets

  1  from django.core.management.base import BaseCommand, AppCommand 
  2  from optparse import make_option 
  3  import os 
  4   
5 -class Command(BaseCommand):
6 option_list = AppCommand.option_list + ( 7 make_option('--ext', action='store', dest='ext', default=None, 8 help="Extension to be used for rasters in folder."), 9 make_option('--dirpath', action='store', dest='dirpath', 10 help="Denotes directory location."), 11 make_option('--filepath', action='store', dest='filepath', 12 help="Denotes file location."), 13 make_option('--storepath', action='store', dest='storepath', 14 help="Denotes alternate directory path."), 15 make_option('--name', action='store', dest='name', default=None, 16 help="Name given to raster dataset (only used for --filepath uploads)."), 17 make_option('--type', action='store', dest='type', default='continuous', 18 help="Type assigned to raster dataset (used for both --filepath and --dirpath uploads)."), 19 make_option('--force', action='store_true', dest='force', default=False, 20 help="Force raster dataset upload even if filepath does not exist."), 21 make_option('--prefix', action='store', dest='prefix', default='', 22 help="String that is prepended to the name field of each rasterdataset found in the directory path."), 23 ) 24 help = """Populates the raster_stats_rasterdataset model with rasters 25 26 Use the --dirpath with --ext option when providing a directory of rasters 27 28 python manage.py update_rasterdatasets --dirpath path_to_rasters/raster_dir --ext .tif 29 30 Use -ext grid when using ESRI Grid raster types 31 32 python manage.py update_rasterdatasets --dirpath path_to_rasters/raster_dir --ext grid 33 34 Use the --filepath option (without the --ext option) when providing a path to a single file 35 36 python manage.py update_rasterdatasets --filepath path_to_rasters/raster_dir/my_raster.tif 37 38 The --force option can be used with the --filepath option for times when the filepath does not actually exist on your system 39 40 python manage.py update_rasterdatasets --filepath path_to_rasters/raster_dir/my_raster.tif --force 41 42 Use the --name option with the --filepath option to provide a name value to the RasterDataset 43 44 python manage.py update_rasterdatasets --filepath path_to_rasters/raster_dir/my_raster.tif --name "my favorite raster" 45 46 Use the --type option with either the --filepath or the --dirpath options to denote raster type (default is 'continuous') 47 48 python manage.py update_rasterdatasets --filepath path_to_rasters/raster_dir/my_raster.tif --name "my favorite raster" --type "continuous" 49 50 The --storepath option can be used as an alternative filepath value. Generally used with --dirpath, the value of this option will be joined with the filenames to create new filepaths used when creating the RasterDataset 51 52 python manage.py update_rasterdatasets --dirpath path_to_rasters/raster_dir --ext .tif --storepath server_path/raster_dir 53 54 The --prefix option can be used in conjunction with --dirpath to prepend a string to the raster names 55 56 python manage.py update_rasterdatasets --dirpath path_to_rasters/raster_dir --ext .tif --prefix my_ 57 58 NOTE: The RasterDataset model expects absolute paths. Absolute paths will be stored regardless of whether filepath or dirpath is absolute or relative. 59 NOTE: This function is not recursive, it loads only rasters found in the immediate directory 60 61 """ 62 args = '[ext, dirpath, filepath, name, type, force, storepath, prefix]' 63
64 - def handle(self, **options):
65 from madrona.raster_stats.models import RasterDataset 66 ext = options.get('ext') 67 dirpath = options.get('dirpath') 68 filepath = options.get('filepath') 69 name = options.get('name') 70 type = options.get('type') 71 force = options.get('force') 72 storepath = options.get('storepath') 73 prefix = options.get('prefix') 74 print 75 print 76 try: 77 if filepath: 78 if name is None: 79 path, filename = os.path.split(filepath) 80 name = filename 81 if os.path.exists(filepath) or force: 82 if storepath: 83 path = os.path.join(storepath, name) 84 else: 85 path = os.path.join(filepath, name) 86 abspath = os.path.abspath(path) 87 RasterDataset.objects.create(name=prefix + name, filepath=abspath, type=type) 88 else: 89 print 'Filepath: %s does not exist' % filepath 90 raise 91 elif dirpath: 92 if ext is None: 93 print "The --ext option is required for --dirpath uploads." 94 raise 95 if not os.path.exists(dirpath): 96 print 'Dirpath: %s does not exist' % dirpath 97 raise 98 for content in os.listdir(dirpath): 99 if ext == 'grid': 100 filepath = os.path.join(dirpath, content) 101 if os.path.isdir(filepath): 102 if 'log' in os.listdir(filepath) and any('.adf' in filename for filename in os.listdir(filepath)): 103 name = content 104 if storepath: 105 path = os.path.join(storepath, name) 106 else: 107 path = os.path.join(filepath) 108 abspath = os.path.abspath(path) 109 RasterDataset.objects.create(name=prefix + name, filepath=abspath, type=type) 110 else: 111 root, file_extension = os.path.splitext(content) 112 if file_extension == ext or file_extension.replace('.','') == ext: 113 name = content 114 if storepath: 115 path = os.path.join(storepath, name) 116 else: 117 path = os.path.join(dirpath, name) 118 abspath = os.path.abspath(path) 119 RasterDataset.objects.create(name=prefix + name, filepath=abspath, type=type) 120 else: 121 print "Either --filepath or --dirpath is required." 122 raise 123 124 except Exception as inst: 125 print 'No rasterdatasets were loaded.'
126