cog_validator.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:cog_validator 作者: rouault 项目源码 文件源码
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" }
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号