def validate(args):
if 'url' not in args:
return json.dumps({'status': 'failure', 'error': 'url missing'}), 400, \
{ "Content-Type": "application/json" }
remove_tmpfile = False
url = args.get('url')
if 'local_filename' in args:
ds = gdal.OpenEx(args['local_filename'], allowed_drivers = ['GTiff'])
else:
use_vsicurl = args.get('use_vsicurl', 'true')
if use_vsicurl.lower() not in ('true', 'false'):
return json.dumps({'status': 'failure', 'error': 'invalid value for use_vsicurl option. Expected true or false'}), 400, { "Content-Type": "application/json" }
use_vsicurl = use_vsicurl.lower() == 'true'
gdal.SetConfigOption('GDAL_DISABLE_READDIR_ON_OPEN', 'EMPTY_DIR')
if use_vsicurl:
ds = gdal.OpenEx('/vsicurl/' + url, allowed_drivers = ['GTiff'])
if ds is None:
f = gdal.VSIFOpenL('/vsicurl/' + url, 'rb')
if f is None:
return json.dumps({'status': 'failure', 'error': 'Cannot download %s' % url}), 400, { "Content-Type": "application/json" }
data = gdal.VSIFReadL(1,1,f)
gdal.VSIFCloseL(f)
if len(data) == 0:
error_msg = 'Cannot download %s' % url
gdal_error_msg = gdal.GetLastErrorMsg()
if gdal_error_msg == '':
gdal_error_msg = gdal.VSIGetLastErrorMsg()
if gdal_error_msg != '':
error_msg += ': '+ gdal_error_msg
return json.dumps({'status': 'failure', 'error': error_msg}), 400, { "Content-Type": "application/json" }
else:
try:
r = requests.get(url)
except Exception, e:
return json.dumps({'status': 'failure', 'error': 'Cannot download %s' % url}), 400, { "Content-Type": "application/json" }
remove_tmpfile = True
f = open(tmpfilename, 'wb')
f.write(r.content)
f.close()
ds = gdal.OpenEx(tmpfilename, allowed_drivers = ['GTiff'])
if ds is None:
return json.dumps({'status': 'failure', 'error': '%s is not a GTiff file' % url}), 400, { "Content-Type": "application/json" }
errors, details = validate_cloud_optimized_geotiff.validate(ds)
info = gdal.Info(ds, format = 'json')
if 'local_filename' in args or remove_tmpfile:
del info['files']
info['description'] = url
ds = None
if remove_tmpfile:
os.unlink(tmpfilename)
if len(errors) == 0:
return json.dumps({'status': 'success', 'gdal_info' : info, 'details': details}), 200, { "Content-Type": "application/json" }
else:
return json.dumps({'status': 'failure', 'gdal_info' : info, 'details': details, 'validation_errors': errors}), 400, { "Content-Type": "application/json" }
评论列表
文章目录