LNT: Auto-reformat files with black & isort

This commit is contained in:
snowman2 2022-08-11 20:49:57 -05:00
parent 80526c82d9
commit 6d5e76aee6
594 changed files with 141289 additions and 86109 deletions

View File

@ -3,238 +3,258 @@
# ----------------------------------
with section("parse"):
# Specify structure for custom cmake functions
additional_commands = {
'add_gdal_driver': { 'flags': ['BUILTIN', 'PLUGIN'], 'kwargs': {'DEF': 1, 'TARGET': 1, 'SOURCES': '+'}},
'gdal_driver': {
'flags': ['BUILTIN'],
'kwargs': {'TARGET': 1, 'SOURCES': '+', 'INCLUDES': '*', 'LIBRARIES': '*', 'DEFINITIONS': '*'}},
'gdal_dependent_format': {'pargs': {'nargs': 3}},
'gdal_driver_standard_includes': {'pargs': {'nargs': 1}},
'gdal_format': {'pargs': {'nargs': 2}},
'gdal_optional_format': {'pargs': {'nargs': 2}},
'gdal_target_interfaces': {'pargs': {'nargs': 1}},
'gdal_target_link_libraries': {'kwargs': {'TARGET': 1, 'LIBRARIES': '*'}},
'is_plugin': {'pargs': {'nargs': 2}},
'ogr_default_driver': {'pargs': {'nargs': 2}},
'ogr_default_driver2': {'pargs': {'nargs': 3}},
'ogr_dependent_driver': {'pargs': {'nargs': 3}},
'ogr_optional_driver': {'pargs': {'nargs': 2}},
'gdal_swig_binding_target': {'kwargs': {}, 'pargs': {'flags': [], 'nargs': '*'}},
'gdal_swig_bindings': {'kwargs': {}, 'pargs': {'flags': [], 'nargs': '*'}},
'gdal_csharp_dll': {
'flags': [],
'kwargs': {'TARGET': 1, 'NAMESPACE': 1, 'WRAPPER': 1, 'SWIG_INTERFACE': 1,
'CS_SOURCES': '*', 'WORKING_DIRECTORY': '*', 'DEPENDS': '*'}},
'gdal_java_wrap': { 'flags': ['CXX'], 'kwargs': {'TARGET': '*', 'ARGS': '*'}}
}
# Specify structure for custom cmake functions
additional_commands = {
"add_gdal_driver": {
"flags": ["BUILTIN", "PLUGIN"],
"kwargs": {"DEF": 1, "TARGET": 1, "SOURCES": "+"},
},
"gdal_driver": {
"flags": ["BUILTIN"],
"kwargs": {
"TARGET": 1,
"SOURCES": "+",
"INCLUDES": "*",
"LIBRARIES": "*",
"DEFINITIONS": "*",
},
},
"gdal_dependent_format": {"pargs": {"nargs": 3}},
"gdal_driver_standard_includes": {"pargs": {"nargs": 1}},
"gdal_format": {"pargs": {"nargs": 2}},
"gdal_optional_format": {"pargs": {"nargs": 2}},
"gdal_target_interfaces": {"pargs": {"nargs": 1}},
"gdal_target_link_libraries": {"kwargs": {"TARGET": 1, "LIBRARIES": "*"}},
"is_plugin": {"pargs": {"nargs": 2}},
"ogr_default_driver": {"pargs": {"nargs": 2}},
"ogr_default_driver2": {"pargs": {"nargs": 3}},
"ogr_dependent_driver": {"pargs": {"nargs": 3}},
"ogr_optional_driver": {"pargs": {"nargs": 2}},
"gdal_swig_binding_target": {
"kwargs": {},
"pargs": {"flags": [], "nargs": "*"},
},
"gdal_swig_bindings": {"kwargs": {}, "pargs": {"flags": [], "nargs": "*"}},
"gdal_csharp_dll": {
"flags": [],
"kwargs": {
"TARGET": 1,
"NAMESPACE": 1,
"WRAPPER": 1,
"SWIG_INTERFACE": 1,
"CS_SOURCES": "*",
"WORKING_DIRECTORY": "*",
"DEPENDS": "*",
},
},
"gdal_java_wrap": {"flags": ["CXX"], "kwargs": {"TARGET": "*", "ARGS": "*"}},
}
# Specify variable tags.
vartags = []
# Specify variable tags.
vartags = []
# Specify property tags.
proptags = []
# Specify property tags.
proptags = []
# -----------------------------
# Options affecting formatting.
# -----------------------------
with section("format"):
# How wide to allow formatted cmake files
line_width = 120
# How wide to allow formatted cmake files
line_width = 120
# How many spaces to tab for indent
tab_size = 2
# How many spaces to tab for indent
tab_size = 2
# If an argument group contains more than this many sub-groups (parg or kwarg
# groups) then force it to a vertical layout.
max_subgroups_hwrap = 2
# If an argument group contains more than this many sub-groups (parg or kwarg
# groups) then force it to a vertical layout.
max_subgroups_hwrap = 2
# If a positional argument group contains more than this many arguments, then
# force it to a vertical layout.
max_pargs_hwrap = 6
# If a positional argument group contains more than this many arguments, then
# force it to a vertical layout.
max_pargs_hwrap = 6
# If a cmdline positional group consumes more than this many lines without
# nesting, then invalidate the layout (and nest)
max_rows_cmdline = 2
# If a cmdline positional group consumes more than this many lines without
# nesting, then invalidate the layout (and nest)
max_rows_cmdline = 2
# If true, separate flow control names from their parentheses with a space
separate_ctrl_name_with_space = True
# If true, separate flow control names from their parentheses with a space
separate_ctrl_name_with_space = True
# If true, separate function names from parentheses with a space
separate_fn_name_with_space = False
# If true, separate function names from parentheses with a space
separate_fn_name_with_space = False
# If a statement is wrapped to more than one line, than dangle the closing
# parenthesis on its own line.
dangle_parens = False
# If a statement is wrapped to more than one line, than dangle the closing
# parenthesis on its own line.
dangle_parens = False
# If the trailing parenthesis must be 'dangled' on its on line, then align it
# to this reference: `prefix`: the start of the statement, `prefix-indent`:
# the start of the statement, plus one indentation level, `child`: align to
# the column of the arguments
dangle_align = 'prefix-indent'
# If the trailing parenthesis must be 'dangled' on its on line, then align it
# to this reference: `prefix`: the start of the statement, `prefix-indent`:
# the start of the statement, plus one indentation level, `child`: align to
# the column of the arguments
dangle_align = "prefix-indent"
# If the statement spelling length (including space and parenthesis) is
# smaller than this amount, then force reject nested layouts.
min_prefix_chars = 4
# If the statement spelling length (including space and parenthesis) is
# smaller than this amount, then force reject nested layouts.
min_prefix_chars = 4
# If the statement spelling length (including space and parenthesis) is larger
# than the tab width by more than this amount, then force reject un-nested
# layouts.
max_prefix_chars = 16
# If the statement spelling length (including space and parenthesis) is larger
# than the tab width by more than this amount, then force reject un-nested
# layouts.
max_prefix_chars = 16
# If a candidate layout is wrapped horizontally but it exceeds this many
# lines, then reject the layout.
max_lines_hwrap = 2
# If a candidate layout is wrapped horizontally but it exceeds this many
# lines, then reject the layout.
max_lines_hwrap = 2
# What style line endings to use in the output.
line_ending = 'unix'
# What style line endings to use in the output.
line_ending = "unix"
# Format command names consistently as 'lower' or 'upper' case
command_case = 'canonical'
# Format command names consistently as 'lower' or 'upper' case
command_case = "canonical"
# Format keywords consistently as 'lower' or 'upper' case
keyword_case = 'unchanged'
# Format keywords consistently as 'lower' or 'upper' case
keyword_case = "unchanged"
# A list of command names which should always be wrapped
always_wrap = []
# A list of command names which should always be wrapped
always_wrap = []
# If true, the argument lists which are known to be sortable will be sorted
# lexicographicall
enable_sort = True
# If true, the argument lists which are known to be sortable will be sorted
# lexicographicall
enable_sort = True
# If true, the parsers may infer whether or not an argument list is sortable
# (without annotation).
autosort = False
# If true, the parsers may infer whether or not an argument list is sortable
# (without annotation).
autosort = False
# By default, if cmake-format cannot successfully fit everything into the
# desired linewidth it will apply the last, most aggressive attempt that it
# made. If this flag is True, however, cmake-format will print error, exit
# with non-zero status code, and write-out nothing
require_valid_layout = False
# By default, if cmake-format cannot successfully fit everything into the
# desired linewidth it will apply the last, most aggressive attempt that it
# made. If this flag is True, however, cmake-format will print error, exit
# with non-zero status code, and write-out nothing
require_valid_layout = False
# A dictionary mapping layout nodes to a list of wrap decisions. See the
# documentation for more information.
layout_passes = {}
# A dictionary mapping layout nodes to a list of wrap decisions. See the
# documentation for more information.
layout_passes = {}
# ------------------------------------------------
# Options affecting comment reflow and formatting.
# ------------------------------------------------
with section("markup"):
# What character to use for bulleted lists
bullet_char = '*'
# What character to use for bulleted lists
bullet_char = "*"
# What character to use as punctuation after numerals in an enumerated list
enum_char = '.'
# What character to use as punctuation after numerals in an enumerated list
enum_char = "."
# If comment markup is enabled, don't reflow the first comment block in each
# listfile. Use this to preserve formatting of your copyright/license
# statements.
first_comment_is_literal = False
# If comment markup is enabled, don't reflow the first comment block in each
# listfile. Use this to preserve formatting of your copyright/license
# statements.
first_comment_is_literal = False
# If comment markup is enabled, don't reflow any comment block which matches
# this (regex) pattern. Default is `None` (disabled).
literal_comment_pattern = None
# If comment markup is enabled, don't reflow any comment block which matches
# this (regex) pattern. Default is `None` (disabled).
literal_comment_pattern = None
# Regular expression to match preformat fences in comments default=
# ``r'^\s*([`~]{3}[`~]*)(.*)$'``
fence_pattern = '^\\s*([`~]{3}[`~]*)(.*)$'
# Regular expression to match preformat fences in comments default=
# ``r'^\s*([`~]{3}[`~]*)(.*)$'``
fence_pattern = "^\\s*([`~]{3}[`~]*)(.*)$"
# Regular expression to match rulers in comments default=
# ``r'^\s*[^\w\s]{3}.*[^\w\s]{3}$'``
ruler_pattern = '^\\s*[^\\w\\s]{3}.*[^\\w\\s]{3}$'
# Regular expression to match rulers in comments default=
# ``r'^\s*[^\w\s]{3}.*[^\w\s]{3}$'``
ruler_pattern = "^\\s*[^\\w\\s]{3}.*[^\\w\\s]{3}$"
# If a comment line matches starts with this pattern then it is explicitly a
# trailing comment for the preceding argument. Default is '#<'
explicit_trailing_pattern = '#<'
# If a comment line matches starts with this pattern then it is explicitly a
# trailing comment for the preceding argument. Default is '#<'
explicit_trailing_pattern = "#<"
# If a comment line starts with at least this many consecutive hash
# characters, then don't lstrip() them off. This allows for lazy hash rulers
# where the first hash char is not separated by space
hashruler_min_length = 10
# If a comment line starts with at least this many consecutive hash
# characters, then don't lstrip() them off. This allows for lazy hash rulers
# where the first hash char is not separated by space
hashruler_min_length = 10
# If true, then insert a space between the first hash char and remaining hash
# chars in a hash ruler, and normalize its length to fill the column
canonicalize_hashrulers = True
# If true, then insert a space between the first hash char and remaining hash
# chars in a hash ruler, and normalize its length to fill the column
canonicalize_hashrulers = True
# enable comment markup parsing and reflow
enable_markup = True
# enable comment markup parsing and reflow
enable_markup = True
# ----------------------------
# Options affecting the linter
# ----------------------------
with section("lint"):
# a list of lint codes to disable
disabled_codes = []
# a list of lint codes to disable
disabled_codes = []
# regular expression pattern describing valid function names
function_pattern = '[0-9a-z_]+'
# regular expression pattern describing valid function names
function_pattern = "[0-9a-z_]+"
# regular expression pattern describing valid macro names
macro_pattern = '[0-9A-Z_]+'
# regular expression pattern describing valid macro names
macro_pattern = "[0-9A-Z_]+"
# regular expression pattern describing valid names for variables with global
# scope
global_var_pattern = '[0-9A-Z][0-9A-Z_]+'
# regular expression pattern describing valid names for variables with global
# scope
global_var_pattern = "[0-9A-Z][0-9A-Z_]+"
# regular expression pattern describing valid names for variables with global
# scope (but internal semantic)
internal_var_pattern = '_[0-9A-Z][0-9A-Z_]+'
# regular expression pattern describing valid names for variables with global
# scope (but internal semantic)
internal_var_pattern = "_[0-9A-Z][0-9A-Z_]+"
# regular expression pattern describing valid names for variables with local
# scope
local_var_pattern = '[0-9a-z_]+'
# regular expression pattern describing valid names for variables with local
# scope
local_var_pattern = "[0-9a-z_]+"
# regular expression pattern describing valid names for privatedirectory
# variables
private_var_pattern = '_[0-9a-z_]+'
# regular expression pattern describing valid names for privatedirectory
# variables
private_var_pattern = "_[0-9a-z_]+"
# regular expression pattern describing valid names for publicdirectory
# variables
public_var_pattern = '[0-9A-Z][0-9A-Z_]+'
# regular expression pattern describing valid names for publicdirectory
# variables
public_var_pattern = "[0-9A-Z][0-9A-Z_]+"
# regular expression pattern describing valid names for keywords used in
# functions or macros
keyword_pattern = '[0-9A-Z_]+'
# regular expression pattern describing valid names for keywords used in
# functions or macros
keyword_pattern = "[0-9A-Z_]+"
# In the heuristic for C0201, how many conditionals to match within a loop in
# before considering the loop a parser.
max_conditionals_custom_parser = 2
# In the heuristic for C0201, how many conditionals to match within a loop in
# before considering the loop a parser.
max_conditionals_custom_parser = 2
# Require at least this many newlines between statements
min_statement_spacing = 1
# Require at least this many newlines between statements
min_statement_spacing = 1
# Require no more than this many newlines between statements
max_statement_spacing = 1
max_returns = 6
max_branches = 12
max_arguments = 5
max_localvars = 15
max_statements = 50
# Require no more than this many newlines between statements
max_statement_spacing = 1
max_returns = 6
max_branches = 12
max_arguments = 5
max_localvars = 15
max_statements = 50
# -------------------------------
# Options affecting file encoding
# -------------------------------
with section("encode"):
# If true, emit the unicode byte-order mark (BOM) at the start of the file
emit_byteorder_mark = False
# If true, emit the unicode byte-order mark (BOM) at the start of the file
emit_byteorder_mark = False
# Specify the encoding of the input file. Defaults to utf-8
input_encoding = 'utf-8'
# Specify the encoding of the input file. Defaults to utf-8
input_encoding = "utf-8"
# Specify the encoding of the output file. Defaults to utf-8. Note that cmake
# only claims to support utf-8 so be careful when using anything else
output_encoding = 'utf-8'
# Specify the encoding of the output file. Defaults to utf-8. Note that cmake
# only claims to support utf-8 so be careful when using anything else
output_encoding = "utf-8"
# -------------------------------------
# Miscellaneous configurations options.
# -------------------------------------
with section("misc"):
# A dictionary containing any per-command configuration overrides. Currently
# only `command_case` is supported.
per_command = {}
# A dictionary containing any per-command configuration overrides. Currently
# only `command_case` is supported.
per_command = {}

View File

@ -30,8 +30,8 @@
###############################################################################
import gdaltest
from osgeo import gdal, osr
###############################################################################
@ -40,10 +40,9 @@ from osgeo import gdal, osr
def test_applyverticalshiftgrid_1():
src_ds = gdal.Open('../gcore/data/byte.tif')
src_ds = gdal.Translate('', src_ds, format='MEM',
width=20, height=40)
grid_ds = gdal.Translate('', src_ds, format='MEM')
src_ds = gdal.Open("../gcore/data/byte.tif")
src_ds = gdal.Translate("", src_ds, format="MEM", width=20, height=40)
grid_ds = gdal.Translate("", src_ds, format="MEM")
out_ds = gdal.ApplyVerticalShiftGrid(src_ds, grid_ds)
assert out_ds.GetRasterBand(1).DataType == gdal.GDT_Byte
assert out_ds.RasterXSize == src_ds.RasterXSize
@ -57,23 +56,23 @@ def test_applyverticalshiftgrid_1():
cs = out_ds.GetRasterBand(1).Checksum()
assert cs == 10038
src_ds = gdal.Open('../gcore/data/byte.tif')
src_ds = gdal.Translate('', src_ds, format='MEM',
width=20, height=40)
src_ds = gdal.Open("../gcore/data/byte.tif")
src_ds = gdal.Translate("", src_ds, format="MEM", width=20, height=40)
# Test block size
out_ds = gdal.ApplyVerticalShiftGrid(src_ds, src_ds,
options=['BLOCKSIZE=15'])
out_ds = gdal.ApplyVerticalShiftGrid(src_ds, src_ds, options=["BLOCKSIZE=15"])
cs = out_ds.GetRasterBand(1).Checksum()
assert cs == 10038
# Inverse transformer
out_ds = gdal.ApplyVerticalShiftGrid(src_ds, src_ds, True,
options=['DATATYPE=Float32'])
out_ds = gdal.ApplyVerticalShiftGrid(
src_ds, src_ds, True, options=["DATATYPE=Float32"]
)
assert out_ds.GetRasterBand(1).DataType == gdal.GDT_Float32
cs = out_ds.GetRasterBand(1).Checksum()
assert cs == 0
###############################################################################
# Error cases
@ -83,14 +82,14 @@ def test_applyverticalshiftgrid_2():
sr = osr.SpatialReference()
sr.SetFromUserInput("WGS84")
for i in range(6):
src_ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
src_ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
if i != 0:
src_ds.SetGeoTransform([0, 1, 0, 0, 0, -1])
if i != 1:
src_ds.SetProjection(sr.ExportToWkt())
if i == 2:
src_ds.AddBand(gdal.GDT_Byte)
grid_ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
grid_ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
if i != 3:
grid_ds.SetGeoTransform([0, 1, 0, 0, 0, -1])
if i != 4:
@ -102,10 +101,10 @@ def test_applyverticalshiftgrid_2():
assert out_ds is None, i
# Non invertable source geotransform
src_ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
src_ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
src_ds.SetGeoTransform([0, 0, 0, 0, 0, 0])
src_ds.SetProjection(sr.ExportToWkt())
grid_ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
grid_ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
grid_ds.SetGeoTransform([0, 1, 0, 0, 0, -1])
grid_ds.SetProjection(sr.ExportToWkt())
with gdaltest.error_handler():
@ -113,10 +112,10 @@ def test_applyverticalshiftgrid_2():
assert out_ds is None
# Non invertable grid geotransform
src_ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
src_ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
src_ds.SetGeoTransform([0, 1, 0, 0, 0, -1])
src_ds.SetProjection(sr.ExportToWkt())
grid_ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
grid_ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
grid_ds.SetGeoTransform([0, 0, 0, 0, 0, 0])
grid_ds.SetProjection(sr.ExportToWkt())
with gdaltest.error_handler():
@ -125,10 +124,10 @@ def test_applyverticalshiftgrid_2():
# No PROJ.4 translation for source SRS, coordinate transformation
# initialization has failed
src_ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
src_ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
src_ds.SetGeoTransform([0, 1, 0, 0, 0, -1])
src_ds.SetProjection(sr.ExportToWkt())
grid_ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
grid_ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
grid_ds.SetGeoTransform([0, 1, 0, 0, 0, -1])
grid_ds.SetProjection('LOCAL_CS["foo"]')
with gdaltest.error_handler():
@ -136,41 +135,44 @@ def test_applyverticalshiftgrid_2():
assert out_ds is None
# Out of memory
if gdal.GetConfigOption('SKIP_MEM_INTENSIVE_TEST') is None:
src_ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
if gdal.GetConfigOption("SKIP_MEM_INTENSIVE_TEST") is None:
src_ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
src_ds.SetGeoTransform([0, 1, 0, 0, 0, -1])
src_ds.SetProjection(sr.ExportToWkt())
grid_ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
grid_ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
grid_ds.SetGeoTransform([0, 1, 0, 0, 0, -1])
grid_ds.SetProjection(sr.ExportToWkt())
with gdaltest.error_handler():
out_ds = gdal.ApplyVerticalShiftGrid(src_ds, grid_ds,
options=['BLOCKSIZE=2000000000'])
out_ds = gdal.ApplyVerticalShiftGrid(
src_ds, grid_ds, options=["BLOCKSIZE=2000000000"]
)
assert out_ds is None
# Wrong DATATYPE
src_ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
src_ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
src_ds.SetGeoTransform([0, 1, 0, 0, 0, -1])
src_ds.SetProjection(sr.ExportToWkt())
grid_ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
grid_ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
grid_ds.SetGeoTransform([0, 1, 0, 0, 0, -1])
grid_ds.SetProjection(sr.ExportToWkt())
with gdaltest.error_handler():
out_ds = gdal.ApplyVerticalShiftGrid(src_ds, grid_ds,
options=['DATATYPE=x'])
out_ds = gdal.ApplyVerticalShiftGrid(src_ds, grid_ds, options=["DATATYPE=x"])
assert out_ds is None
###############################################################################
# Test with grid and src not in same projection
def test_applyverticalshiftgrid_3():
src_ds = gdal.Open('../gcore/data/byte.tif')
grid_ds = gdal.Warp('', src_ds, format='MEM', dstSRS='EPSG:4326',
width=40, height=40)
out_ds = gdal.ApplyVerticalShiftGrid(src_ds, grid_ds,
options=['RESAMPLING=NEAREST'])
src_ds = gdal.Open("../gcore/data/byte.tif")
grid_ds = gdal.Warp(
"", src_ds, format="MEM", dstSRS="EPSG:4326", width=40, height=40
)
out_ds = gdal.ApplyVerticalShiftGrid(
src_ds, grid_ds, options=["RESAMPLING=NEAREST"]
)
assert out_ds.RasterXSize == src_ds.RasterXSize
assert out_ds.RasterYSize == src_ds.RasterYSize
assert out_ds.GetGeoTransform() == src_ds.GetGeoTransform()
@ -178,15 +180,16 @@ def test_applyverticalshiftgrid_3():
cs = out_ds.GetRasterBand(1).Checksum()
assert cs == 5112
out_ds = gdal.ApplyVerticalShiftGrid(src_ds, grid_ds,
options=['RESAMPLING=BILINEAR'])
out_ds = gdal.ApplyVerticalShiftGrid(
src_ds, grid_ds, options=["RESAMPLING=BILINEAR"]
)
cs = out_ds.GetRasterBand(1).Checksum()
assert cs == 4867 or cs == 4868
out_ds = gdal.ApplyVerticalShiftGrid(src_ds, grid_ds,
options=['RESAMPLING=CUBIC'])
out_ds = gdal.ApplyVerticalShiftGrid(src_ds, grid_ds, options=["RESAMPLING=CUBIC"])
cs = out_ds.GetRasterBand(1).Checksum()
assert cs in (4841, 4854, 4842) # 4842 on Mac / Conda
assert cs in (4841, 4854, 4842) # 4842 on Mac / Conda
###############################################################################
# Test nodata
@ -198,12 +201,12 @@ def test_applyverticalshiftgrid_4():
sr.SetFromUserInput("WGS84")
# Nodata on source
src_ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
src_ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
src_ds.SetGeoTransform([0, 1, 0, 0, 0, -1])
src_ds.SetProjection(sr.ExportToWkt())
src_ds.GetRasterBand(1).Fill(1)
src_ds.GetRasterBand(1).SetNoDataValue(1)
grid_ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
grid_ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
grid_ds.SetGeoTransform([0, 1, 0, 0, 0, -1])
grid_ds.SetProjection(sr.ExportToWkt())
grid_ds.GetRasterBand(1).Fill(30)
@ -213,11 +216,11 @@ def test_applyverticalshiftgrid_4():
assert cs == 1
# Nodata on grid
src_ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
src_ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
src_ds.SetGeoTransform([0, 1, 0, 0, 0, -1])
src_ds.SetProjection(sr.ExportToWkt())
src_ds.GetRasterBand(1).Fill(1)
grid_ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
grid_ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
grid_ds.SetGeoTransform([0, 1, 0, 0, 0, -1])
grid_ds.SetProjection(sr.ExportToWkt())
grid_ds.GetRasterBand(1).Fill(30)
@ -228,79 +231,97 @@ def test_applyverticalshiftgrid_4():
assert cs == 1
# ERROR_ON_MISSING_VERT_SHIFT due to non compatible extents
src_ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
src_ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
src_ds.SetGeoTransform([0, 1, 0, 0, 0, -1])
src_ds.SetProjection(sr.ExportToWkt())
src_ds.GetRasterBand(1).Fill(255)
grid_ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
grid_ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
grid_ds.SetGeoTransform([10, 1, 0, 0, 0, -1])
grid_ds.SetProjection(sr.ExportToWkt())
out_ds = gdal.ApplyVerticalShiftGrid(src_ds, grid_ds,
options=['ERROR_ON_MISSING_VERT_SHIFT=YES'])
out_ds = gdal.ApplyVerticalShiftGrid(
src_ds, grid_ds, options=["ERROR_ON_MISSING_VERT_SHIFT=YES"]
)
with gdaltest.error_handler():
data = out_ds.GetRasterBand(1).ReadRaster()
assert data is None
# ERROR_ON_MISSING_VERT_SHIFT due to nodata in grid
src_ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
src_ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
src_ds.SetGeoTransform([0, 1, 0, 0, 0, -1])
src_ds.SetProjection(sr.ExportToWkt())
src_ds.GetRasterBand(1).Fill(255)
grid_ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
grid_ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
grid_ds.SetGeoTransform([0, 1, 0, 0, 0, -1])
grid_ds.SetProjection(sr.ExportToWkt())
grid_ds.GetRasterBand(1).SetNoDataValue(0)
out_ds = gdal.ApplyVerticalShiftGrid(src_ds, grid_ds,
options=['ERROR_ON_MISSING_VERT_SHIFT=YES'])
out_ds = gdal.ApplyVerticalShiftGrid(
src_ds, grid_ds, options=["ERROR_ON_MISSING_VERT_SHIFT=YES"]
)
with gdaltest.error_handler():
data = out_ds.GetRasterBand(1).ReadRaster()
assert data is None
###############################################################################
# Test scaling parameters
def test_applyverticalshiftgrid_5():
src_ds = gdal.Open('../gcore/data/byte.tif')
grid_ds = gdal.Translate('', src_ds, format='MEM')
src_ds = gdal.Open("../gcore/data/byte.tif")
grid_ds = gdal.Translate("", src_ds, format="MEM")
grid_ds.GetRasterBand(1).Fill(0)
src_ds = gdal.Translate('', src_ds, format='MEM',
outputType=gdal.GDT_Float32,
scaleParams=[[0, 1, 0, 0.5]])
src_ds = gdal.Translate(
"",
src_ds,
format="MEM",
outputType=gdal.GDT_Float32,
scaleParams=[[0, 1, 0, 0.5]],
)
out_ds = gdal.ApplyVerticalShiftGrid(src_ds, grid_ds, srcUnitToMeter=2)
cs = out_ds.GetRasterBand(1).Checksum()
assert cs == 4672
src_ds = gdal.Open('../gcore/data/byte.tif')
grid_ds = gdal.Translate('', src_ds, format='MEM')
src_ds = gdal.Open("../gcore/data/byte.tif")
grid_ds = gdal.Translate("", src_ds, format="MEM")
grid_ds.GetRasterBand(1).Fill(0)
src_ds = gdal.Translate('', src_ds, format='MEM',
outputType=gdal.GDT_Float32,
scaleParams=[[0, 1, 0, 0.5]])
src_ds = gdal.Translate(
"",
src_ds,
format="MEM",
outputType=gdal.GDT_Float32,
scaleParams=[[0, 1, 0, 0.5]],
)
out_ds = gdal.ApplyVerticalShiftGrid(src_ds, grid_ds, dstUnitToMeter=0.5)
cs = out_ds.GetRasterBand(1).Checksum()
assert cs == 4672
###############################################################################
# Simulate EGM grids
def test_applyverticalshiftgrid_6():
grid_ds = gdal.GetDriverByName('GTX').Create(
'tmp/applyverticalshiftgrid_6.gtx', 1440, 721, 1, gdal.GDT_Float32)
grid_ds = gdal.GetDriverByName("GTX").Create(
"tmp/applyverticalshiftgrid_6.gtx", 1440, 721, 1, gdal.GDT_Float32
)
grid_ds.SetGeoTransform([-180.125, 0.25, 0, 90.125, 0, -0.25])
grid_ds.GetRasterBand(1).Fill(10)
grid_ds = None
ds = gdal.Warp('', '../gcore/data/byte.tif', format='MEM',
srcSRS='EPSG:32611',
dstSRS='+proj=utm +zone=11 +datum=WGS84 +geoidgrids=./tmp/applyverticalshiftgrid_6.gtx +vunits=m +no_defs')
ds = gdal.Warp(
"",
"../gcore/data/byte.tif",
format="MEM",
srcSRS="EPSG:32611",
dstSRS="+proj=utm +zone=11 +datum=WGS84 +geoidgrids=./tmp/applyverticalshiftgrid_6.gtx +vunits=m +no_defs",
)
cs = ds.GetRasterBand(1).Checksum()
assert cs == 4783
gdal.Unlink('tmp/applyverticalshiftgrid_6.gtx')
gdal.Unlink("tmp/applyverticalshiftgrid_6.gtx")
###############################################################################
# Simulate USA geoid grids with long origin > 180
@ -308,19 +329,21 @@ def test_applyverticalshiftgrid_6():
def test_applyverticalshiftgrid_7():
grid_ds = gdal.GetDriverByName('GTX').Create(
'tmp/applyverticalshiftgrid_7.gtx', 700, 721, 1, gdal.GDT_Float32)
grid_ds = gdal.GetDriverByName("GTX").Create(
"tmp/applyverticalshiftgrid_7.gtx", 700, 721, 1, gdal.GDT_Float32
)
grid_ds.SetGeoTransform([-150 + 360, 0.25, 0, 90.125, 0, -0.25])
grid_ds.GetRasterBand(1).Fill(10)
grid_ds = None
ds = gdal.Warp('', '../gcore/data/byte.tif', format='MEM',
srcSRS='EPSG:32611',
dstSRS='+proj=utm +zone=11 +datum=WGS84 +geoidgrids=./tmp/applyverticalshiftgrid_7.gtx +vunits=m +no_defs')
ds = gdal.Warp(
"",
"../gcore/data/byte.tif",
format="MEM",
srcSRS="EPSG:32611",
dstSRS="+proj=utm +zone=11 +datum=WGS84 +geoidgrids=./tmp/applyverticalshiftgrid_7.gtx +vunits=m +no_defs",
)
cs = ds.GetRasterBand(1).Checksum()
assert cs == 4783
gdal.Unlink('tmp/applyverticalshiftgrid_7.gtx')
gdal.Unlink("tmp/applyverticalshiftgrid_7.gtx")

View File

@ -28,16 +28,15 @@
# DEALINGS IN THE SOFTWARE.
###############################################################################
import struct
import os
import struct
from osgeo import gdal
from osgeo import ogr
import gdaltest
import ogrtest
import pytest
from osgeo import gdal, ogr
###############################################################################
# Test with -a and -i options
@ -45,59 +44,79 @@ import pytest
def test_contour_1():
try:
os.remove('tmp/contour.shp')
os.remove("tmp/contour.shp")
except OSError:
pass
try:
os.remove('tmp/contour.dbf')
os.remove("tmp/contour.dbf")
except OSError:
pass
try:
os.remove('tmp/contour.shx')
os.remove("tmp/contour.shx")
except OSError:
pass
drv = gdal.GetDriverByName('GTiff')
wkt = 'GEOGCS[\"WGS 84\",DATUM[\"WGS_1984\",SPHEROID[\"WGS 84\",6378137,298.257223563,AUTHORITY[\"EPSG\",\"7030\"]],AUTHORITY[\"EPSG\",\"6326\"]],PRIMEM[\"Greenwich\",0,AUTHORITY[\"EPSG\",\"8901\"]],UNIT[\"degree\",0.0174532925199433,AUTHORITY[\"EPSG\",\"9108\"]],AUTHORITY[\"EPSG\",\"4326\"]]'
drv = gdal.GetDriverByName("GTiff")
wkt = 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9108"]],AUTHORITY["EPSG","4326"]]'
size = 160
precision = 1.0 / size
ds = drv.Create('tmp/gdal_contour.tif', size, size, 1)
ds = drv.Create("tmp/gdal_contour.tif", size, size, 1)
ds.SetProjection(wkt)
ds.SetGeoTransform([1, precision, 0, 50, 0, -precision])
raw_data = struct.pack('h', 10) * int(size / 2)
raw_data = struct.pack("h", 10) * int(size / 2)
for i in range(int(size / 2)):
ds.WriteRaster(int(size / 4), i + int(size / 4), int(size / 2), 1, raw_data,
buf_type=gdal.GDT_Int16,
band_list=[1])
ds.WriteRaster(
int(size / 4),
i + int(size / 4),
int(size / 2),
1,
raw_data,
buf_type=gdal.GDT_Int16,
band_list=[1],
)
raw_data = struct.pack('h', 20) * int(size / 2)
raw_data = struct.pack("h", 20) * int(size / 2)
for i in range(int(size / 4)):
ds.WriteRaster(int(size / 4) + int(size / 8), i + int(size / 4) + int(size / 8), int(size / 4), 1, raw_data,
buf_type=gdal.GDT_Int16,
band_list=[1])
ds.WriteRaster(
int(size / 4) + int(size / 8),
i + int(size / 4) + int(size / 8),
int(size / 4),
1,
raw_data,
buf_type=gdal.GDT_Int16,
band_list=[1],
)
raw_data = struct.pack('h', 25) * int(size / 4)
raw_data = struct.pack("h", 25) * int(size / 4)
for i in range(int(size / 8)):
ds.WriteRaster(int(size / 4) + int(size / 8) + int(size / 16), i + int(size / 4) + int(size / 8) + int(size / 16), int(size / 8), 1, raw_data,
buf_type=gdal.GDT_Int16,
band_list=[1])
ds.WriteRaster(
int(size / 4) + int(size / 8) + int(size / 16),
i + int(size / 4) + int(size / 8) + int(size / 16),
int(size / 8),
1,
raw_data,
buf_type=gdal.GDT_Int16,
band_list=[1],
)
ogr_ds = ogr.GetDriverByName('ESRI Shapefile').CreateDataSource('tmp/contour.shp')
ogr_lyr = ogr_ds.CreateLayer('contour')
field_defn = ogr.FieldDefn('ID', ogr.OFTInteger)
ogr_ds = ogr.GetDriverByName("ESRI Shapefile").CreateDataSource("tmp/contour.shp")
ogr_lyr = ogr_ds.CreateLayer("contour")
field_defn = ogr.FieldDefn("ID", ogr.OFTInteger)
ogr_lyr.CreateField(field_defn)
field_defn = ogr.FieldDefn('elev', ogr.OFTReal)
field_defn = ogr.FieldDefn("elev", ogr.OFTReal)
ogr_lyr.CreateField(field_defn)
gdal.ContourGenerate(ds.GetRasterBand(1), 10, 0, [], 0, 0, ogr_lyr, 0, 1)
ds = None
expected_envelopes = [[1.25, 1.75, 49.25, 49.75],
[1.25 + 0.125, 1.75 - 0.125, 49.25 + 0.125, 49.75 - 0.125]]
expected_envelopes = [
[1.25, 1.75, 49.25, 49.75],
[1.25 + 0.125, 1.75 - 0.125, 49.25 + 0.125, 49.75 - 0.125],
]
expected_height = [10, 20]
lyr = ogr_ds.ExecuteSQL("select * from contour order by elev asc")
@ -108,18 +127,23 @@ def test_contour_1():
feat = lyr.GetNextFeature()
while feat is not None:
envelope = feat.GetGeometryRef().GetEnvelope()
assert feat.GetField('elev') == expected_height[i]
assert feat.GetField("elev") == expected_height[i]
for j in range(4):
if expected_envelopes[i][j] != pytest.approx(envelope[j], abs=precision / 2 * 1.001):
print('i=%d, wkt=%s' % (i, feat.GetGeometryRef().ExportToWkt()))
if expected_envelopes[i][j] != pytest.approx(
envelope[j], abs=precision / 2 * 1.001
):
print("i=%d, wkt=%s" % (i, feat.GetGeometryRef().ExportToWkt()))
print(feat.GetGeometryRef().GetEnvelope())
pytest.fail('%f, %f' % (expected_envelopes[i][j] - envelope[j], precision / 2))
pytest.fail(
"%f, %f" % (expected_envelopes[i][j] - envelope[j], precision / 2)
)
i = i + 1
feat = lyr.GetNextFeature()
ogr_ds.ReleaseResultSet(lyr)
ogr_ds.Destroy()
###############################################################################
# Test with -fl option and -3d option
@ -127,35 +151,42 @@ def test_contour_1():
def test_contour_2():
try:
os.remove('tmp/contour.shp')
os.remove("tmp/contour.shp")
except OSError:
pass
try:
os.remove('tmp/contour.dbf')
os.remove("tmp/contour.dbf")
except OSError:
pass
try:
os.remove('tmp/contour.shx')
os.remove("tmp/contour.shx")
except OSError:
pass
ogr_ds = ogr.GetDriverByName('ESRI Shapefile').CreateDataSource('tmp/contour.shp')
ogr_lyr = ogr_ds.CreateLayer('contour', geom_type=ogr.wkbLineString25D)
field_defn = ogr.FieldDefn('ID', ogr.OFTInteger)
ogr_ds = ogr.GetDriverByName("ESRI Shapefile").CreateDataSource("tmp/contour.shp")
ogr_lyr = ogr_ds.CreateLayer("contour", geom_type=ogr.wkbLineString25D)
field_defn = ogr.FieldDefn("ID", ogr.OFTInteger)
ogr_lyr.CreateField(field_defn)
field_defn = ogr.FieldDefn('elev', ogr.OFTReal)
field_defn = ogr.FieldDefn("elev", ogr.OFTReal)
ogr_lyr.CreateField(field_defn)
ds = gdal.Open('tmp/gdal_contour.tif')
ds = gdal.Open("tmp/gdal_contour.tif")
gdal.ContourGenerate(ds.GetRasterBand(1), 0, 0, [10, 20, 25], 0, 0, ogr_lyr, 0, 1)
ds = None
size = 160
precision = 1. / size
precision = 1.0 / size
expected_envelopes = [[1.25, 1.75, 49.25, 49.75],
[1.25 + 0.125, 1.75 - 0.125, 49.25 + 0.125, 49.75 - 0.125],
[1.25 + 0.125 + 0.0625, 1.75 - 0.125 - 0.0625, 49.25 + 0.125 + 0.0625, 49.75 - 0.125 - 0.0625]]
expected_envelopes = [
[1.25, 1.75, 49.25, 49.75],
[1.25 + 0.125, 1.75 - 0.125, 49.25 + 0.125, 49.75 - 0.125],
[
1.25 + 0.125 + 0.0625,
1.75 - 0.125 - 0.0625,
49.25 + 0.125 + 0.0625,
49.75 - 0.125 - 0.0625,
],
]
expected_height = [10, 20, 25, 10000]
lyr = ogr_ds.ExecuteSQL("select * from contour order by elev asc")
@ -167,82 +198,109 @@ def test_contour_2():
while feat is not None:
assert feat.GetGeometryRef().GetZ(0) == expected_height[i]
envelope = feat.GetGeometryRef().GetEnvelope()
assert feat.GetField('elev') == expected_height[i]
assert feat.GetField("elev") == expected_height[i]
for j in range(4):
if expected_envelopes[i][j] != pytest.approx(envelope[j], abs=precision / 2 * 1.001):
print('i=%d, wkt=%s' % (i, feat.GetGeometryRef().ExportToWkt()))
if expected_envelopes[i][j] != pytest.approx(
envelope[j], abs=precision / 2 * 1.001
):
print("i=%d, wkt=%s" % (i, feat.GetGeometryRef().ExportToWkt()))
print(feat.GetGeometryRef().GetEnvelope())
pytest.fail('%f, %f' % (expected_envelopes[i][j] - envelope[j], precision / 2))
pytest.fail(
"%f, %f" % (expected_envelopes[i][j] - envelope[j], precision / 2)
)
i = i + 1
feat = lyr.GetNextFeature()
ogr_ds.ReleaseResultSet(lyr)
ogr_ds.Destroy()
###############################################################################
#
def test_contour_real_world_case():
ogr_ds = ogr.GetDriverByName('Memory').CreateDataSource('')
ogr_lyr = ogr_ds.CreateLayer('contour', geom_type=ogr.wkbLineString)
field_defn = ogr.FieldDefn('ID', ogr.OFTInteger)
ogr_ds = ogr.GetDriverByName("Memory").CreateDataSource("")
ogr_lyr = ogr_ds.CreateLayer("contour", geom_type=ogr.wkbLineString)
field_defn = ogr.FieldDefn("ID", ogr.OFTInteger)
ogr_lyr.CreateField(field_defn)
field_defn = ogr.FieldDefn('elev', ogr.OFTReal)
field_defn = ogr.FieldDefn("elev", ogr.OFTReal)
ogr_lyr.CreateField(field_defn)
ds = gdal.Open('data/contour_in.tif')
ds = gdal.Open("data/contour_in.tif")
gdal.ContourGenerate(ds.GetRasterBand(1), 10, 0, [], 0, 0, ogr_lyr, 0, 1)
ds = None
ogr_lyr.SetAttributeFilter('elev = 330')
ogr_lyr.SetAttributeFilter("elev = 330")
assert ogr_lyr.GetFeatureCount() == 1
f = ogr_lyr.GetNextFeature()
assert ogrtest.check_feature_geometry(f, 'LINESTRING (4.50497512437811 11.5,4.5 11.501996007984,3.5 11.8333333333333,2.5 11.5049751243781,2.490099009901 11.5,2.0 10.5,2.5 10.1666666666667,3.0 9.5,3.5 9.21428571428571,4.49800399201597 8.5,4.5 8.49857346647646,5.5 8.16666666666667,6.5 8.0,7.5 8.0,8.0 7.5,8.5 7.0,9.490099009901 6.5,9.5 6.49667774086379,10.5 6.16666666666667,11.4950248756219 5.5,11.5 5.49833610648919,12.5 5.49667774086379,13.5 5.49800399201597,13.501996007984 5.5,13.5 5.50199600798403,12.501996007984 6.5,12.5 6.50142653352354,11.5 6.509900990099,10.509900990099 7.5,10.5 7.50142653352354,9.5 7.9,8.50332225913621 8.5,8.5 8.50249376558603,7.83333333333333 9.5,7.5 10.0,7.0 10.5,6.5 10.7857142857143,5.5 11.1666666666667,4.50497512437811 11.5)', 0.01) == 0
assert (
ogrtest.check_feature_geometry(
f,
"LINESTRING (4.50497512437811 11.5,4.5 11.501996007984,3.5 11.8333333333333,2.5 11.5049751243781,2.490099009901 11.5,2.0 10.5,2.5 10.1666666666667,3.0 9.5,3.5 9.21428571428571,4.49800399201597 8.5,4.5 8.49857346647646,5.5 8.16666666666667,6.5 8.0,7.5 8.0,8.0 7.5,8.5 7.0,9.490099009901 6.5,9.5 6.49667774086379,10.5 6.16666666666667,11.4950248756219 5.5,11.5 5.49833610648919,12.5 5.49667774086379,13.5 5.49800399201597,13.501996007984 5.5,13.5 5.50199600798403,12.501996007984 6.5,12.5 6.50142653352354,11.5 6.509900990099,10.509900990099 7.5,10.5 7.50142653352354,9.5 7.9,8.50332225913621 8.5,8.5 8.50249376558603,7.83333333333333 9.5,7.5 10.0,7.0 10.5,6.5 10.7857142857143,5.5 11.1666666666667,4.50497512437811 11.5)",
0.01,
)
== 0
)
# Test with -p option (polygonize)
def test_contour_3():
try:
os.remove('tmp/contour.shp')
os.remove("tmp/contour.shp")
except OSError:
pass
try:
os.remove('tmp/contour.dbf')
os.remove("tmp/contour.dbf")
except OSError:
pass
try:
os.remove('tmp/contour.shx')
os.remove("tmp/contour.shx")
except OSError:
pass
ogr_ds = ogr.GetDriverByName('ESRI Shapefile').CreateDataSource('tmp/contour.shp')
ogr_lyr = ogr_ds.CreateLayer('contour', geom_type=ogr.wkbMultiPolygon)
field_defn = ogr.FieldDefn('ID', ogr.OFTInteger)
ogr_ds = ogr.GetDriverByName("ESRI Shapefile").CreateDataSource("tmp/contour.shp")
ogr_lyr = ogr_ds.CreateLayer("contour", geom_type=ogr.wkbMultiPolygon)
field_defn = ogr.FieldDefn("ID", ogr.OFTInteger)
ogr_lyr.CreateField(field_defn)
field_defn = ogr.FieldDefn('elevMin', ogr.OFTReal)
field_defn = ogr.FieldDefn("elevMin", ogr.OFTReal)
ogr_lyr.CreateField(field_defn)
field_defn = ogr.FieldDefn('elevMax', ogr.OFTReal)
field_defn = ogr.FieldDefn("elevMax", ogr.OFTReal)
ogr_lyr.CreateField(field_defn)
ds = gdal.Open('tmp/gdal_contour.tif')
#gdal.ContourGenerateEx(ds.GetRasterBand(1), 0, 0, 0, [10, 20, 25], 0, 0, ogr_lyr, 0, 1, 1)
gdal.ContourGenerateEx(ds.GetRasterBand(1), ogr_lyr, options = [ "FIXED_LEVELS=10,20,25",
"ID_FIELD=0",
"ELEV_FIELD_MIN=1",
"ELEV_FIELD_MAX=2",
"POLYGONIZE=TRUE" ] )
ds = gdal.Open("tmp/gdal_contour.tif")
# gdal.ContourGenerateEx(ds.GetRasterBand(1), 0, 0, 0, [10, 20, 25], 0, 0, ogr_lyr, 0, 1, 1)
gdal.ContourGenerateEx(
ds.GetRasterBand(1),
ogr_lyr,
options=[
"FIXED_LEVELS=10,20,25",
"ID_FIELD=0",
"ELEV_FIELD_MIN=1",
"ELEV_FIELD_MAX=2",
"POLYGONIZE=TRUE",
],
)
ds = None
size = 160
precision = 1. / size
precision = 1.0 / size
expected_envelopes = [[1.0, 2.0, 49.0, 50.0],
[1.25, 1.75, 49.25, 49.75],
[1.25 + 0.125, 1.75 - 0.125, 49.25 + 0.125, 49.75 - 0.125],
[1.25 + 0.125 + 0.0625, 1.75 - 0.125 - 0.0625, 49.25 + 0.125 + 0.0625, 49.75 - 0.125 - 0.0625]]
expected_envelopes = [
[1.0, 2.0, 49.0, 50.0],
[1.25, 1.75, 49.25, 49.75],
[1.25 + 0.125, 1.75 - 0.125, 49.25 + 0.125, 49.75 - 0.125],
[
1.25 + 0.125 + 0.0625,
1.75 - 0.125 - 0.0625,
49.25 + 0.125 + 0.0625,
49.75 - 0.125 - 0.0625,
],
]
expected_height = [10, 20, 25, 10000]
lyr = ogr_ds.ExecuteSQL("select * from contour order by elevMin asc")
@ -252,17 +310,27 @@ def test_contour_3():
i = 0
feat = lyr.GetNextFeature()
while feat is not None:
if i < 3 and feat.GetField('elevMax') != expected_height[i]:
pytest.fail('Got %f as z. Expected %f' % (feat.GetField('elevMax'), expected_height[i]))
elif i > 0 and i < 3 and feat.GetField('elevMin') != expected_height[i-1]:
pytest.fail('Got %f as z. Expected %f' % (feat.GetField('elevMin'), expected_height[i-1]))
if i < 3 and feat.GetField("elevMax") != expected_height[i]:
pytest.fail(
"Got %f as z. Expected %f"
% (feat.GetField("elevMax"), expected_height[i])
)
elif i > 0 and i < 3 and feat.GetField("elevMin") != expected_height[i - 1]:
pytest.fail(
"Got %f as z. Expected %f"
% (feat.GetField("elevMin"), expected_height[i - 1])
)
envelope = feat.GetGeometryRef().GetEnvelope()
for j in range(4):
if expected_envelopes[i][j] != pytest.approx(envelope[j], abs=precision / 2 * 1.001):
print('i=%d, wkt=%s' % (i, feat.GetGeometryRef().ExportToWkt()))
if expected_envelopes[i][j] != pytest.approx(
envelope[j], abs=precision / 2 * 1.001
):
print("i=%d, wkt=%s" % (i, feat.GetGeometryRef().ExportToWkt()))
print(feat.GetGeometryRef().GetEnvelope())
pytest.fail('%f, %f' % (expected_envelopes[i][j] - envelope[j], precision / 2))
pytest.fail(
"%f, %f" % (expected_envelopes[i][j] - envelope[j], precision / 2)
)
i = i + 1
feat = lyr.GetNextFeature()
@ -273,27 +341,36 @@ def test_contour_3():
# Check behaviour when the nodata value as a double isn't exactly the Float32 pixel value
def test_contour_nodata_precision_issue_float32():
ogr_ds = ogr.GetDriverByName('ESRI Shapefile').CreateDataSource('/vsimem/contour.shp')
ogr_lyr = ogr_ds.CreateLayer('contour', geom_type=ogr.wkbLineString)
field_defn = ogr.FieldDefn('ID', ogr.OFTInteger)
ogr_ds = ogr.GetDriverByName("ESRI Shapefile").CreateDataSource(
"/vsimem/contour.shp"
)
ogr_lyr = ogr_ds.CreateLayer("contour", geom_type=ogr.wkbLineString)
field_defn = ogr.FieldDefn("ID", ogr.OFTInteger)
ogr_lyr.CreateField(field_defn)
ds = gdal.Open('data/nodata_precision_issue_float32.tif')
gdal.ContourGenerateEx(ds.GetRasterBand(1), ogr_lyr, options = [ "LEVEL_INTERVAL=0.1",
"ID_FIELD=0",
"NODATA=%.19g" % ds.GetRasterBand(1).GetNoDataValue()] )
ds = gdal.Open("data/nodata_precision_issue_float32.tif")
gdal.ContourGenerateEx(
ds.GetRasterBand(1),
ogr_lyr,
options=[
"LEVEL_INTERVAL=0.1",
"ID_FIELD=0",
"NODATA=%.19g" % ds.GetRasterBand(1).GetNoDataValue(),
],
)
ds = None
assert ogr_lyr.GetFeatureCount() == 0
ogr_ds = None
ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('/vsimem/contour.shp')
ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("/vsimem/contour.shp")
def test_contour_too_many_levels():
ogr_ds = ogr.GetDriverByName('ESRI Shapefile').CreateDataSource('/vsimem/contour.shp')
ogr_lyr = ogr_ds.CreateLayer('contour', geom_type=ogr.wkbLineString)
field_defn = ogr.FieldDefn('ID', ogr.OFTInteger)
ogr_ds = ogr.GetDriverByName("ESRI Shapefile").CreateDataSource(
"/vsimem/contour.shp"
)
ogr_lyr = ogr_ds.CreateLayer("contour", geom_type=ogr.wkbLineString)
field_defn = ogr.FieldDefn("ID", ogr.OFTInteger)
ogr_lyr.CreateField(field_defn)
content1 = """ncols 2
@ -313,52 +390,65 @@ cellsize 1
0 0"""
for content in (content1, content2):
with gdaltest.tempfile('/vsimem/test.asc', content):
ds = gdal.Open('/vsimem/test.asc')
with gdaltest.tempfile("/vsimem/test.asc", content):
ds = gdal.Open("/vsimem/test.asc")
with gdaltest.error_handler():
assert gdal.ContourGenerateEx(ds.GetRasterBand(1), ogr_lyr,
options = [ "LEVEL_INTERVAL=1",
"ID_FIELD=0"] ) != 0
assert (
gdal.ContourGenerateEx(
ds.GetRasterBand(1),
ogr_lyr,
options=["LEVEL_INTERVAL=1", "ID_FIELD=0"],
)
!= 0
)
with gdaltest.tempfile('/vsimem/test.asc', content):
ds = gdal.Open('/vsimem/test.asc')
with gdaltest.tempfile("/vsimem/test.asc", content):
ds = gdal.Open("/vsimem/test.asc")
with gdaltest.error_handler():
assert gdal.ContourGenerateEx(ds.GetRasterBand(1), ogr_lyr,
options = [ "LEVEL_INTERVAL=1",
"LEVEL_EXP_BASE=1.0001",
"ID_FIELD=0"] ) != 0
assert (
gdal.ContourGenerateEx(
ds.GetRasterBand(1),
ogr_lyr,
options=[
"LEVEL_INTERVAL=1",
"LEVEL_EXP_BASE=1.0001",
"ID_FIELD=0",
],
)
!= 0
)
ogr_ds = None
ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('/vsimem/contour.shp')
ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("/vsimem/contour.shp")
###############################################################################
def test_contour_raster_acquisition_error():
ogr_ds = ogr.GetDriverByName('Memory').CreateDataSource('')
ogr_lyr = ogr_ds.CreateLayer('contour', geom_type=ogr.wkbLineString)
field_defn = ogr.FieldDefn('ID', ogr.OFTInteger)
ogr_ds = ogr.GetDriverByName("Memory").CreateDataSource("")
ogr_lyr = ogr_ds.CreateLayer("contour", geom_type=ogr.wkbLineString)
field_defn = ogr.FieldDefn("ID", ogr.OFTInteger)
ogr_lyr.CreateField(field_defn)
ds = gdal.Open('../gcore/data/byte_truncated.tif')
ds = gdal.Open("../gcore/data/byte_truncated.tif")
with gdaltest.error_handler():
assert gdal.ContourGenerateEx(ds.GetRasterBand(1), ogr_lyr,
options = [ "LEVEL_INTERVAL=1",
"ID_FIELD=0"] ) != 0
assert (
gdal.ContourGenerateEx(
ds.GetRasterBand(1), ogr_lyr, options=["LEVEL_INTERVAL=1", "ID_FIELD=0"]
)
!= 0
)
###############################################################################
# Cleanup
def test_contour_cleanup():
ogr.GetDriverByName('ESRI Shapefile').DeleteDataSource('tmp/contour.shp')
ogr.GetDriverByName("ESRI Shapefile").DeleteDataSource("tmp/contour.shp")
try:
os.remove('tmp/gdal_contour.tif')
os.remove("tmp/gdal_contour.tif")
except OSError:
pass

View File

@ -31,20 +31,21 @@
###############################################################################
import gdaltest
import ogrtest
import pytest
from osgeo import gdal
import ogrtest
import gdaltest
import pytest
###############################################################################
def test_cutline_1():
tst = gdaltest.GDALTest('VRT', 'cutline_noblend.vrt', 1, 11409)
tst = gdaltest.GDALTest("VRT", "cutline_noblend.vrt", 1, 11409)
return tst.testOpen()
###############################################################################
@ -53,9 +54,10 @@ def test_cutline_2():
if not ogrtest.have_geos():
pytest.skip()
tst = gdaltest.GDALTest('VRT', 'cutline_blend.vrt', 1, 21395)
tst = gdaltest.GDALTest("VRT", "cutline_blend.vrt", 1, 21395)
return tst.testOpen()
###############################################################################
@ -64,9 +66,10 @@ def test_cutline_3():
if not ogrtest.have_geos():
pytest.skip()
tst = gdaltest.GDALTest('VRT', 'cutline_multipolygon.vrt', 1, 20827)
tst = gdaltest.GDALTest("VRT", "cutline_multipolygon.vrt", 1, 20827)
return tst.testOpen()
###############################################################################
@ -75,11 +78,12 @@ def test_cutline_4():
if not ogrtest.have_geos():
pytest.skip()
ds = gdal.Translate('/vsimem/utmsmall.tif', '../gcore/data/utmsmall.tif')
ds.BuildOverviews('NEAR', [2])
ds = gdal.Translate("/vsimem/utmsmall.tif", "../gcore/data/utmsmall.tif")
ds.BuildOverviews("NEAR", [2])
ds = None
ds = gdal.Open("""<VRTDataset rasterXSize="100" rasterYSize="100" subClass="VRTWarpedDataset">
ds = gdal.Open(
"""<VRTDataset rasterXSize="100" rasterYSize="100" subClass="VRTWarpedDataset">
<SRS>PROJCS[&quot;NAD27 / UTM zone 11N&quot;,GEOGCS[&quot;NAD27&quot;,DATUM[&quot;North_American_Datum_1927&quot;,SPHEROID[&quot;Clarke 1866&quot;,6378206.4,294.9786982139006,AUTHORITY[&quot;EPSG&quot;,&quot;7008&quot;]],AUTHORITY[&quot;EPSG&quot;,&quot;6267&quot;]],PRIMEM[&quot;Greenwich&quot;,0],UNIT[&quot;degree&quot;,0.0174532925199433],AUTHORITY[&quot;EPSG&quot;,&quot;4267&quot;]],PROJECTION[&quot;Transverse_Mercator&quot;],PARAMETER[&quot;latitude_of_origin&quot;,0],PARAMETER[&quot;central_meridian&quot;,-117],PARAMETER[&quot;scale_factor&quot;,0.9996],PARAMETER[&quot;false_easting&quot;,500000],PARAMETER[&quot;false_northing&quot;,0],UNIT[&quot;metre&quot;,1,AUTHORITY[&quot;EPSG&quot;,&quot;9001&quot;]],AUTHORITY[&quot;EPSG&quot;,&quot;26711&quot;]]</SRS>
<GeoTransform> 4.4072000000000000e+05, 5.9999999999999993e+01, 0.0000000000000000e+00, 3.7513200000000000e+06, 0.0000000000000000e+00, -5.9999999999999993e+01</GeoTransform>
<VRTRasterBand dataType="Byte" band="1" subClass="VRTWarpedRasterBand"/>
@ -109,14 +113,13 @@ def test_cutline_4():
</BandList>
<Cutline>MULTIPOLYGON(((10 10,10 50,60 50, 10 10)),((70 70,70 100,100 100,100 70,70 70),(80 80,80 90,90 90,90 80,80 80)))</Cutline>
</GDALWarpOptions>
</VRTDataset>""")
out_ds = gdal.Translate('', ds, options='-of MEM -outsize 50%% 50%%')
</VRTDataset>"""
)
out_ds = gdal.Translate("", ds, options="-of MEM -outsize 50%% 50%%")
cs = out_ds.GetRasterBand(1).Checksum()
assert cs == 5170
gdal.Unlink('/vsimem/utmsmall.tif')
gdal.Unlink("/vsimem/utmsmall.tif")
###############################################################################

View File

@ -30,10 +30,9 @@
###############################################################################
import pytest
from osgeo import gdal
import pytest
###############################################################################
# Test
@ -41,15 +40,16 @@ import pytest
def test_dither_1():
drv = gdal.GetDriverByName('GTiff')
drv = gdal.GetDriverByName("GTiff")
src_ds = gdal.Open('../gdrivers/data/rgbsmall.tif')
src_ds = gdal.Open("../gdrivers/data/rgbsmall.tif")
r_band = src_ds.GetRasterBand(1)
g_band = src_ds.GetRasterBand(2)
b_band = src_ds.GetRasterBand(3)
dst_ds = drv.Create('tmp/rgbsmall.tif', src_ds.RasterXSize,
src_ds.RasterYSize, 1, gdal.GDT_Byte)
dst_ds = drv.Create(
"tmp/rgbsmall.tif", src_ds.RasterXSize, src_ds.RasterYSize, 1, gdal.GDT_Byte
)
dst_band = dst_ds.GetRasterBand(1)
ct = gdal.ColorTable()
@ -67,10 +67,18 @@ def test_dither_1():
dst_band = None
dst_ds = None
assert ct.GetCount() == nColors, 'color table size wrong'
assert ct.GetCount() == nColors, "color table size wrong"
ref_ct = [(36, 48, 32, 255), (92, 120, 20, 255), (88, 96, 20, 255), (92, 132, 56, 255),
(0, 0, 0, 255), (96, 152, 24, 255), (60, 112, 32, 255), (164, 164, 108, 255)]
ref_ct = [
(36, 48, 32, 255),
(92, 120, 20, 255),
(88, 96, 20, 255),
(92, 132, 56, 255),
(0, 0, 0, 255),
(96, 152, 24, 255),
(60, 112, 32, 255),
(164, 164, 108, 255),
]
for i in range(nColors):
ct_data = ct.GetColorEntry(i)
@ -82,16 +90,11 @@ def test_dither_1():
for k in range(nColors):
print(ct.GetColorEntry(k))
print(ref_ct[k])
pytest.fail('color table mismatch')
pytest.fail("color table mismatch")
if cs == cs_expected or gdal.GetConfigOption('CPL_DEBUG', 'OFF') != 'ON':
drv.Delete('tmp/rgbsmall.tif')
if cs == cs_expected or gdal.GetConfigOption("CPL_DEBUG", "OFF") != "ON":
drv.Delete("tmp/rgbsmall.tif")
if cs != cs_expected:
print('Got: ', cs)
pytest.fail('got wrong checksum')
print("Got: ", cs)
pytest.fail("got wrong checksum")

View File

@ -28,118 +28,113 @@
# DEALINGS IN THE SOFTWARE.
###############################################################################
from osgeo import gdal
import struct
import pytest
from osgeo import gdal
def test_fillnodata_1x1_no_nodata():
ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
ar = struct.pack('B' * 1, 1)
ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
ar = struct.pack("B" * 1, 1)
ds.WriteRaster(0, 0, ds.RasterXSize, ds.RasterYSize, ar)
gdal.FillNodata(targetBand = ds.GetRasterBand(1),
maxSearchDist = 1, maskBand = None, smoothingIterations = 0)
gdal.FillNodata(
targetBand=ds.GetRasterBand(1),
maxSearchDist=1,
maskBand=None,
smoothingIterations=0,
)
ar = ds.ReadRaster()
ar = struct.unpack('B' * 1, ar)
ar = struct.unpack("B" * 1, ar)
assert ar == (1,)
fillnodata_tests = {
# (width, height, input_ar, maxSearchDist, expected, smoothingIterations):
'1x1_nodata_but_pixel_valid': (1, 1, (1,), 1, (1,), 0),
'1x1_nodata_pixel_invalid': (1, 1, (0,), 1, (0,), 0),
'2x1_valid_invalid': (2, 1, (1, 0), 1, (1, 1), 0),
'2x1_invalid_valid': (2, 1, (0, 1), 1, (1, 1), 0),
'3x1_valid_invalid_valid': (3, 1, (2, 0, 4), 1, (2, 3, 4), 0),
'4x1_valid_invalid_invalid_valid': (4, 1, (2, 0, 0, 4), 1, (2, 2, 4, 4), 0),
'1x2_valid_invalid': (1, 2, (1, 0), 1, (1, 1), 0),
'1x2_invalid_valid': (1, 2, (0, 1), 1, (1, 1), 0),
'1x3_valid_invalid_valid': (1, 3, (2, 0, 4), 1, (2, 3, 4), 0),
'1x4_valid_invalid_invalid_valid': (1, 4, (2, 0, 0, 4), 1, (2, 2, 4, 4), 0),
'3x3_central_column_invalid': (
3, 3,
(2, 0, 4,
4, 0, 6,
6, 0, 8),
"1x1_nodata_but_pixel_valid": (1, 1, (1,), 1, (1,), 0),
"1x1_nodata_pixel_invalid": (1, 1, (0,), 1, (0,), 0),
"2x1_valid_invalid": (2, 1, (1, 0), 1, (1, 1), 0),
"2x1_invalid_valid": (2, 1, (0, 1), 1, (1, 1), 0),
"3x1_valid_invalid_valid": (3, 1, (2, 0, 4), 1, (2, 3, 4), 0),
"4x1_valid_invalid_invalid_valid": (4, 1, (2, 0, 0, 4), 1, (2, 2, 4, 4), 0),
"1x2_valid_invalid": (1, 2, (1, 0), 1, (1, 1), 0),
"1x2_invalid_valid": (1, 2, (0, 1), 1, (1, 1), 0),
"1x3_valid_invalid_valid": (1, 3, (2, 0, 4), 1, (2, 3, 4), 0),
"1x4_valid_invalid_invalid_valid": (1, 4, (2, 0, 0, 4), 1, (2, 2, 4, 4), 0),
"3x3_central_column_invalid": (
3,
3,
(2, 0, 4, 4, 0, 6, 6, 0, 8),
1,
(2, 3, 4,
4, 5, 6,
6, 7, 8), 0),
'3x3_central_line_invalid': (
3, 3,
(2, 3, 4,
0, 0, 0,
6, 7, 8),
(2, 3, 4, 4, 5, 6, 6, 7, 8),
0,
),
"3x3_central_line_invalid": (
3,
3,
(2, 3, 4, 0, 0, 0, 6, 7, 8),
1,
(2, 3, 4,
4, 5, 6,
6, 7, 8),
0),
'3x3_central_column_and_line_invalid': (
3, 3,
(2, 0, 4,
0, 0, 0,
6, 0, 8),
(2, 3, 4, 4, 5, 6, 6, 7, 8),
0,
),
"3x3_central_column_and_line_invalid": (
3,
3,
(2, 0, 4, 0, 0, 0, 6, 0, 8),
1,
(2, 3, 4,
4, 0, 6,
6, 7, 8),
0),
(2, 3, 4, 4, 0, 6, 6, 7, 8),
0,
),
# 1.5 > sqrt(2)
'3x3_central_column_and_line_invalid_search_dist_1_5': (
3, 3,
(2, 0, 4,
0, 0, 0,
6, 0, 8),
"3x3_central_column_and_line_invalid_search_dist_1_5": (
3,
3,
(2, 0, 4, 0, 0, 0, 6, 0, 8),
1.5,
(2, 3, 4,
4, 5, 6,
6, 7, 8),
0),
'4x4': (
4, 4,
(20, 30, 40, 50,
30, 0, 0, 60,
40, 0, 0, 70,
50, 60, 70, 80),
(2, 3, 4, 4, 5, 6, 6, 7, 8),
0,
),
"4x4": (
4,
4,
(20, 30, 40, 50, 30, 0, 0, 60, 40, 0, 0, 70, 50, 60, 70, 80),
1,
(20, 30, 40, 50,
30, 30, 50, 60,
40, 50, 70, 70,
50, 60, 70, 80),
0),
'4x4_smooth_1': (
4, 4,
(20, 30, 40, 50,
30, 0, 0, 60,
40, 0, 0, 70,
50, 60, 70, 80),
(20, 30, 40, 50, 30, 30, 50, 60, 40, 50, 70, 70, 50, 60, 70, 80),
0,
),
"4x4_smooth_1": (
4,
4,
(20, 30, 40, 50, 30, 0, 0, 60, 40, 0, 0, 70, 50, 60, 70, 80),
1,
(20, 30, 40, 50,
30, 40, 50, 60,
40, 50, 60, 70,
50, 60, 70, 80),
1),
(20, 30, 40, 50, 30, 40, 50, 60, 40, 50, 60, 70, 50, 60, 70, 80),
1,
),
}
@pytest.mark.parametrize('width, height, input_ar, maxSearchDist, expected, smoothingIterations',
fillnodata_tests.values(),
ids=fillnodata_tests.keys())
def test_fillnodata_nodata(width, height, input_ar, maxSearchDist, expected, smoothingIterations):
ds = gdal.GetDriverByName('MEM').Create('', width, height)
@pytest.mark.parametrize(
"width, height, input_ar, maxSearchDist, expected, smoothingIterations",
fillnodata_tests.values(),
ids=fillnodata_tests.keys(),
)
def test_fillnodata_nodata(
width, height, input_ar, maxSearchDist, expected, smoothingIterations
):
ds = gdal.GetDriverByName("MEM").Create("", width, height)
npixels = ds.RasterXSize * ds.RasterYSize
ds.GetRasterBand(1).SetNoDataValue(0)
ar = struct.pack('B', input_ar[0])
ar = struct.pack("B", input_ar[0])
for i in range(1, len(input_ar)):
ar += struct.pack('B', input_ar[i])
ar += struct.pack("B", input_ar[i])
ds.WriteRaster(0, 0, ds.RasterXSize, ds.RasterYSize, ar)
gdal.FillNodata(targetBand = ds.GetRasterBand(1),
maxSearchDist = maxSearchDist,
maskBand = None,
smoothingIterations = smoothingIterations)
gdal.FillNodata(
targetBand=ds.GetRasterBand(1),
maxSearchDist=maxSearchDist,
maskBand=None,
smoothingIterations=smoothingIterations,
)
ar = ds.ReadRaster()
assert struct.unpack('B' * npixels, ar) == expected
assert struct.unpack("B" * npixels, ar) == expected

View File

@ -30,8 +30,8 @@
###############################################################################
from collections import defaultdict
import struct
from collections import defaultdict
import ogrtest
@ -43,16 +43,16 @@ from osgeo import gdal, ogr
def test_polygonize_1(is_int_polygonize=True):
src_ds = gdal.Open('data/polygonize_in.grd')
src_ds = gdal.Open("data/polygonize_in.grd")
src_band = src_ds.GetRasterBand(1)
# Create a memory OGR datasource to put results in.
mem_drv = ogr.GetDriverByName('Memory')
mem_ds = mem_drv.CreateDataSource('out')
mem_drv = ogr.GetDriverByName("Memory")
mem_ds = mem_drv.CreateDataSource("out")
mem_layer = mem_ds.CreateLayer('poly', None, ogr.wkbPolygon)
mem_layer = mem_ds.CreateLayer("poly", None, ogr.wkbPolygon)
fd = ogr.FieldDefn('DN', ogr.OFTInteger)
fd = ogr.FieldDefn("DN", ogr.OFTInteger)
mem_layer.CreateField(fd)
# run the algorithm.
@ -60,23 +60,28 @@ def test_polygonize_1(is_int_polygonize=True):
result = gdal.Polygonize(src_band, src_band.GetMaskBand(), mem_layer, 0)
else:
result = gdal.FPolygonize(src_band, src_band.GetMaskBand(), mem_layer, 0)
assert result == 0, 'Polygonize failed'
assert result == 0, "Polygonize failed"
# Confirm we get the set of expected features in the output layer.
expected_feature_number = 13
assert mem_layer.GetFeatureCount() == expected_feature_number
expect = [107, 123, 115, 115, 140, 148, 123, 140, 156,
100, 101, 102, 103]
expect = [107, 123, 115, 115, 140, 148, 123, 140, 156, 100, 101, 102, 103]
tr = ogrtest.check_features_against_list(mem_layer, 'DN', expect)
tr = ogrtest.check_features_against_list(mem_layer, "DN", expect)
# check at least one geometry.
if tr:
mem_layer.SetAttributeFilter('dn = 156')
mem_layer.SetAttributeFilter("dn = 156")
feat_read = mem_layer.GetNextFeature()
if ogrtest.check_feature_geometry(feat_read, 'POLYGON ((440720 3751200,440720 3751020,440900 3751020,440900 3751200,440720 3751200),(440780 3751140,440780 3751080,440840 3751080,440840 3751140,440780 3751140))') != 0:
if (
ogrtest.check_feature_geometry(
feat_read,
"POLYGON ((440720 3751200,440720 3751020,440900 3751020,440900 3751200,440720 3751200),(440780 3751140,440780 3751080,440840 3751080,440840 3751140,440780 3751140))",
)
!= 0
):
tr = 0
feat_read.Destroy()
@ -86,61 +91,80 @@ def test_polygonize_1(is_int_polygonize=True):
def test_polygonize_1_float():
return test_polygonize_1(is_int_polygonize=False)
###############################################################################
# Test a simple case without masking.
def test_polygonize_2():
src_ds = gdal.Open('data/polygonize_in.grd')
src_ds = gdal.Open("data/polygonize_in.grd")
src_band = src_ds.GetRasterBand(1)
# Create a memory OGR datasource to put results in.
mem_drv = ogr.GetDriverByName('Memory')
mem_ds = mem_drv.CreateDataSource('out')
mem_drv = ogr.GetDriverByName("Memory")
mem_ds = mem_drv.CreateDataSource("out")
mem_layer = mem_ds.CreateLayer('poly', None, ogr.wkbPolygon)
mem_layer = mem_ds.CreateLayer("poly", None, ogr.wkbPolygon)
fd = ogr.FieldDefn('DN', ogr.OFTInteger)
fd = ogr.FieldDefn("DN", ogr.OFTInteger)
mem_layer.CreateField(fd)
# run the algorithm.
result = gdal.Polygonize(src_band, None, mem_layer, 0)
assert result == 0, 'Polygonize failed'
assert result == 0, "Polygonize failed"
# Confirm we get the set of expected features in the output layer.
expected_feature_number = 17
assert mem_layer.GetFeatureCount() == expected_feature_number
expect = [107, 123, 115, 132, 115, 132, 140, 132, 148, 123, 140,
132, 156, 100, 101, 102, 103]
expect = [
107,
123,
115,
132,
115,
132,
140,
132,
148,
123,
140,
132,
156,
100,
101,
102,
103,
]
tr = ogrtest.check_features_against_list(mem_layer, 'DN', expect)
tr = ogrtest.check_features_against_list(mem_layer, "DN", expect)
assert tr
###############################################################################
# A more involved case with a complex looping.
def test_polygonize_3():
src_ds = gdal.Open('data/polygonize_in_2.grd')
src_ds = gdal.Open("data/polygonize_in_2.grd")
src_band = src_ds.GetRasterBand(1)
# Create a memory OGR datasource to put results in.
mem_drv = ogr.GetDriverByName('Memory')
mem_ds = mem_drv.CreateDataSource('out')
mem_drv = ogr.GetDriverByName("Memory")
mem_ds = mem_drv.CreateDataSource("out")
mem_layer = mem_ds.CreateLayer('poly', None, ogr.wkbPolygon)
mem_layer = mem_ds.CreateLayer("poly", None, ogr.wkbPolygon)
fd = ogr.FieldDefn('DN', ogr.OFTInteger)
fd = ogr.FieldDefn("DN", ogr.OFTInteger)
mem_layer.CreateField(fd)
# run the algorithm.
result = gdal.Polygonize(src_band, None, mem_layer, 0)
assert result == 0, 'Polygonize failed'
assert result == 0, "Polygonize failed"
# Confirm we get the expected count of features.
@ -148,9 +172,15 @@ def test_polygonize_3():
assert mem_layer.GetFeatureCount() == expected_feature_number
# check at least one geometry.
mem_layer.SetAttributeFilter('dn = 0')
mem_layer.SetAttributeFilter("dn = 0")
feat_read = mem_layer.GetNextFeature()
if ogrtest.check_feature_geometry(feat_read, 'POLYGON ((6 -3,6 -40,19 -40,19 -39,25 -39,25 -38,27 -38,27 -37,28 -37,28 -36,29 -36,29 -35,30 -35,30 -34,31 -34,31 -25,30 -25,30 -24,29 -24,29 -23,28 -23,28 -22,27 -22,27 -21,24 -21,24 -20,23 -20,23 -19,26 -19,26 -18,27 -18,27 -17,28 -17,28 -16,29 -16,29 -8,28 -8,28 -7,27 -7,27 -6,26 -6,26 -5,24 -5,24 -4,18 -4,18 -3,6 -3),(11 -7,11 -18,23 -18,23 -17,24 -17,24 -16,25 -16,25 -9,24 -9,24 -8,23 -8,23 -7,11 -7),(11 -22,11 -36,24 -36,24 -35,26 -35,26 -33,27 -33,27 -25,26 -25,26 -23,24 -23,24 -22,11 -22))') != 0:
if (
ogrtest.check_feature_geometry(
feat_read,
"POLYGON ((6 -3,6 -40,19 -40,19 -39,25 -39,25 -38,27 -38,27 -37,28 -37,28 -36,29 -36,29 -35,30 -35,30 -34,31 -34,31 -25,30 -25,30 -24,29 -24,29 -23,28 -23,28 -22,27 -22,27 -21,24 -21,24 -20,23 -20,23 -19,26 -19,26 -18,27 -18,27 -17,28 -17,28 -16,29 -16,29 -8,28 -8,28 -7,27 -7,27 -6,26 -6,26 -5,24 -5,24 -4,18 -4,18 -3,6 -3),(11 -7,11 -18,23 -18,23 -17,24 -17,24 -16,25 -16,25 -9,24 -9,24 -8,23 -8,23 -7,11 -7),(11 -22,11 -36,24 -36,24 -35,26 -35,26 -33,27 -33,27 -25,26 -25,26 -23,24 -23,24 -22,11 -22))",
)
!= 0
):
print(feat_read.GetGeometryRef().ExportToWkt())
tr = 0
else:
@ -159,61 +189,79 @@ def test_polygonize_3():
assert tr
###############################################################################
# Test a simple case without masking but with 8-connectedness.
def test_polygonize_4():
src_ds = gdal.Open('data/polygonize_in.grd')
src_ds = gdal.Open("data/polygonize_in.grd")
src_band = src_ds.GetRasterBand(1)
# Create a memory OGR datasource to put results in.
mem_drv = ogr.GetDriverByName('Memory')
mem_ds = mem_drv.CreateDataSource('out')
mem_drv = ogr.GetDriverByName("Memory")
mem_ds = mem_drv.CreateDataSource("out")
mem_layer = mem_ds.CreateLayer('poly', None, ogr.wkbPolygon)
mem_layer = mem_ds.CreateLayer("poly", None, ogr.wkbPolygon)
fd = ogr.FieldDefn('DN', ogr.OFTInteger)
fd = ogr.FieldDefn("DN", ogr.OFTInteger)
mem_layer.CreateField(fd)
# run the algorithm.
result = gdal.Polygonize(src_band, None, mem_layer, 0, ["8CONNECTED=8"])
assert result == 0, 'Polygonize failed'
assert result == 0, "Polygonize failed"
# Confirm we get the set of expected features in the output layer.
expected_feature_number = 16
assert mem_layer.GetFeatureCount() == expected_feature_number
expect = [107, 123, 132, 115, 132, 115, 140, 148,
123, 140, 132, 156, 100, 101, 102, 103]
expect = [
107,
123,
132,
115,
132,
115,
140,
148,
123,
140,
132,
156,
100,
101,
102,
103,
]
tr = ogrtest.check_features_against_list(mem_layer, 'DN', expect)
tr = ogrtest.check_features_against_list(mem_layer, "DN", expect)
assert tr
###############################################################################
# Test a simple case with two inner wholes touchs at a vertex.
def test_polygonize_5():
src_ds = gdal.Open('data/polygonize_in_3.grd')
src_ds = gdal.Open("data/polygonize_in_3.grd")
src_band = src_ds.GetRasterBand(1)
# Create a memory OGR datasource to put results in.
mem_drv = ogr.GetDriverByName('Memory')
mem_ds = mem_drv.CreateDataSource('out')
mem_drv = ogr.GetDriverByName("Memory")
mem_ds = mem_drv.CreateDataSource("out")
mem_layer = mem_ds.CreateLayer('poly', None, ogr.wkbPolygon)
mem_layer = mem_ds.CreateLayer("poly", None, ogr.wkbPolygon)
fd = ogr.FieldDefn('DN', ogr.OFTInteger)
fd = ogr.FieldDefn("DN", ogr.OFTInteger)
mem_layer.CreateField(fd)
# run the algorithm.
result = gdal.Polygonize(src_band, None, mem_layer, 0)
assert result == 0, 'Polygonize failed'
assert result == 0, "Polygonize failed"
# Confirm we get the set of expected features in the output layer.
@ -222,9 +270,9 @@ def test_polygonize_5():
expect = [1, 0, 0]
expect_wkt = [
'POLYGON ((0 4,0 0,4 0,4 4,0 4),(1 3,1 2,2 2,2 3,1 3),(2 2,2 1,3 1,3 2,2 2))',
'POLYGON ((1 3,1 2,2 2,2 3,1 3))',
'POLYGON ((2 2,2 1,3 1,3 2,2 2))'
"POLYGON ((0 4,0 0,4 0,4 4,0 4),(1 3,1 2,2 2,2 3,1 3),(2 2,2 1,3 1,3 2,2 2))",
"POLYGON ((1 3,1 2,2 2,2 3,1 3))",
"POLYGON ((2 2,2 1,3 1,3 2,2 2))",
]
idx = 0
@ -238,26 +286,28 @@ def test_polygonize_5():
idx += 1
###############################################################################
# Test a simple case with two inner wholes touchs at a vertex.
def test_polygonize_6():
src_ds = gdal.Open('data/polygonize_in_4.grd')
src_ds = gdal.Open("data/polygonize_in_4.grd")
src_band = src_ds.GetRasterBand(1)
# Create a memory OGR datasource to put results in.
mem_drv = ogr.GetDriverByName('Memory')
mem_ds = mem_drv.CreateDataSource('out')
mem_drv = ogr.GetDriverByName("Memory")
mem_ds = mem_drv.CreateDataSource("out")
mem_layer = mem_ds.CreateLayer('poly', None, ogr.wkbPolygon)
mem_layer = mem_ds.CreateLayer("poly", None, ogr.wkbPolygon)
fd = ogr.FieldDefn('DN', ogr.OFTInteger)
fd = ogr.FieldDefn("DN", ogr.OFTInteger)
mem_layer.CreateField(fd)
# run the algorithm.
result = gdal.Polygonize(src_band, None, mem_layer, 0)
assert result == 0, 'Polygonize failed'
assert result == 0, "Polygonize failed"
# Confirm we get the set of expected features in the output layer.
@ -266,9 +316,9 @@ def test_polygonize_6():
expect = [0, 0, 1]
expect_wkt = [
'POLYGON ((2 3,2 2,3 2,3 3,2 3))',
'POLYGON ((1 2,1 1,2 1,2 2,1 2))',
'POLYGON ((0 4,0 0,4 0,4 4,0 4),(2 3,2 2,3 2,3 3,2 3),(1 2,1 1,2 1,2 2,1 2))'
"POLYGON ((2 3,2 2,3 2,3 3,2 3))",
"POLYGON ((1 2,1 1,2 1,2 2,1 2))",
"POLYGON ((0 4,0 0,4 0,4 4,0 4),(2 3,2 2,3 2,3 3,2 3),(1 2,1 1,2 1,2 2,1 2))",
]
idx = 0
@ -282,37 +332,41 @@ def test_polygonize_6():
idx += 1
###############################################################################
# Test a complex case to make sure the polygonized area match original raster.
def test_polygonize_7():
src_ds = gdal.Open('data/polygonize_check_area.tif')
src_ds = gdal.Open("data/polygonize_check_area.tif")
src_band = src_ds.GetRasterBand(1)
# Create a memory OGR datasource to put results in.
mem_drv = ogr.GetDriverByName('Memory')
mem_ds = mem_drv.CreateDataSource('out')
mem_drv = ogr.GetDriverByName("Memory")
mem_ds = mem_drv.CreateDataSource("out")
mem_layer = mem_ds.CreateLayer('poly', None, ogr.wkbPolygon)
mem_layer = mem_ds.CreateLayer("poly", None, ogr.wkbPolygon)
fd = ogr.FieldDefn('DN', ogr.OFTInteger)
fd = ogr.FieldDefn("DN", ogr.OFTInteger)
mem_layer.CreateField(fd)
# run the algorithm.
result = gdal.Polygonize(src_band, src_band.GetMaskBand(), mem_layer, 0)
assert result == 0, 'Polygonize failed'
assert result == 0, "Polygonize failed"
# collect raster image areas by DN value
transform = src_ds.GetGeoTransform()
pixel_area = abs(transform[1] * transform[5])
data = struct.unpack('h' * src_ds.RasterXSize * src_ds.RasterYSize,
src_band.ReadRaster(0, 0, src_ds.RasterXSize, src_ds.RasterYSize))
data = struct.unpack(
"h" * src_ds.RasterXSize * src_ds.RasterYSize,
src_band.ReadRaster(0, 0, src_ds.RasterXSize, src_ds.RasterYSize),
)
dn_area_raster = defaultdict(int)
for v in data:
if v != src_band.GetNoDataValue():
if v != src_band.GetNoDataValue():
dn_area_raster[v] += pixel_area
# collect vector image areas by DN value
@ -323,7 +377,9 @@ def test_polygonize_7():
geom = feature.GetGeometryRef()
dn_area_vector[id] += geom.GetArea()
assert len(dn_area_raster) == len(dn_area_vector), 'DN value inconsistent'
assert len(dn_area_raster) == len(dn_area_vector), "DN value inconsistent"
for key, value in dn_area_raster.items():
assert abs(value - dn_area_vector[key]) < pixel_area, 'polygonized vector area not match raster area'
assert (
abs(value - dn_area_vector[key]) < pixel_area
), "polygonized vector area not match raster area"

View File

@ -29,10 +29,9 @@
###############################################################################
import pytest
from osgeo import gdal
import pytest
###############################################################################
# Test a fairly default case.
@ -40,11 +39,11 @@ import pytest
def test_proximity_1():
drv = gdal.GetDriverByName('GTiff')
src_ds = gdal.Open('data/pat.tif')
drv = gdal.GetDriverByName("GTiff")
src_ds = gdal.Open("data/pat.tif")
src_band = src_ds.GetRasterBand(1)
dst_ds = drv.Create('tmp/proximity_1.tif', 25, 25, 1, gdal.GDT_Byte)
dst_ds = drv.Create("tmp/proximity_1.tif", 25, 25, 1, gdal.GDT_Byte)
dst_band = dst_ds.GetRasterBand(1)
gdal.ComputeProximity(src_band, dst_band)
@ -55,32 +54,32 @@ def test_proximity_1():
dst_band = None
dst_ds = None
if cs == cs_expected \
or gdal.GetConfigOption('CPL_DEBUG', 'OFF') != 'ON':
drv.Delete('tmp/proximity_1.tif')
if cs == cs_expected or gdal.GetConfigOption("CPL_DEBUG", "OFF") != "ON":
drv.Delete("tmp/proximity_1.tif")
if cs != cs_expected:
print('Got: ', cs)
pytest.fail('got wrong checksum')
print("Got: ", cs)
pytest.fail("got wrong checksum")
###############################################################################
# Try several options
def test_proximity_2():
drv = gdal.GetDriverByName('GTiff')
src_ds = gdal.Open('data/pat.tif')
drv = gdal.GetDriverByName("GTiff")
src_ds = gdal.Open("data/pat.tif")
src_band = src_ds.GetRasterBand(1)
dst_ds = drv.Create('tmp/proximity_2.tif', 25, 25, 1, gdal.GDT_Float32)
dst_ds = drv.Create("tmp/proximity_2.tif", 25, 25, 1, gdal.GDT_Float32)
dst_band = dst_ds.GetRasterBand(1)
gdal.ComputeProximity(src_band, dst_band,
options=['VALUES=65,64',
'MAXDIST=12',
'NODATA=-1',
'FIXED_BUF_VAL=255'])
gdal.ComputeProximity(
src_band,
dst_band,
options=["VALUES=65,64", "MAXDIST=12", "NODATA=-1", "FIXED_BUF_VAL=255"],
)
cs_expected = 3256
cs = dst_band.Checksum()
@ -88,32 +87,32 @@ def test_proximity_2():
dst_band = None
dst_ds = None
if cs == cs_expected \
or gdal.GetConfigOption('CPL_DEBUG', 'OFF') != 'ON':
drv.Delete('tmp/proximity_2.tif')
if cs == cs_expected or gdal.GetConfigOption("CPL_DEBUG", "OFF") != "ON":
drv.Delete("tmp/proximity_2.tif")
if cs != cs_expected:
print('Got: ', cs)
pytest.fail('got wrong checksum')
print("Got: ", cs)
pytest.fail("got wrong checksum")
###############################################################################
# Try input nodata option
def test_proximity_3():
drv = gdal.GetDriverByName('GTiff')
src_ds = gdal.Open('data/pat.tif')
drv = gdal.GetDriverByName("GTiff")
src_ds = gdal.Open("data/pat.tif")
src_band = src_ds.GetRasterBand(1)
dst_ds = drv.Create('tmp/proximity_3.tif', 25, 25, 1, gdal.GDT_Byte)
dst_ds = drv.Create("tmp/proximity_3.tif", 25, 25, 1, gdal.GDT_Byte)
dst_band = dst_ds.GetRasterBand(1)
gdal.ComputeProximity(src_band, dst_band,
options=['VALUES=65,64',
'MAXDIST=12',
'USE_INPUT_NODATA=YES',
'NODATA=0'])
gdal.ComputeProximity(
src_band,
dst_band,
options=["VALUES=65,64", "MAXDIST=12", "USE_INPUT_NODATA=YES", "NODATA=0"],
)
cs_expected = 1465
cs = dst_band.Checksum()
@ -121,13 +120,9 @@ def test_proximity_3():
dst_band = None
dst_ds = None
if cs == cs_expected \
or gdal.GetConfigOption('CPL_DEBUG', 'OFF') != 'ON':
drv.Delete('tmp/proximity_3.tif')
if cs == cs_expected or gdal.GetConfigOption("CPL_DEBUG", "OFF") != "ON":
drv.Delete("tmp/proximity_3.tif")
if cs != cs_expected:
print('Got: ', cs)
pytest.fail('got wrong checksum')
print("Got: ", cs)
pytest.fail("got wrong checksum")

View File

@ -30,11 +30,10 @@
import struct
import ogrtest
import pytest
from osgeo import gdal, ogr, osr
import pytest
###############################################################################
# Simple polygon rasterization.
@ -48,20 +47,18 @@ def test_rasterize_1():
# Create a memory raster to rasterize into.
target_ds = gdal.GetDriverByName('MEM').Create('', 100, 100, 3,
gdal.GDT_Byte)
target_ds = gdal.GetDriverByName("MEM").Create("", 100, 100, 3, gdal.GDT_Byte)
target_ds.SetGeoTransform((1000, 1, 0, 1100, 0, -1))
target_ds.SetProjection(sr_wkt)
# Create a memory layer to rasterize from.
rast_ogr_ds = \
ogr.GetDriverByName('Memory').CreateDataSource('wrk')
rast_mem_lyr = rast_ogr_ds.CreateLayer('poly', srs=sr)
rast_ogr_ds = ogr.GetDriverByName("Memory").CreateDataSource("wrk")
rast_mem_lyr = rast_ogr_ds.CreateLayer("poly", srs=sr)
# Add a polygon.
wkt_geom = 'POLYGON((1020 1030,1020 1045,1050 1045,1050 1030,1020 1030))'
wkt_geom = "POLYGON((1020 1030,1020 1045,1050 1045,1050 1030,1020 1030))"
feat = ogr.Feature(rast_mem_lyr.GetLayerDefn())
feat.SetGeometryDirectly(ogr.Geometry(wkt=wkt_geom))
@ -70,7 +67,7 @@ def test_rasterize_1():
# Add a linestring.
wkt_geom = 'LINESTRING(1000 1000, 1100 1050)'
wkt_geom = "LINESTRING(1000 1000, 1100 1050)"
feat = ogr.Feature(rast_mem_lyr.GetLayerDefn())
feat.SetGeometryDirectly(ogr.Geometry(wkt=wkt_geom))
@ -79,10 +76,11 @@ def test_rasterize_1():
# Run the algorithm.
err = gdal.RasterizeLayer(target_ds, [3, 2, 1], rast_mem_lyr,
burn_values=[256, 220, -1])
err = gdal.RasterizeLayer(
target_ds, [3, 2, 1], rast_mem_lyr, burn_values=[256, 220, -1]
)
assert err == 0, 'got non-zero result code from RasterizeLayer'
assert err == 0, "got non-zero result code from RasterizeLayer"
# Check results.
@ -90,8 +88,8 @@ def test_rasterize_1():
checksum = target_ds.GetRasterBand(2).Checksum()
if checksum != expected:
print(checksum)
gdal.GetDriverByName('GTiff').CreateCopy('tmp/rasterize_1.tif', target_ds)
pytest.fail('Did not get expected image checksum')
gdal.GetDriverByName("GTiff").CreateCopy("tmp/rasterize_1.tif", target_ds)
pytest.fail("Did not get expected image checksum")
_, maxval = target_ds.GetRasterBand(3).ComputeRasterMinMax()
assert maxval == 255
@ -100,7 +98,6 @@ def test_rasterize_1():
assert minval == 0
###############################################################################
# Test rasterization with ALL_TOUCHED.
@ -112,24 +109,27 @@ def test_rasterize_2():
# Create a memory raster to rasterize into.
target_ds = gdal.GetDriverByName('MEM').Create('', 12, 12, 3,
gdal.GDT_Byte)
target_ds = gdal.GetDriverByName("MEM").Create("", 12, 12, 3, gdal.GDT_Byte)
target_ds.SetGeoTransform((0, 1, 0, 12, 0, -1))
target_ds.SetProjection(sr_wkt)
# Create a memory layer to rasterize from.
cutline_ds = ogr.Open('data/cutline.csv')
cutline_ds = ogr.Open("data/cutline.csv")
# Run the algorithm.
gdal.PushErrorHandler('CPLQuietErrorHandler')
err = gdal.RasterizeLayer(target_ds, [3, 2, 1], cutline_ds.GetLayer(0),
burn_values=[200, 220, 240],
options=["ALL_TOUCHED=TRUE"])
gdal.PushErrorHandler("CPLQuietErrorHandler")
err = gdal.RasterizeLayer(
target_ds,
[3, 2, 1],
cutline_ds.GetLayer(0),
burn_values=[200, 220, 240],
options=["ALL_TOUCHED=TRUE"],
)
gdal.PopErrorHandler()
assert err == 0, 'got non-zero result code from RasterizeLayer'
assert err == 0, "got non-zero result code from RasterizeLayer"
# Check results.
@ -137,8 +137,8 @@ def test_rasterize_2():
checksum = target_ds.GetRasterBand(2).Checksum()
if checksum != expected:
print(checksum)
gdal.GetDriverByName('GTiff').CreateCopy('tmp/rasterize_2.tif', target_ds)
pytest.fail('Did not get expected image checksum')
gdal.GetDriverByName("GTiff").CreateCopy("tmp/rasterize_2.tif", target_ds)
pytest.fail("Did not get expected image checksum")
###############################################################################
@ -153,23 +153,23 @@ def test_rasterize_3():
# Create a memory raster to rasterize into.
target_ds = gdal.GetDriverByName('MEM').Create('', 100, 100, 3,
gdal.GDT_Byte)
target_ds = gdal.GetDriverByName("MEM").Create("", 100, 100, 3, gdal.GDT_Byte)
target_ds.SetGeoTransform((1000, 1, 0, 1100, 0, -1))
target_ds.SetProjection(sr_wkt)
# Create a memory layer to rasterize from.
rast_ogr_ds = \
ogr.GetDriverByName('Memory').CreateDataSource('wrk')
rast_mem_lyr = rast_ogr_ds.CreateLayer('poly', srs=sr)
rast_ogr_ds = ogr.GetDriverByName("Memory").CreateDataSource("wrk")
rast_mem_lyr = rast_ogr_ds.CreateLayer("poly", srs=sr)
# Add polygons and linestrings.
wkt_geom = ['POLYGON((1020 1030 40,1020 1045 30,1050 1045 20,1050 1030 35,1020 1030 40))',
'POLYGON((1010 1046 85,1015 1055 35,1055 1060 26,1054 1048 35,1010 1046 85))',
'POLYGON((1020 1076 190,1025 1085 35,1065 1090 26,1064 1078 35,1020 1076 190),(1023 1079 5,1061 1081 35,1062 1087 26,1028 1082 35,1023 1079 85))',
'LINESTRING(1005 1000 10, 1100 1050 120)',
'LINESTRING(1000 1000 150, 1095 1050 -5, 1080 1080 200)']
wkt_geom = [
"POLYGON((1020 1030 40,1020 1045 30,1050 1045 20,1050 1030 35,1020 1030 40))",
"POLYGON((1010 1046 85,1015 1055 35,1055 1060 26,1054 1048 35,1010 1046 85))",
"POLYGON((1020 1076 190,1025 1085 35,1065 1090 26,1064 1078 35,1020 1076 190),(1023 1079 5,1061 1081 35,1062 1087 26,1028 1082 35,1023 1079 85))",
"LINESTRING(1005 1000 10, 1100 1050 120)",
"LINESTRING(1000 1000 150, 1095 1050 -5, 1080 1080 200)",
]
for g in wkt_geom:
feat = ogr.Feature(rast_mem_lyr.GetLayerDefn())
feat.SetGeometryDirectly(ogr.Geometry(wkt=g))
@ -177,10 +177,15 @@ def test_rasterize_3():
# Run the algorithm.
err = gdal.RasterizeLayer(target_ds, [3, 2, 1], rast_mem_lyr,
burn_values=[10, 10, 55], options=["BURN_VALUE_FROM=Z"])
err = gdal.RasterizeLayer(
target_ds,
[3, 2, 1],
rast_mem_lyr,
burn_values=[10, 10, 55],
options=["BURN_VALUE_FROM=Z"],
)
assert err == 0, 'got non-zero result code from RasterizeLayer'
assert err == 0, "got non-zero result code from RasterizeLayer"
# Check results.
@ -188,8 +193,8 @@ def test_rasterize_3():
checksum = target_ds.GetRasterBand(2).Checksum()
if checksum != expected:
print(checksum)
gdal.GetDriverByName('GTiff').CreateCopy('tmp/rasterize_3.tif', target_ds)
pytest.fail('Did not get expected image checksum')
gdal.GetDriverByName("GTiff").CreateCopy("tmp/rasterize_3.tif", target_ds)
pytest.fail("Did not get expected image checksum")
###############################################################################
@ -203,47 +208,48 @@ def test_rasterize_4():
sr = osr.SpatialReference(sr_wkt)
# Create a memory raster to rasterize into.
target_ds = gdal.GetDriverByName('MEM').Create('', 100, 100, 3,
gdal.GDT_Byte)
target_ds = gdal.GetDriverByName("MEM").Create("", 100, 100, 3, gdal.GDT_Byte)
target_ds.SetGeoTransform((1000, 1, 0, 1100, 0, -1))
target_ds.SetProjection(sr_wkt)
# Create a memory layer to rasterize from.
rast_ogr_ds = ogr.GetDriverByName('Memory').CreateDataSource('wrk')
rast_mem_lyr = rast_ogr_ds.CreateLayer('poly', srs=sr)
rast_ogr_ds = ogr.GetDriverByName("Memory").CreateDataSource("wrk")
rast_mem_lyr = rast_ogr_ds.CreateLayer("poly", srs=sr)
# Setup Schema
ogrtest.quick_create_layer_def(rast_mem_lyr,
[('CELSIUS', ogr.OFTReal)])
ogrtest.quick_create_layer_def(rast_mem_lyr, [("CELSIUS", ogr.OFTReal)])
# Add polygons and linestrings and a field named CELSIUS.
wkt_geom = ['POLYGON((1020 1030 40,1020 1045 30,1050 1045 20,1050 1030 35,1020 1030 40))',
'POLYGON((1010 1046 85,1015 1055 35,1055 1060 26,1054 1048 35,1010 1046 85))',
'POLYGON((1020 1076 190,1025 1085 35,1065 1090 26,1064 1078 35,1020 1076 190),(1023 1079 5,1061 1081 35,1062 1087 26,1028 1082 35,1023 1079 85))',
'LINESTRING(1005 1000 10, 1100 1050 120)',
'LINESTRING(1000 1000 150, 1095 1050 -5, 1080 1080 200)']
wkt_geom = [
"POLYGON((1020 1030 40,1020 1045 30,1050 1045 20,1050 1030 35,1020 1030 40))",
"POLYGON((1010 1046 85,1015 1055 35,1055 1060 26,1054 1048 35,1010 1046 85))",
"POLYGON((1020 1076 190,1025 1085 35,1065 1090 26,1064 1078 35,1020 1076 190),(1023 1079 5,1061 1081 35,1062 1087 26,1028 1082 35,1023 1079 85))",
"LINESTRING(1005 1000 10, 1100 1050 120)",
"LINESTRING(1000 1000 150, 1095 1050 -5, 1080 1080 200)",
]
celsius_field_values = [50, 255, 60, 100, 180]
i = 0
for g in wkt_geom:
feat = ogr.Feature(rast_mem_lyr.GetLayerDefn())
feat.SetGeometryDirectly(ogr.Geometry(wkt=g))
feat.SetField('CELSIUS', celsius_field_values[i])
feat.SetField("CELSIUS", celsius_field_values[i])
rast_mem_lyr.CreateFeature(feat)
i = i + 1
# Run the algorithm.
err = gdal.RasterizeLayer(target_ds, [1, 2, 3], rast_mem_lyr,
options=["ATTRIBUTE=CELSIUS"])
err = gdal.RasterizeLayer(
target_ds, [1, 2, 3], rast_mem_lyr, options=["ATTRIBUTE=CELSIUS"]
)
assert err == 0, 'got non-zero result code from RasterizeLayer'
assert err == 0, "got non-zero result code from RasterizeLayer"
# Check results.
expected = 16265
checksum = target_ds.GetRasterBand(2).Checksum()
if checksum != expected:
print(checksum)
gdal.GetDriverByName('GTiff').CreateCopy('tmp/rasterize_4.tif', target_ds)
pytest.fail('Did not get expected image checksum')
gdal.GetDriverByName("GTiff").CreateCopy("tmp/rasterize_4.tif", target_ds)
pytest.fail("Did not get expected image checksum")
###############################################################################
@ -258,48 +264,50 @@ def test_rasterize_5():
# Create a memory raster to rasterize into.
target_ds = gdal.GetDriverByName('MEM').Create('', 100, 100, 3,
gdal.GDT_Byte)
target_ds = gdal.GetDriverByName("MEM").Create("", 100, 100, 3, gdal.GDT_Byte)
target_ds.SetGeoTransform((1000, 1, 0, 1100, 0, -1))
target_ds.SetProjection(sr_wkt)
# Create a memory layer to rasterize from.
rast_ogr_ds = \
ogr.GetDriverByName('Memory').CreateDataSource('wrk')
rast_mem_lyr = rast_ogr_ds.CreateLayer('poly', srs=sr)
rast_ogr_ds = ogr.GetDriverByName("Memory").CreateDataSource("wrk")
rast_mem_lyr = rast_ogr_ds.CreateLayer("poly", srs=sr)
# Add polygons.
wkt_geom = 'POLYGON((1020 1030,1020 1045,1050 1045,1050 1030,1020 1030))'
wkt_geom = "POLYGON((1020 1030,1020 1045,1050 1045,1050 1030,1020 1030))"
feat = ogr.Feature(rast_mem_lyr.GetLayerDefn())
feat.SetGeometryDirectly(ogr.Geometry(wkt=wkt_geom))
rast_mem_lyr.CreateFeature(feat)
wkt_geom = 'POLYGON((1045 1050,1055 1050,1055 1020,1045 1020,1045 1050))'
wkt_geom = "POLYGON((1045 1050,1055 1050,1055 1020,1045 1020,1045 1050))"
feat = ogr.Feature(rast_mem_lyr.GetLayerDefn())
feat.SetGeometryDirectly(ogr.Geometry(wkt=wkt_geom))
rast_mem_lyr.CreateFeature(feat)
# Add linestrings.
wkt_geom = 'LINESTRING(1000 1000, 1100 1050)'
wkt_geom = "LINESTRING(1000 1000, 1100 1050)"
feat = ogr.Feature(rast_mem_lyr.GetLayerDefn())
feat.SetGeometryDirectly(ogr.Geometry(wkt=wkt_geom))
rast_mem_lyr.CreateFeature(feat)
wkt_geom = 'LINESTRING(1005 1000, 1000 1050)'
wkt_geom = "LINESTRING(1005 1000, 1000 1050)"
feat = ogr.Feature(rast_mem_lyr.GetLayerDefn())
feat.SetGeometryDirectly(ogr.Geometry(wkt=wkt_geom))
rast_mem_lyr.CreateFeature(feat)
# Run the algorithm.
err = gdal.RasterizeLayer(target_ds, [1, 2, 3], rast_mem_lyr,
burn_values=[256, 110, -1],
options=["MERGE_ALG=ADD"])
err = gdal.RasterizeLayer(
target_ds,
[1, 2, 3],
rast_mem_lyr,
burn_values=[256, 110, -1],
options=["MERGE_ALG=ADD"],
)
assert err == 0, 'got non-zero result code from RasterizeLayer'
assert err == 0, "got non-zero result code from RasterizeLayer"
# Check results.
@ -307,8 +315,8 @@ def test_rasterize_5():
checksum = target_ds.GetRasterBand(2).Checksum()
if checksum != expected:
print(checksum)
gdal.GetDriverByName('GTiff').CreateCopy('tmp/rasterize_5.tif', target_ds)
pytest.fail('Did not get expected image checksum')
gdal.GetDriverByName("GTiff").CreateCopy("tmp/rasterize_5.tif", target_ds)
pytest.fail("Did not get expected image checksum")
_, maxval = target_ds.GetRasterBand(1).ComputeRasterMinMax()
assert maxval == 255
@ -320,21 +328,117 @@ def test_rasterize_5():
###############################################################################
# Test bug fix for #5580 (used to hang)
def test_rasterize_6():
# Setup working spatial reference
sr_wkt = 'LOCAL_CS["arbitrary"]'
sr = osr.SpatialReference(sr_wkt)
wkb = struct.pack('B' * 93, 0, 0, 0, 0, 3, 0, 0, 0, 1, 0, 0, 0, 5, 65, 28, 138, 141, 120, 239, 76, 104, 65, 87, 9, 185, 80, 29, 20, 208, 65, 28, 144, 191, 125, 165, 41, 54, 65, 87, 64, 14, 111, 103, 53, 124, 65, 30, 132, 127, 255, 255, 255, 254, 65, 87, 63, 241, 218, 241, 62, 127, 65, 30, 132, 128, 0, 0, 0, 0, 65, 87, 9, 156, 142, 126, 144, 236, 65, 28, 138, 141, 120, 239, 76, 104, 65, 87, 9, 185, 80, 29, 20, 208)
wkb = struct.pack(
"B" * 93,
0,
0,
0,
0,
3,
0,
0,
0,
1,
0,
0,
0,
5,
65,
28,
138,
141,
120,
239,
76,
104,
65,
87,
9,
185,
80,
29,
20,
208,
65,
28,
144,
191,
125,
165,
41,
54,
65,
87,
64,
14,
111,
103,
53,
124,
65,
30,
132,
127,
255,
255,
255,
254,
65,
87,
63,
241,
218,
241,
62,
127,
65,
30,
132,
128,
0,
0,
0,
0,
65,
87,
9,
156,
142,
126,
144,
236,
65,
28,
138,
141,
120,
239,
76,
104,
65,
87,
9,
185,
80,
29,
20,
208,
)
data_source = ogr.GetDriverByName('MEMORY').CreateDataSource('')
layer = data_source.CreateLayer('', sr, geom_type=ogr.wkbPolygon)
data_source = ogr.GetDriverByName("MEMORY").CreateDataSource("")
layer = data_source.CreateLayer("", sr, geom_type=ogr.wkbPolygon)
feature = ogr.Feature(layer.GetLayerDefn())
feature.SetGeometryDirectly(ogr.CreateGeometryFromWkb(wkb))
layer.CreateFeature(feature)
mask_ds = gdal.GetDriverByName('Mem').Create('', 5000, 5000, 1, gdal.GDT_Byte)
mask_ds = gdal.GetDriverByName("Mem").Create("", 5000, 5000, 1, gdal.GDT_Byte)
mask_ds.SetGeoTransform([499000, 0.4, 0, 6095000, 0, -0.4])
mask_ds.SetProjection(sr_wkt)
@ -345,58 +449,250 @@ def test_rasterize_6():
# Test rasterizing linestring with multiple segments and MERGE_ALG=ADD
# Tests https://github.com/OSGeo/gdal/issues/1307
def test_rasterize_merge_alg_add_multiple_segment_linestring():
# Setup working spatial reference
sr_wkt = 'LOCAL_CS["arbitrary"]'
sr = osr.SpatialReference(sr_wkt)
data_source = ogr.GetDriverByName('MEMORY').CreateDataSource('')
layer = data_source.CreateLayer('', sr, geom_type=ogr.wkbLineString)
data_source = ogr.GetDriverByName("MEMORY").CreateDataSource("")
layer = data_source.CreateLayer("", sr, geom_type=ogr.wkbLineString)
feature = ogr.Feature(layer.GetLayerDefn())
# Diagonal segments
feature.SetGeometryDirectly(ogr.CreateGeometryFromWkt('LINESTRING(0.5 0.5,100.5 50.5,199.5 99.5)'))
feature.SetGeometryDirectly(
ogr.CreateGeometryFromWkt("LINESTRING(0.5 0.5,100.5 50.5,199.5 99.5)")
)
layer.CreateFeature(feature)
feature = ogr.Feature(layer.GetLayerDefn())
# Vertical and horizontal segments
feature.SetGeometryDirectly(ogr.CreateGeometryFromWkt('LINESTRING(30.5 40.5,30.5 70.5,50.5 70.5)'))
feature.SetGeometryDirectly(
ogr.CreateGeometryFromWkt("LINESTRING(30.5 40.5,30.5 70.5,50.5 70.5)")
)
layer.CreateFeature(feature)
ds = gdal.GetDriverByName('Mem').Create('', 10, 10, 1, gdal.GDT_Byte)
ds = gdal.GetDriverByName("Mem").Create("", 10, 10, 1, gdal.GDT_Byte)
ds.SetGeoTransform([0, 20, 0, 100, 0, -10])
ds.SetProjection(sr_wkt)
ds.GetRasterBand(1).Fill(0)
gdal.RasterizeLayer(ds, [1], layer, burn_values=[1], options=["MERGE_ALG=ADD"])
got = struct.unpack('B' * 100, ds.ReadRaster())
expected = (0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
0, 1, 1, 0, 0, 0, 0, 1, 0, 0,
0, 1, 0, 0, 0, 0, 1, 0, 0, 0,
0, 1, 0, 0, 0, 1, 0, 0, 0, 0,
0, 1, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0)
assert got == expected, '%s' % str(got)
got = struct.unpack("B" * 100, ds.ReadRaster())
expected = (
0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0,
0,
0,
0,
0,
1,
0,
0,
1,
1,
0,
0,
0,
0,
1,
0,
0,
0,
1,
0,
0,
0,
0,
1,
0,
0,
0,
0,
1,
0,
0,
0,
1,
0,
0,
0,
0,
0,
1,
0,
0,
1,
0,
0,
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
)
assert got == expected, "%s" % str(got)
ds.GetRasterBand(1).Fill(0)
gdal.RasterizeLayer(ds, [1], layer, burn_values=[1], options=["MERGE_ALG=ADD", "ALL_TOUCHED"])
gdal.RasterizeLayer(
ds, [1], layer, burn_values=[1], options=["MERGE_ALG=ADD", "ALL_TOUCHED"]
)
got = struct.unpack("B" * 100, ds.ReadRaster())
expected = (
0,
0,
0,
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0,
0,
0,
0,
0,
1,
1,
0,
1,
1,
0,
0,
0,
1,
1,
1,
0,
0,
1,
0,
0,
0,
1,
1,
0,
0,
0,
0,
1,
0,
0,
1,
1,
0,
0,
0,
0,
0,
1,
0,
1,
1,
0,
0,
0,
0,
0,
0,
0,
1,
1,
0,
0,
0,
0,
0,
0,
0,
1,
1,
0,
0,
0,
0,
0,
0,
0,
1,
1,
0,
0,
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
)
assert got == expected, "%s" % str(got)
got = struct.unpack('B' * 100, ds.ReadRaster())
expected = (0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
0, 1, 1, 0, 0, 0, 1, 1, 1, 0,
0, 1, 0, 0, 0, 1, 1, 0, 0, 0,
0, 1, 0, 0, 1, 1, 0, 0, 0, 0,
0, 1, 0, 1, 1, 0, 0, 0, 0, 0,
0, 0, 1, 1, 0, 0, 0, 0, 0, 0,
0, 1, 1, 0, 0, 0, 0, 0, 0, 0,
1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0)
assert got == expected, '%s' % str(got)
###############################################################################
# Test rasterizing polygon with horizontal segments and MERGE_ALG=ADD
@ -404,33 +700,56 @@ def test_rasterize_merge_alg_add_multiple_segment_linestring():
# the winding order
@pytest.mark.parametrize("wkt",
['POLYGON((0 0,0 1,1 1,1 0,0 0))',
'POLYGON((0 0,1 0,1 1,0 1,0 0))'],
ids=['clockwise', 'counterclockwise'])
@pytest.mark.parametrize(
"wkt",
["POLYGON((0 0,0 1,1 1,1 0,0 0))", "POLYGON((0 0,1 0,1 1,0 1,0 0))"],
ids=["clockwise", "counterclockwise"],
)
def test_rasterize_merge_alg_add_polygon(wkt):
# Setup working spatial reference
sr_wkt = 'LOCAL_CS["arbitrary"]'
sr = osr.SpatialReference(sr_wkt)
data_source = ogr.GetDriverByName('MEMORY').CreateDataSource('')
layer = data_source.CreateLayer('', sr, geom_type=ogr.wkbPolygon)
data_source = ogr.GetDriverByName("MEMORY").CreateDataSource("")
layer = data_source.CreateLayer("", sr, geom_type=ogr.wkbPolygon)
feature = ogr.Feature(layer.GetLayerDefn())
feature.SetGeometryDirectly(ogr.CreateGeometryFromWkt(wkt))
layer.CreateFeature(feature)
ds = gdal.GetDriverByName('Mem').Create('', 5, 5, 1, gdal.GDT_Byte)
ds = gdal.GetDriverByName("Mem").Create("", 5, 5, 1, gdal.GDT_Byte)
ds.SetGeoTransform([-0.125, 0.25, 0, 1.125, 0, -0.25])
ds.SetProjection(sr_wkt)
ds.GetRasterBand(1).Fill(0)
gdal.RasterizeLayer(ds, [1], layer, burn_values=[10], options=["MERGE_ALG=ADD"])
got = struct.unpack('B' * 25, ds.ReadRaster())
expected = (0, 10, 10, 10, 10,
0, 10, 10, 10, 10,
0, 10, 10, 10, 10,
0, 10, 10, 10, 10,
0, 10, 10, 10, 10,)
assert got == expected, '%s' % str(got)
got = struct.unpack("B" * 25, ds.ReadRaster())
expected = (
0,
10,
10,
10,
10,
0,
10,
10,
10,
10,
0,
10,
10,
10,
10,
0,
10,
10,
10,
10,
0,
10,
10,
10,
10,
)
assert got == expected, "%s" % str(got)

View File

@ -30,22 +30,22 @@
###############################################################################
from osgeo import gdal
from osgeo import osr
import pytest
from osgeo import gdal, osr
###############################################################################
# Test a trivial case.
def test_reproject_1():
drv = gdal.GetDriverByName('GTiff')
src_ds = gdal.Open('../gcore/data/byte.tif')
drv = gdal.GetDriverByName("GTiff")
src_ds = gdal.Open("../gcore/data/byte.tif")
dst_ds = drv.Create('tmp/byte.tif', src_ds.RasterXSize, src_ds.RasterYSize, gdal.GDT_Byte)
dst_ds = drv.Create(
"tmp/byte.tif", src_ds.RasterXSize, src_ds.RasterYSize, gdal.GDT_Byte
)
dst_ds.SetProjection(src_ds.GetProjectionRef())
dst_ds.SetGeoTransform(src_ds.GetGeoTransform())
@ -56,12 +56,13 @@ def test_reproject_1():
dst_ds = None
drv.Delete('tmp/byte.tif')
drv.Delete("tmp/byte.tif")
if cs != cs_expected:
print('Got: ', cs)
pytest.fail('got wrong checksum')
print("Got: ", cs)
pytest.fail("got wrong checksum")
###############################################################################
# Test a real reprojection case.
@ -74,11 +75,20 @@ def test_reproject_2():
sr2 = osr.SpatialReference()
sr2.ImportFromEPSG(4326)
drv = gdal.GetDriverByName('GTiff')
src_ds = gdal.Open('../gcore/data/byte.tif')
drv = gdal.GetDriverByName("GTiff")
src_ds = gdal.Open("../gcore/data/byte.tif")
dst_ds = drv.Create('tmp/byte_4326.tif', 22, 18, gdal.GDT_Byte)
dst_ds.SetGeoTransform([-117.641169915168746, 0.000598105625684, 0, 33.900668703925191, 0, -0.000598105625684])
dst_ds = drv.Create("tmp/byte_4326.tif", 22, 18, gdal.GDT_Byte)
dst_ds.SetGeoTransform(
[
-117.641169915168746,
0.000598105625684,
0,
33.900668703925191,
0,
-0.000598105625684,
]
)
gdal.ReprojectImage(src_ds, dst_ds, sr.ExportToWkt(), sr2.ExportToWkt())
@ -87,60 +97,65 @@ def test_reproject_2():
dst_ds = None
drv.Delete('tmp/byte_4326.tif')
drv.Delete("tmp/byte_4326.tif")
if cs != cs_expected:
print('Got: ', cs)
pytest.fail('got wrong checksum')
print("Got: ", cs)
pytest.fail("got wrong checksum")
###############################################################################
# Test nodata values
def test_reproject_3():
data = '\x02\x7f\x7f\x02\x02\x7f\x7f\x02\x02\x7f\x7f\x02'
src_ds = gdal.GetDriverByName('MEM').Create('', 4, 3)
data = "\x02\x7f\x7f\x02\x02\x7f\x7f\x02\x02\x7f\x7f\x02"
src_ds = gdal.GetDriverByName("MEM").Create("", 4, 3)
src_ds.GetRasterBand(1).WriteRaster(0, 0, 4, 3, data)
src_ds.GetRasterBand(1).SetNoDataValue(2)
src_ds.SetGeoTransform([10, 1, 0, 10, 0, -1])
dst_ds = gdal.GetDriverByName('MEM').Create('', 6, 3)
dst_ds = gdal.GetDriverByName("MEM").Create("", 6, 3)
dst_ds.GetRasterBand(1).SetNoDataValue(3)
dst_ds.GetRasterBand(1).Fill(3)
dst_ds.SetGeoTransform([10, 2. / 3., 0, 10, 0, -1])
dst_ds.SetGeoTransform([10, 2.0 / 3.0, 0, 10, 0, -1])
gdal.ReprojectImage(src_ds, dst_ds, '', '', gdal.GRA_Bilinear)
got_data = dst_ds.GetRasterBand(1).ReadRaster(0, 0, 6, 3).decode('latin1')
expected_data = '\x03\x7f\x7f\x7f\x03\x03\x03\x7f\x7f\x7f\x03\x03\x03\x7f\x7f\x7f\x03\x03'
gdal.ReprojectImage(src_ds, dst_ds, "", "", gdal.GRA_Bilinear)
got_data = dst_ds.GetRasterBand(1).ReadRaster(0, 0, 6, 3).decode("latin1")
expected_data = (
"\x03\x7f\x7f\x7f\x03\x03\x03\x7f\x7f\x7f\x03\x03\x03\x7f\x7f\x7f\x03\x03"
)
if got_data != expected_data:
import struct
pytest.fail(struct.unpack('B' * 18, got_data))
pytest.fail(struct.unpack("B" * 18, got_data))
###############################################################################
# Test warp options
def test_reproject_4():
data = '\x02\x7f\x7f\x02\x02\x7f\x7f\x02\x02\x7f\x7f\x02'
src_ds = gdal.GetDriverByName('MEM').Create('', 4, 3)
data = "\x02\x7f\x7f\x02\x02\x7f\x7f\x02\x02\x7f\x7f\x02"
src_ds = gdal.GetDriverByName("MEM").Create("", 4, 3)
src_ds.GetRasterBand(1).WriteRaster(0, 0, 4, 3, data)
src_ds.GetRasterBand(1).SetNoDataValue(2)
src_ds.SetGeoTransform([10, 1, 0, 10, 0, -1])
dst_ds = gdal.GetDriverByName('MEM').Create('', 6, 3)
dst_ds = gdal.GetDriverByName("MEM").Create("", 6, 3)
dst_ds.GetRasterBand(1).SetNoDataValue(3)
dst_ds.SetGeoTransform([10, 2. / 3., 0, 10, 0, -1])
dst_ds.SetGeoTransform([10, 2.0 / 3.0, 0, 10, 0, -1])
gdal.ReprojectImage(src_ds, dst_ds, '', '', gdal.GRA_Bilinear, options=['INIT_DEST=NO_DATA'])
got_data = dst_ds.GetRasterBand(1).ReadRaster(0, 0, 6, 3).decode('latin1')
expected_data = '\x03\x7f\x7f\x7f\x03\x03\x03\x7f\x7f\x7f\x03\x03\x03\x7f\x7f\x7f\x03\x03'
gdal.ReprojectImage(
src_ds, dst_ds, "", "", gdal.GRA_Bilinear, options=["INIT_DEST=NO_DATA"]
)
got_data = dst_ds.GetRasterBand(1).ReadRaster(0, 0, 6, 3).decode("latin1")
expected_data = (
"\x03\x7f\x7f\x7f\x03\x03\x03\x7f\x7f\x7f\x03\x03\x03\x7f\x7f\x7f\x03\x03"
)
if got_data != expected_data:
import struct
pytest.fail(struct.unpack('B' * 18, got_data))
pytest.fail(struct.unpack("B" * 18, got_data))

View File

@ -30,10 +30,9 @@
###############################################################################
import pytest
from osgeo import gdal
import pytest
###############################################################################
# Test a fairly default case.
@ -41,11 +40,11 @@ import pytest
def test_sieve_1():
drv = gdal.GetDriverByName('GTiff')
src_ds = gdal.Open('data/sieve_src.grd')
drv = gdal.GetDriverByName("GTiff")
src_ds = gdal.Open("data/sieve_src.grd")
src_band = src_ds.GetRasterBand(1)
dst_ds = drv.Create('tmp/sieve_1.tif', 5, 7, 1, gdal.GDT_Byte)
dst_ds = drv.Create("tmp/sieve_1.tif", 5, 7, 1, gdal.GDT_Byte)
dst_band = dst_ds.GetRasterBand(1)
gdal.SieveFilter(src_band, None, dst_band, 2, 4)
@ -56,25 +55,25 @@ def test_sieve_1():
dst_band = None
dst_ds = None
if cs == cs_expected \
or gdal.GetConfigOption('CPL_DEBUG', 'OFF') != 'ON':
drv.Delete('tmp/sieve_1.tif')
if cs == cs_expected or gdal.GetConfigOption("CPL_DEBUG", "OFF") != "ON":
drv.Delete("tmp/sieve_1.tif")
if cs != cs_expected:
print('Got: ', cs)
pytest.fail('got wrong checksum')
print("Got: ", cs)
pytest.fail("got wrong checksum")
###############################################################################
# Try eight connected.
def test_sieve_2():
drv = gdal.GetDriverByName('GTiff')
src_ds = gdal.Open('data/sieve_src.grd')
drv = gdal.GetDriverByName("GTiff")
src_ds = gdal.Open("data/sieve_src.grd")
src_band = src_ds.GetRasterBand(1)
dst_ds = drv.Create('tmp/sieve_2.tif', 5, 7, 1, gdal.GDT_Byte)
dst_ds = drv.Create("tmp/sieve_2.tif", 5, 7, 1, gdal.GDT_Byte)
dst_band = dst_ds.GetRasterBand(1)
gdal.SieveFilter(src_band, None, dst_band, 2, 8)
@ -85,25 +84,25 @@ def test_sieve_2():
dst_band = None
dst_ds = None
if cs == cs_expected \
or gdal.GetConfigOption('CPL_DEBUG', 'OFF') != 'ON':
drv.Delete('tmp/sieve_2.tif')
if cs == cs_expected or gdal.GetConfigOption("CPL_DEBUG", "OFF") != "ON":
drv.Delete("tmp/sieve_2.tif")
if cs != cs_expected:
print('Got: ', cs)
pytest.fail('got wrong checksum')
print("Got: ", cs)
pytest.fail("got wrong checksum")
###############################################################################
# Do a sieve resulting in unmergable polygons.
def test_sieve_3():
drv = gdal.GetDriverByName('GTiff')
src_ds = gdal.Open('data/unmergable.grd')
drv = gdal.GetDriverByName("GTiff")
src_ds = gdal.Open("data/unmergable.grd")
src_band = src_ds.GetRasterBand(1)
dst_ds = drv.Create('tmp/sieve_3.tif', 5, 7, 1, gdal.GDT_Byte)
dst_ds = drv.Create("tmp/sieve_3.tif", 5, 7, 1, gdal.GDT_Byte)
dst_band = dst_ds.GetRasterBand(1)
gdal.SieveFilter(src_band, None, dst_band, 2, 8)
@ -115,25 +114,25 @@ def test_sieve_3():
dst_band = None
dst_ds = None
if cs == cs_expected \
or gdal.GetConfigOption('CPL_DEBUG', 'OFF') != 'ON':
drv.Delete('tmp/sieve_3.tif')
if cs == cs_expected or gdal.GetConfigOption("CPL_DEBUG", "OFF") != "ON":
drv.Delete("tmp/sieve_3.tif")
if cs != cs_expected:
print('Got: ', cs)
pytest.fail('got wrong checksum')
print("Got: ", cs)
pytest.fail("got wrong checksum")
###############################################################################
# Try the bug 2634 simplified data.
def test_sieve_4():
drv = gdal.GetDriverByName('GTiff')
src_ds = gdal.Open('data/sieve_2634.grd')
drv = gdal.GetDriverByName("GTiff")
src_ds = gdal.Open("data/sieve_2634.grd")
src_band = src_ds.GetRasterBand(1)
dst_ds = drv.Create('tmp/sieve_4.tif', 10, 8, 1, gdal.GDT_Byte)
dst_ds = drv.Create("tmp/sieve_4.tif", 10, 8, 1, gdal.GDT_Byte)
dst_band = dst_ds.GetRasterBand(1)
gdal.SieveFilter(src_band, None, dst_band, 2, 4)
@ -144,26 +143,26 @@ def test_sieve_4():
dst_band = None
dst_ds = None
if cs == cs_expected \
or gdal.GetConfigOption('CPL_DEBUG', 'OFF') != 'ON':
drv.Delete('tmp/sieve_4.tif')
if cs == cs_expected or gdal.GetConfigOption("CPL_DEBUG", "OFF") != "ON":
drv.Delete("tmp/sieve_4.tif")
if cs != cs_expected:
print('Got: ', cs)
pytest.fail('got wrong checksum')
print("Got: ", cs)
pytest.fail("got wrong checksum")
###############################################################################
# Same as sieve_1, but we provide a mask band
# This should yield the same result as we use an opaque band
def test_sieve_5():
drv = gdal.GetDriverByName('GTiff')
src_ds = gdal.Open('data/sieve_src.grd')
drv = gdal.GetDriverByName("GTiff")
src_ds = gdal.Open("data/sieve_src.grd")
src_band = src_ds.GetRasterBand(1)
dst_ds = drv.Create('tmp/sieve_1.tif', 5, 7, 1, gdal.GDT_Byte)
dst_ds = drv.Create("tmp/sieve_1.tif", 5, 7, 1, gdal.GDT_Byte)
dst_band = dst_ds.GetRasterBand(1)
gdal.SieveFilter(src_band, dst_band.GetMaskBand(), dst_band, 2, 4)
@ -174,14 +173,14 @@ def test_sieve_5():
dst_band = None
dst_ds = None
if cs == cs_expected \
or gdal.GetConfigOption('CPL_DEBUG', 'OFF') != 'ON':
drv.Delete('tmp/sieve_1.tif')
if cs == cs_expected or gdal.GetConfigOption("CPL_DEBUG", "OFF") != "ON":
drv.Delete("tmp/sieve_1.tif")
if cs != cs_expected:
print('Got: ', cs)
pytest.fail('got wrong checksum')
print("Got: ", cs)
pytest.fail("got wrong checksum")
###############################################################################
# Performance test. When increasing the 'size' parameter, performance
# should stay roughly linear with the number of pixels (i.e. size^2)
@ -197,7 +196,7 @@ def test_sieve_6():
# Try 3002. Should run in less than 10 seconds
# size = 3002
size = 102
ds = gdal.GetDriverByName('MEM').Create('', size + 1, size)
ds = gdal.GetDriverByName("MEM").Create("", size + 1, size)
ar = numpy.zeros((size, size + 1), dtype=numpy.uint8)
for i in range(size):
for j in range(int(size / 3)):
@ -216,17 +215,19 @@ def test_sieve_6():
cs = band.Checksum()
if (size == 102 and cs != 60955) or (size == 3002 and cs != 63178):
print('Got: ', cs)
pytest.fail('got wrong checksum')
print("Got: ", cs)
pytest.fail("got wrong checksum")
###############################################################################
# Test with nodata
def test_sieve_7():
gdal.FileFromMemBuffer('/vsimem/sieve_7.asc', """ncols 7
gdal.FileFromMemBuffer(
"/vsimem/sieve_7.asc",
"""ncols 7
nrows 7
xllcorner 440720.000000000000
yllcorner 3750120.000000000000
@ -239,13 +240,14 @@ NODATA_value 0
0 1 1 2 1 2 1
0 1 1 2 2 2 1
0 1 1 1 1 1 1
""")
""",
)
drv = gdal.GetDriverByName('GTiff')
src_ds = gdal.Open('/vsimem/sieve_7.asc')
drv = gdal.GetDriverByName("GTiff")
src_ds = gdal.Open("/vsimem/sieve_7.asc")
src_band = src_ds.GetRasterBand(1)
dst_ds = drv.Create('/vsimem/sieve_7.tif', 7, 7, 1, gdal.GDT_Byte)
dst_ds = drv.Create("/vsimem/sieve_7.tif", 7, 7, 1, gdal.GDT_Byte)
dst_band = dst_ds.GetRasterBand(1)
gdal.SieveFilter(src_band, src_band.GetMaskBand(), dst_band, 4, 4)
@ -256,11 +258,10 @@ NODATA_value 0
dst_band = None
dst_ds = None
gdal.Unlink('/vsimem/sieve_7.asc')
gdal.Unlink("/vsimem/sieve_7.asc")
if cs == cs_expected \
or gdal.GetConfigOption('CPL_DEBUG', 'OFF') != 'ON':
drv.Delete('/vsimem/sieve_7.tif')
if cs == cs_expected or gdal.GetConfigOption("CPL_DEBUG", "OFF") != "ON":
drv.Delete("/vsimem/sieve_7.tif")
# Expected:
# [[0 0 0 0 0 0 0]
@ -272,17 +273,19 @@ NODATA_value 0
# [0 1 1 1 1 1 1]]
if cs != cs_expected:
print('Got: ', cs)
pytest.fail('got wrong checksum')
print("Got: ", cs)
pytest.fail("got wrong checksum")
###############################################################################
# Test propagation in our search of biggest neighbour
def test_sieve_8():
gdal.FileFromMemBuffer('/vsimem/sieve_8.asc',
"""ncols 7
gdal.FileFromMemBuffer(
"/vsimem/sieve_8.asc",
"""ncols 7
nrows 7
xllcorner 440720.000000000000
yllcorner 3750120.000000000000
@ -294,13 +297,14 @@ cellsize 60.000000000000
0 0 7 6 5 9 0
0 0 0 0 9 9 0
0 0 0 0 0 0 0
""")
""",
)
drv = gdal.GetDriverByName('GTiff')
src_ds = gdal.Open('/vsimem/sieve_8.asc')
drv = gdal.GetDriverByName("GTiff")
src_ds = gdal.Open("/vsimem/sieve_8.asc")
src_band = src_ds.GetRasterBand(1)
dst_ds = drv.Create('/vsimem/sieve_8.tif', 7, 7, 1, gdal.GDT_Byte)
dst_ds = drv.Create("/vsimem/sieve_8.tif", 7, 7, 1, gdal.GDT_Byte)
dst_band = dst_ds.GetRasterBand(1)
gdal.SieveFilter(src_band, src_band.GetMaskBand(), dst_band, 4, 4)
@ -312,15 +316,11 @@ cellsize 60.000000000000
dst_band = None
dst_ds = None
gdal.Unlink('/vsimem/sieve_8.asc')
gdal.Unlink("/vsimem/sieve_8.asc")
if cs == cs_expected \
or gdal.GetConfigOption('CPL_DEBUG', 'OFF') != 'ON':
drv.Delete('/vsimem/sieve_8.tif')
if cs == cs_expected or gdal.GetConfigOption("CPL_DEBUG", "OFF") != "ON":
drv.Delete("/vsimem/sieve_8.tif")
if cs != cs_expected:
print('Got: ', cs)
pytest.fail('got wrong checksum')
print("Got: ", cs)
pytest.fail("got wrong checksum")

View File

@ -31,11 +31,10 @@
###############################################################################
from osgeo import gdal
from osgeo import osr
import pytest
from osgeo import gdal, osr
###############################################################################
# Test a fairly default case.
@ -49,13 +48,11 @@ def test_transformgeoloc_1():
# Setup 2x2 geolocation arrays in a memory dataset with lat/long values.
drv = gdal.GetDriverByName('MEM')
geoloc_ds = drv.Create('geoloc_1', 2, 2, 3, gdal.GDT_Float64)
drv = gdal.GetDriverByName("MEM")
geoloc_ds = drv.Create("geoloc_1", 2, 2, 3, gdal.GDT_Float64)
lon_array = numpy.asarray([[-117.0, -116.0],
[-116.5, -115.5]])
lat_array = numpy.asarray([[45.0, 45.5],
[44.0, 44.5]])
lon_array = numpy.asarray([[-117.0, -116.0], [-116.5, -115.5]])
lat_array = numpy.asarray([[45.0, 45.5], [44.0, 44.5]])
geoloc_ds.GetRasterBand(1).WriteArray(lon_array)
geoloc_ds.GetRasterBand(2).WriteArray(lat_array)
@ -63,32 +60,31 @@ def test_transformgeoloc_1():
# Create a wgs84 to utm transformer.
wgs84_wkt = osr.GetUserInputAsWKT('WGS84')
utm_wkt = osr.GetUserInputAsWKT('+proj=utm +zone=11 +datum=WGS84')
wgs84_wkt = osr.GetUserInputAsWKT("WGS84")
utm_wkt = osr.GetUserInputAsWKT("+proj=utm +zone=11 +datum=WGS84")
ll_utm_transformer = gdal.Transformer(None, None,
['SRC_SRS=' + wgs84_wkt,
'DST_SRS=' + utm_wkt])
ll_utm_transformer = gdal.Transformer(
None, None, ["SRC_SRS=" + wgs84_wkt, "DST_SRS=" + utm_wkt]
)
# transform the geoloc dataset in place.
status = ll_utm_transformer.TransformGeolocations(
geoloc_ds.GetRasterBand(1),
geoloc_ds.GetRasterBand(2),
geoloc_ds.GetRasterBand(3))
geoloc_ds.GetRasterBand(3),
)
assert status == 0
expected = numpy.asarray(
[[[ 500000. , 578126.73752062],
[ 540087.07398217, 619246.88515195]],
[[4982950.40022655, 5038982.81207855],
[4871994.34702622, 4928503.38229753]],
[[ 0. , 0. ],
[ 0. , 0. ]]])
[
[[500000.0, 578126.73752062], [540087.07398217, 619246.88515195]],
[
[4982950.40022655, 5038982.81207855],
[4871994.34702622, 4928503.38229753],
],
[[0.0, 0.0], [0.0, 0.0]],
]
)
assert numpy.allclose(geoloc_ds.ReadAsArray(), expected)

File diff suppressed because it is too large Load Diff

View File

@ -17,17 +17,20 @@ sys.path.insert(1, os.path.dirname(__file__))
# These files may be non-importable, and don't contain tests anyway.
# So we skip searching them during test collection.
collect_ignore = ["kml_generate_test_files.py",
"gdrivers/netcdf_cfchecks.py",
"gdrivers/generate_bag.py",
"gdrivers/generate_fits.py"]
collect_ignore = [
"kml_generate_test_files.py",
"gdrivers/netcdf_cfchecks.py",
"gdrivers/generate_bag.py",
"gdrivers/generate_fits.py",
]
# we set ECW to not resolve projection and datum strings to get 3.x behavior.
gdal.SetConfigOption("ECW_DO_NOT_RESOLVE_DATUM_PROJECTION", "YES")
if 'APPLY_LOCALE' in os.environ:
if "APPLY_LOCALE" in os.environ:
import locale
locale.setlocale(locale.LC_ALL, '')
locale.setlocale(locale.LC_ALL, "")
@pytest.fixture(scope="module", autouse=True)
@ -62,16 +65,16 @@ def pytest_collection_modifyitems(config, items):
drivers_checked = {}
for item in items:
for mark in item.iter_markers('require_driver'):
for mark in item.iter_markers("require_driver"):
driver_name = mark.args[0]
if driver_name not in drivers_checked:
driver = gdal.GetDriverByName(driver_name)
drivers_checked[driver_name] = bool(driver)
if driver:
# Store the driver on gdaltest module so test functions can assume it's there.
setattr(gdaltest, '%s_drv' % driver_name.lower(), driver)
setattr(gdaltest, "%s_drv" % driver_name.lower(), driver)
if not drivers_checked[driver_name]:
item.add_marker(skip_driver_not_present)
if not gdal.GetConfigOption('RUN_ON_DEMAND'):
for mark in item.iter_markers('require_run_on_demand'):
if not gdal.GetConfigOption("RUN_ON_DEMAND"):
for mark in item.iter_markers("require_run_on_demand"):
item.add_marker(skip_run_on_demand_not_set)

View File

@ -25,24 +25,23 @@
# Boston, MA 02111-1307, USA.
###############################################################################
import pytest
import gdaltest
import pytest
###############################################################################
# When imported build a list of units based on the files available.
init_list = [
('byte.tif.grd', 4672),
("byte.tif.grd", 4672),
]
@pytest.mark.parametrize(
'filename,checksum',
"filename,checksum",
init_list,
ids=[tup[0].split('.')[0] for tup in init_list],
ids=[tup[0].split(".")[0] for tup in init_list],
)
@pytest.mark.require_driver('AAIGrid')
@pytest.mark.require_driver("AAIGrid")
def test_aaigrid_open(filename, checksum):
ut = gdaltest.GDALTest('AAIGrid', filename, 1, checksum)
ut = gdaltest.GDALTest("AAIGrid", filename, 1, checksum)
ut.testOpen()

View File

@ -26,27 +26,27 @@
# Boston, MA 02111-1307, USA.
###############################################################################
import pytest
import gdaltest
import pytest
###############################################################################
# When imported build a list of units based on the files available.
init_list = [
('byte.tif', 4672),
('int16.tif', 4672),
('uint16.tif', 4672),
('float32.tif', 4672),
('utmsmall.tif', 50054)]
("byte.tif", 4672),
("int16.tif", 4672),
("uint16.tif", 4672),
("float32.tif", 4672),
("utmsmall.tif", 50054),
]
@pytest.mark.parametrize(
'filename,checksum',
"filename,checksum",
init_list,
ids=[tup[0].split('.')[0] for tup in init_list],
ids=[tup[0].split(".")[0] for tup in init_list],
)
@pytest.mark.require_driver('AAIGrid')
@pytest.mark.require_driver("AAIGrid")
def test_aaigrid_create(filename, checksum):
ut = gdaltest.GDALTest('AAIGrid', filename, 1, checksum)
ut = gdaltest.GDALTest("AAIGrid", filename, 1, checksum)
ut.testCreateCopy()

View File

@ -29,7 +29,6 @@
###############################################################################
from osgeo import gdal
###############################################################################
@ -38,16 +37,23 @@ from osgeo import gdal
def test_asyncreader_1():
ds = gdal.Open('data/rgbsmall.tif')
ds = gdal.Open("data/rgbsmall.tif")
asyncreader = ds.BeginAsyncReader(0, 0, ds.RasterXSize, ds.RasterYSize)
buf = asyncreader.GetBuffer()
result = asyncreader.GetNextUpdatedRegion(0)
assert result == [gdal.GARIO_COMPLETE, 0, 0, ds.RasterXSize, ds.RasterYSize], \
'wrong return values for GetNextUpdatedRegion()'
assert result == [
gdal.GARIO_COMPLETE,
0,
0,
ds.RasterXSize,
ds.RasterYSize,
], "wrong return values for GetNextUpdatedRegion()"
ds.EndAsyncReader(asyncreader)
asyncreader = None
out_ds = gdal.GetDriverByName('GTiff').Create('/vsimem/asyncresult.tif', ds.RasterXSize, ds.RasterYSize, ds.RasterCount)
out_ds = gdal.GetDriverByName("GTiff").Create(
"/vsimem/asyncresult.tif", ds.RasterXSize, ds.RasterYSize, ds.RasterCount
)
out_ds.WriteRaster(0, 0, ds.RasterXSize, ds.RasterYSize, buf)
expected_cs = [ds.GetRasterBand(i + 1).Checksum() for i in range(ds.RasterCount)]
@ -55,12 +61,9 @@ def test_asyncreader_1():
ds = None
out_ds = None
gdal.Unlink('/vsimem/asyncresult.tif')
gdal.Unlink("/vsimem/asyncresult.tif")
for i, csum in enumerate(cs):
assert csum == expected_cs[i], ('did not get expected checksum for band %d' % (i + 1))
assert csum == expected_cs[i], "did not get expected checksum for band %d" % (
i + 1
)

View File

@ -33,38 +33,41 @@ import os
import subprocess
import sys
import gdaltest
from osgeo import gdal
import pytest
from osgeo import gdal
# Nothing exciting here. Just trying to open non existing files,
# or empty names, or files that are not valid datasets...
def matches_non_existing_error_msg(msg):
m1 = "does not exist in the file system, and is not recognized as a supported dataset name." in msg
m2 = 'No such file or directory' in msg
m3 = 'Permission denied' in msg
m1 = (
"does not exist in the file system, and is not recognized as a supported dataset name."
in msg
)
m2 = "No such file or directory" in msg
m3 = "Permission denied" in msg
return m1 or m2 or m3
def test_basic_test_1():
gdal.PushErrorHandler('CPLQuietErrorHandler')
ds = gdal.Open('non_existing_ds', gdal.GA_ReadOnly)
gdal.PushErrorHandler("CPLQuietErrorHandler")
ds = gdal.Open("non_existing_ds", gdal.GA_ReadOnly)
gdal.PopErrorHandler()
if ds is None and matches_non_existing_error_msg(gdal.GetLastErrorMsg()):
return
pytest.fail('did not get expected error message, got %s' % gdal.GetLastErrorMsg())
pytest.fail("did not get expected error message, got %s" % gdal.GetLastErrorMsg())
@pytest.mark.skipif(sys.platform != 'linux', reason='Incorrect platform')
@pytest.mark.skipif(sys.platform != "linux", reason="Incorrect platform")
def test_basic_test_strace_non_existing_file():
python_exe = sys.executable
cmd = "strace -f %s -c \"from osgeo import gdal; " % python_exe + (
"gdal.OpenEx('non_existing_ds', gdal.OF_RASTER)"
" \" ")
cmd = 'strace -f %s -c "from osgeo import gdal; ' % python_exe + (
"gdal.OpenEx('non_existing_ds', gdal.OF_RASTER)" ' " '
)
try:
(_, err) = gdaltest.runexternal_out_and_err(cmd)
except Exception:
@ -72,50 +75,51 @@ def test_basic_test_strace_non_existing_file():
pytest.skip()
interesting_lines = []
for line in err.split('\n'):
if 'non_existing_ds' in line:
interesting_lines += [ line ]
for line in err.split("\n"):
if "non_existing_ds" in line:
interesting_lines += [line]
# Only 3 calls on the file are legit: open(), stat() and readlink()
assert len(interesting_lines) <= 3, 'too many system calls accessing file'
assert len(interesting_lines) <= 3, "too many system calls accessing file"
def test_basic_test_2():
gdal.PushErrorHandler('CPLQuietErrorHandler')
ds = gdal.Open('non_existing_ds', gdal.GA_Update)
gdal.PushErrorHandler("CPLQuietErrorHandler")
ds = gdal.Open("non_existing_ds", gdal.GA_Update)
gdal.PopErrorHandler()
if ds is None and matches_non_existing_error_msg(gdal.GetLastErrorMsg()):
return
pytest.fail('did not get expected error message, got %s' % gdal.GetLastErrorMsg())
pytest.fail("did not get expected error message, got %s" % gdal.GetLastErrorMsg())
def test_basic_test_3():
gdal.PushErrorHandler('CPLQuietErrorHandler')
ds = gdal.Open('', gdal.GA_ReadOnly)
gdal.PushErrorHandler("CPLQuietErrorHandler")
ds = gdal.Open("", gdal.GA_ReadOnly)
gdal.PopErrorHandler()
if ds is None and matches_non_existing_error_msg(gdal.GetLastErrorMsg()):
return
pytest.fail('did not get expected error message, got %s' % gdal.GetLastErrorMsg())
pytest.fail("did not get expected error message, got %s" % gdal.GetLastErrorMsg())
def test_basic_test_4():
gdal.PushErrorHandler('CPLQuietErrorHandler')
ds = gdal.Open('', gdal.GA_Update)
gdal.PushErrorHandler("CPLQuietErrorHandler")
ds = gdal.Open("", gdal.GA_Update)
gdal.PopErrorHandler()
if ds is None and matches_non_existing_error_msg(gdal.GetLastErrorMsg()):
return
pytest.fail('did not get expected error message, got %s' % gdal.GetLastErrorMsg())
pytest.fail("did not get expected error message, got %s" % gdal.GetLastErrorMsg())
def test_basic_test_5():
gdal.PushErrorHandler('CPLQuietErrorHandler')
ds = gdal.Open('data/doctype.xml', gdal.GA_ReadOnly)
gdal.PushErrorHandler("CPLQuietErrorHandler")
ds = gdal.Open("data/doctype.xml", gdal.GA_ReadOnly)
gdal.PopErrorHandler()
last_error = gdal.GetLastErrorMsg()
expected = '`data/doctype.xml\' not recognized as a supported file format'
expected = "`data/doctype.xml' not recognized as a supported file format"
if ds is None and expected in last_error:
return
pytest.fail()
###############################################################################
# Issue several AllRegister() to check that GDAL drivers are good citizens
@ -125,6 +129,7 @@ def test_basic_test_6():
gdal.AllRegister()
gdal.AllRegister()
###############################################################################
# Test fix for #3077 (check that errors are cleared when using UseExceptions())
@ -132,22 +137,26 @@ def test_basic_test_6():
def basic_test_7_internal():
with pytest.raises(Exception):
gdal.Open('non_existing_ds', gdal.GA_ReadOnly)
gdal.Open("non_existing_ds", gdal.GA_ReadOnly)
# Special case: we should still be able to get the error message
# until we call a new GDAL function
assert matches_non_existing_error_msg(gdal.GetLastErrorMsg()), ('did not get expected error message, got %s' % gdal.GetLastErrorMsg())
assert matches_non_existing_error_msg(gdal.GetLastErrorMsg()), (
"did not get expected error message, got %s" % gdal.GetLastErrorMsg()
)
# Special case: we should still be able to get the error message
# until we call a new GDAL function
assert matches_non_existing_error_msg(gdal.GetLastErrorMsg()), 'did not get expected error message, got %s' % gdal.GetLastErrorMsg()
assert matches_non_existing_error_msg(gdal.GetLastErrorMsg()), (
"did not get expected error message, got %s" % gdal.GetLastErrorMsg()
)
assert gdal.GetLastErrorType() != 0, 'did not get expected error type'
assert gdal.GetLastErrorType() != 0, "did not get expected error type"
# Should issue an implicit CPLErrorReset()
gdal.GetCacheMax()
assert gdal.GetLastErrorType() == 0, 'got unexpected error type'
assert gdal.GetLastErrorType() == 0, "got unexpected error type"
def test_basic_test_7():
@ -158,38 +167,36 @@ def test_basic_test_7():
gdal.DontUseExceptions()
return ret
###############################################################################
# Test gdal.VersionInfo('RELEASE_DATE') and gdal.VersionInfo('LICENSE')
def test_basic_test_8():
ret = gdal.VersionInfo('RELEASE_DATE')
ret = gdal.VersionInfo("RELEASE_DATE")
assert len(ret) == 8
python_exe = sys.executable
if sys.platform == 'win32':
python_exe = python_exe.replace('\\', '/')
if sys.platform == "win32":
python_exe = python_exe.replace("\\", "/")
license_text = gdal.VersionInfo('LICENSE')
license_text = gdal.VersionInfo("LICENSE")
assert (
license_text.startswith('GDAL/OGR is released under the MIT license')
or 'GDAL/OGR Licensing' in license_text
license_text.startswith("GDAL/OGR is released under the MIT license")
or "GDAL/OGR Licensing" in license_text
)
# Use a subprocess to avoid the cached license text
env = os.environ.copy()
env['GDAL_DATA'] = 'tmp'
with open('tmp/LICENSE.TXT', 'wt') as f:
f.write('fake_license')
env["GDAL_DATA"] = "tmp"
with open("tmp/LICENSE.TXT", "wt") as f:
f.write("fake_license")
license_text = subprocess.check_output(
[sys.executable, 'basic_test_subprocess.py'],
env=env
).decode('utf-8')
os.unlink('tmp/LICENSE.TXT')
assert (
license_text.startswith(u'fake_license')
)
[sys.executable, "basic_test_subprocess.py"], env=env
).decode("utf-8")
os.unlink("tmp/LICENSE.TXT")
assert license_text.startswith("fake_license")
###############################################################################
@ -206,16 +213,17 @@ def test_basic_test_9():
gdaltest.eErrClass = 0
gdaltest.err_no = 0
gdaltest.msg = ''
gdaltest.msg = ""
gdal.PushErrorHandler(my_python_error_handler)
gdal.Error(1, 2, 'test')
gdal.Error(1, 2, "test")
gdal.PopErrorHandler()
assert gdaltest.eErrClass == 1
assert gdaltest.err_no == 2
assert gdaltest.msg == 'test'
assert gdaltest.msg == "test"
###############################################################################
# Test gdal.PushErrorHandler() with a Python error handler as a method (#5186)
@ -237,19 +245,20 @@ def test_basic_test_10():
# Check that reference counting works OK
gdal.PushErrorHandler(my_python_error_handler_class().handler)
gdal.Error(1, 2, 'test')
gdal.Error(1, 2, "test")
gdal.PopErrorHandler()
error_handler = my_python_error_handler_class()
gdal.PushErrorHandler(error_handler.handler)
gdal.Error(1, 2, 'test')
gdal.Error(1, 2, "test")
gdal.PopErrorHandler()
assert error_handler.eErrClass == 1
assert error_handler.err_no == 2
assert error_handler.msg == 'test'
assert error_handler.msg == "test"
###############################################################################
# Test gdal.OpenEx()
@ -257,90 +266,95 @@ def test_basic_test_10():
def test_basic_test_11():
ds = gdal.OpenEx('data/byte.tif')
ds = gdal.OpenEx("data/byte.tif")
assert ds is not None
ds = gdal.OpenEx('data/byte.tif', gdal.OF_RASTER)
ds = gdal.OpenEx("data/byte.tif", gdal.OF_RASTER)
assert ds is not None
ds = gdal.OpenEx('data/byte.tif', gdal.OF_VECTOR)
ds = gdal.OpenEx("data/byte.tif", gdal.OF_VECTOR)
assert ds is None
ds = gdal.OpenEx('data/byte.tif', gdal.OF_RASTER | gdal.OF_VECTOR)
ds = gdal.OpenEx("data/byte.tif", gdal.OF_RASTER | gdal.OF_VECTOR)
assert ds is not None
ds = gdal.OpenEx('data/byte.tif', gdal.OF_ALL)
ds = gdal.OpenEx("data/byte.tif", gdal.OF_ALL)
assert ds is not None
ds = gdal.OpenEx('data/byte.tif', gdal.OF_UPDATE)
ds = gdal.OpenEx("data/byte.tif", gdal.OF_UPDATE)
assert ds is not None
ds = gdal.OpenEx('data/byte.tif', gdal.OF_RASTER | gdal.OF_VECTOR | gdal.OF_UPDATE | gdal.OF_VERBOSE_ERROR)
ds = gdal.OpenEx(
"data/byte.tif",
gdal.OF_RASTER | gdal.OF_VECTOR | gdal.OF_UPDATE | gdal.OF_VERBOSE_ERROR,
)
assert ds is not None
ds = gdal.OpenEx('data/byte.tif', allowed_drivers=[])
ds = gdal.OpenEx("data/byte.tif", allowed_drivers=[])
assert ds is not None
ds = gdal.OpenEx('data/byte.tif', allowed_drivers=['GTiff'])
ds = gdal.OpenEx("data/byte.tif", allowed_drivers=["GTiff"])
assert ds is not None
ds = gdal.OpenEx('data/byte.tif', allowed_drivers=['PNG'])
ds = gdal.OpenEx("data/byte.tif", allowed_drivers=["PNG"])
assert ds is None
with gdaltest.error_handler():
ds = gdal.OpenEx('data/byte.tif', open_options=['FOO'])
ds = gdal.OpenEx("data/byte.tif", open_options=["FOO"])
assert ds is not None
ar_ds = [gdal.OpenEx('data/byte.tif', gdal.OF_SHARED) for _ in range(1024)]
ar_ds = [gdal.OpenEx("data/byte.tif", gdal.OF_SHARED) for _ in range(1024)]
assert ar_ds[1023] is not None
ar_ds = None
ds = gdal.OpenEx('../ogr/data/poly.shp', gdal.OF_RASTER)
ds = gdal.OpenEx("../ogr/data/poly.shp", gdal.OF_RASTER)
assert ds is None
ds = gdal.OpenEx('../ogr/data/poly.shp', gdal.OF_VECTOR)
ds = gdal.OpenEx("../ogr/data/poly.shp", gdal.OF_VECTOR)
assert ds is not None
assert ds.GetLayerCount() == 1
assert ds.GetLayer(0) is not None
ds.GetLayer(0).GetMetadata()
ds = gdal.OpenEx('../ogr/data/poly.shp', allowed_drivers=['ESRI Shapefile'])
ds = gdal.OpenEx("../ogr/data/poly.shp", allowed_drivers=["ESRI Shapefile"])
assert ds is not None
ds = gdal.OpenEx('../ogr/data/poly.shp', gdal.OF_RASTER | gdal.OF_VECTOR)
ds = gdal.OpenEx("../ogr/data/poly.shp", gdal.OF_RASTER | gdal.OF_VECTOR)
assert ds is not None
ds = gdal.OpenEx('non existing')
assert ds is None and gdal.GetLastErrorMsg() == ''
ds = gdal.OpenEx("non existing")
assert ds is None and gdal.GetLastErrorMsg() == ""
gdal.PushErrorHandler('CPLQuietErrorHandler')
ds = gdal.OpenEx('non existing', gdal.OF_VERBOSE_ERROR)
gdal.PushErrorHandler("CPLQuietErrorHandler")
ds = gdal.OpenEx("non existing", gdal.OF_VERBOSE_ERROR)
gdal.PopErrorHandler()
assert ds is None and gdal.GetLastErrorMsg() != ''
assert ds is None and gdal.GetLastErrorMsg() != ""
old_use_exceptions_status = gdal.GetUseExceptions()
gdal.UseExceptions()
got_exception = False
try:
ds = gdal.OpenEx('non existing')
ds = gdal.OpenEx("non existing")
except RuntimeError:
got_exception = True
if old_use_exceptions_status == 0:
gdal.DontUseExceptions()
assert got_exception
###############################################################################
# Test GDAL layer API
def test_basic_test_12():
ds = gdal.GetDriverByName('MEMORY').Create('bar', 0, 0, 0)
assert ds.GetDescription() == 'bar'
ds = gdal.GetDriverByName("MEMORY").Create("bar", 0, 0, 0)
assert ds.GetDescription() == "bar"
lyr = ds.CreateLayer("foo")
assert lyr is not None
assert lyr.GetDescription() == 'foo'
assert lyr.GetDescription() == "foo"
from osgeo import ogr
assert lyr.TestCapability(ogr.OLCCreateField) == 1
assert ds.GetLayerCount() == 1
lyr = ds.GetLayerByName("foo")
@ -350,23 +364,24 @@ def test_basic_test_12():
lyr = ds.GetLayer(0)
assert lyr is not None
assert not ds.IsLayerPrivate(0)
sql_lyr = ds.ExecuteSQL('SELECT * FROM foo')
sql_lyr = ds.ExecuteSQL("SELECT * FROM foo")
assert sql_lyr is not None
ds.ReleaseResultSet(sql_lyr)
new_lyr = ds.CopyLayer(lyr, 'bar')
new_lyr = ds.CopyLayer(lyr, "bar")
assert new_lyr is not None
assert ds.DeleteLayer(0) == 0
assert ds.DeleteLayer('bar') == 0
assert ds.DeleteLayer("bar") == 0
ds.SetStyleTable(ds.GetStyleTable())
ds = None
###############################################################################
# Test correct sorting of StringList / metadata (#5540, #5557)
def test_basic_test_13():
ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
for i in range(3):
if i == 0:
ds.SetMetadataItem("ScaleBounds", "True")
@ -381,18 +396,18 @@ def test_basic_test_13():
ds.SetMetadataItem("ScaleBounds", "True")
ds.SetMetadataItem("ScaleBounds.MaxScale", "2000000")
assert ds.GetMetadataItem('scalebounds') == 'True'
assert ds.GetMetadataItem('ScaleBounds') == 'True'
assert ds.GetMetadataItem('SCALEBOUNDS') == 'True'
assert ds.GetMetadataItem('ScaleBounds.MinScale') == '0'
assert ds.GetMetadataItem('ScaleBounds.MaxScale') == '2000000'
assert ds.GetMetadataItem("scalebounds") == "True"
assert ds.GetMetadataItem("ScaleBounds") == "True"
assert ds.GetMetadataItem("SCALEBOUNDS") == "True"
assert ds.GetMetadataItem("ScaleBounds.MinScale") == "0"
assert ds.GetMetadataItem("ScaleBounds.MaxScale") == "2000000"
ds = None
ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
for i in range(200):
ds.SetMetadataItem("FILENAME_%d" % i, "%d" % i)
for i in range(200):
assert ds.GetMetadataItem("FILENAME_%d" % i) == '%d' % i
assert ds.GetMetadataItem("FILENAME_%d" % i) == "%d" % i
###############################################################################
@ -401,70 +416,70 @@ def test_basic_test_13():
def test_basic_test_14():
ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
ds.SetMetadata('foo')
assert ds.GetMetadata_List() == ['foo']
ds.SetMetadata("foo")
assert ds.GetMetadata_List() == ["foo"]
with pytest.raises(Exception):
ds.SetMetadata(5)
ds.SetMetadata(['foo=bar'])
assert ds.GetMetadata_List() == ['foo=bar']
ds.SetMetadata(["foo=bar"])
assert ds.GetMetadata_List() == ["foo=bar"]
with gdaltest.error_handler():
with pytest.raises(Exception):
ds.SetMetadata([5])
ds.SetMetadata({"foo": "baz"})
assert ds.GetMetadata_List() == ["foo=baz"]
ds.SetMetadata({'foo': 'baz'})
assert ds.GetMetadata_List() == ['foo=baz']
ds.SetMetadata({'foo': b'baz'})
assert ds.GetMetadata_List() == ['foo=baz']
ds.SetMetadata({"foo": b"baz"})
assert ds.GetMetadata_List() == ["foo=baz"]
with gdaltest.error_handler():
with pytest.raises(Exception):
ds.SetMetadata({'foo': b'zero_byte_in_string\0'})
ds.SetMetadata({"foo": b"zero_byte_in_string\0"})
ds.SetMetadata({'foo': 5})
assert ds.GetMetadata_List() == ['foo=5']
ds.SetMetadata({"foo": 5})
assert ds.GetMetadata_List() == ["foo=5"]
ds.SetMetadata({5: 'baz'})
assert ds.GetMetadata_List() == ['5=baz']
ds.SetMetadata({5: "baz"})
assert ds.GetMetadata_List() == ["5=baz"]
ds.SetMetadata({5: 6})
assert ds.GetMetadata_List() == ['5=6']
assert ds.GetMetadata_List() == ["5=6"]
val = '\u00e9ven'
val = "\u00e9ven"
ds.SetMetadata({'bar': val})
assert ds.GetMetadata()['bar'] == val
ds.SetMetadata({"bar": val})
assert ds.GetMetadata()["bar"] == val
ds.SetMetadata({val: 'baz'})
assert ds.GetMetadata()[val] == 'baz'
ds.SetMetadata({val: "baz"})
assert ds.GetMetadata()[val] == "baz"
ds.SetMetadata({val: 5})
assert ds.GetMetadata_List() == ['\u00e9ven=5']
assert ds.GetMetadata_List() == ["\u00e9ven=5"]
ds.SetMetadata({5: val})
assert ds.GetMetadata_List() == ['5=\u00e9ven']
assert ds.GetMetadata_List() == ["5=\u00e9ven"]
class ClassWithoutStrRepr:
def __init__(self):
pass
def __str__(self):
raise Exception('no string representation')
raise Exception("no string representation")
with pytest.raises(Exception):
ds.SetMetadata({'a': ClassWithoutStrRepr()})
ds.SetMetadata({"a": ClassWithoutStrRepr()})
with pytest.raises(Exception):
ds.SetMetadata({ClassWithoutStrRepr(): 'a'})
ds.SetMetadata({ClassWithoutStrRepr(): "a"})
ds.SetMetadata([b"foo=\xE8\x03"])
assert ds.GetMetadata_List() == [b"foo=\xe8\x03"]
ds.SetMetadata([b'foo=\xE8\x03'])
assert ds.GetMetadata_List() == [b'foo=\xe8\x03']
###############################################################################
# Test errors with progress callback
@ -481,29 +496,37 @@ def basic_test_15_cbk_no_ret(a, b, c):
def basic_test_15_cbk_bad_ret(a, b, c):
# pylint: disable=unused-argument
return 'ok'
return "ok"
def test_basic_test_15():
mem_driver = gdal.GetDriverByName('MEM')
mem_driver = gdal.GetDriverByName("MEM")
with pytest.raises(Exception):
with gdaltest.error_handler():
gdal.GetDriverByName('MEM').CreateCopy('', gdal.GetDriverByName('MEM').Create('', 1, 1), callback='foo')
gdal.GetDriverByName("MEM").CreateCopy(
"", gdal.GetDriverByName("MEM").Create("", 1, 1), callback="foo"
)
with gdaltest.error_handler():
ds = mem_driver.CreateCopy('', mem_driver.Create('', 1, 1), callback=basic_test_15_cbk_no_argument)
ds = mem_driver.CreateCopy(
"", mem_driver.Create("", 1, 1), callback=basic_test_15_cbk_no_argument
)
assert ds is None
with gdaltest.error_handler():
ds = mem_driver.CreateCopy('', mem_driver.Create('', 1, 1), callback=basic_test_15_cbk_no_ret)
ds = mem_driver.CreateCopy(
"", mem_driver.Create("", 1, 1), callback=basic_test_15_cbk_no_ret
)
assert ds is not None
with gdaltest.error_handler():
ds = mem_driver.CreateCopy('', mem_driver.Create('', 1, 1), callback=basic_test_15_cbk_bad_ret)
ds = mem_driver.CreateCopy(
"", mem_driver.Create("", 1, 1), callback=basic_test_15_cbk_bad_ret
)
assert ds is None
###############################################################################
# Test unrecognized and recognized open options prefixed by @
@ -511,15 +534,18 @@ def test_basic_test_15():
def test_basic_test_16():
gdal.ErrorReset()
gdal.OpenEx('data/byte.tif', open_options=['@UNRECOGNIZED=FOO'])
assert gdal.GetLastErrorMsg() == ''
gdal.OpenEx("data/byte.tif", open_options=["@UNRECOGNIZED=FOO"])
assert gdal.GetLastErrorMsg() == ""
gdal.ErrorReset()
gdal.Translate('/vsimem/temp.tif', 'data/byte.tif', options='-co BLOCKYSIZE=10')
gdal.Translate("/vsimem/temp.tif", "data/byte.tif", options="-co BLOCKYSIZE=10")
with gdaltest.error_handler():
gdal.OpenEx('/vsimem/temp.tif', gdal.OF_UPDATE, open_options=['@NUM_THREADS=INVALID'])
gdal.Unlink('/vsimem/temp.tif')
assert 'Invalid value for NUM_THREADS: INVALID' in gdal.GetLastErrorMsg()
gdal.OpenEx(
"/vsimem/temp.tif", gdal.OF_UPDATE, open_options=["@NUM_THREADS=INVALID"]
)
gdal.Unlink("/vsimem/temp.tif")
assert "Invalid value for NUM_THREADS: INVALID" in gdal.GetLastErrorMsg()
###############################################################################
# Test mix of gdal/ogr.UseExceptions()/DontUseExceptions()
@ -534,11 +560,11 @@ def test_basic_test_17():
gdal.UseExceptions()
flag = False
try:
gdal.Open('do_not_exist')
gdal.Open("do_not_exist")
flag = True
except RuntimeError:
pass
assert not flag, 'expected failure'
assert not flag, "expected failure"
gdal.DontUseExceptions()
ogr.DontUseExceptions()
assert not gdal.GetUseExceptions()
@ -565,25 +591,25 @@ def test_basic_test_17_part_2():
except:
gdal.DontUseExceptions()
ogr.DontUseExceptions()
assert not flag, 'expected failure'
assert not flag, "expected failure"
assert not gdal.GetUseExceptions()
assert not ogr.GetUseExceptions()
def test_gdal_getspatialref():
ds = gdal.Open('data/byte.tif')
ds = gdal.Open("data/byte.tif")
assert ds.GetSpatialRef() is not None
ds = gdal.Open('data/minfloat.tif')
ds = gdal.Open("data/minfloat.tif")
assert ds.GetSpatialRef() is None
def test_gdal_setspatialref():
ds = gdal.Open('data/byte.tif')
ds = gdal.Open("data/byte.tif")
sr = ds.GetSpatialRef()
ds = gdal.GetDriverByName('MEM').Create('',1,1)
ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
assert ds.SetSpatialRef(sr) == gdal.CE_None
sr_got = ds.GetSpatialRef()
assert sr_got
@ -592,18 +618,18 @@ def test_gdal_setspatialref():
def test_gdal_getgcpspatialref():
ds = gdal.Open('data/byte.tif')
ds = gdal.Open("data/byte.tif")
assert ds.GetGCPSpatialRef() is None
ds = gdal.Open('data/byte_gcp.tif')
ds = gdal.Open("data/byte_gcp.tif")
assert ds.GetGCPSpatialRef() is not None
def test_gdal_setgcpspatialref():
ds = gdal.Open('data/byte.tif')
ds = gdal.Open("data/byte.tif")
sr = ds.GetSpatialRef()
ds = gdal.GetDriverByName('MEM').Create('',1,1)
ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
gcp = gdal.GCP()
gcp.GCPPixel = 0
gcp.GCPLine = 0
@ -617,111 +643,109 @@ def test_gdal_setgcpspatialref():
def test_gdal_getdatatypename():
assert gdal.GetDataTypeName(gdal.GDT_Byte) == 'Byte'
assert gdal.GetDataTypeName(gdal.GDT_Byte) == "Byte"
with pytest.raises(Exception):
gdal.GetDataTypeName(-1)
with pytest.raises(Exception):
gdal.GetDataTypeName(100)
with pytest.raises(Exception):
gdal.GetDataTypeName('invalid')
gdal.GetDataTypeName("invalid")
def test_gdal_EscapeString():
assert gdal.EscapeString( '', scheme = gdal.CPLES_XML ) == ''
assert gdal.EscapeString("", scheme=gdal.CPLES_XML) == ""
assert gdal.EscapeString( b'', scheme = gdal.CPLES_XML ) == b''
assert gdal.EscapeString(b"", scheme=gdal.CPLES_XML) == b""
assert gdal.EscapeString( '&', gdal.CPLES_XML ) == '&amp;'
assert gdal.EscapeString("&", gdal.CPLES_XML) == "&amp;"
assert gdal.EscapeString( '<', gdal.CPLES_XML ) == '&lt;'
assert gdal.EscapeString("<", gdal.CPLES_XML) == "&lt;"
assert gdal.EscapeString( '>', gdal.CPLES_XML ) == '&gt;'
assert gdal.EscapeString(">", gdal.CPLES_XML) == "&gt;"
assert gdal.EscapeString( '"', gdal.CPLES_XML ) == '&quot;'
assert gdal.EscapeString('"', gdal.CPLES_XML) == "&quot;"
assert gdal.EscapeString( b'\xEF\xBB\xBF', gdal.CPLES_XML ) == b'&#xFEFF;'
assert gdal.EscapeString(b"\xEF\xBB\xBF", gdal.CPLES_XML) == b"&#xFEFF;"
assert gdal.EscapeString( '\t', gdal.CPLES_XML ) == '\t'
assert gdal.EscapeString("\t", gdal.CPLES_XML) == "\t"
assert gdal.EscapeString( '\n', gdal.CPLES_XML ) == '\n'
assert gdal.EscapeString("\n", gdal.CPLES_XML) == "\n"
assert gdal.EscapeString( b'\x01a', gdal.CPLES_XML ) == b'a'
assert gdal.EscapeString(b"\x01a", gdal.CPLES_XML) == b"a"
assert gdal.EscapeString("", gdal.CPLES_XML_BUT_QUOTES) == ""
assert gdal.EscapeString( '', gdal.CPLES_XML_BUT_QUOTES ) == ''
assert gdal.EscapeString("&", gdal.CPLES_XML_BUT_QUOTES) == "&amp;"
assert gdal.EscapeString( '&', gdal.CPLES_XML_BUT_QUOTES ) == '&amp;'
assert gdal.EscapeString("<", gdal.CPLES_XML_BUT_QUOTES) == "&lt;"
assert gdal.EscapeString( '<', gdal.CPLES_XML_BUT_QUOTES ) == '&lt;'
assert gdal.EscapeString(">", gdal.CPLES_XML_BUT_QUOTES) == "&gt;"
assert gdal.EscapeString( '>', gdal.CPLES_XML_BUT_QUOTES ) == '&gt;'
assert gdal.EscapeString('"', gdal.CPLES_XML_BUT_QUOTES) == '"'
assert gdal.EscapeString( '"', gdal.CPLES_XML_BUT_QUOTES ) == '"'
assert gdal.EscapeString(b"\xEF\xBB\xBF", gdal.CPLES_XML_BUT_QUOTES) == b"&#xFEFF;"
assert gdal.EscapeString( b'\xEF\xBB\xBF', gdal.CPLES_XML_BUT_QUOTES ) == b'&#xFEFF;'
assert gdal.EscapeString("\t", gdal.CPLES_XML_BUT_QUOTES) == "\t"
assert gdal.EscapeString( '\t', gdal.CPLES_XML_BUT_QUOTES ) == '\t'
assert gdal.EscapeString("\n", gdal.CPLES_XML_BUT_QUOTES) == "\n"
assert gdal.EscapeString( '\n', gdal.CPLES_XML_BUT_QUOTES ) == '\n'
assert gdal.EscapeString(b"\x01a", gdal.CPLES_XML_BUT_QUOTES) == b"a"
assert gdal.EscapeString( b'\x01a', gdal.CPLES_XML_BUT_QUOTES ) == b'a'
assert gdal.EscapeString("", gdal.CPLES_BackslashQuotable) == ""
assert gdal.EscapeString("a", gdal.CPLES_BackslashQuotable) == "a"
assert gdal.EscapeString( '', gdal.CPLES_BackslashQuotable ) == ''
assert gdal.EscapeString(b"\x00x", gdal.CPLES_BackslashQuotable) == b"\\0x"
assert gdal.EscapeString( 'a', gdal.CPLES_BackslashQuotable ) == 'a'
assert gdal.EscapeString(b"\x01", gdal.CPLES_BackslashQuotable) == b"\x01"
assert gdal.EscapeString( b'\x00x', gdal.CPLES_BackslashQuotable ) == b'\\0x'
assert gdal.EscapeString("\\", gdal.CPLES_BackslashQuotable) == "\\\\"
assert gdal.EscapeString( b'\x01', gdal.CPLES_BackslashQuotable ) == b'\x01'
assert gdal.EscapeString("\n", gdal.CPLES_BackslashQuotable) == "\\n"
assert gdal.EscapeString( '\\', gdal.CPLES_BackslashQuotable ) == '\\\\'
assert gdal.EscapeString('"', gdal.CPLES_BackslashQuotable) == '\\"'
assert gdal.EscapeString( '\n', gdal.CPLES_BackslashQuotable ) == '\\n'
assert gdal.EscapeString("", gdal.CPLES_URL) == ""
assert gdal.EscapeString( '"', gdal.CPLES_BackslashQuotable ) == '\\"'
assert (
gdal.EscapeString("aZAZ09$-_.+!*'(), ", gdal.CPLES_URL)
== "aZAZ09$-_.+!*'(),%20"
)
assert gdal.EscapeString("", gdal.CPLES_SQL) == ""
assert gdal.EscapeString( '', gdal.CPLES_URL ) == ''
assert gdal.EscapeString("a", gdal.CPLES_SQL) == "a"
assert gdal.EscapeString( 'aZAZ09$-_.+!*\'(), ', gdal.CPLES_URL ) == 'aZAZ09$-_.+!*\'(),%20'
assert gdal.EscapeString("a'a", gdal.CPLES_SQL) == "a''a"
assert gdal.EscapeString("", gdal.CPLES_CSV) == ""
assert gdal.EscapeString( "", gdal.CPLES_SQL ) == ""
assert gdal.EscapeString("a'b", gdal.CPLES_CSV) == "a'b"
assert gdal.EscapeString( "a", gdal.CPLES_SQL ) == "a"
assert gdal.EscapeString('a"b', gdal.CPLES_CSV) == '"a""b"'
assert gdal.EscapeString( "a'a", gdal.CPLES_SQL ) == "a''a"
assert gdal.EscapeString("a,b", gdal.CPLES_CSV) == '"a,b"'
assert gdal.EscapeString("a,b", gdal.CPLES_CSV) == '"a,b"'
assert gdal.EscapeString( "", gdal.CPLES_CSV ) == ""
assert gdal.EscapeString("a\tb", gdal.CPLES_CSV) == '"a\tb"'
assert gdal.EscapeString( "a'b", gdal.CPLES_CSV ) == "a'b"
assert gdal.EscapeString("a\nb", gdal.CPLES_CSV) == '"a\nb"'
assert gdal.EscapeString( 'a"b', gdal.CPLES_CSV ) == '"a""b"'
assert gdal.EscapeString( 'a,b', gdal.CPLES_CSV ) == '"a,b"'
assert gdal.EscapeString( 'a,b', gdal.CPLES_CSV ) == '"a,b"'
assert gdal.EscapeString( 'a\tb', gdal.CPLES_CSV ) == '"a\tb"'
assert gdal.EscapeString( 'a\nb', gdal.CPLES_CSV ) == '"a\nb"'
assert gdal.EscapeString( 'a\rb', gdal.CPLES_CSV ) == '"a\rb"'
assert gdal.EscapeString("a\rb", gdal.CPLES_CSV) == '"a\rb"'
def test_gdal_EscapeString_errors():
if sys.maxsize > 2**32:
pytest.skip('Test not available on 64 bit')
pytest.skip("Test not available on 64 bit")
# Allocation will be < 4 GB, but will fail being > 2 GB
assert gdal.EscapeString( b'"' * (((1 << 32)-1) // 6), gdal.CPLES_XML ) is None
assert gdal.EscapeString(b'"' * (((1 << 32) - 1) // 6), gdal.CPLES_XML) is None
# Allocation will be > 4 GB
assert gdal.EscapeString( b'"' * (((1 << 32)-1) // 6 + 1), gdal.CPLES_XML ) is None
assert gdal.EscapeString(b'"' * (((1 << 32) - 1) // 6 + 1), gdal.CPLES_XML) is None
def test_gdal_DataTypeUnion():

View File

@ -31,7 +31,6 @@
from osgeo import gdal
if __name__ == '__main__':
if __name__ == "__main__":
# test_basic_test_8
print(gdal.VersionInfo('LICENSE'))
print(gdal.VersionInfo("LICENSE"))

View File

@ -27,49 +27,53 @@
###############################################################################
import gdaltest
import pytest
import gdaltest
init_list = [
('1bit.bmp', 200),
('4bit_pal.bmp', 2587),
('8bit_pal.bmp', 4672),
('byte_rle8.bmp', 4672)]
("1bit.bmp", 200),
("4bit_pal.bmp", 2587),
("8bit_pal.bmp", 4672),
("byte_rle8.bmp", 4672),
]
@pytest.mark.parametrize(
'filename,checksum',
"filename,checksum",
init_list,
ids=[tup[0].split('.')[0] for tup in init_list],
ids=[tup[0].split(".")[0] for tup in init_list],
)
@pytest.mark.require_driver('BMP')
@pytest.mark.require_driver("BMP")
def test_bmp_open(filename, checksum):
ut = gdaltest.GDALTest('BMP', filename, 1, checksum)
ut = gdaltest.GDALTest("BMP", filename, 1, checksum)
ut.testOpen()
def test_bmp_online_1():
if not gdaltest.download_file('http://download.osgeo.org/gdal/data/bmp/8bit_pal_rle.bmp', '8bit_pal_rle.bmp'):
if not gdaltest.download_file(
"http://download.osgeo.org/gdal/data/bmp/8bit_pal_rle.bmp", "8bit_pal_rle.bmp"
):
pytest.skip()
tst = gdaltest.GDALTest('BMP', 'tmp/cache/8bit_pal_rle.bmp', 1, 17270, filename_absolute=1)
tst = gdaltest.GDALTest(
"BMP", "tmp/cache/8bit_pal_rle.bmp", 1, 17270, filename_absolute=1
)
return tst.testOpen()
def test_bmp_online_2():
if not gdaltest.download_file('http://download.osgeo.org/gdal/data/bmp/24bit.bmp', '24bit.bmp'):
if not gdaltest.download_file(
"http://download.osgeo.org/gdal/data/bmp/24bit.bmp", "24bit.bmp"
):
pytest.skip()
tst = gdaltest.GDALTest('BMP', 'tmp/cache/24bit.bmp', 1, 7158, filename_absolute=1)
if tst == 'success':
tst = gdaltest.GDALTest('BMP', 'tmp/cache/24bit.bmp', 3, 27670, filename_absolute=1)
tst = gdaltest.GDALTest("BMP", "tmp/cache/24bit.bmp", 1, 7158, filename_absolute=1)
if tst == "success":
tst = gdaltest.GDALTest(
"BMP", "tmp/cache/24bit.bmp", 3, 27670, filename_absolute=1
)
return tst.testOpen()

View File

@ -28,9 +28,8 @@
###############################################################################
import pytest
import gdaltest
import pytest
###############################################################################
# Test creating an in memory copy.
@ -38,7 +37,7 @@ import gdaltest
def test_bmp_vsimem():
tst = gdaltest.GDALTest('BMP', 'byte.tif', 1, 4672)
tst = gdaltest.GDALTest("BMP", "byte.tif", 1, 4672)
return tst.testCreateCopy(vsimem=1)
@ -47,27 +46,26 @@ def test_bmp_vsimem():
# When imported build a list of units based on the files available.
init_list = [
('byte.tif', 4672),
('utmsmall.tif', 50054),
('8bit_pal.bmp', 4672), ]
("byte.tif", 4672),
("utmsmall.tif", 50054),
("8bit_pal.bmp", 4672),
]
@pytest.mark.parametrize(
'filename,checksum',
"filename,checksum",
init_list,
ids=[tup[0].split('.')[0] for tup in init_list],
ids=[tup[0].split(".")[0] for tup in init_list],
)
@pytest.mark.parametrize(
'testfunction', [
'testCreateCopy',
'testCreate',
]
"testfunction",
[
"testCreateCopy",
"testCreate",
],
)
@pytest.mark.require_driver('BMP')
@pytest.mark.require_driver("BMP")
def test_bmp_create(filename, checksum, testfunction):
ut = gdaltest.GDALTest('BMP', filename, 1, checksum)
ut = gdaltest.GDALTest("BMP", filename, 1, checksum)
getattr(ut, testfunction)()

File diff suppressed because it is too large Load Diff

View File

@ -31,11 +31,11 @@
###############################################################################
import gdaltest
from osgeo import gdal
import pytest
from osgeo import gdal
###############################################################################
# Create a color table.
@ -48,7 +48,7 @@ def test_colortable_1():
for i in range(len(gdaltest.test_ct_data)):
gdaltest.test_ct.SetColorEntry(i, gdaltest.test_ct_data[i])
###############################################################################
# verify contents.
@ -65,9 +65,9 @@ def test_colortable_2():
else:
o_v = o_data[j]
assert g_data[j] == o_v, 'color table mismatch'
assert g_data[j] == o_v, "color table mismatch"
###############################################################################
# Test CreateColorRamp()
@ -86,12 +86,10 @@ def test_colortable_3():
assert ct.GetColorEntry(255) == (0, 0, 255, 255)
###############################################################################
# Cleanup.
def test_colortable_cleanup():
gdaltest.test_ct = None

View File

@ -28,10 +28,10 @@
###############################################################################
import gdaltest
import pytest
from osgeo import gdal
import gdaltest
###############################################################################
# Test GDAL_READDIR_LIMIT_ON_OPEN
@ -39,41 +39,39 @@ import gdaltest
def test_envi_1():
gdal.SetConfigOption('GDAL_READDIR_LIMIT_ON_OPEN', '1')
gdal.SetConfigOption("GDAL_READDIR_LIMIT_ON_OPEN", "1")
ds = gdal.Open('data/utmsmall.raw')
ds = gdal.Open("data/utmsmall.raw")
filelist = ds.GetFileList()
gdal.SetConfigOption('GDAL_READDIR_LIMIT_ON_OPEN', None)
gdal.SetConfigOption("GDAL_READDIR_LIMIT_ON_OPEN", None)
assert len(filelist) == 2, "did not get expected file list."
assert len(filelist) == 2, 'did not get expected file list.'
###############################################################################
# When imported build a list of units based on the files available.
init_list = [
('byte.raw', 4672),
('int16.raw', 4672),
('uint16.raw', 4672),
('int32.raw', 4672),
('uint32.raw', 4672),
('float32.raw', 4672),
('float64.raw', 4672),
("byte.raw", 4672),
("int16.raw", 4672),
("uint16.raw", 4672),
("int32.raw", 4672),
("uint32.raw", 4672),
("float32.raw", 4672),
("float64.raw", 4672),
# ('cfloat32.raw', 5028),
# ('cfloat64.raw', 5028),
]
@pytest.mark.parametrize(
'filename,checksum',
"filename,checksum",
init_list,
ids=[tup[0].split('.')[0] for tup in init_list],
ids=[tup[0].split(".")[0] for tup in init_list],
)
@pytest.mark.require_driver('ENVI')
@pytest.mark.require_driver("ENVI")
def test_envi_open(filename, checksum):
ut = gdaltest.GDALTest('ENVI', filename, 1, checksum)
ut = gdaltest.GDALTest("ENVI", filename, 1, checksum)
ut.testOpen()

View File

@ -29,11 +29,10 @@
# DEALINGS IN THE SOFTWARE.
###############################################################################
from osgeo import gdal
import gdaltest
from osgeo import gdal
###############################################################################
# Helper to make gcps
@ -53,15 +52,22 @@ def _list2gcps(src_list):
###############################################################################
# Test simple exact case of turning GCPs into a GeoTransform.
def test_gcps2gt_1():
gt = gdal.GCPsToGeoTransform(_list2gcps([
(0.0, 0.0, 400000, 370000),
(100.0, 0.0, 410000, 370000),
(100.0, 200.0, 410000, 368000)
]))
gt = gdal.GCPsToGeoTransform(
_list2gcps(
[
(0.0, 0.0, 400000, 370000),
(100.0, 0.0, 410000, 370000),
(100.0, 200.0, 410000, 368000),
]
)
)
assert gdaltest.geotransform_equals(
gt, (400000.0, 100.0, 0.0, 370000.0, 0.0, -10.0), 0.000001)
gt, (400000.0, 100.0, 0.0, 370000.0, 0.0, -10.0), 0.000001
)
###############################################################################
# Similar but non-exact.
@ -69,14 +75,20 @@ def test_gcps2gt_1():
def test_gcps2gt_2():
gt = gdal.GCPsToGeoTransform(_list2gcps([
(0.0, 0.0, 400000, 370000),
(100.0, 0.0, 410000, 370000),
(100.0, 200.0, 410000, 368000),
(0.0, 200.0, 400000, 368000.01)
]))
gt = gdal.GCPsToGeoTransform(
_list2gcps(
[
(0.0, 0.0, 400000, 370000),
(100.0, 0.0, 410000, 370000),
(100.0, 200.0, 410000, 368000),
(0.0, 200.0, 400000, 368000.01),
]
)
)
assert gdaltest.geotransform_equals(
gt, (400000.0, 100.0, 0.0, 370000.0025, -5e-05, -9.999975), 0.000001)
gt, (400000.0, 100.0, 0.0, 370000.0025, -5e-05, -9.999975), 0.000001
)
###############################################################################
# bApproxOK false, and no good solution.
@ -85,13 +97,19 @@ def test_gcps2gt_2():
def test_gcps2gt_3():
approx_ok = 0
gt = gdal.GCPsToGeoTransform(_list2gcps([
(0.0, 0.0, 400000, 370000),
(100.0, 0.0, 410000, 370000),
(100.0, 200.0, 410000, 368000),
(0.0, 200.0, 400000, 360000)
]), approx_ok)
assert gt is None, 'Expected failure when no good solution.'
gt = gdal.GCPsToGeoTransform(
_list2gcps(
[
(0.0, 0.0, 400000, 370000),
(100.0, 0.0, 410000, 370000),
(100.0, 200.0, 410000, 368000),
(0.0, 200.0, 400000, 360000),
]
),
approx_ok,
)
assert gt is None, "Expected failure when no good solution."
###############################################################################
# Single point - Should return None.
@ -99,10 +117,15 @@ def test_gcps2gt_3():
def test_gcps2gt_4():
gt = gdal.GCPsToGeoTransform(_list2gcps([
(0.0, 0.0, 400000, 370000),
]))
assert gt is None, 'Expected failure for single GCP.'
gt = gdal.GCPsToGeoTransform(
_list2gcps(
[
(0.0, 0.0, 400000, 370000),
]
)
)
assert gt is None, "Expected failure for single GCP."
###############################################################################
# Two points - simple offset and scale, no rotation.
@ -110,12 +133,18 @@ def test_gcps2gt_4():
def test_gcps2gt_5():
gt = gdal.GCPsToGeoTransform(_list2gcps([
(0.0, 0.0, 400000, 370000),
(100.0, 200.0, 410000, 368000),
]))
gt = gdal.GCPsToGeoTransform(
_list2gcps(
[
(0.0, 0.0, 400000, 370000),
(100.0, 200.0, 410000, 368000),
]
)
)
assert gdaltest.geotransform_equals(
gt, (400000.0, 100.0, 0.0, 370000.0, 0.0, -10.0), 0.000001)
gt, (400000.0, 100.0, 0.0, 370000.0, 0.0, -10.0), 0.000001
)
###############################################################################
# Special case for four points in a particular order. Exact result.
@ -123,14 +152,18 @@ def test_gcps2gt_5():
def test_gcps2gt_6():
gt = gdal.GCPsToGeoTransform(_list2gcps([
(400000, 370000, 400000, 370000),
(410000, 370000, 410000, 370000),
(410000, 368000, 410000, 368000),
(400000, 368000, 400000, 368000),
]))
assert gdaltest.geotransform_equals(
gt, (0.0, 1.0, 0.0, 0.0, 0.0, 1.0), 0.000001)
gt = gdal.GCPsToGeoTransform(
_list2gcps(
[
(400000, 370000, 400000, 370000),
(410000, 370000, 410000, 370000),
(410000, 368000, 410000, 368000),
(400000, 368000, 400000, 368000),
]
)
)
assert gdaltest.geotransform_equals(gt, (0.0, 1.0, 0.0, 0.0, 0.0, 1.0), 0.000001)
###############################################################################
# Try a case that is hard to do without normalization.
@ -138,14 +171,18 @@ def test_gcps2gt_6():
def test_gcps2gt_7():
gt = gdal.GCPsToGeoTransform(_list2gcps([
(400000, 370000, 400000, 370000),
(410000, 368000, 410000, 368000),
(410000, 370000, 410000, 370000),
(400000, 368000, 400000, 368000),
]))
assert gdaltest.geotransform_equals(
gt, (0.0, 1.0, 0.0, 0.0, 0.0, 1.0), 0.000001)
gt = gdal.GCPsToGeoTransform(
_list2gcps(
[
(400000, 370000, 400000, 370000),
(410000, 368000, 410000, 368000),
(410000, 370000, 410000, 370000),
(400000, 368000, 400000, 368000),
]
)
)
assert gdaltest.geotransform_equals(gt, (0.0, 1.0, 0.0, 0.0, 0.0, 1.0), 0.000001)
###############################################################################
# A fairly messy real world case without a easy to predict result.
@ -153,17 +190,24 @@ def test_gcps2gt_7():
def test_gcps2gt_8():
gt = gdal.GCPsToGeoTransform(_list2gcps([
(0.01, 0.04, -87.05528672907, 39.22759504228),
(0.01, 2688.02, -86.97079900719, 39.27075713986),
(4031.99, 2688.04, -87.05960736744, 39.37569137000),
(1988.16, 1540.80, -87.055069186699924, 39.304963106777514),
(1477.41, 2400.83, -87.013419295885001, 39.304705030894979),
(1466.02, 2376.92, -87.013906298363295, 39.304056190007913),
]))
gt_expected = (-87.056612873288, -2.232795668658e-05, 3.178617809303e-05,
39.227856615716, 2.6091510188921e-05, 1.596921026218e-05)
gt = gdal.GCPsToGeoTransform(
_list2gcps(
[
(0.01, 0.04, -87.05528672907, 39.22759504228),
(0.01, 2688.02, -86.97079900719, 39.27075713986),
(4031.99, 2688.04, -87.05960736744, 39.37569137000),
(1988.16, 1540.80, -87.055069186699924, 39.304963106777514),
(1477.41, 2400.83, -87.013419295885001, 39.304705030894979),
(1466.02, 2376.92, -87.013906298363295, 39.304056190007913),
]
)
)
gt_expected = (
-87.056612873288,
-2.232795668658e-05,
3.178617809303e-05,
39.227856615716,
2.6091510188921e-05,
1.596921026218e-05,
)
assert gdaltest.geotransform_equals(gt, gt_expected, 0.00001)

File diff suppressed because it is too large Load Diff

View File

@ -31,18 +31,19 @@
import array
import random
from osgeo import gdal, osr
import gdaltest
import pytest
from osgeo import gdal, osr
###############################################################################
# Verify warped result.
def test_geoloc_1():
tst = gdaltest.GDALTest('VRT', 'warpsst.vrt', 1, 63034)
tst = gdaltest.GDALTest("VRT", "warpsst.vrt", 1, 63034)
return tst.testOpen(check_filelist=False)
@ -50,68 +51,90 @@ def test_geoloc_1():
# Test that we take into account the min/max of the geoloc arrays
@pytest.mark.parametrize("use_temp_datasets", ['YES', 'NO'])
@pytest.mark.parametrize("use_temp_datasets", ["YES", "NO"])
def test_geoloc_bounds(use_temp_datasets):
lon_ds = gdal.GetDriverByName('GTiff').Create('/vsimem/lon.tif', 360, 1, 1, gdal.GDT_Float32)
lon_ds.WriteRaster(0, 0, 360, 1, array.array('f', [91 + 0.5 * x for x in range(178)] + [-179.9 + 0.5 * x for x in range(182)]))
lon_ds = gdal.GetDriverByName("GTiff").Create(
"/vsimem/lon.tif", 360, 1, 1, gdal.GDT_Float32
)
lon_ds.WriteRaster(
0,
0,
360,
1,
array.array(
"f",
[91 + 0.5 * x for x in range(178)] + [-179.9 + 0.5 * x for x in range(182)],
),
)
lon_ds = None
lat_ds = gdal.GetDriverByName('GTiff').Create('/vsimem/lat.tif', 80, 1, 1, gdal.GDT_Float32)
lat_ds.WriteRaster(0, 0, 80, 1, array.array('f', [60.4 + 0.5 * x for x in range(60)] + [89 - 0.5 * x for x in range(20)]))
lat_ds = gdal.GetDriverByName("GTiff").Create(
"/vsimem/lat.tif", 80, 1, 1, gdal.GDT_Float32
)
lat_ds.WriteRaster(
0,
0,
80,
1,
array.array(
"f", [60.4 + 0.5 * x for x in range(60)] + [89 - 0.5 * x for x in range(20)]
),
)
lat_ds = None
ds = gdal.GetDriverByName('MEM').Create('', 360, 80)
ds = gdal.GetDriverByName("MEM").Create("", 360, 80)
md = {
'LINE_OFFSET': '0',
'LINE_STEP': '1',
'PIXEL_OFFSET': '0',
'PIXEL_STEP': '1',
'X_DATASET': '/vsimem/lon.tif',
'X_BAND' : '1',
'Y_DATASET': '/vsimem/lat.tif',
'Y_BAND' : '1'
"LINE_OFFSET": "0",
"LINE_STEP": "1",
"PIXEL_OFFSET": "0",
"PIXEL_STEP": "1",
"X_DATASET": "/vsimem/lon.tif",
"X_BAND": "1",
"Y_DATASET": "/vsimem/lat.tif",
"Y_BAND": "1",
}
ds.SetMetadata(md, 'GEOLOCATION')
with gdaltest.config_option('GDAL_GEOLOC_USE_TEMP_DATASETS', use_temp_datasets):
warped_ds = gdal.Warp('', ds, format='MEM')
ds.SetMetadata(md, "GEOLOCATION")
with gdaltest.config_option("GDAL_GEOLOC_USE_TEMP_DATASETS", use_temp_datasets):
warped_ds = gdal.Warp("", ds, format="MEM")
assert warped_ds
gdal.Unlink('/vsimem/lon.tif')
gdal.Unlink('/vsimem/lat.tif')
gdal.Unlink("/vsimem/lon.tif")
gdal.Unlink("/vsimem/lat.tif")
gt = warped_ds.GetGeoTransform()
assert gt[0] == pytest.approx(-179.9)
assert gt[3] == pytest.approx(60.4 + 0.5 * 59)
###############################################################################
# Test that the line filling logic works
@pytest.mark.parametrize("use_temp_datasets", ['YES', 'NO'])
@pytest.mark.parametrize("use_temp_datasets", ["YES", "NO"])
def test_geoloc_fill_line(use_temp_datasets):
ds = gdal.GetDriverByName('MEM').Create('', 200, 372)
ds = gdal.GetDriverByName("MEM").Create("", 200, 372)
md = {
'LINE_OFFSET': '0',
'LINE_STEP': '1',
'PIXEL_OFFSET': '0',
'PIXEL_STEP': '1',
'X_DATASET': '../alg/data/geoloc/longitude_including_pole.tif',
'X_BAND' : '1',
'Y_DATASET': '../alg/data/geoloc/latitude_including_pole.tif',
'Y_BAND' : '1',
'SRS': 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]'
"LINE_OFFSET": "0",
"LINE_STEP": "1",
"PIXEL_OFFSET": "0",
"PIXEL_STEP": "1",
"X_DATASET": "../alg/data/geoloc/longitude_including_pole.tif",
"X_BAND": "1",
"Y_DATASET": "../alg/data/geoloc/latitude_including_pole.tif",
"Y_BAND": "1",
"SRS": 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]',
}
ds.SetMetadata(md, 'GEOLOCATION')
ds.SetMetadata(md, "GEOLOCATION")
ds.GetRasterBand(1).Fill(1)
with gdaltest.config_option('GDAL_GEOLOC_USE_TEMP_DATASETS', use_temp_datasets):
warped_ds = gdal.Warp('', ds, format='MEM')
with gdaltest.config_option("GDAL_GEOLOC_USE_TEMP_DATASETS", use_temp_datasets):
warped_ds = gdal.Warp("", ds, format="MEM")
assert warped_ds
assert warped_ds.GetRasterBand(1).Checksum() in (22339,
22336) # 22336 with Intel(R) oneAPI DPC++/C++ Compiler 2022.1.0
assert warped_ds.GetRasterBand(1).Checksum() in (
22339,
22336,
) # 22336 with Intel(R) oneAPI DPC++/C++ Compiler 2022.1.0
###############################################################################
@ -120,30 +143,36 @@ def test_geoloc_fill_line(use_temp_datasets):
def test_geoloc_warp_to_geoloc():
lon_ds = gdal.GetDriverByName('GTiff').Create('/vsimem/lon.tif', 10, 1, 1, gdal.GDT_Float32)
lon_ds.WriteRaster(0, 0, 10, 1, array.array('f', [-79.5 + 1 * x for x in range(10)]))
lon_ds = gdal.GetDriverByName("GTiff").Create(
"/vsimem/lon.tif", 10, 1, 1, gdal.GDT_Float32
)
lon_ds.WriteRaster(
0, 0, 10, 1, array.array("f", [-79.5 + 1 * x for x in range(10)])
)
lon_ds = None
lat_ds = gdal.GetDriverByName('GTiff').Create('/vsimem/lat.tif', 10, 1, 1, gdal.GDT_Float32)
lat_ds.WriteRaster(0, 0, 10, 1, array.array('f', [49.5 - 1 * x for x in range(10)]))
lat_ds = gdal.GetDriverByName("GTiff").Create(
"/vsimem/lat.tif", 10, 1, 1, gdal.GDT_Float32
)
lat_ds.WriteRaster(0, 0, 10, 1, array.array("f", [49.5 - 1 * x for x in range(10)]))
lat_ds = None
ds = gdal.GetDriverByName('MEM').Create('', 10, 10)
ds = gdal.GetDriverByName("MEM").Create("", 10, 10)
md = {
'LINE_OFFSET': '0',
'LINE_STEP': '1',
'PIXEL_OFFSET': '0',
'PIXEL_STEP': '1',
'X_DATASET': '/vsimem/lon.tif',
'X_BAND' : '1',
'Y_DATASET': '/vsimem/lat.tif',
'Y_BAND' : '1',
'SRS': 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]',
'GEOREFERENCING_CONVENTION': 'PIXEL_CENTER'
"LINE_OFFSET": "0",
"LINE_STEP": "1",
"PIXEL_OFFSET": "0",
"PIXEL_STEP": "1",
"X_DATASET": "/vsimem/lon.tif",
"X_BAND": "1",
"Y_DATASET": "/vsimem/lat.tif",
"Y_BAND": "1",
"SRS": 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]',
"GEOREFERENCING_CONVENTION": "PIXEL_CENTER",
}
ds.SetMetadata(md, 'GEOLOCATION')
ds.SetMetadata(md, "GEOLOCATION")
input_ds = gdal.GetDriverByName('MEM').Create('', 10, 10)
input_ds = gdal.GetDriverByName("MEM").Create("", 10, 10)
input_ds.SetGeoTransform([-80, 1, 0, 50, 0, -1])
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
@ -163,8 +192,17 @@ def test_geoloc_warp_to_geoloc():
assert ds.GetRasterBand(1).ComputeRasterMinMax() == (255, 255), ds.ReadAsArray()
# Try with projected coordinates
input_ds = gdal.GetDriverByName('MEM').Create('', 10, 10)
input_ds.SetGeoTransform([-8905559.26346189, (-7792364.35552915 - -8905559.26346189)/10, 0, 6446275.84101716, 0, -(6446275.84101716-4865942.27950318)/10])
input_ds = gdal.GetDriverByName("MEM").Create("", 10, 10)
input_ds.SetGeoTransform(
[
-8905559.26346189,
(-7792364.35552915 - -8905559.26346189) / 10,
0,
6446275.84101716,
0,
-(6446275.84101716 - 4865942.27950318) / 10,
]
)
srs = osr.SpatialReference()
srs.ImportFromEPSG(3857)
srs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)
@ -182,10 +220,8 @@ def test_geoloc_warp_to_geoloc():
assert ds.GetRasterBand(1).ComputeRasterMinMax() == (255, 255), ds.ReadAsArray()
gdal.Unlink('/vsimem/lon.tif')
gdal.Unlink('/vsimem/lat.tif')
gdal.Unlink("/vsimem/lon.tif")
gdal.Unlink("/vsimem/lat.tif")
###############################################################################
@ -194,16 +230,22 @@ def test_geoloc_warp_to_geoloc():
def test_geoloc_error_cases():
lon_ds = gdal.GetDriverByName('GTiff').Create('/vsimem/lon.tif', 10, 1, 1, gdal.GDT_Float32)
lon_ds.WriteRaster(0, 0, 10, 1, array.array('f', [-179.5 + 1 * x for x in range(10)]))
lon_ds = gdal.GetDriverByName("GTiff").Create(
"/vsimem/lon.tif", 10, 1, 1, gdal.GDT_Float32
)
lon_ds.WriteRaster(
0, 0, 10, 1, array.array("f", [-179.5 + 1 * x for x in range(10)])
)
lon_ds = None
lat_ds = gdal.GetDriverByName('GTiff').Create('/vsimem/lat.tif', 10, 1, 1, gdal.GDT_Float32)
lat_ds.WriteRaster(0, 0, 10, 1, array.array('f', [89.5 - 1 * x for x in range(10)]))
lat_ds = gdal.GetDriverByName("GTiff").Create(
"/vsimem/lat.tif", 10, 1, 1, gdal.GDT_Float32
)
lat_ds.WriteRaster(0, 0, 10, 1, array.array("f", [89.5 - 1 * x for x in range(10)]))
lat_ds = None
ds = gdal.GetDriverByName('MEM').Create('', 10, 10)
ds.SetMetadata({'invalid': 'content'}, 'GEOLOCATION')
ds = gdal.GetDriverByName("MEM").Create("", 10, 10)
ds.SetMetadata({"invalid": "content"}, "GEOLOCATION")
with gdaltest.error_handler():
transformer = gdal.Transformer(ds, None, [])
@ -219,38 +261,42 @@ def test_geoloc_error_cases():
@pytest.mark.parametrize("step", [1, 2])
@pytest.mark.parametrize("convention", ['TOP_LEFT_CORNER', 'PIXEL_CENTER'])
@pytest.mark.parametrize("inverse_method", ['BACKMAP', 'QUADTREE'])
@pytest.mark.parametrize("convention", ["TOP_LEFT_CORNER", "PIXEL_CENTER"])
@pytest.mark.parametrize("inverse_method", ["BACKMAP", "QUADTREE"])
def test_geoloc_affine_transformation(step, convention, inverse_method):
shift = 0.5 if convention == 'PIXEL_CENTER' else 0
lon_ds = gdal.GetDriverByName('GTiff').Create('/vsimem/lon.tif', 20 // step, 1, 1, gdal.GDT_Float32)
vals = array.array('f', [-80 + step * (x + shift) for x in range(20 // step)])
shift = 0.5 if convention == "PIXEL_CENTER" else 0
lon_ds = gdal.GetDriverByName("GTiff").Create(
"/vsimem/lon.tif", 20 // step, 1, 1, gdal.GDT_Float32
)
vals = array.array("f", [-80 + step * (x + shift) for x in range(20 // step)])
lon_ds.WriteRaster(0, 0, 20 // step, 1, vals)
lon_ds = None
lat_ds = gdal.GetDriverByName('GTiff').Create('/vsimem/lat.tif', 20 // step, 1, 1, gdal.GDT_Float32)
vals = array.array('f', [50 - step * (x + shift) for x in range(20 // step)])
lat_ds = gdal.GetDriverByName("GTiff").Create(
"/vsimem/lat.tif", 20 // step, 1, 1, gdal.GDT_Float32
)
vals = array.array("f", [50 - step * (x + shift) for x in range(20 // step)])
lat_ds.WriteRaster(0, 0, 20 // step, 1, vals)
lat_ds = None
ds = gdal.GetDriverByName('MEM').Create('', 20, 20)
ds = gdal.GetDriverByName("MEM").Create("", 20, 20)
md = {
'LINE_OFFSET': '0',
'LINE_STEP': str(step),
'PIXEL_OFFSET': '0',
'PIXEL_STEP': str(step),
'X_DATASET': '/vsimem/lon.tif',
'X_BAND' : '1',
'Y_DATASET': '/vsimem/lat.tif',
'Y_BAND' : '1',
'SRS': 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]',
'GEOREFERENCING_CONVENTION' : convention
"LINE_OFFSET": "0",
"LINE_STEP": str(step),
"PIXEL_OFFSET": "0",
"PIXEL_STEP": str(step),
"X_DATASET": "/vsimem/lon.tif",
"X_BAND": "1",
"Y_DATASET": "/vsimem/lat.tif",
"Y_BAND": "1",
"SRS": 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]',
"GEOREFERENCING_CONVENTION": convention,
}
ds.SetMetadata(md, 'GEOLOCATION')
ds.SetMetadata(md, "GEOLOCATION")
with gdaltest.config_option('GDAL_GEOLOC_INVERSE_METHOD', inverse_method):
with gdaltest.config_option("GDAL_GEOLOC_INVERSE_METHOD", inverse_method):
tr = gdal.Transformer(ds, None, [])
def check_point(x,y,X,Y):
def check_point(x, y, X, Y):
success, pnt = tr.TransformPoint(False, x, y)
assert success
assert pnt == (X, Y, 0)
@ -261,15 +307,15 @@ def test_geoloc_affine_transformation(step, convention, inverse_method):
check_point(10, 10, -70.0, 40.0)
check_point(1.23, 2.34, -78.77, 47.66)
check_point(0, 0, -80.0, 50.0)
check_point(20, 0, -60.0, 50.0)
check_point(0, 20, -80.0, 30.0)
check_point(0, 0, -80.0, 50.0)
check_point(20, 0, -60.0, 50.0)
check_point(0, 20, -80.0, 30.0)
check_point(20, 20, -60.0, 30.0)
ds = None
gdal.Unlink('/vsimem/lon.tif')
gdal.Unlink('/vsimem/lat.tif')
gdal.Unlink("/vsimem/lon.tif")
gdal.Unlink("/vsimem/lat.tif")
###############################################################################
@ -277,39 +323,55 @@ def test_geoloc_affine_transformation(step, convention, inverse_method):
@pytest.mark.parametrize("step", [1, 2])
@pytest.mark.parametrize("convention", ['TOP_LEFT_CORNER', 'PIXEL_CENTER'])
@pytest.mark.parametrize("convention", ["TOP_LEFT_CORNER", "PIXEL_CENTER"])
def test_geoloc_affine_transformation_with_noise(step, convention):
r = random.Random(0)
shift = 0.5 if convention == 'PIXEL_CENTER' else 0
lon_ds = gdal.GetDriverByName('GTiff').Create('/vsimem/lon.tif', 20 // step, 20 // step, 1, gdal.GDT_Float32)
shift = 0.5 if convention == "PIXEL_CENTER" else 0
lon_ds = gdal.GetDriverByName("GTiff").Create(
"/vsimem/lon.tif", 20 // step, 20 // step, 1, gdal.GDT_Float32
)
for y in range(lon_ds.RasterYSize):
vals = array.array('f', [-80 + step * (x + shift) + r.uniform(-0.25,0.25) for x in range(lon_ds.RasterXSize)])
vals = array.array(
"f",
[
-80 + step * (x + shift) + r.uniform(-0.25, 0.25)
for x in range(lon_ds.RasterXSize)
],
)
lon_ds.WriteRaster(0, y, lon_ds.RasterXSize, 1, vals)
lon_ds = None
lat_ds = gdal.GetDriverByName('GTiff').Create('/vsimem/lat.tif', 20 // step, 20 // step, 1, gdal.GDT_Float32)
lat_ds = gdal.GetDriverByName("GTiff").Create(
"/vsimem/lat.tif", 20 // step, 20 // step, 1, gdal.GDT_Float32
)
for x in range(lat_ds.RasterXSize):
vals = array.array('f', [50 - step * (y + shift) + r.uniform(-0.25,0.25) for y in range(lat_ds.RasterYSize)])
vals = array.array(
"f",
[
50 - step * (y + shift) + r.uniform(-0.25, 0.25)
for y in range(lat_ds.RasterYSize)
],
)
lat_ds.WriteRaster(x, 0, 1, lat_ds.RasterYSize, vals)
lat_ds = None
ds = gdal.GetDriverByName('MEM').Create('', 20, 20)
ds = gdal.GetDriverByName("MEM").Create("", 20, 20)
md = {
'LINE_OFFSET': '0',
'LINE_STEP': str(step),
'PIXEL_OFFSET': '0',
'PIXEL_STEP': str(step),
'X_DATASET': '/vsimem/lon.tif',
'X_BAND' : '1',
'Y_DATASET': '/vsimem/lat.tif',
'Y_BAND' : '1',
'SRS': 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]',
'GEOREFERENCING_CONVENTION' : convention
"LINE_OFFSET": "0",
"LINE_STEP": str(step),
"PIXEL_OFFSET": "0",
"PIXEL_STEP": str(step),
"X_DATASET": "/vsimem/lon.tif",
"X_BAND": "1",
"Y_DATASET": "/vsimem/lat.tif",
"Y_BAND": "1",
"SRS": 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]',
"GEOREFERENCING_CONVENTION": convention,
}
ds.SetMetadata(md, 'GEOLOCATION')
ds.SetMetadata(md, "GEOLOCATION")
tr = gdal.Transformer(ds, None, [])
def check_point(x,y):
def check_point(x, y):
success, pnt = tr.TransformPoint(False, x, y)
assert success
success, pnt = tr.TransformPoint(True, pnt[0], pnt[1])
@ -325,8 +387,8 @@ def test_geoloc_affine_transformation_with_noise(step, convention):
ds = None
gdal.Unlink('/vsimem/lon.tif')
gdal.Unlink('/vsimem/lat.tif')
gdal.Unlink("/vsimem/lon.tif")
gdal.Unlink("/vsimem/lat.tif")
###############################################################################
@ -336,34 +398,40 @@ def test_geoloc_affine_transformation_with_noise(step, convention):
def test_geoloc_GEOLOC_ARRAY_transformer_option():
step = 1
convention = 'TOP_LEFT_CORNER'
convention = "TOP_LEFT_CORNER"
x0 = -80
y0 = 50
shift = 0.5 if convention == 'PIXEL_CENTER' else 0
geoloc_ds = gdal.GetDriverByName('GTiff').Create('/vsimem/lonlat.tif', 20 // step, 20 // step, 2, gdal.GDT_Float32)
shift = 0.5 if convention == "PIXEL_CENTER" else 0
geoloc_ds = gdal.GetDriverByName("GTiff").Create(
"/vsimem/lonlat.tif", 20 // step, 20 // step, 2, gdal.GDT_Float32
)
lon_band = geoloc_ds.GetRasterBand(1)
lat_band = geoloc_ds.GetRasterBand(2)
for y in range(geoloc_ds.RasterYSize):
vals = array.array('f', [x0 + step * (x + shift) for x in range(geoloc_ds.RasterXSize)])
vals = array.array(
"f", [x0 + step * (x + shift) for x in range(geoloc_ds.RasterXSize)]
)
lon_band.WriteRaster(0, y, geoloc_ds.RasterXSize, 1, vals)
for x in range(geoloc_ds.RasterXSize):
vals = array.array('f', [y0 - step * (y + shift) for y in range(geoloc_ds.RasterYSize)])
vals = array.array(
"f", [y0 - step * (y + shift) for y in range(geoloc_ds.RasterYSize)]
)
lat_band.WriteRaster(x, 0, 1, geoloc_ds.RasterYSize, vals)
geoloc_ds = None
ds = gdal.GetDriverByName('MEM').Create('', 20, 20)
ds = gdal.GetDriverByName("MEM").Create("", 20, 20)
# Non-existing GEOLOC_ARRAY
with gdaltest.error_handler():
assert gdal.Transformer(ds, None, ['GEOLOC_ARRAY=/vsimem/invalid.tif' ]) is None
assert gdal.Transformer(ds, None, ["GEOLOC_ARRAY=/vsimem/invalid.tif"]) is None
# Existing GEOLOC_ARRAY but single band
with gdaltest.error_handler():
assert gdal.Transformer(ds, None, ['GEOLOC_ARRAY=data/byte.tif' ]) is None
assert gdal.Transformer(ds, None, ["GEOLOC_ARRAY=data/byte.tif"]) is None
# Test SRC_GEOLOC_ARRAY transformer option
tr = gdal.Transformer(ds, None, ['SRC_GEOLOC_ARRAY=/vsimem/lonlat.tif' ])
tr = gdal.Transformer(ds, None, ["SRC_GEOLOC_ARRAY=/vsimem/lonlat.tif"])
assert tr
x = 1
y = 20
@ -372,44 +440,54 @@ def test_geoloc_GEOLOC_ARRAY_transformer_option():
assert pnt == pytest.approx((x0 + x * step, y0 - y * step, 0))
# Test GEOLOC_ARRAY transformer option
tr = gdal.Transformer(ds, None, ['GEOLOC_ARRAY=/vsimem/lonlat.tif' ])
tr = gdal.Transformer(ds, None, ["GEOLOC_ARRAY=/vsimem/lonlat.tif"])
assert tr
success, pnt = tr.TransformPoint(False, x, y)
assert success
assert pnt == pytest.approx((x0 + x * step, y0 - y * step, 0))
# Test with a GEOLOCATION metadata domain
geoloc_ds = gdal.Open('/vsimem/lonlat.tif', gdal.GA_Update)
geoloc_ds = gdal.Open("/vsimem/lonlat.tif", gdal.GA_Update)
srs = osr.SpatialReference()
srs.ImportFromEPSG(32631)
srs.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)
geoloc_ds.SetSpatialRef(srs)
md = {
'X_BAND': '2',
'Y_BAND': '1',
'PIXEL_OFFSET': 15,
'PIXEL_STEP': '1',
'LINE_OFFSET': 5,
'LINE_STEP': '1',
'X_DATASET': 'ignored',
'Y_DATASET': 'ignored',
"X_BAND": "2",
"Y_BAND": "1",
"PIXEL_OFFSET": 15,
"PIXEL_STEP": "1",
"LINE_OFFSET": 5,
"LINE_STEP": "1",
"X_DATASET": "ignored",
"Y_DATASET": "ignored",
}
geoloc_ds.SetMetadata(md, 'GEOLOCATION')
geoloc_ds.SetMetadata(md, "GEOLOCATION")
geoloc_ds = None
tr = gdal.Transformer(ds, None, ['DST_SRS=EPSG:32731', 'GEOLOC_ARRAY=/vsimem/lonlat.tif' ])
tr = gdal.Transformer(
ds, None, ["DST_SRS=EPSG:32731", "GEOLOC_ARRAY=/vsimem/lonlat.tif"]
)
assert tr
success, pnt = tr.TransformPoint(False, x, y)
assert success
# The 10e6 offset is the northing offset between EPSG:32631 and EPSG:32731
assert pnt == pytest.approx((y0 - y * step + md['LINE_OFFSET'], 10e6 + x0 + x * step - md['PIXEL_OFFSET'], 0))
assert pnt == pytest.approx(
(
y0 - y * step + md["LINE_OFFSET"],
10e6 + x0 + x * step - md["PIXEL_OFFSET"],
0,
)
)
warped_ds = gdal.Warp('', ds, format='MEM', transformerOptions=['GEOLOC_ARRAY=/vsimem/lonlat.tif'])
assert warped_ds.GetSpatialRef().GetAuthorityCode(None) == '32631'
warped_ds = gdal.Warp(
"", ds, format="MEM", transformerOptions=["GEOLOC_ARRAY=/vsimem/lonlat.tif"]
)
assert warped_ds.GetSpatialRef().GetAuthorityCode(None) == "32631"
ds = None
gdal.Unlink('/vsimem/lonlat.tif')
gdal.Unlink("/vsimem/lonlat.tif")
###############################################################################
@ -418,21 +496,27 @@ def test_geoloc_GEOLOC_ARRAY_transformer_option():
def test_geoloc_DST_GEOLOC_ARRAY_transformer_option():
geoloc_ds = gdal.GetDriverByName('GTiff').Create('/vsimem/lonlat_DST_GEOLOC_ARRAY.tif', 10, 10, 2, gdal.GDT_Float32)
geoloc_ds.SetMetadataItem('GEOREFERENCING_CONVENTION', 'PIXEL_CENTER')
geoloc_ds = gdal.GetDriverByName("GTiff").Create(
"/vsimem/lonlat_DST_GEOLOC_ARRAY.tif", 10, 10, 2, gdal.GDT_Float32
)
geoloc_ds.SetMetadataItem("GEOREFERENCING_CONVENTION", "PIXEL_CENTER")
lon_band = geoloc_ds.GetRasterBand(1)
lat_band = geoloc_ds.GetRasterBand(2)
x0 = -80
y0 = 50
for y in range(geoloc_ds.RasterYSize):
lon_band.WriteRaster(0, 0, 10, 1, array.array('f', [x0 + 0.5 + 1 * x for x in range(10)]))
lon_band.WriteRaster(
0, 0, 10, 1, array.array("f", [x0 + 0.5 + 1 * x for x in range(10)])
)
for x in range(geoloc_ds.RasterXSize):
lat_band.WriteRaster(0, 0, 10, 1, array.array('f', [y0 - 0.5 - 1 * x for x in range(10)]))
lat_band.WriteRaster(
0, 0, 10, 1, array.array("f", [y0 - 0.5 - 1 * x for x in range(10)])
)
geoloc_ds = None
ds = gdal.GetDriverByName('MEM').Create('', 10, 10)
ds = gdal.GetDriverByName("MEM").Create("", 10, 10)
input_ds = gdal.GetDriverByName('MEM').Create('', 10, 10)
input_ds = gdal.GetDriverByName("MEM").Create("", 10, 10)
input_ds.SetGeoTransform([x0, 1, 0, y0, 0, -1])
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
@ -441,9 +525,14 @@ def test_geoloc_DST_GEOLOC_ARRAY_transformer_option():
# Non-existing DST_GEOLOC_ARRAY
with gdaltest.error_handler():
assert gdal.Transformer(input_ds, ds, ['DST_GEOLOC_ARRAY=/vsimem/invalid.tif' ]) is None
assert (
gdal.Transformer(input_ds, ds, ["DST_GEOLOC_ARRAY=/vsimem/invalid.tif"])
is None
)
tr = gdal.Transformer(input_ds, ds, ['DST_GEOLOC_ARRAY=/vsimem/lonlat_DST_GEOLOC_ARRAY.tif' ])
tr = gdal.Transformer(
input_ds, ds, ["DST_GEOLOC_ARRAY=/vsimem/lonlat_DST_GEOLOC_ARRAY.tif"]
)
success, pnt = tr.TransformPoint(0, 0.5, 0.5)
assert success
@ -451,4 +540,4 @@ def test_geoloc_DST_GEOLOC_ARRAY_transformer_option():
ds = None
gdal.Unlink('/vsimem/lonlat_DST_GEOLOC_ARRAY.tif')
gdal.Unlink("/vsimem/lonlat_DST_GEOLOC_ARRAY.tif")

View File

@ -25,52 +25,54 @@
# Boston, MA 02111-1307, USA.
###############################################################################
import gdaltest
import pytest
import gdaltest
init_list = [
('byte.tif', 4672),
('int16.tif', 4672),
('uint16.tif', 4672),
('int32.tif', 4672),
('uint32.tif', 4672),
('float32.tif', 4672),
('float64.tif', 4672),
('cint16.tif', 5028),
('cint32.tif', 5028),
('cfloat32.tif', 5028),
('cfloat64.tif', 5028)]
("byte.tif", 4672),
("int16.tif", 4672),
("uint16.tif", 4672),
("int32.tif", 4672),
("uint32.tif", 4672),
("float32.tif", 4672),
("float64.tif", 4672),
("cint16.tif", 5028),
("cint32.tif", 5028),
("cfloat32.tif", 5028),
("cfloat64.tif", 5028),
]
# Some tests we don't need to do for each type.
@pytest.mark.parametrize(
'testfunction', [
'testSetGeoTransform',
'testSetProjection',
'testSetMetadata',
]
"testfunction",
[
"testSetGeoTransform",
"testSetProjection",
"testSetMetadata",
],
)
@pytest.mark.require_driver('GTiff')
@pytest.mark.require_driver("GTiff")
def test_gtiff_set(testfunction):
ut = gdaltest.GDALTest('GTiff', 'byte.tif', 1, 4672)
ut = gdaltest.GDALTest("GTiff", "byte.tif", 1, 4672)
getattr(ut, testfunction)()
# Others we do for each pixel type.
@pytest.mark.parametrize(
'filename,checksum',
"filename,checksum",
init_list,
ids=[tup[0].split('.')[0] for tup in init_list],
ids=[tup[0].split(".")[0] for tup in init_list],
)
@pytest.mark.parametrize(
'testfunction', [
'testCreateCopy',
'testCreate',
'testSetNoDataValue',
]
"testfunction",
[
"testCreateCopy",
"testCreate",
"testSetNoDataValue",
],
)
@pytest.mark.require_driver('GTiff')
@pytest.mark.require_driver("GTiff")
def test_gtiff_create(filename, checksum, testfunction):
ut = gdaltest.GDALTest('GTiff', filename, 1, checksum)
ut = gdaltest.GDALTest("GTiff", filename, 1, checksum)
getattr(ut, testfunction)()

View File

@ -27,40 +27,41 @@
###############################################################################
import gdaltest
import pytest
import gdaltest
from osgeo import gdal
pytestmark = pytest.mark.require_driver('HDF4')
pytestmark = pytest.mark.require_driver("HDF4")
init_list = [
('byte_3.hdf', 4672),
('int16_3.hdf', 4672),
('uint16_3.hdf', 4672),
('int32_3.hdf', 4672),
('uint32_3.hdf', 4672),
('float32_3.hdf', 4672),
('float64_3.hdf', 4672),
('utmsmall_3.hdf', 50054),
('byte_2.hdf', 4672),
('int16_2.hdf', 4672),
('uint16_2.hdf', 4672),
('int32_2.hdf', 4672),
('uint32_2.hdf', 4672),
('float32_2.hdf', 4672),
('float64_2.hdf', 4672),
('utmsmall_2.hdf', 50054)]
("byte_3.hdf", 4672),
("int16_3.hdf", 4672),
("uint16_3.hdf", 4672),
("int32_3.hdf", 4672),
("uint32_3.hdf", 4672),
("float32_3.hdf", 4672),
("float64_3.hdf", 4672),
("utmsmall_3.hdf", 50054),
("byte_2.hdf", 4672),
("int16_2.hdf", 4672),
("uint16_2.hdf", 4672),
("int32_2.hdf", 4672),
("uint32_2.hdf", 4672),
("float32_2.hdf", 4672),
("float64_2.hdf", 4672),
("utmsmall_2.hdf", 50054),
]
@pytest.mark.parametrize(
'filename,checksum',
"filename,checksum",
init_list,
ids=[tup[0].split('.')[0] for tup in init_list],
ids=[tup[0].split(".")[0] for tup in init_list],
)
@pytest.mark.require_driver('HDF4Image')
@pytest.mark.require_driver("HDF4Image")
def test_hdf4_open(filename, checksum):
ut = gdaltest.GDALTest('HDF4Image', filename, 1, checksum)
ut = gdaltest.GDALTest("HDF4Image", filename, 1, checksum)
ut.testOpen()
@ -71,13 +72,14 @@ def test_hdf4_open(filename, checksum):
def test_hdf4_read_gr():
# Generated by https://support.hdfgroup.org/ftp/HDF/HDF_Current/src/unpacked/hdf/examples/GR_create_and_write_image.c
ds = gdal.Open('data/General_RImages.hdf')
ds = gdal.Open("data/General_RImages.hdf")
assert ds
assert ds.RasterCount == 2
assert ds.GetRasterBand(1).Checksum() == 361
assert not ds.GetRasterBand(1).GetColorTable()
assert ds.GetRasterBand(2).Checksum() == 400
###############################################################################
# Test reading a GR dataset with a palette
@ -85,29 +87,40 @@ def test_hdf4_read_gr():
def test_hdf4_read_gr_palette():
# Generated by https://support.hdfgroup.org/ftp/HDF/HDF_Current/src/unpacked/hdf/examples/GR_write_palette.c
ds = gdal.Open('data/Image_with_Palette.hdf')
ds = gdal.Open("data/Image_with_Palette.hdf")
assert ds
assert ds.RasterCount == 1
assert ds.GetRasterBand(1).GetColorTable()
###############################################################################
# Test HDF4_SDS with single subdataset
def test_hdf4_read_online_1():
gdaltest.hdf4_drv = gdal.GetDriverByName('HDF4')
gdaltest.hdf4_drv = gdal.GetDriverByName("HDF4")
if gdaltest.hdf4_drv is None:
pytest.skip()
if not gdaltest.download_file('http://download.osgeo.org/gdal/data/hdf4/A2004259075000.L2_LAC_SST.hdf', 'A2004259075000.L2_LAC_SST.hdf'):
if not gdaltest.download_file(
"http://download.osgeo.org/gdal/data/hdf4/A2004259075000.L2_LAC_SST.hdf",
"A2004259075000.L2_LAC_SST.hdf",
):
pytest.skip()
tst = gdaltest.GDALTest('HDF4Image', 'tmp/cache/A2004259075000.L2_LAC_SST.hdf', 1, 28189, filename_absolute=1)
tst = gdaltest.GDALTest(
"HDF4Image",
"tmp/cache/A2004259075000.L2_LAC_SST.hdf",
1,
28189,
filename_absolute=1,
)
return tst.testOpen()
###############################################################################
# Test HDF4_SDS with GEOLOCATION info
@ -117,47 +130,71 @@ def test_hdf4_read_online_2():
if gdaltest.hdf4_drv is None:
pytest.skip()
if not gdaltest.download_file('http://download.osgeo.org/gdal/data/hdf4/A2006005182000.L2_LAC_SST.x.hdf', 'A2006005182000.L2_LAC_SST.x.hdf'):
if not gdaltest.download_file(
"http://download.osgeo.org/gdal/data/hdf4/A2006005182000.L2_LAC_SST.x.hdf",
"A2006005182000.L2_LAC_SST.x.hdf",
):
pytest.skip()
tst = gdaltest.GDALTest('HDF4Image', 'HDF4_SDS:UNKNOWN:"tmp/cache/A2006005182000.L2_LAC_SST.x.hdf":13', 1, 13209, filename_absolute=1)
tst = gdaltest.GDALTest(
"HDF4Image",
'HDF4_SDS:UNKNOWN:"tmp/cache/A2006005182000.L2_LAC_SST.x.hdf":13',
1,
13209,
filename_absolute=1,
)
tst.testOpen()
ds = gdal.Open('HDF4_SDS:UNKNOWN:"tmp/cache/A2006005182000.L2_LAC_SST.x.hdf":13')
md = ds.GetMetadata('GEOLOCATION')
md = ds.GetMetadata("GEOLOCATION")
ds = None
assert md['X_DATASET'] == 'HDF4_SDS:UNKNOWN:"tmp/cache/A2006005182000.L2_LAC_SST.x.hdf":11', \
'Did not get expected X_DATASET'
assert (
md["X_DATASET"]
== 'HDF4_SDS:UNKNOWN:"tmp/cache/A2006005182000.L2_LAC_SST.x.hdf":11'
), "Did not get expected X_DATASET"
###############################################################################
# Test HDF4_EOS:EOS_GRID
def test_hdf4_read_online_3():
if gdaltest.hdf4_drv is None:
pytest.skip()
if not gdaltest.download_file('http://download.osgeo.org/gdal/data/hdf4/MO36MW14.chlor_MODIS.ADD2001089.004.2002186190207.hdf', 'MO36MW14.chlor_MODIS.ADD2001089.004.2002186190207.hdf'):
if not gdaltest.download_file(
"http://download.osgeo.org/gdal/data/hdf4/MO36MW14.chlor_MODIS.ADD2001089.004.2002186190207.hdf",
"MO36MW14.chlor_MODIS.ADD2001089.004.2002186190207.hdf",
):
pytest.skip()
tst = gdaltest.GDALTest('HDF4Image', 'tmp/cache/MO36MW14.chlor_MODIS.ADD2001089.004.2002186190207.hdf', 1, 34723, filename_absolute=1)
tst = gdaltest.GDALTest(
"HDF4Image",
"tmp/cache/MO36MW14.chlor_MODIS.ADD2001089.004.2002186190207.hdf",
1,
34723,
filename_absolute=1,
)
tst.testOpen()
ds = gdal.Open('tmp/cache/MO36MW14.chlor_MODIS.ADD2001089.004.2002186190207.hdf')
ds = gdal.Open("tmp/cache/MO36MW14.chlor_MODIS.ADD2001089.004.2002186190207.hdf")
gt = ds.GetGeoTransform()
expected_gt = [-180.0, 0.3515625, 0.0, 90.0, 0.0, -0.3515625]
for i in range(6):
assert gt[i] == pytest.approx(expected_gt[i], abs=1e-8), 'did not get expected gt'
assert gt[i] == pytest.approx(
expected_gt[i], abs=1e-8
), "did not get expected gt"
srs = ds.GetProjectionRef()
assert srs.find('Clarke') != -1, 'did not get expected projection'
assert srs.find("Clarke") != -1, "did not get expected projection"
ds = None
###############################################################################
# Test HDF4_SDS:SEAWIFS_L1A
@ -167,18 +204,28 @@ def test_hdf4_read_online_4():
if gdaltest.hdf4_drv is None:
pytest.skip()
if not gdaltest.download_file('http://download.osgeo.org/gdal/data/hdf4/S2002196124536.L1A_HDUN.BartonBendish.extract.hdf', 'S2002196124536.L1A_HDUN.BartonBendish.extract.hdf'):
if not gdaltest.download_file(
"http://download.osgeo.org/gdal/data/hdf4/S2002196124536.L1A_HDUN.BartonBendish.extract.hdf",
"S2002196124536.L1A_HDUN.BartonBendish.extract.hdf",
):
pytest.skip()
tst = gdaltest.GDALTest('HDF4Image', 'tmp/cache/S2002196124536.L1A_HDUN.BartonBendish.extract.hdf', 1, 33112, filename_absolute=1)
tst = gdaltest.GDALTest(
"HDF4Image",
"tmp/cache/S2002196124536.L1A_HDUN.BartonBendish.extract.hdf",
1,
33112,
filename_absolute=1,
)
tst.testOpen()
ds = gdal.Open('tmp/cache/S2002196124536.L1A_HDUN.BartonBendish.extract.hdf')
assert ds.RasterCount == 8, 'did not get expected band number'
ds = gdal.Open("tmp/cache/S2002196124536.L1A_HDUN.BartonBendish.extract.hdf")
assert ds.RasterCount == 8, "did not get expected band number"
ds = None
###############################################################################
# Test fix for #2208
@ -189,13 +236,23 @@ def test_hdf4_read_online_5():
pytest.skip()
# 13 MB
if not gdaltest.download_file('ftp://data.nodc.noaa.gov/pub/data.nodc/pathfinder/Version5.0/Monthly/1991/199101.s04m1pfv50-sst-16b.hdf', '199101.s04m1pfv50-sst-16b.hdf'):
if not gdaltest.download_file(
"ftp://data.nodc.noaa.gov/pub/data.nodc/pathfinder/Version5.0/Monthly/1991/199101.s04m1pfv50-sst-16b.hdf",
"199101.s04m1pfv50-sst-16b.hdf",
):
pytest.skip()
tst = gdaltest.GDALTest('HDF4Image', 'tmp/cache/199101.s04m1pfv50-sst-16b.hdf', 1, 41173, filename_absolute=1)
tst = gdaltest.GDALTest(
"HDF4Image",
"tmp/cache/199101.s04m1pfv50-sst-16b.hdf",
1,
41173,
filename_absolute=1,
)
tst.testOpen()
###############################################################################
# Test fix for #3386 where block size is dataset size
@ -206,25 +263,37 @@ def test_hdf4_read_online_6():
pytest.skip()
# 1 MB
if not gdaltest.download_file('http://download.osgeo.org/gdal/data/hdf4/MOD09Q1G_EVI.A2006233.h07v03.005.2008338190308.hdf', 'MOD09Q1G_EVI.A2006233.h07v03.005.2008338190308.hdf'):
if not gdaltest.download_file(
"http://download.osgeo.org/gdal/data/hdf4/MOD09Q1G_EVI.A2006233.h07v03.005.2008338190308.hdf",
"MOD09Q1G_EVI.A2006233.h07v03.005.2008338190308.hdf",
):
pytest.skip()
# Test with quoting of components
tst = gdaltest.GDALTest('HDF4Image', 'HDF4_EOS:EOS_GRID:"tmp/cache/MOD09Q1G_EVI.A2006233.h07v03.005.2008338190308.hdf":"MODIS_NACP_EVI":"MODIS_EVI"', 1, 12197, filename_absolute=1)
tst = gdaltest.GDALTest(
"HDF4Image",
'HDF4_EOS:EOS_GRID:"tmp/cache/MOD09Q1G_EVI.A2006233.h07v03.005.2008338190308.hdf":"MODIS_NACP_EVI":"MODIS_EVI"',
1,
12197,
filename_absolute=1,
)
tst.testOpen()
ds = gdal.Open('HDF4_EOS:EOS_GRID:tmp/cache/MOD09Q1G_EVI.A2006233.h07v03.005.2008338190308.hdf:MODIS_NACP_EVI:MODIS_EVI')
ds = gdal.Open(
"HDF4_EOS:EOS_GRID:tmp/cache/MOD09Q1G_EVI.A2006233.h07v03.005.2008338190308.hdf:MODIS_NACP_EVI:MODIS_EVI"
)
if 'GetBlockSize' in dir(gdal.Band):
if "GetBlockSize" in dir(gdal.Band):
(blockx, blocky) = ds.GetRasterBand(1).GetBlockSize()
assert blockx == 4800 and blocky == 4800, "Did not get expected block size"
cs = ds.GetRasterBand(1).Checksum()
assert cs == 12197, 'did not get expected checksum'
assert cs == 12197, "did not get expected checksum"
ds = None
###############################################################################
# Test fix for #3386 where block size is smaller than dataset size
@ -235,21 +304,32 @@ def test_hdf4_read_online_7():
pytest.skip()
# 4 MB
if not gdaltest.download_file('http://download.osgeo.org/gdal/data/hdf4/MOD09A1.A2010041.h06v03.005.2010051001103.hdf', 'MOD09A1.A2010041.h06v03.005.2010051001103.hdf'):
if not gdaltest.download_file(
"http://download.osgeo.org/gdal/data/hdf4/MOD09A1.A2010041.h06v03.005.2010051001103.hdf",
"MOD09A1.A2010041.h06v03.005.2010051001103.hdf",
):
pytest.skip()
tst = gdaltest.GDALTest('HDF4Image', 'HDF4_EOS:EOS_GRID:tmp/cache/MOD09A1.A2010041.h06v03.005.2010051001103.hdf:MOD_Grid_500m_Surface_Reflectance:sur_refl_b01', 1, 54894, filename_absolute=1)
tst = gdaltest.GDALTest(
"HDF4Image",
"HDF4_EOS:EOS_GRID:tmp/cache/MOD09A1.A2010041.h06v03.005.2010051001103.hdf:MOD_Grid_500m_Surface_Reflectance:sur_refl_b01",
1,
54894,
filename_absolute=1,
)
tst.testOpen()
ds = gdal.Open('HDF4_EOS:EOS_GRID:tmp/cache/MOD09A1.A2010041.h06v03.005.2010051001103.hdf:MOD_Grid_500m_Surface_Reflectance:sur_refl_b01')
ds = gdal.Open(
"HDF4_EOS:EOS_GRID:tmp/cache/MOD09A1.A2010041.h06v03.005.2010051001103.hdf:MOD_Grid_500m_Surface_Reflectance:sur_refl_b01"
)
if 'GetBlockSize' in dir(gdal.Band):
if "GetBlockSize" in dir(gdal.Band):
(blockx, blocky) = ds.GetRasterBand(1).GetBlockSize()
assert blockx == 2400 and blocky == 32, "Did not get expected block size"
cs = ds.GetRasterBand(1).Checksum()
assert cs == 54894, 'did not get expected checksum'
assert cs == 54894, "did not get expected checksum"
ds = None
@ -258,32 +338,45 @@ def test_hdf4_read_online_7():
# Test reading a HDF4_EOS:EOS_GRID where preferred block height reported would be 1
# but that will lead to very poor performance (#3386)
def test_hdf4_read_online_8():
if gdaltest.hdf4_drv is None:
pytest.skip()
# 5 MB
if not gdaltest.download_file('https://e4ftl01.cr.usgs.gov/MOLT/MOD13Q1.006/2006.06.10/MOD13Q1.A2006161.h34v09.006.2015161173716.hdf', 'MOD13Q1.A2006161.h34v09.006.2015161173716.hdf'):
if not gdaltest.download_file(
"https://e4ftl01.cr.usgs.gov/MOLT/MOD13Q1.006/2006.06.10/MOD13Q1.A2006161.h34v09.006.2015161173716.hdf",
"MOD13Q1.A2006161.h34v09.006.2015161173716.hdf",
):
pytest.skip()
tst = gdaltest.GDALTest('HDF4Image', 'HDF4_EOS:EOS_GRID:tmp/cache/MOD13Q1.A2006161.h34v09.006.2015161173716.hdf:MODIS_Grid_16DAY_250m_500m_VI:250m 16 days NDVI', 1, 44174, filename_absolute=1)
tst = gdaltest.GDALTest(
"HDF4Image",
"HDF4_EOS:EOS_GRID:tmp/cache/MOD13Q1.A2006161.h34v09.006.2015161173716.hdf:MODIS_Grid_16DAY_250m_500m_VI:250m 16 days NDVI",
1,
44174,
filename_absolute=1,
)
tst.testOpen()
ds = gdal.Open('HDF4_EOS:EOS_GRID:tmp/cache/MOD13Q1.A2006161.h34v09.006.2015161173716.hdf:MODIS_Grid_16DAY_250m_500m_VI:250m 16 days NDVI')
ds = gdal.Open(
"HDF4_EOS:EOS_GRID:tmp/cache/MOD13Q1.A2006161.h34v09.006.2015161173716.hdf:MODIS_Grid_16DAY_250m_500m_VI:250m 16 days NDVI"
)
cs = ds.GetRasterBand(1).Checksum()
assert cs == 44174, 'did not get expected checksum'
assert cs == 44174, "did not get expected checksum"
if 'GetBlockSize' in dir(gdal.Band):
if "GetBlockSize" in dir(gdal.Band):
(blockx, blocky) = ds.GetRasterBand(1).GetBlockSize()
if blockx != 4800 or blocky == 1:
print('blockx=%d, blocky=%d' % (blockx, blocky))
print("blockx=%d, blocky=%d" % (blockx, blocky))
pytest.fail("Did not get expected block size")
ds = None
###############################################################################
# Test reading L1G MTL metadata metadata
@ -293,20 +386,27 @@ def test_hdf4_read_online_9():
if gdaltest.hdf4_drv is None:
pytest.skip()
if not gdaltest.download_file('ftp://ftp.maps.canada.ca/pub/nrcan_rncan/archive/image/landsat_7/geobase_hdf/L71002025_02520010722/L71002025_02520010722_MTL.L1G', 'L71002025_02520010722_MTL.L1G'):
if not gdaltest.download_file(
"ftp://ftp.maps.canada.ca/pub/nrcan_rncan/archive/image/landsat_7/geobase_hdf/L71002025_02520010722/L71002025_02520010722_MTL.L1G",
"L71002025_02520010722_MTL.L1G",
):
pytest.skip()
if not gdaltest.download_file('ftp://ftp.maps.canada.ca/pub/nrcan_rncan/archive/image/landsat_7/geobase_hdf/L71002025_02520010722/L71002025_02520010722_HDF.L1G', 'L71002025_02520010722_HDF.L1G'):
if not gdaltest.download_file(
"ftp://ftp.maps.canada.ca/pub/nrcan_rncan/archive/image/landsat_7/geobase_hdf/L71002025_02520010722/L71002025_02520010722_HDF.L1G",
"L71002025_02520010722_HDF.L1G",
):
pytest.skip()
f = open('tmp/cache/L71002025_02520010722_B10.L1G', 'wb')
f = open("tmp/cache/L71002025_02520010722_B10.L1G", "wb")
f.close()
ds = gdal.Open('HDF4_SDS:UNKNOWN:"tmp/cache/L71002025_02520010722_HDF.L1G":0')
gcp_count = ds.GetGCPCount()
ds = None
assert gcp_count == 4, 'did not get expected gcp count'
assert gcp_count == 4, "did not get expected gcp count"
###############################################################################
# Test that non-tiled access works (#4672)
@ -317,22 +417,26 @@ def test_hdf4_read_online_10():
if gdaltest.hdf4_drv is None:
pytest.skip()
if not gdaltest.download_file('http://trac.osgeo.org/gdal/raw-attachment/ticket/4672/MOD16A2.A2000M01.h14v02.105.2010357183410.hdf', 'MOD16A2.A2000M01.h14v02.105.2010357183410.hdf'):
if not gdaltest.download_file(
"http://trac.osgeo.org/gdal/raw-attachment/ticket/4672/MOD16A2.A2000M01.h14v02.105.2010357183410.hdf",
"MOD16A2.A2000M01.h14v02.105.2010357183410.hdf",
):
pytest.skip()
ds = gdal.Open('HDF4_EOS:EOS_GRID:"tmp/cache/MOD16A2.A2000M01.h14v02.105.2010357183410.hdf":MOD_Grid_MOD16A2:ET_1km')
ds = gdal.Open(
'HDF4_EOS:EOS_GRID:"tmp/cache/MOD16A2.A2000M01.h14v02.105.2010357183410.hdf":MOD_Grid_MOD16A2:ET_1km'
)
if 'GetBlockSize' in dir(gdal.Band):
if "GetBlockSize" in dir(gdal.Band):
(blockx, blocky) = ds.GetRasterBand(1).GetBlockSize()
assert blockx == 1200 and blocky == 833, "Did not get expected block size"
cs = ds.GetRasterBand(1).Checksum()
assert cs == 20976, 'did not get expected checksum'
assert cs == 20976, "did not get expected checksum"
ds = None
###############################################################################
# Test reading HDFEOS SWATH projducts
@ -342,16 +446,24 @@ def test_hdf4_read_online_11():
if gdaltest.hdf4_drv is None:
pytest.skip()
if not gdaltest.download_file('https://gamma.hdfgroup.org/ftp/pub/outgoing/NASAHDFfiles2/eosweb/hdf4/hdfeos2-swath-wo-dimmaps/AMSR_E_L2_Ocean_B01_200206182340_A.hdf', 'AMSR_E_L2_Ocean_B01_200206182340_A.hdf'):
if not gdaltest.download_file(
"https://gamma.hdfgroup.org/ftp/pub/outgoing/NASAHDFfiles2/eosweb/hdf4/hdfeos2-swath-wo-dimmaps/AMSR_E_L2_Ocean_B01_200206182340_A.hdf",
"AMSR_E_L2_Ocean_B01_200206182340_A.hdf",
):
pytest.skip()
ds = gdal.Open('HDF4_EOS:EOS_SWATH:"tmp/cache/AMSR_E_L2_Ocean_B01_200206182340_A.hdf":Swath1:Ocean_products_quality_flag')
ds = gdal.Open(
'HDF4_EOS:EOS_SWATH:"tmp/cache/AMSR_E_L2_Ocean_B01_200206182340_A.hdf":Swath1:Ocean_products_quality_flag'
)
cs = ds.GetRasterBand(1).Checksum()
assert cs == 7809, 'did not get expected checksum'
assert cs == 7809, "did not get expected checksum"
ds = gdal.Open('tmp/cache/AMSR_E_L2_Ocean_B01_200206182340_A.hdf')
ds = gdal.Open("tmp/cache/AMSR_E_L2_Ocean_B01_200206182340_A.hdf")
assert len(ds.GetSubDatasets()) == 7
ds = gdal.OpenEx('tmp/cache/AMSR_E_L2_Ocean_B01_200206182340_A.hdf', open_options = ['LIST_SDS=YES'])
ds = gdal.OpenEx(
"tmp/cache/AMSR_E_L2_Ocean_B01_200206182340_A.hdf",
open_options=["LIST_SDS=YES"],
)
assert len(ds.GetSubDatasets()) == 16

View File

@ -25,39 +25,40 @@
# Boston, MA 02111-1307, USA.
###############################################################################
import pytest
import gdaltest
import pytest
init_list = [
('byte.tif', 4672),
('int16.tif', 4672),
('uint16.tif', 4672),
('int32.tif', 4672),
('uint32.tif', 4672),
('float32.tif', 4672),
('float64.tif', 4672),
('utmsmall.tif', 50054)
("byte.tif", 4672),
("int16.tif", 4672),
("uint16.tif", 4672),
("int32.tif", 4672),
("uint32.tif", 4672),
("float32.tif", 4672),
("float64.tif", 4672),
("utmsmall.tif", 50054),
]
@pytest.mark.parametrize("rank", [2, 3], ids=lambda x: "rank%d" % x)
@pytest.mark.parametrize(
'rank', [2, 3], ids=lambda x: 'rank%d' % x
"filename,checksum", init_list, ids=[arg[0].split(".")[0] for arg in init_list]
)
@pytest.mark.parametrize(
'filename,checksum', init_list, ids=[arg[0].split('.')[0] for arg in init_list]
"testfunction",
[
"testCreateCopy",
"testCreate",
"testSetGeoTransform",
"testSetProjection",
"testSetMetadata",
"testSetNoDataValue",
"testSetDescription",
],
)
@pytest.mark.parametrize(
'testfunction', [
'testCreateCopy',
'testCreate',
'testSetGeoTransform',
'testSetProjection',
'testSetMetadata',
'testSetNoDataValue',
'testSetDescription',
]
)
@pytest.mark.require_driver('HDF4Image')
@pytest.mark.require_driver("HDF4Image")
def test_hdf4_write(filename, checksum, testfunction, rank):
ut = gdaltest.GDALTest('HDF4Image', filename, 1, checksum, options=['RANK=%d' % rank])
ut = gdaltest.GDALTest(
"HDF4Image", filename, 1, checksum, options=["RANK=%d" % rank]
)
getattr(ut, testfunction)()

View File

@ -28,13 +28,14 @@
# DEALINGS IN THE SOFTWARE.
###############################################################################
import struct
import gdaltest
import pytest
import struct
from osgeo import gdal
pytestmark = pytest.mark.require_driver('HDF4')
pytestmark = pytest.mark.require_driver("HDF4")
###############################################################################
@ -43,86 +44,89 @@ pytestmark = pytest.mark.require_driver('HDF4')
def test_hdf4multidim_hdfeos_swath():
if not gdaltest.download_file('https://gamma.hdfgroup.org/ftp/pub/outgoing/NASAHDFfiles2/eosweb/hdf4/hdfeos2-swath-wo-dimmaps/AMSR_E_L2_Ocean_B01_200206182340_A.hdf', 'AMSR_E_L2_Ocean_B01_200206182340_A.hdf'):
if not gdaltest.download_file(
"https://gamma.hdfgroup.org/ftp/pub/outgoing/NASAHDFfiles2/eosweb/hdf4/hdfeos2-swath-wo-dimmaps/AMSR_E_L2_Ocean_B01_200206182340_A.hdf",
"AMSR_E_L2_Ocean_B01_200206182340_A.hdf",
):
pytest.skip()
ds = gdal.OpenEx('tmp/cache/AMSR_E_L2_Ocean_B01_200206182340_A.hdf', gdal.OF_MULTIDIM_RASTER)
ds = gdal.OpenEx(
"tmp/cache/AMSR_E_L2_Ocean_B01_200206182340_A.hdf", gdal.OF_MULTIDIM_RASTER
)
assert ds
rg = ds.GetRootGroup()
assert rg
assert rg.GetGroupNames() == ['swaths']
assert rg.GetGroupNames() == ["swaths"]
attrs = rg.GetAttributes()
assert attrs
attr = rg.GetAttribute('HDFEOSVersion')
assert attr.Read() == 'HDFEOS_V2.7.2'
swaths = rg.OpenGroup('swaths')
attr = rg.GetAttribute("HDFEOSVersion")
assert attr.Read() == "HDFEOS_V2.7.2"
swaths = rg.OpenGroup("swaths")
assert swaths
assert not rg.OpenGroup('foo')
assert swaths.GetGroupNames() == ['Swath1']
swath1 = swaths.OpenGroup('Swath1')
assert not rg.OpenGroup("foo")
assert swaths.GetGroupNames() == ["Swath1"]
swath1 = swaths.OpenGroup("Swath1")
assert swath1
assert not swaths.OpenGroup('foo')
assert not swaths.OpenGroup("foo")
attrs = swath1.GetAttributes()
assert len(attrs) == 3
attr = swath1.GetAttribute('SoftwareRevisionDate')
attr = swath1.GetAttribute("SoftwareRevisionDate")
assert attr.Read()[0:-1] == "November 7, 2003"
attr = swath1.GetAttribute('SoftwareBuildNumber')
attr = swath1.GetAttribute("SoftwareBuildNumber")
assert attr.Read() == 1
dims = swath1.GetDimensions()
assert len(dims) == 4
dim = next((x for x in dims if x.GetName() == 'DataXtrack_lo'), None)
dim = next((x for x in dims if x.GetName() == "DataXtrack_lo"), None)
assert dim
assert dim.GetFullName() == '/swaths/Swath1/DataXtrack_lo'
assert dim.GetSize() == 196
assert swath1.GetGroupNames() == ['Data Fields', 'Geolocation Fields']
assert not swath1.OpenGroup('foo')
datafields = swath1.OpenGroup('Data Fields')
assert dim.GetFullName() == "/swaths/Swath1/DataXtrack_lo"
assert dim.GetSize() == 196
assert swath1.GetGroupNames() == ["Data Fields", "Geolocation Fields"]
assert not swath1.OpenGroup("foo")
datafields = swath1.OpenGroup("Data Fields")
assert datafields
assert len(datafields.GetMDArrayNames()) == 10
assert not datafields.OpenMDArray('foo')
array = datafields.OpenMDArray('High_res_cloud')
assert not datafields.OpenMDArray("foo")
array = datafields.OpenMDArray("High_res_cloud")
assert array
dims = array.GetDimensions()
assert len(dims) == 2
assert dims[0].GetFullName() == '/swaths/Swath1/DataTrack_lo'
assert dims[0].GetFullName() == "/swaths/Swath1/DataTrack_lo"
assert array.GetDataType().GetNumericDataType() == gdal.GDT_Int16
attr = array.GetAttribute('Scale')
attr = array.GetAttribute("Scale")
assert attr.Read() == 9.999999747378752e-05
got_data = array.Read(array_start_idx = [13, 0], count = [3, 2])
got_data = array.Read(array_start_idx=[13, 0], count=[3, 2])
assert len(got_data) == 3 * 2 * 2
assert struct.unpack('h' * 6, got_data) == (0, 0,
17318, 17317,
17318, 17317)
assert struct.unpack("h" * 6, got_data) == (0, 0, 17318, 17317, 17318, 17317)
got_data = array.Read(array_start_idx = [13, 0], count = [3, 2],
buffer_datatype = gdal.ExtendedDataType.Create(gdal.GDT_Int32))
got_data = array.Read(
array_start_idx=[13, 0],
count=[3, 2],
buffer_datatype=gdal.ExtendedDataType.Create(gdal.GDT_Int32),
)
assert len(got_data) == 3 * 2 * 4
assert struct.unpack('i' * 6, got_data) == (0, 0,
17318, 17317,
17318, 17317)
assert struct.unpack("i" * 6, got_data) == (0, 0, 17318, 17317, 17318, 17317)
got_data = array.Read(array_start_idx = [13, 0], count = [3, 2], array_step = [2, 1])
got_data = array.Read(array_start_idx=[13, 0], count=[3, 2], array_step=[2, 1])
assert len(got_data) == 3 * 2 * 2
assert struct.unpack('h' * 6, got_data) == (0, 0,
17318, 17317,
17317, 17317)
assert struct.unpack("h" * 6, got_data) == (0, 0, 17318, 17317, 17317, 17317)
got_data = array.Read(array_start_idx = [15, 1], count = [3, 2], array_step = [-1, -1])
got_data = array.Read(array_start_idx=[15, 1], count=[3, 2], array_step=[-1, -1])
assert len(got_data) == 3 * 2 * 2
assert struct.unpack('h' * 6, got_data) == (17317, 17318,
17317, 17318,
0, 0)
assert struct.unpack("h" * 6, got_data) == (17317, 17318, 17317, 17318, 0, 0)
got_data = array.Read(array_start_idx = [13, 0], count = [3, 2], buffer_stride = [1, 3])
got_data = array.Read(array_start_idx=[13, 0], count=[3, 2], buffer_stride=[1, 3])
assert len(got_data) == 3 * 2 * 2
assert struct.unpack('h' * 6, got_data) == (0, 17318, 17318,
0, 17317, 17317)
assert struct.unpack("h" * 6, got_data) == (0, 17318, 17318, 0, 17317, 17317)
ds = gdal.OpenEx('tmp/cache/AMSR_E_L2_Ocean_B01_200206182340_A.hdf', gdal.OF_MULTIDIM_RASTER, open_options = ['LIST_SDS=YES'])
ds = gdal.OpenEx(
"tmp/cache/AMSR_E_L2_Ocean_B01_200206182340_A.hdf",
gdal.OF_MULTIDIM_RASTER,
open_options=["LIST_SDS=YES"],
)
rg = ds.GetRootGroup()
assert rg.GetGroupNames() == ['swaths', 'scientific_datasets']
assert rg.GetGroupNames() == ["swaths", "scientific_datasets"]
###############################################################################
# Test reading HDFEOS GRID products
@ -130,71 +134,82 @@ def test_hdf4multidim_hdfeos_swath():
def test_hdf4multidim_hdfeos_grid():
if not gdaltest.download_file('http://download.osgeo.org/gdal/data/hdf4/MOD09A1.A2010041.h06v03.005.2010051001103.hdf', 'MOD09A1.A2010041.h06v03.005.2010051001103.hdf'):
if not gdaltest.download_file(
"http://download.osgeo.org/gdal/data/hdf4/MOD09A1.A2010041.h06v03.005.2010051001103.hdf",
"MOD09A1.A2010041.h06v03.005.2010051001103.hdf",
):
pytest.skip()
ds = gdal.OpenEx('tmp/cache/MOD09A1.A2010041.h06v03.005.2010051001103.hdf', gdal.OF_MULTIDIM_RASTER)
ds = gdal.OpenEx(
"tmp/cache/MOD09A1.A2010041.h06v03.005.2010051001103.hdf",
gdal.OF_MULTIDIM_RASTER,
)
assert ds
rg = ds.GetRootGroup()
assert rg.GetGroupNames() == ['eos_grids']
assert rg.GetGroupNames() == ["eos_grids"]
attrs = rg.GetAttributes()
assert attrs
attr = rg.GetAttribute('HDFEOSVersion')
assert attr.Read() == 'HDFEOS_V2.9'
eos_grids = rg.OpenGroup('eos_grids')
attr = rg.GetAttribute("HDFEOSVersion")
assert attr.Read() == "HDFEOS_V2.9"
eos_grids = rg.OpenGroup("eos_grids")
assert eos_grids
assert not rg.OpenGroup('foo')
assert eos_grids.GetGroupNames() == ['MOD_Grid_500m_Surface_Reflectance']
MOD_Grid_500m_Surface_Reflectance = eos_grids.OpenGroup('MOD_Grid_500m_Surface_Reflectance')
assert not rg.OpenGroup("foo")
assert eos_grids.GetGroupNames() == ["MOD_Grid_500m_Surface_Reflectance"]
MOD_Grid_500m_Surface_Reflectance = eos_grids.OpenGroup(
"MOD_Grid_500m_Surface_Reflectance"
)
assert MOD_Grid_500m_Surface_Reflectance
assert not eos_grids.OpenGroup('foo')
assert not eos_grids.OpenGroup("foo")
attrs = MOD_Grid_500m_Surface_Reflectance.GetAttributes()
assert len(attrs) == 0
assert MOD_Grid_500m_Surface_Reflectance.GetGroupNames() == ['Data Fields']
assert not MOD_Grid_500m_Surface_Reflectance.OpenGroup('foo')
datafields = MOD_Grid_500m_Surface_Reflectance.OpenGroup('Data Fields')
assert MOD_Grid_500m_Surface_Reflectance.GetGroupNames() == ["Data Fields"]
assert not MOD_Grid_500m_Surface_Reflectance.OpenGroup("foo")
datafields = MOD_Grid_500m_Surface_Reflectance.OpenGroup("Data Fields")
assert datafields
assert len(datafields.GetMDArrayNames()) == 13
assert not datafields.OpenMDArray('foo')
array = datafields.OpenMDArray('sur_refl_b01')
assert not datafields.OpenMDArray("foo")
array = datafields.OpenMDArray("sur_refl_b01")
assert array
dims = array.GetDimensions()
assert len(dims) == 2
assert dims[0].GetFullName() == '/eos_grids/MOD_Grid_500m_Surface_Reflectance/YDim'
assert dims[0].GetFullName() == "/eos_grids/MOD_Grid_500m_Surface_Reflectance/YDim"
assert dims[0].GetSize() == 2400
assert dims[1].GetFullName() == '/eos_grids/MOD_Grid_500m_Surface_Reflectance/XDim'
assert dims[1].GetFullName() == "/eos_grids/MOD_Grid_500m_Surface_Reflectance/XDim"
assert dims[1].GetSize() == 2400
assert array.GetDataType().GetNumericDataType() == gdal.GDT_Int16
assert array.GetNoDataValueAsDouble() == -28672.0
assert array.GetOffset() == 0
assert array.GetScale() == 0.0001
assert array.GetUnit() == 'reflectance'
attr = array.GetAttribute('valid_range')
assert array.GetUnit() == "reflectance"
attr = array.GetAttribute("valid_range")
assert attr.Read() == (-100, 16000)
assert array.GetSpatialRef()
got_data = array.Read(array_start_idx = [2398, 2398], count = [2, 2])
got_data = array.Read(array_start_idx=[2398, 2398], count=[2, 2])
assert len(got_data) == 2 * 2 * 2
assert struct.unpack('h' * 4, got_data) == (-24, 0,
-15, -22)
assert struct.unpack("h" * 4, got_data) == (-24, 0, -15, -22)
dims = MOD_Grid_500m_Surface_Reflectance.GetDimensions()
assert len(dims) == 2
assert dims[0].GetName() == 'YDim'
assert dims[0].GetName() == "YDim"
assert dims[0].GetIndexingVariable()
assert dims[1].GetName() == 'XDim'
assert dims[1].GetName() == "XDim"
assert dims[1].GetIndexingVariable()
assert MOD_Grid_500m_Surface_Reflectance.GetMDArrayNames() == ['YDim', 'XDim']
XDim = MOD_Grid_500m_Surface_Reflectance.OpenMDArray('XDim')
assert MOD_Grid_500m_Surface_Reflectance.GetMDArrayNames() == ["YDim", "XDim"]
XDim = MOD_Grid_500m_Surface_Reflectance.OpenMDArray("XDim")
assert XDim
YDim = MOD_Grid_500m_Surface_Reflectance.OpenMDArray('YDim')
YDim = MOD_Grid_500m_Surface_Reflectance.OpenMDArray("YDim")
assert YDim
assert not MOD_Grid_500m_Surface_Reflectance.OpenMDArray('foo')
assert not MOD_Grid_500m_Surface_Reflectance.OpenMDArray("foo")
ds = gdal.OpenEx('tmp/cache/MOD09A1.A2010041.h06v03.005.2010051001103.hdf', gdal.OF_MULTIDIM_RASTER, open_options = ['LIST_SDS=YES'])
ds = gdal.OpenEx(
"tmp/cache/MOD09A1.A2010041.h06v03.005.2010051001103.hdf",
gdal.OF_MULTIDIM_RASTER,
open_options=["LIST_SDS=YES"],
)
rg = ds.GetRootGroup()
assert rg.GetGroupNames() == ['eos_grids', 'scientific_datasets']
assert rg.GetGroupNames() == ["eos_grids", "scientific_datasets"]
###############################################################################
@ -203,39 +218,44 @@ def test_hdf4multidim_hdfeos_grid():
def test_hdf4multidim_gdal_sds_2d():
ds = gdal.OpenEx('data/byte_2.hdf', gdal.OF_MULTIDIM_RASTER)
ds = gdal.OpenEx("data/byte_2.hdf", gdal.OF_MULTIDIM_RASTER)
assert ds
rg = ds.GetRootGroup()
assert rg.GetGroupNames() is None
assert rg.OpenGroup('scientific_datasets') is None
assert rg.GetMDArrayNames() == ['Band0', 'X', 'Y']
assert rg.OpenGroup("scientific_datasets") is None
assert rg.GetMDArrayNames() == ["Band0", "X", "Y"]
dims = rg.GetDimensions()
assert len(dims) == 2
array = rg.OpenMDArray('Band0')
array = rg.OpenMDArray("Band0")
assert array
dims = array.GetDimensions()
assert len(dims) == 2
assert dims[0].GetFullName() == '/Y'
assert dims[1].GetFullName() == '/X'
assert dims[0].GetFullName() == "/Y"
assert dims[1].GetFullName() == "/X"
assert array.GetDataType().GetNumericDataType() == gdal.GDT_Byte
got_data = array.Read(array_start_idx = [0, 0], count = [2, 2])
got_data = array.Read(array_start_idx=[0, 0], count=[2, 2])
assert len(got_data) == 2 * 2
assert struct.unpack('B' * 4, got_data) == (107, 123,
115, 132)
assert struct.unpack("B" * 4, got_data) == (107, 123, 115, 132)
assert array.GetSpatialRef()
assert array.GetUnit() == ''
assert array.GetUnit() == ""
assert not array.GetOffset()
assert not array.GetScale()
assert not array.GetNoDataValueAsDouble()
X = dims[0].GetIndexingVariable()
assert X
assert struct.unpack('d' * 2, X.Read(array_start_idx = [0], count = [2])) == (3751290.0, 3751230.0)
assert struct.unpack("d" * 2, X.Read(array_start_idx=[0], count=[2])) == (
3751290.0,
3751230.0,
)
Y = dims[1].GetIndexingVariable()
assert Y
assert struct.unpack('d' * 2, Y.Read(array_start_idx = [0], count = [2])) == (440750.0, 440810.0)
assert struct.unpack("d" * 2, Y.Read(array_start_idx=[0], count=[2])) == (
440750.0,
440810.0,
)
###############################################################################
@ -244,44 +264,50 @@ def test_hdf4multidim_gdal_sds_2d():
def test_hdf4multidim_gdal_sds_3d():
ds = gdal.OpenEx('data/byte_3.hdf', gdal.OF_MULTIDIM_RASTER)
ds = gdal.OpenEx("data/byte_3.hdf", gdal.OF_MULTIDIM_RASTER)
assert ds
rg = ds.GetRootGroup()
assert rg.GetGroupNames() is None
assert rg.OpenGroup('scientific_datasets') is None
assert rg.GetMDArrayNames() == ['3-dimensional Scientific Dataset', 'X', 'Y']
assert rg.OpenGroup("scientific_datasets") is None
assert rg.GetMDArrayNames() == ["3-dimensional Scientific Dataset", "X", "Y"]
dims = rg.GetDimensions()
assert len(dims) == 3
array = rg.OpenMDArray('3-dimensional Scientific Dataset')
array = rg.OpenMDArray("3-dimensional Scientific Dataset")
assert array
dims = array.GetDimensions()
assert len(dims) == 3
assert dims[0].GetFullName() == '/Y'
assert dims[1].GetFullName() == '/X'
assert dims[2].GetFullName() == '/Band'
assert dims[0].GetFullName() == "/Y"
assert dims[1].GetFullName() == "/X"
assert dims[2].GetFullName() == "/Band"
assert array.GetDataType().GetNumericDataType() == gdal.GDT_Byte
got_data = array.Read(array_start_idx = [0, 0, 0], count = [2, 2, 1])
got_data = array.Read(array_start_idx=[0, 0, 0], count=[2, 2, 1])
assert len(got_data) == 2 * 2
assert struct.unpack('B' * 4, got_data) == (107, 123,
115, 132)
assert struct.unpack("B" * 4, got_data) == (107, 123, 115, 132)
got_data = array.Transpose([2,1,0]).Read(array_start_idx = [0, 0, 0], count = [1, 2, 2])
got_data = array.Transpose([2, 1, 0]).Read(
array_start_idx=[0, 0, 0], count=[1, 2, 2]
)
assert len(got_data) == 2 * 2
assert struct.unpack('B' * 4, got_data) == (107, 115,
123, 132)
assert struct.unpack("B" * 4, got_data) == (107, 115, 123, 132)
assert array.GetSpatialRef()
X = dims[0].GetIndexingVariable()
assert X
assert struct.unpack('d' * 2, X.Read(array_start_idx = [0], count = [2])) == (3751290.0, 3751230.0)
assert rg.OpenMDArray('X')
assert struct.unpack("d" * 2, X.Read(array_start_idx=[0], count=[2])) == (
3751290.0,
3751230.0,
)
assert rg.OpenMDArray("X")
Y = dims[1].GetIndexingVariable()
assert Y
assert struct.unpack('d' * 2, Y.Read(array_start_idx = [0], count = [2])) == (440750.0, 440810.0)
assert rg.OpenMDArray('Y')
assert struct.unpack("d" * 2, Y.Read(array_start_idx=[0], count=[2])) == (
440750.0,
440810.0,
)
assert rg.OpenMDArray("Y")
###############################################################################
@ -294,39 +320,39 @@ def test_hdf4multidim_sds():
# https://support.hdfgroup.org/ftp/HDF/HDF_Current/src/unpacked/mfhdf/examples/SD_create_sds.c
# + https://support.hdfgroup.org/ftp/HDF/HDF_Current/src/unpacked/mfhdf/examples/SD_set_get_dim_info.c
# + https://support.hdfgroup.org/ftp/HDF/HDF_Current/src/unpacked/mfhdf/examples/SD_set_attr.c
ds = gdal.OpenEx('data/SDS.hdf', gdal.OF_MULTIDIM_RASTER)
ds = gdal.OpenEx("data/SDS.hdf", gdal.OF_MULTIDIM_RASTER)
assert ds
rg = ds.GetRootGroup()
assert rg.GetGroupNames() == ['scientific_datasets']
subg = rg.OpenGroup('scientific_datasets')
assert rg.GetGroupNames() == ["scientific_datasets"]
subg = rg.OpenGroup("scientific_datasets")
assert subg
attrs = rg.GetAttributes()
assert len(attrs) == 1
assert attrs[0].GetName() == 'File_contents'
assert attrs[0].Read() == 'Storm_track_data'
assert not rg.OpenGroup('foo')
assert attrs[0].GetName() == "File_contents"
assert attrs[0].Read() == "Storm_track_data"
assert not rg.OpenGroup("foo")
assert not rg.GetMDArrayNames()
assert not rg.OpenMDArray('foo')
assert not rg.OpenMDArray("foo")
assert not subg.GetGroupNames()
assert not subg.OpenGroup('foo')
assert subg.GetMDArrayNames() == ['SDStemplate', 'Y_Axis', 'X_Axis']
assert not subg.OpenGroup("foo")
assert subg.GetMDArrayNames() == ["SDStemplate", "Y_Axis", "X_Axis"]
dims = subg.GetDimensions()
assert len(dims) == 2
array = subg.OpenMDArray('SDStemplate')
array = subg.OpenMDArray("SDStemplate")
assert array
dims = array.GetDimensions()
assert len(dims) == 2
assert dims[0].GetFullName() == '/scientific_datasets/Y_Axis'
assert dims[0].GetFullName() == "/scientific_datasets/Y_Axis"
assert dims[0].GetIndexingVariable()
assert dims[1].GetFullName() == '/scientific_datasets/X_Axis'
assert dims[1].GetFullName() == "/scientific_datasets/X_Axis"
assert dims[1].GetIndexingVariable()
attrs = array.GetAttributes()
assert len(attrs) == 1
assert attrs[0].GetName() == 'Valid_range'
attr = array.GetAttribute('Valid_range')
assert attrs[0].GetName() == "Valid_range"
attr = array.GetAttribute("Valid_range")
assert attr
assert attr.Read() == (2, 10)
assert array.GetUnit() == ''
assert array.GetUnit() == ""
assert not array.GetSpatialRef()
@ -338,20 +364,20 @@ def test_hdf4multidim_sds_unlimited_dim():
# Generated with
# hhttps://support.hdfgroup.org/ftp/HDF/HDF_Current/src/unpacked/mfhdf/examples/SD_unlimited_sds.c
ds = gdal.OpenEx('data/SDSUNLIMITED.hdf', gdal.OF_MULTIDIM_RASTER)
ds = gdal.OpenEx("data/SDSUNLIMITED.hdf", gdal.OF_MULTIDIM_RASTER)
assert ds
rg = ds.GetRootGroup()
assert rg.GetGroupNames() == ['scientific_datasets']
subg = rg.OpenGroup('scientific_datasets')
assert rg.GetGroupNames() == ["scientific_datasets"]
subg = rg.OpenGroup("scientific_datasets")
assert subg
dims = subg.GetDimensions()
assert len(dims) == 2
assert dims[0].GetName() == 'fakeDim0'
assert dims[0].GetName() == "fakeDim0"
assert dims[0].GetSize() == 11
array = subg.OpenMDArray('AppendableData')
array = subg.OpenMDArray("AppendableData")
dims = array.GetDimensions()
assert len(dims) == 2
assert dims[0].GetName() == 'fakeDim0'
assert dims[0].GetName() == "fakeDim0"
assert dims[0].GetSize() == 11
assert len(array.Read()) == 11 * 10 * 4
@ -362,67 +388,75 @@ def test_hdf4multidim_sds_unlimited_dim():
def test_hdf4multidim_sds_read_world():
if not gdaltest.download_file('http://download.osgeo.org/gdal/data/hdf4/A2004259075000.L2_LAC_SST.hdf', 'A2004259075000.L2_LAC_SST.hdf'):
if not gdaltest.download_file(
"http://download.osgeo.org/gdal/data/hdf4/A2004259075000.L2_LAC_SST.hdf",
"A2004259075000.L2_LAC_SST.hdf",
):
pytest.skip()
ds = gdal.OpenEx('tmp/cache/A2004259075000.L2_LAC_SST.hdf', gdal.OF_MULTIDIM_RASTER)
ds = gdal.OpenEx("tmp/cache/A2004259075000.L2_LAC_SST.hdf", gdal.OF_MULTIDIM_RASTER)
assert ds
rg = ds.GetRootGroup()
assert rg.GetGroupNames() == ['scientific_datasets']
subg = rg.OpenGroup('scientific_datasets')
assert rg.GetGroupNames() == ["scientific_datasets"]
subg = rg.OpenGroup("scientific_datasets")
assert subg
assert not rg.OpenGroup('foo')
assert not rg.OpenGroup("foo")
assert not rg.GetMDArrayNames()
assert not rg.OpenMDArray('foo')
assert not rg.OpenMDArray("foo")
assert not subg.GetGroupNames()
assert not subg.OpenGroup('foo')
assert subg.GetMDArrayNames() == ['sst']
assert not subg.OpenGroup("foo")
assert subg.GetMDArrayNames() == ["sst"]
dims = subg.GetDimensions()
assert len(dims) == 2
sst = subg.OpenMDArray('sst')
sst = subg.OpenMDArray("sst")
assert sst
dims = sst.GetDimensions()
assert len(dims) == 2
assert dims[0].GetFullName() == '/scientific_datasets/fakeDim0'
assert dims[1].GetFullName() == '/scientific_datasets/fakeDim1'
assert dims[0].GetFullName() == "/scientific_datasets/fakeDim0"
assert dims[1].GetFullName() == "/scientific_datasets/fakeDim1"
attrs = sst.GetAttributes()
assert len(attrs) == 5
attr = sst.GetAttribute('long_name')
attr = sst.GetAttribute("long_name")
assert attr
assert attr.Read() == 'Sea Surface Temperature'
assert sst.GetUnit() == 'degrees-C'
assert attr.Read() == "Sea Surface Temperature"
assert sst.GetUnit() == "degrees-C"
assert not sst.GetSpatialRef()
###############################################################################
# Test reading a SDS product with indexed dimensions
def test_hdf4multidim_sds_read_world_with_indexing_variable():
if not gdaltest.download_file('https://download.osgeo.org/gdal/data/hdf4/REANALYSIS_1999217.hdf', 'REANALYSIS_1999217.hdf'):
if not gdaltest.download_file(
"https://download.osgeo.org/gdal/data/hdf4/REANALYSIS_1999217.hdf",
"REANALYSIS_1999217.hdf",
):
pytest.skip()
ds = gdal.OpenEx('tmp/cache/REANALYSIS_1999217.hdf', gdal.OF_MULTIDIM_RASTER)
ds = gdal.OpenEx("tmp/cache/REANALYSIS_1999217.hdf", gdal.OF_MULTIDIM_RASTER)
assert ds
rg = ds.GetRootGroup()
assert rg.GetGroupNames() == ['scientific_datasets']
subg = rg.OpenGroup('scientific_datasets')
assert rg.GetGroupNames() == ["scientific_datasets"]
subg = rg.OpenGroup("scientific_datasets")
assert subg
dims = subg.GetDimensions()
assert len(dims) == 3
assert dims[0].GetFullName() == '/scientific_datasets/lat'
assert dims[0].GetFullName() == "/scientific_datasets/lat"
assert dims[0].GetIndexingVariable()
assert dims[1].GetFullName() == '/scientific_datasets/lon'
assert dims[1].GetFullName() == "/scientific_datasets/lon"
assert dims[1].GetIndexingVariable()
assert dims[2].GetFullName() == '/scientific_datasets/time'
assert dims[2].GetFullName() == "/scientific_datasets/time"
assert not dims[2].GetIndexingVariable()
assert subg.GetMDArrayNames() == ['lat', 'lon', 'slp', 'pr_wtr', 'air']
slp = subg.OpenMDArray('slp')
assert subg.GetMDArrayNames() == ["lat", "lon", "slp", "pr_wtr", "air"]
slp = subg.OpenMDArray("slp")
assert slp
assert slp.GetUnit() == 'Pascals'
assert slp.GetUnit() == "Pascals"
assert slp.GetOffset() == 119765.0
assert slp.GetScale() == 1.0
###############################################################################
# Test reading a GR dataset
@ -431,73 +465,56 @@ def test_hdf4multidim_gr():
# Generated by https://support.hdfgroup.org/ftp/HDF/HDF_Current/src/unpacked/hdf/examples/GR_create_and_write_image.c
ds = gdal.OpenEx('data/General_RImages.hdf', gdal.OF_MULTIDIM_RASTER)
ds = gdal.OpenEx("data/General_RImages.hdf", gdal.OF_MULTIDIM_RASTER)
assert ds
rg = ds.GetRootGroup()
assert rg.GetGroupNames() == ['general_rasters']
subg = rg.OpenGroup('general_rasters')
assert rg.GetGroupNames() == ["general_rasters"]
subg = rg.OpenGroup("general_rasters")
assert subg
attrs = subg.GetAttributes()
assert len(attrs) == 2
attr = subg.GetAttribute('File Attribute 1')
assert attr.Read() == 'Contents of First FILE Attribute'
attr = subg.GetAttribute("File Attribute 1")
assert attr.Read() == "Contents of First FILE Attribute"
assert not subg.GetGroupNames()
assert not subg.OpenGroup('foo')
assert subg.GetMDArrayNames() == ['Image Array 1']
array = subg.OpenMDArray('Image Array 1')
assert not subg.OpenMDArray('foo')
assert not subg.OpenGroup("foo")
assert subg.GetMDArrayNames() == ["Image Array 1"]
array = subg.OpenMDArray("Image Array 1")
assert not subg.OpenMDArray("foo")
dims = array.GetDimensions()
assert len(dims) == 3
assert dims[0].GetName() == 'y'
assert dims[0].GetName() == "y"
assert dims[0].GetSize() == 10
assert dims[1].GetName() == 'x'
assert dims[1].GetName() == "x"
assert dims[1].GetSize() == 5
assert dims[2].GetName() == 'bands'
assert dims[2].GetName() == "bands"
assert dims[2].GetSize() == 2
got_data = array.Read(array_start_idx = [1, 2, 0], count = [3, 2, 2])
got_data = array.Read(array_start_idx=[1, 2, 0], count=[3, 2, 2])
assert len(got_data) == 3 * 2 * 2 * 2
assert struct.unpack('h' * 12, got_data) == (4, 5,
5, 6,
6, 7,
5, 6,
6, 7,
7, 8)
assert struct.unpack("h" * 12, got_data) == (4, 5, 5, 6, 6, 7, 5, 6, 6, 7, 7, 8)
got_data = array.Read(array_start_idx = [1, 2, 0], count = [3, 2, 1])
got_data = array.Read(array_start_idx=[1, 2, 0], count=[3, 2, 1])
assert len(got_data) == 3 * 2 * 1 * 2
assert struct.unpack('h' * 6, got_data) == (4,
5,
6,
5,
6,
7)
assert struct.unpack("h" * 6, got_data) == (4, 5, 6, 5, 6, 7)
got_data = array.Read(array_start_idx = [1, 2, 1], count = [3, 2, 1])
got_data = array.Read(array_start_idx=[1, 2, 1], count=[3, 2, 1])
assert len(got_data) == 3 * 2 * 1 * 2
assert struct.unpack('h' * 6, got_data) == (5,
6,
7,
6,
7,
8)
assert struct.unpack("h" * 6, got_data) == (5, 6, 7, 6, 7, 8)
got_data = array.Read(array_start_idx = [1, 2, 1], count = [3, 2, 2], array_step = [1, 1, -1])
got_data = array.Read(
array_start_idx=[1, 2, 1], count=[3, 2, 2], array_step=[1, 1, -1]
)
assert len(got_data) == 3 * 2 * 2 * 2
assert struct.unpack('h' * 12, got_data) == (5, 4,
6, 5,
7, 6,
6, 5,
7, 6,
8, 7)
assert struct.unpack("h" * 12, got_data) == (5, 4, 6, 5, 7, 6, 6, 5, 7, 6, 8, 7)
attrs = array.GetAttributes()
assert len(attrs) == 2
attr = array.GetAttribute('Image Attribute 1')
attr = array.GetAttribute("Image Attribute 1")
assert attr.Read() == "Contents of IMAGE's First Attribute"
attr = array.GetAttribute('Image Attribute 2')
attr = array.GetAttribute("Image Attribute 2")
assert attr.Read() == (1, 2, 3, 4, 5, 6)
###############################################################################
# Test reading a GR dataset with a palette
@ -507,14 +524,14 @@ def test_hdf4multidim_gr_palette():
# Generated by https://support.hdfgroup.org/ftp/HDF/HDF_Current/src/unpacked/hdf/examples/GR_write_palette.c
def get_lut():
ds = gdal.OpenEx('data/Image_with_Palette.hdf', gdal.OF_MULTIDIM_RASTER)
ds = gdal.OpenEx("data/Image_with_Palette.hdf", gdal.OF_MULTIDIM_RASTER)
assert ds
rg = ds.GetRootGroup()
assert rg.GetGroupNames() == ['general_rasters']
subg = rg.OpenGroup('general_rasters')
array = subg.OpenMDArray('Image with Palette')
assert rg.GetGroupNames() == ["general_rasters"]
subg = rg.OpenGroup("general_rasters")
array = subg.OpenMDArray("Image with Palette")
assert array
lut = array.GetAttribute('lut')
lut = array.GetAttribute("lut")
return lut
lut = get_lut()
@ -527,4 +544,3 @@ def test_hdf4multidim_gr_palette():
assert got[255 * 3 + 0] == 255
assert got[255 * 3 + 1] == 0
assert got[255 * 3 + 2] == 1

View File

@ -29,41 +29,40 @@
###############################################################################
import gdaltest
import pytest
from osgeo import gdal
import gdaltest
init_list = [
('byte.img', 4672),
('int16.img', 4672),
('uint16.img', 4672),
('int32.img', 4672),
('uint32.img', 4672),
('float32.img', 4672),
('float64.img', 4672),
('utmsmall.img', 50054),
('2bit_compressed.img', 11918)]
("byte.img", 4672),
("int16.img", 4672),
("uint16.img", 4672),
("int32.img", 4672),
("uint32.img", 4672),
("float32.img", 4672),
("float64.img", 4672),
("utmsmall.img", 50054),
("2bit_compressed.img", 11918),
]
@pytest.mark.parametrize(
'filename,checksum',
"filename,checksum",
init_list,
ids=[tup[0].split('.')[0] for tup in init_list],
ids=[tup[0].split(".")[0] for tup in init_list],
)
@pytest.mark.require_driver('HFA')
@pytest.mark.require_driver("HFA")
def test_hfa_open(filename, checksum):
ut = gdaltest.GDALTest('HFA', filename, 1, checksum)
ut = gdaltest.GDALTest("HFA", filename, 1, checksum)
ut.testOpen()
###############################################################################
# Test bugfix for https://oss-fuzz.com/v2/testcase-detail/6053338875428864
def test_hfa_read_completedefn_recursion():
with gdaltest.error_handler():
gdal.Open('data/hfa_completedefn_recursion.img')
gdal.Open("data/hfa_completedefn_recursion.img")

View File

@ -30,23 +30,39 @@
# DEALINGS IN THE SOFTWARE.
###############################################################################
from osgeo import gdal
import pytest
from osgeo import gdal
# All tests will be skipped if numpy is unavailable.
np = pytest.importorskip('numpy')
np = pytest.importorskip("numpy")
INT_DATA = np.array([197, 83, 46, 29, 1, 78, 23, 90, 12, 45])
DOUBLE_DATA = np.array([0.1, 43.2, 78.1, 9.9, 23.0, 0.92, 82.5, 0.0, 1.0, 99.0])
STRING_DATA = np.array([
"sddf", "wess", "grbgr", "dewd", "ddww", "qwsqw", "gbfgbf", "wwqw3", "e", ""])
STRING_DATA_INTS = np.array(["197", "83", "46", "29", "1", "78", "23", "90", "12", "45"])
STRING_DATA_DOUBLES = np.array([
"0.1", "43.2", "78.1", "9.9", "23.0", "0.92", "82.5", "0.0", "1.0", "99.0"])
LONG_STRING_DATA = np.array(["sdfsdfsdfs", "sdweddw", "sdewdweee", "3423dedd", "jkejjjdjd",
"edcdcdcdc", "fcdkmk4m534m", "edwededdd", "dedwedew", "wdedefrfrfrf"])
STRING_DATA = np.array(
["sddf", "wess", "grbgr", "dewd", "ddww", "qwsqw", "gbfgbf", "wwqw3", "e", ""]
)
STRING_DATA_INTS = np.array(
["197", "83", "46", "29", "1", "78", "23", "90", "12", "45"]
)
STRING_DATA_DOUBLES = np.array(
["0.1", "43.2", "78.1", "9.9", "23.0", "0.92", "82.5", "0.0", "1.0", "99.0"]
)
LONG_STRING_DATA = np.array(
[
"sdfsdfsdfs",
"sdweddw",
"sdewdweee",
"3423dedd",
"jkejjjdjd",
"edcdcdcdc",
"fcdkmk4m534m",
"edwededdd",
"dedwedew",
"wdedefrfrfrf",
]
)
class HFATestError(Exception):
@ -85,35 +101,50 @@ def CreateAndWriteRAT(fname):
raise HFATestError("Create column failed")
# for writing as different type
if rat.CreateColumn("IntAsDouble", gdal.GFT_Integer, gdal.GFU_Generic) != gdal.CE_None:
if (
rat.CreateColumn("IntAsDouble", gdal.GFT_Integer, gdal.GFU_Generic)
!= gdal.CE_None
):
raise HFATestError("Create column failed")
if rat.CreateColumn("IntsAsString", gdal.GFT_Integer, gdal.GFU_Generic) != gdal.CE_None:
if (
rat.CreateColumn("IntsAsString", gdal.GFT_Integer, gdal.GFU_Generic)
!= gdal.CE_None
):
raise HFATestError("Create column failed")
if rat.CreateColumn("DoubleAsInt", gdal.GFT_Real, gdal.GFU_Generic) != gdal.CE_None:
raise HFATestError("Create column failed")
if rat.CreateColumn("DoubleAsString", gdal.GFT_Real, gdal.GFU_Generic) != gdal.CE_None:
if (
rat.CreateColumn("DoubleAsString", gdal.GFT_Real, gdal.GFU_Generic)
!= gdal.CE_None
):
raise HFATestError("Create column failed")
if rat.CreateColumn("StringAsInt", gdal.GFT_String, gdal.GFU_Generic) != gdal.CE_None:
if (
rat.CreateColumn("StringAsInt", gdal.GFT_String, gdal.GFU_Generic)
!= gdal.CE_None
):
raise HFATestError("Create column failed")
if rat.CreateColumn("StringAsDouble", gdal.GFT_String, gdal.GFU_Generic) != gdal.CE_None:
if (
rat.CreateColumn("StringAsDouble", gdal.GFT_String, gdal.GFU_Generic)
!= gdal.CE_None
):
raise HFATestError("Create column failed")
rat.SetRowCount(INT_DATA.size)
# some basic checks
if rat.GetRowCount() != INT_DATA.size:
raise HFATestError('Wrong RowCount')
raise HFATestError("Wrong RowCount")
if rat.GetColumnCount() != 9:
raise HFATestError('Wrong Column Count')
raise HFATestError("Wrong Column Count")
if rat.GetNameOfCol(1) != "Doubles":
raise HFATestError('Wrong Column Count')
raise HFATestError("Wrong Column Count")
if rat.GetUsageOfCol(1) != gdal.GFU_Generic:
raise HFATestError("Wrong column usage")
@ -225,7 +256,7 @@ def CheckSetGetValues(fname):
rat.SetValueAsInt(i, 0, int(INT_DATA[i] + 1))
rat.SetValueAsDouble(i, 1, DOUBLE_DATA[i] + 1)
s = STRING_DATA[i]
s = s + 'z'
s = s + "z"
rat.SetValueAsString(i, 2, s)
# read data and check
@ -235,7 +266,7 @@ def CheckSetGetValues(fname):
if rat.GetValueAsDouble(i, 1) != (DOUBLE_DATA[i] + 1):
raise HFATestError("GetValueAsDouble not reading correctly")
s = STRING_DATA[i]
s = s + 'z'
s = s + "z"
if rat.GetValueAsString(i, 2) != s:
raise HFATestError("GetValueAsString not reading correctly")
@ -378,6 +409,7 @@ def CheckClone(fname):
# print("cloned ok")
ds = None
# basic tests
@ -388,36 +420,42 @@ def test_hfa_rfc40_1():
def test_hfa_rfc40_2():
return ReadAndCheckValues("tmp/test.img", 10)
# the older interface
def test_hfa_rfc40_3():
return CheckSetGetValues("tmp/test.img")
# make sure original data not changed
def test_hfa_rfc40_4():
return ReadAndCheckValues("tmp/test.img", 10)
# make it longer - data will be re-written
def test_hfa_rfc40_5():
return ExtendAndWrite("tmp/test.img")
# make sure old data not changed
def test_hfa_rfc40_6():
return ReadAndCheckValues("tmp/test.img", 20)
# new data at the end ok?
def test_hfa_rfc40_7():
return CheckExtension("tmp/test.img")
# write some longer strings - string column will
# have to be re-written
@ -425,41 +463,44 @@ def test_hfa_rfc40_7():
def test_hfa_rfc40_8():
return WriteLongStrings("tmp/test.img")
# make sure old data not changed
def test_hfa_rfc40_9():
return ReadAndCheckValues("tmp/test.img", 20)
# check new data ok
def test_hfa_rfc40_10():
return CheckLongStrings("tmp/test.img")
# linear binning
def test_hfa_rfc40_11():
return SetLinearBinning("tmp/test.img")
# linear binning
def test_hfa_rfc40_12():
return CheckLinearBinning("tmp/test.img")
# clone
def test_hfa_rfc40_13():
return CheckClone("tmp/test.img")
# serialize not available from Python...
def test_hfa_rfc40_cleanup():
gdal.GetDriverByName('HFA').Delete("tmp/test.img")
gdal.GetDriverByName("HFA").Delete("tmp/test.img")

View File

@ -31,9 +31,7 @@
import pytest
from osgeo import gdal
from osgeo import osr
from osgeo import gdal, osr
###############################################################################
# Write a HFA/Imagine and read it back to check its SRS
@ -45,7 +43,7 @@ epsg_list = [
[32661, True], # stere
[6931, False], # laea
[2062, False], # lcc
#[2065, True], # krovak South-West
# [2065, True], # krovak South-West
[5221, True], # krovak east-north
[2066, False], # cass
[2964, False], # aea
@ -62,7 +60,7 @@ epsg_list = [
@pytest.mark.parametrize(
'epsg_code,epsg_broken',
"epsg_code,epsg_broken",
epsg_list,
ids=[str(r[0]) for r in epsg_list],
)
@ -70,73 +68,90 @@ def test_hfa_srs(epsg_code, epsg_broken):
sr = osr.SpatialReference()
sr.ImportFromEPSG(epsg_code)
ds = gdal.GetDriverByName('HFA').Create('/vsimem/TestHFASRS.img', 1, 1)
ds = gdal.GetDriverByName("HFA").Create("/vsimem/TestHFASRS.img", 1, 1)
ds.SetProjection(sr.ExportToWkt())
ds = None
ds = gdal.Open('/vsimem/TestHFASRS.img')
ds = gdal.Open("/vsimem/TestHFASRS.img")
wkt = ds.GetProjectionRef()
sr2 = osr.SpatialReference()
sr2.SetFromUserInput(wkt)
ds = None
gdal.Unlink('/vsimem/TestHFASRS.img')
gdal.Unlink("/vsimem/TestHFASRS.img")
# For EPSG:2065. Those 2 datums are translated into D_S_JTSK in ESRI WKT... So for the purpose of
# comparison, substitute one for another
if sr.ExportToWkt().find('"System_Jednotne_Trigonometricke_Site_Katastralni_Ferro"') != -1 and \
sr2.ExportToWkt().find('"System_Jednotne_Trigonometricke_Site_Katastralni"') != -1:
wkt2 = sr2.ExportToWkt().replace('"System_Jednotne_Trigonometricke_Site_Katastralni"', '"System_Jednotne_Trigonometricke_Site_Katastralni_Ferro"')
if (
sr.ExportToWkt().find(
'"System_Jednotne_Trigonometricke_Site_Katastralni_Ferro"'
)
!= -1
and sr2.ExportToWkt().find('"System_Jednotne_Trigonometricke_Site_Katastralni"')
!= -1
):
wkt2 = sr2.ExportToWkt().replace(
'"System_Jednotne_Trigonometricke_Site_Katastralni"',
'"System_Jednotne_Trigonometricke_Site_Katastralni_Ferro"',
)
sr2.SetFromUserInput(wkt2)
if (epsg_code == 4326 and sr2.GetAuthorityCode(None) != '4326') or sr.IsSame(sr2) != 1:
if (epsg_code == 4326 and sr2.GetAuthorityCode(None) != "4326") or sr.IsSame(
sr2
) != 1:
if epsg_broken:
pytest.xfail('did not get expected SRS. known to be broken currently. FIXME!')
pytest.xfail(
"did not get expected SRS. known to be broken currently. FIXME!"
)
print(sr)
print(sr2)
assert False, 'did not get expected SRS'
assert False, "did not get expected SRS"
def test_hfa_srs_wisconsin_tmerc():
ds = gdal.Open('data/esri_103300_NAD_1983_HARN_WISCRS_Adams_County_Meters_transverse_mercator.img')
ds = gdal.Open(
"data/esri_103300_NAD_1983_HARN_WISCRS_Adams_County_Meters_transverse_mercator.img"
)
wkt = ds.GetProjectionRef()
sr = osr.SpatialReference()
sr.SetFromUserInput(wkt)
assert sr.GetAuthorityCode(None) == '103300'
assert sr.GetAuthorityCode(None) == "103300"
def test_hfa_srs_NAD83_UTM():
sr = osr.SpatialReference()
sr.ImportFromEPSG(26915)
ds = gdal.GetDriverByName('HFA').Create('/vsimem/TestHFASRS.img', 1, 1)
ds = gdal.GetDriverByName("HFA").Create("/vsimem/TestHFASRS.img", 1, 1)
ds.SetProjection(sr.ExportToWkt())
ds = None
ds = gdal.Open('/vsimem/TestHFASRS.img')
ds = gdal.Open("/vsimem/TestHFASRS.img")
wkt = ds.GetProjectionRef()
assert ds.GetSpatialRef().GetAuthorityCode(None) == '26915'
assert ds.GetSpatialRef().GetAuthorityCode(None) == "26915"
ds = None
gdal.Unlink('/vsimem/TestHFASRS.img')
gdal.Unlink("/vsimem/TestHFASRS.img")
assert 'TOWGS84' not in wkt
assert "TOWGS84" not in wkt
def test_hfa_srs_NAD83_CORS96_UTM():
sr = osr.SpatialReference()
sr.SetFromUserInput('PROJCS["NAD_1983_CORS96_UTM_Zone_11N",GEOGCS["NAD83(CORS96)",DATUM["NAD83_Continuously_Operating_Reference_Station_1996",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],AUTHORITY["EPSG","1133"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","6783"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-117],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["ESRI","102411"]]')
sr.SetFromUserInput(
'PROJCS["NAD_1983_CORS96_UTM_Zone_11N",GEOGCS["NAD83(CORS96)",DATUM["NAD83_Continuously_Operating_Reference_Station_1996",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],AUTHORITY["EPSG","1133"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","6783"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-117],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["ESRI","102411"]]'
)
ds = gdal.GetDriverByName('HFA').Create('/vsimem/TestHFASRS.img', 1, 1)
ds = gdal.GetDriverByName("HFA").Create("/vsimem/TestHFASRS.img", 1, 1)
ds.SetProjection(sr.ExportToWkt())
ds = None
ds = gdal.Open('/vsimem/TestHFASRS.img')
ds = gdal.Open("/vsimem/TestHFASRS.img")
srs_got = ds.GetSpatialRef()
assert srs_got.GetAuthorityName(None) is None
assert srs_got.IsSame(sr), srs_got.ExportToWkt()
ds = None
gdal.Unlink('/vsimem/TestHFASRS.img')
gdal.Unlink("/vsimem/TestHFASRS.img")

View File

@ -33,79 +33,79 @@
import os
import shutil
import gdaltest
import pytest
from osgeo import gdal, osr
import gdaltest
###############################################################################
# test that we can write a small file with a custom layer name.
def test_hfa_write_desc():
src_ds = gdal.Open('data/byte.tif')
src_ds = gdal.Open("data/byte.tif")
new_ds = gdal.GetDriverByName('HFA').CreateCopy('tmp/test_desc.img',
src_ds)
new_ds = gdal.GetDriverByName("HFA").CreateCopy("tmp/test_desc.img", src_ds)
bnd = new_ds.GetRasterBand(1)
bnd.SetDescription('CustomBandName')
bnd.SetDescription("CustomBandName")
bnd = None
src_ds = None
new_ds = None
new_ds = gdal.Open('tmp/test_desc.img')
new_ds = gdal.Open("tmp/test_desc.img")
bnd = new_ds.GetRasterBand(1)
assert bnd.GetDescription() == 'CustomBandName', 'Didnt get custom band name.'
assert bnd.GetDescription() == "CustomBandName", "Didnt get custom band name."
bnd = None
new_ds = None
gdal.GetDriverByName('HFA').Delete('tmp/test_desc.img')
gdal.GetDriverByName("HFA").Delete("tmp/test_desc.img")
###############################################################################
# test writing 4 bit files.
def test_hfa_write_4bit():
drv = gdal.GetDriverByName('HFA')
src_ds = gdal.Open('data/byte.tif')
ds = drv.CreateCopy('tmp/4bit.img', src_ds, options=['NBITS=1'])
drv = gdal.GetDriverByName("HFA")
src_ds = gdal.Open("data/byte.tif")
ds = drv.CreateCopy("tmp/4bit.img", src_ds, options=["NBITS=1"])
ds = None
src_ds = None
ds = gdal.Open('tmp/4bit.img')
ds = gdal.Open("tmp/4bit.img")
cs = ds.GetRasterBand(1).Checksum()
assert cs == 252, 'Got wrong checksum on 4bit image.'
assert cs == 252, "Got wrong checksum on 4bit image."
ds = None
drv.Delete('tmp/4bit.img')
drv.Delete("tmp/4bit.img")
###############################################################################
# test writing 4 bit files compressed.
def test_hfa_write_4bit_compressed():
drv = gdal.GetDriverByName('HFA')
src_ds = gdal.Open('data/byte.tif')
ds = drv.CreateCopy('tmp/4bitc.img', src_ds,
options=['NBITS=1', 'COMPRESSED=YES'])
drv = gdal.GetDriverByName("HFA")
src_ds = gdal.Open("data/byte.tif")
ds = drv.CreateCopy("tmp/4bitc.img", src_ds, options=["NBITS=1", "COMPRESSED=YES"])
ds = None
src_ds = None
ds = gdal.Open('tmp/4bitc.img')
ds = gdal.Open("tmp/4bitc.img")
cs = ds.GetRasterBand(1).Checksum()
assert cs == 252, 'Got wrong checksum on 4bit image.'
assert cs == 252, "Got wrong checksum on 4bit image."
ds = None
drv.Delete('tmp/4bitc.img')
drv.Delete("tmp/4bitc.img")
###############################################################################
# Test creating a file with a nodata value, and fetching otherwise unread
@ -114,19 +114,20 @@ def test_hfa_write_4bit_compressed():
def test_hfa_write_nd_invalid():
drv = gdal.GetDriverByName('HFA')
ds = drv.Create('tmp/ndinvalid.img', 512, 512, 1, gdal.GDT_Byte, [])
drv = gdal.GetDriverByName("HFA")
ds = drv.Create("tmp/ndinvalid.img", 512, 512, 1, gdal.GDT_Byte, [])
ds.GetRasterBand(1).SetNoDataValue(200)
ds = None
ds = gdal.Open('tmp/ndinvalid.img')
ds = gdal.Open("tmp/ndinvalid.img")
cs = ds.GetRasterBand(1).Checksum()
assert cs == 29754, 'Got wrong checksum on invalid image.'
assert cs == 29754, "Got wrong checksum on invalid image."
ds = None
drv.Delete('tmp/ndinvalid.img')
drv.Delete("tmp/ndinvalid.img")
###############################################################################
# Test updating .rrd overviews in place (#2524).
@ -134,51 +135,123 @@ def test_hfa_write_nd_invalid():
def test_hfa_update_overviews():
shutil.copyfile('data/small_ov.img', 'tmp/small.img')
shutil.copyfile('data/small_ov.rrd', 'tmp/small.rrd')
shutil.copyfile("data/small_ov.img", "tmp/small.img")
shutil.copyfile("data/small_ov.rrd", "tmp/small.rrd")
ds = gdal.Open('tmp/small.img', gdal.GA_Update)
ds = gdal.Open("tmp/small.img", gdal.GA_Update)
result = ds.BuildOverviews(overviewlist=[2])
assert result == 0, 'BuildOverviews() failed.'
assert result == 0, "BuildOverviews() failed."
ds = None
###############################################################################
# Test cleaning external overviews.
def test_hfa_clean_external_overviews():
ds = gdal.Open('tmp/small.img', gdal.GA_Update)
ds = gdal.Open("tmp/small.img", gdal.GA_Update)
result = ds.BuildOverviews(overviewlist=[])
assert result == 0, 'BuildOverviews() failed.'
assert result == 0, "BuildOverviews() failed."
assert ds.GetRasterBand(1).GetOverviewCount() == 0, 'Overviews still exist.'
assert ds.GetRasterBand(1).GetOverviewCount() == 0, "Overviews still exist."
ds = None
ds = gdal.Open('tmp/small.img')
assert ds.GetRasterBand(1).GetOverviewCount() == 0, 'Overviews still exist.'
ds = gdal.Open("tmp/small.img")
assert ds.GetRasterBand(1).GetOverviewCount() == 0, "Overviews still exist."
ds = None
assert not os.path.exists('tmp/small.rrd')
assert not os.path.exists("tmp/small.rrd")
gdal.GetDriverByName("HFA").Delete("tmp/small.img")
gdal.GetDriverByName('HFA').Delete('tmp/small.img')
###############################################################################
# Test writing high frequency data (#2525).
def test_hfa_bug_2525():
drv = gdal.GetDriverByName('HFA')
ds = drv.Create('tmp/test_hfa.img', 64, 64, 1, gdal.GDT_UInt16, options=['COMPRESSED=YES'])
drv = gdal.GetDriverByName("HFA")
ds = drv.Create(
"tmp/test_hfa.img", 64, 64, 1, gdal.GDT_UInt16, options=["COMPRESSED=YES"]
)
import struct
data = struct.pack('H' * 64, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535, 0, 65535)
data = struct.pack(
"H" * 64,
0,
65535,
0,
65535,
0,
65535,
0,
65535,
0,
65535,
0,
65535,
0,
65535,
0,
65535,
0,
65535,
0,
65535,
0,
65535,
0,
65535,
0,
65535,
0,
65535,
0,
65535,
0,
65535,
0,
65535,
0,
65535,
0,
65535,
0,
65535,
0,
65535,
0,
65535,
0,
65535,
0,
65535,
0,
65535,
0,
65535,
0,
65535,
0,
65535,
0,
65535,
0,
65535,
0,
65535,
0,
65535,
)
for i in range(64):
ds.GetRasterBand(1).WriteRaster(0, i, 64, 1, data)
ds = None
drv.Delete('tmp/test_hfa.img')
drv.Delete("tmp/test_hfa.img")
###############################################################################
# Test building external overviews with HFA_USE_RRD=YES
@ -186,102 +259,107 @@ def test_hfa_bug_2525():
def test_hfa_use_rrd():
shutil.copyfile('data/small_ov.img', 'tmp/small.img')
shutil.copyfile("data/small_ov.img", "tmp/small.img")
old_value = gdal.GetConfigOption('HFA_USE_RRD', 'NO')
gdal.SetConfigOption('HFA_USE_RRD', 'YES')
ds = gdal.Open('tmp/small.img', gdal.GA_Update)
old_value = gdal.GetConfigOption("HFA_USE_RRD", "NO")
gdal.SetConfigOption("HFA_USE_RRD", "YES")
ds = gdal.Open("tmp/small.img", gdal.GA_Update)
result = ds.BuildOverviews(overviewlist=[2])
gdal.SetConfigOption('HFA_USE_RRD', old_value)
gdal.SetConfigOption("HFA_USE_RRD", old_value)
assert result == 0, 'BuildOverviews() failed.'
assert result == 0, "BuildOverviews() failed."
ds = None
try:
os.stat('tmp/small.rrd')
os.stat("tmp/small.rrd")
except OSError:
pytest.fail('small.rrd not present.')
pytest.fail("small.rrd not present.")
ds = gdal.Open('tmp/small.img')
assert ds.GetRasterBand(1).GetOverview(0).Checksum() == 26148, \
'Unexpected checksum.'
ds = gdal.Open("tmp/small.img")
assert (
ds.GetRasterBand(1).GetOverview(0).Checksum() == 26148
), "Unexpected checksum."
ds = None
gdal.GetDriverByName('HFA').Delete('tmp/small.img')
gdal.GetDriverByName("HFA").Delete("tmp/small.img")
###############################################################################
# Test fix for #4831
def test_hfa_update_existing_aux_overviews():
gdal.SetConfigOption('USE_RRD', 'YES')
gdal.SetConfigOption("USE_RRD", "YES")
ds = gdal.GetDriverByName('BMP').Create('tmp/hfa_update_existing_aux_overviews.bmp', 100, 100, 1)
ds = gdal.GetDriverByName("BMP").Create(
"tmp/hfa_update_existing_aux_overviews.bmp", 100, 100, 1
)
ds.GetRasterBand(1).Fill(255)
ds = None
# Create overviews
ds = gdal.Open('tmp/hfa_update_existing_aux_overviews.bmp')
ds.BuildOverviews('NEAR', overviewlist=[2, 4])
ds = gdal.Open("tmp/hfa_update_existing_aux_overviews.bmp")
ds.BuildOverviews("NEAR", overviewlist=[2, 4])
ds = None
# Save overviews checksum
ds = gdal.Open('tmp/hfa_update_existing_aux_overviews.bmp')
ds = gdal.Open("tmp/hfa_update_existing_aux_overviews.bmp")
cs_ovr0 = ds.GetRasterBand(1).GetOverview(0).Checksum()
cs_ovr1 = ds.GetRasterBand(1).GetOverview(1).Checksum()
# and regenerate them
ds.BuildOverviews('NEAR', overviewlist=[2, 4])
ds.BuildOverviews("NEAR", overviewlist=[2, 4])
ds = None
ds = gdal.Open('tmp/hfa_update_existing_aux_overviews.bmp')
ds = gdal.Open("tmp/hfa_update_existing_aux_overviews.bmp")
# Check overviews checksum
new_cs_ovr0 = ds.GetRasterBand(1).GetOverview(0).Checksum()
new_cs_ovr1 = ds.GetRasterBand(1).GetOverview(1).Checksum()
if cs_ovr0 != new_cs_ovr0:
gdal.SetConfigOption('USE_RRD', None)
gdal.SetConfigOption("USE_RRD", None)
pytest.fail()
if cs_ovr1 != new_cs_ovr1:
gdal.SetConfigOption('USE_RRD', None)
gdal.SetConfigOption("USE_RRD", None)
pytest.fail()
# and regenerate them twice in a row
ds.BuildOverviews('NEAR', overviewlist=[2, 4])
ds.BuildOverviews('NEAR', overviewlist=[2, 4])
ds.BuildOverviews("NEAR", overviewlist=[2, 4])
ds.BuildOverviews("NEAR", overviewlist=[2, 4])
ds = None
ds = gdal.Open('tmp/hfa_update_existing_aux_overviews.bmp')
ds = gdal.Open("tmp/hfa_update_existing_aux_overviews.bmp")
# Check overviews checksum
new_cs_ovr0 = ds.GetRasterBand(1).GetOverview(0).Checksum()
new_cs_ovr1 = ds.GetRasterBand(1).GetOverview(1).Checksum()
if cs_ovr0 != new_cs_ovr0:
gdal.SetConfigOption('USE_RRD', None)
gdal.SetConfigOption("USE_RRD", None)
pytest.fail()
if cs_ovr1 != new_cs_ovr1:
gdal.SetConfigOption('USE_RRD', None)
gdal.SetConfigOption("USE_RRD", None)
pytest.fail()
# and regenerate them with an extra overview level
ds.BuildOverviews('NEAR', overviewlist=[8])
ds.BuildOverviews("NEAR", overviewlist=[8])
ds = None
ds = gdal.Open('tmp/hfa_update_existing_aux_overviews.bmp')
ds = gdal.Open("tmp/hfa_update_existing_aux_overviews.bmp")
# Check overviews checksum
new_cs_ovr0 = ds.GetRasterBand(1).GetOverview(0).Checksum()
new_cs_ovr1 = ds.GetRasterBand(1).GetOverview(1).Checksum()
if cs_ovr0 != new_cs_ovr0:
gdal.SetConfigOption('USE_RRD', None)
gdal.SetConfigOption("USE_RRD", None)
pytest.fail()
if cs_ovr1 != new_cs_ovr1:
gdal.SetConfigOption('USE_RRD', None)
gdal.SetConfigOption("USE_RRD", None)
pytest.fail()
ds = None
gdal.GetDriverByName('BMP').Delete('tmp/hfa_update_existing_aux_overviews.bmp')
gdal.GetDriverByName("BMP").Delete("tmp/hfa_update_existing_aux_overviews.bmp")
gdal.SetConfigOption("USE_RRD", None)
gdal.SetConfigOption('USE_RRD', None)
###############################################################################
# Test writing invalid WKT (#5258)
@ -290,8 +368,9 @@ def test_hfa_update_existing_aux_overviews():
def test_hfa_write_invalid_wkt():
# No GEOGCS
ds = gdal.GetDriverByName('HFA').Create('/vsimem/hfa_write_invalid_wkt.img', 1, 1)
ds.SetProjection("""PROJCS["NAD27 / UTM zone 11N",
ds = gdal.GetDriverByName("HFA").Create("/vsimem/hfa_write_invalid_wkt.img", 1, 1)
ds.SetProjection(
"""PROJCS["NAD27 / UTM zone 11N",
PROJECTION["Transverse_Mercator"],
PARAMETER["latitude_of_origin",0],
PARAMETER["central_meridian",-117],
@ -300,12 +379,14 @@ def test_hfa_write_invalid_wkt():
PARAMETER["false_northing",0],
UNIT["metre",1,
AUTHORITY["EPSG","9001"]],
AUTHORITY["EPSG","26711"]]""")
AUTHORITY["EPSG","26711"]]"""
)
ds = None
# No DATUM in GEOGCS
ds = gdal.GetDriverByName('HFA').Create('/vsimem/hfa_write_invalid_wkt.img', 1, 1)
ds.SetProjection("""PROJCS["NAD27 / UTM zone 11N",
ds = gdal.GetDriverByName("HFA").Create("/vsimem/hfa_write_invalid_wkt.img", 1, 1)
ds.SetProjection(
"""PROJCS["NAD27 / UTM zone 11N",
GEOGCS["NAD27",
AUTHORITY["EPSG","4267"]],
PROJECTION["Transverse_Mercator"],
@ -316,12 +397,14 @@ def test_hfa_write_invalid_wkt():
PARAMETER["false_northing",0],
UNIT["metre",1,
AUTHORITY["EPSG","9001"]],
AUTHORITY["EPSG","26711"]]""")
AUTHORITY["EPSG","26711"]]"""
)
ds = None
# No SPHEROID in DATUM
ds = gdal.GetDriverByName('HFA').Create('/vsimem/hfa_write_invalid_wkt.img', 1, 1)
ds.SetProjection("""PROJCS["NAD27 / UTM zone 11N",
ds = gdal.GetDriverByName("HFA").Create("/vsimem/hfa_write_invalid_wkt.img", 1, 1)
ds.SetProjection(
"""PROJCS["NAD27 / UTM zone 11N",
GEOGCS["NAD27",
DATUM["North_American_Datum_1927",
AUTHORITY["EPSG","6267"]],
@ -336,104 +419,113 @@ def test_hfa_write_invalid_wkt():
PARAMETER["false_northing",0],
UNIT["metre",1,
AUTHORITY["EPSG","9001"]],
AUTHORITY["EPSG","26711"]]""")
AUTHORITY["EPSG","26711"]]"""
)
ds = None
gdal.GetDriverByName('HFA').Delete('/vsimem/hfa_write_invalid_wkt.img')
gdal.GetDriverByName("HFA").Delete("/vsimem/hfa_write_invalid_wkt.img")
###############################################################################
# Get the driver, and verify a few things about it.
init_list = [
('byte.tif', 4672),
('int16.tif', 4672),
('uint16.tif', 4672),
('int32.tif', 4672),
('uint32.tif', 4672),
('float32.tif', 4672),
('float64.tif', 4672),
('cfloat32.tif', 5028),
('cfloat64.tif', 5028),
('utmsmall.tif', 50054)]
("byte.tif", 4672),
("int16.tif", 4672),
("uint16.tif", 4672),
("int32.tif", 4672),
("uint32.tif", 4672),
("float32.tif", 4672),
("float64.tif", 4672),
("cfloat32.tif", 5028),
("cfloat64.tif", 5028),
("utmsmall.tif", 50054),
]
# full set of tests for normal mode.
@pytest.mark.parametrize(
'filename,checksum',
"filename,checksum",
init_list,
ids=[tup[0].split('.')[0] for tup in init_list],
ids=[tup[0].split(".")[0] for tup in init_list],
)
@pytest.mark.parametrize(
'testfunction', [
'testCreateCopy',
'testCreate',
'testSetGeoTransform',
'testSetMetadata',
]
"testfunction",
[
"testCreateCopy",
"testCreate",
"testSetGeoTransform",
"testSetMetadata",
],
)
@pytest.mark.require_driver('HFA')
@pytest.mark.require_driver("HFA")
def test_hfa_create_normal(filename, checksum, testfunction):
ut = gdaltest.GDALTest('HFA', filename, 1, checksum)
ut = gdaltest.GDALTest("HFA", filename, 1, checksum)
getattr(ut, testfunction)()
# Just a few for spill file, and compressed support.
short_list = [
('byte.tif', 4672),
('uint16.tif', 4672),
('float64.tif', 4672)]
short_list = [("byte.tif", 4672), ("uint16.tif", 4672), ("float64.tif", 4672)]
@pytest.mark.parametrize(
'filename,checksum',
"filename,checksum",
short_list,
ids=[tup[0].split('.')[0] for tup in short_list],
ids=[tup[0].split(".")[0] for tup in short_list],
)
@pytest.mark.parametrize(
'testfunction', [
'testCreateCopy',
'testCreate',
]
"testfunction",
[
"testCreateCopy",
"testCreate",
],
)
@pytest.mark.require_driver('HFA')
@pytest.mark.require_driver("HFA")
def test_hfa_create_spill(filename, checksum, testfunction):
ut = gdaltest.GDALTest('HFA', filename, 1, checksum, options=['USE_SPILL=YES'])
ut = gdaltest.GDALTest("HFA", filename, 1, checksum, options=["USE_SPILL=YES"])
getattr(ut, testfunction)()
@pytest.mark.parametrize(
'filename,checksum',
"filename,checksum",
short_list,
ids=[tup[0].split('.')[0] for tup in short_list],
ids=[tup[0].split(".")[0] for tup in short_list],
)
@pytest.mark.parametrize(
'testfunction', [
"testfunction",
[
# 'testCreateCopy',
'testCreate',
]
"testCreate",
],
)
@pytest.mark.require_driver('HFA')
@pytest.mark.require_driver("HFA")
def test_hfa_create_compress(filename, checksum, testfunction):
ut = gdaltest.GDALTest('HFA', filename, 1, checksum, options=['COMPRESS=YES'])
ut = gdaltest.GDALTest("HFA", filename, 1, checksum, options=["COMPRESS=YES"])
getattr(ut, testfunction)()
def test_hfa_create_compress_big_block():
src_ds = gdal.GetDriverByName('MEM').Create('/vsimem/big_block.img', 128, 128, 1, gdal.GDT_UInt32)
src_ds = gdal.GetDriverByName("MEM").Create(
"/vsimem/big_block.img", 128, 128, 1, gdal.GDT_UInt32
)
src_ds.GetRasterBand(1).Fill(4 * 1000 * 1000 * 1000)
src_ds.GetRasterBand(1).WriteRaster(0,0,1,1,b'\0')
gdal.GetDriverByName('HFA').CreateCopy('/vsimem/big_block.img', src_ds, options=['COMPRESS=YES', 'BLOCKSIZE=128'])
ds = gdal.Open('/vsimem/big_block.img')
src_ds.GetRasterBand(1).WriteRaster(0, 0, 1, 1, b"\0")
gdal.GetDriverByName("HFA").CreateCopy(
"/vsimem/big_block.img", src_ds, options=["COMPRESS=YES", "BLOCKSIZE=128"]
)
ds = gdal.Open("/vsimem/big_block.img")
got_data = ds.GetRasterBand(1).ReadRaster()
ds = None
gdal.Unlink('/vsimem/big_block.img')
gdal.Unlink("/vsimem/big_block.img")
assert got_data == src_ds.GetRasterBand(1).ReadRaster()
# GCPs go to PAM currently
def test_hfa_create_gcp():
filename = '/vsimem/test.img'
ds = gdal.GetDriverByName('HFA').Create(filename, 1, 1)
filename = "/vsimem/test.img"
ds = gdal.GetDriverByName("HFA").Create(filename, 1, 1)
gcp1 = gdal.GCP()
gcp1.GCPPixel = 0
gcp1.GCPLine = 0
@ -441,11 +533,11 @@ def test_hfa_create_gcp():
gcp1.GCPY = 3751320.000
sr = osr.SpatialReference()
sr.ImportFromEPSG(4326)
assert ds.SetGCPs((gcp1, ), sr.ExportToWkt()) == gdal.CE_None
assert ds.SetGCPs((gcp1,), sr.ExportToWkt()) == gdal.CE_None
ds = None
ds = gdal.Open(filename)
assert ds.GetGCPCount() == 1
assert ds.GetGCPSpatialRef() is not None
assert len(ds.GetGCPs()) == 1
ds = None
gdal.GetDriverByName('HFA').Delete(filename)
gdal.GetDriverByName("HFA").Delete(filename)

View File

@ -35,24 +35,281 @@ import shutil
import struct
import sys
from osgeo import gdal
import gdaltest
import pytest
from osgeo import gdal
###############################################################################
# Fetch simple histogram.
def test_histogram_1():
ds = gdal.Open('data/utmsmall.tif')
ds = gdal.Open("data/utmsmall.tif")
hist = ds.GetRasterBand(1).GetHistogram()
exp_hist = [2, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, 0, 0, 0, 0, 46, 0, 0, 0, 0, 0, 0, 0, 69, 0, 0, 0, 0, 0, 0, 0, 99, 0, 0, 0, 0, 0, 0, 0, 0, 120, 0, 0, 0, 0, 0, 0, 0, 178, 0, 0, 0, 0, 0, 0, 0, 193, 0, 0, 0, 0, 0, 0, 0, 212, 0, 0, 0, 0, 0, 0, 0, 281, 0, 0, 0, 0, 0, 0, 0, 0, 365, 0, 0, 0, 0, 0, 0, 0, 460, 0, 0, 0, 0, 0, 0, 0, 533, 0, 0, 0, 0, 0, 0, 0, 544, 0, 0, 0, 0, 0, 0, 0, 0, 626, 0, 0, 0, 0, 0, 0, 0, 653, 0, 0, 0, 0, 0, 0, 0, 673, 0, 0, 0, 0, 0, 0, 0, 629, 0, 0, 0, 0, 0, 0, 0, 0, 586, 0, 0, 0, 0, 0, 0, 0, 541, 0, 0, 0, 0, 0, 0, 0, 435, 0, 0, 0, 0, 0, 0, 0, 348, 0, 0, 0, 0, 0, 0, 0, 341, 0, 0, 0, 0, 0, 0, 0, 0, 284, 0, 0, 0, 0, 0, 0, 0, 225, 0, 0, 0, 0, 0, 0, 0, 237, 0, 0, 0, 0, 0, 0, 0, 172, 0, 0, 0, 0, 0, 0, 0, 0, 159, 0, 0, 0, 0, 0, 0, 0, 105, 0, 0, 0, 0, 0, 0, 0, 824]
exp_hist = [
2,
0,
0,
0,
0,
0,
0,
0,
8,
0,
0,
0,
0,
0,
0,
0,
23,
0,
0,
0,
0,
0,
0,
0,
0,
29,
0,
0,
0,
0,
0,
0,
0,
46,
0,
0,
0,
0,
0,
0,
0,
69,
0,
0,
0,
0,
0,
0,
0,
99,
0,
0,
0,
0,
0,
0,
0,
0,
120,
0,
0,
0,
0,
0,
0,
0,
178,
0,
0,
0,
0,
0,
0,
0,
193,
0,
0,
0,
0,
0,
0,
0,
212,
0,
0,
0,
0,
0,
0,
0,
281,
0,
0,
0,
0,
0,
0,
0,
0,
365,
0,
0,
0,
0,
0,
0,
0,
460,
0,
0,
0,
0,
0,
0,
0,
533,
0,
0,
0,
0,
0,
0,
0,
544,
0,
0,
0,
0,
0,
0,
0,
0,
626,
0,
0,
0,
0,
0,
0,
0,
653,
0,
0,
0,
0,
0,
0,
0,
673,
0,
0,
0,
0,
0,
0,
0,
629,
0,
0,
0,
0,
0,
0,
0,
0,
586,
0,
0,
0,
0,
0,
0,
0,
541,
0,
0,
0,
0,
0,
0,
0,
435,
0,
0,
0,
0,
0,
0,
0,
348,
0,
0,
0,
0,
0,
0,
0,
341,
0,
0,
0,
0,
0,
0,
0,
0,
284,
0,
0,
0,
0,
0,
0,
0,
225,
0,
0,
0,
0,
0,
0,
0,
237,
0,
0,
0,
0,
0,
0,
0,
172,
0,
0,
0,
0,
0,
0,
0,
0,
159,
0,
0,
0,
0,
0,
0,
0,
105,
0,
0,
0,
0,
0,
0,
0,
824,
]
assert hist == exp_hist, "did not get expected histogram."
assert hist == exp_hist, 'did not get expected histogram.'
###############################################################################
# Fetch histogram with specified sampling, using keywords.
@ -60,12 +317,30 @@ def test_histogram_1():
def test_histogram_2():
ds = gdal.Open('data/utmsmall.tif')
ds = gdal.Open("data/utmsmall.tif")
hist = ds.GetRasterBand(1).GetHistogram(buckets=16, max=255.5, min=-0.5)
exp_hist = [10, 52, 115, 219, 371, 493, 825, 1077, 1279, 1302, 1127, 783, 625, 462, 331, 929]
exp_hist = [
10,
52,
115,
219,
371,
493,
825,
1077,
1279,
1302,
1127,
783,
625,
462,
331,
929,
]
assert hist == exp_hist, "did not get expected histogram."
assert hist == exp_hist, 'did not get expected histogram.'
###############################################################################
# try on a different data type with out of range values included.
@ -73,14 +348,15 @@ def test_histogram_2():
def test_histogram_3():
ds = gdal.Open('data/int32_withneg.grd')
hist = ds.GetRasterBand(1).GetHistogram(buckets=21, max=100, min=-100,
include_out_of_range=1,
approx_ok=0)
ds = gdal.Open("data/int32_withneg.grd")
hist = ds.GetRasterBand(1).GetHistogram(
buckets=21, max=100, min=-100, include_out_of_range=1, approx_ok=0
)
exp_hist = [0, 0, 0, 0, 0, 1, 0, 1, 1, 3, 3, 2, 0, 5, 3, 4, 0, 1, 1, 2, 3]
assert hist == exp_hist, 'did not get expected histogram.'
assert hist == exp_hist, "did not get expected histogram."
###############################################################################
# try on a different data type without out of range values included.
@ -88,18 +364,19 @@ def test_histogram_3():
def test_histogram_4():
ds = gdal.Open('data/int32_withneg.grd')
hist = ds.GetRasterBand(1).GetHistogram(buckets=21, max=100, min=-100,
include_out_of_range=0,
approx_ok=0)
ds = gdal.Open("data/int32_withneg.grd")
hist = ds.GetRasterBand(1).GetHistogram(
buckets=21, max=100, min=-100, include_out_of_range=0, approx_ok=0
)
exp_hist = [0, 0, 0, 0, 0, 1, 0, 1, 1, 3, 3, 2, 0, 5, 3, 4, 0, 1, 1, 2, 0]
assert hist == exp_hist, 'did not get expected histogram.'
assert hist == exp_hist, "did not get expected histogram."
ds = None
gdal.Unlink('data/int32_withneg.grd.aux.xml')
gdal.Unlink("data/int32_withneg.grd.aux.xml")
###############################################################################
# Test GetDefaultHistogram() on the file.
@ -107,16 +384,279 @@ def test_histogram_4():
def test_histogram_5():
ds = gdal.Open('data/utmsmall.tif')
ds = gdal.Open("data/utmsmall.tif")
hist = ds.GetRasterBand(1).GetDefaultHistogram(force=1)
exp_hist = (-0.5, 255.5, 256, [2, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 23, 0, 0, 0, 0, 0, 0, 0, 0, 29, 0, 0, 0, 0, 0, 0, 0, 46, 0, 0, 0, 0, 0, 0, 0, 69, 0, 0, 0, 0, 0, 0, 0, 99, 0, 0, 0, 0, 0, 0, 0, 0, 120, 0, 0, 0, 0, 0, 0, 0, 178, 0, 0, 0, 0, 0, 0, 0, 193, 0, 0, 0, 0, 0, 0, 0, 212, 0, 0, 0, 0, 0, 0, 0, 281, 0, 0, 0, 0, 0, 0, 0, 0, 365, 0, 0, 0, 0, 0, 0, 0, 460, 0, 0, 0, 0, 0, 0, 0, 533, 0, 0, 0, 0, 0, 0, 0, 544, 0, 0, 0, 0, 0, 0, 0, 0, 626, 0, 0, 0, 0, 0, 0, 0, 653, 0, 0, 0, 0, 0, 0, 0, 673, 0, 0, 0, 0, 0, 0, 0, 629, 0, 0, 0, 0, 0, 0, 0, 0, 586, 0, 0, 0, 0, 0, 0, 0, 541, 0, 0, 0, 0, 0, 0, 0, 435, 0, 0, 0, 0, 0, 0, 0, 348, 0, 0, 0, 0, 0, 0, 0, 341, 0, 0, 0, 0, 0, 0, 0, 0, 284, 0, 0, 0, 0, 0, 0, 0, 225, 0, 0, 0, 0, 0, 0, 0, 237, 0, 0, 0, 0, 0, 0, 0, 172, 0, 0, 0, 0, 0, 0, 0, 0, 159, 0, 0, 0, 0, 0, 0, 0, 105, 0, 0, 0, 0, 0, 0, 0, 824])
exp_hist = (
-0.5,
255.5,
256,
[
2,
0,
0,
0,
0,
0,
0,
0,
8,
0,
0,
0,
0,
0,
0,
0,
23,
0,
0,
0,
0,
0,
0,
0,
0,
29,
0,
0,
0,
0,
0,
0,
0,
46,
0,
0,
0,
0,
0,
0,
0,
69,
0,
0,
0,
0,
0,
0,
0,
99,
0,
0,
0,
0,
0,
0,
0,
0,
120,
0,
0,
0,
0,
0,
0,
0,
178,
0,
0,
0,
0,
0,
0,
0,
193,
0,
0,
0,
0,
0,
0,
0,
212,
0,
0,
0,
0,
0,
0,
0,
281,
0,
0,
0,
0,
0,
0,
0,
0,
365,
0,
0,
0,
0,
0,
0,
0,
460,
0,
0,
0,
0,
0,
0,
0,
533,
0,
0,
0,
0,
0,
0,
0,
544,
0,
0,
0,
0,
0,
0,
0,
0,
626,
0,
0,
0,
0,
0,
0,
0,
653,
0,
0,
0,
0,
0,
0,
0,
673,
0,
0,
0,
0,
0,
0,
0,
629,
0,
0,
0,
0,
0,
0,
0,
0,
586,
0,
0,
0,
0,
0,
0,
0,
541,
0,
0,
0,
0,
0,
0,
0,
435,
0,
0,
0,
0,
0,
0,
0,
348,
0,
0,
0,
0,
0,
0,
0,
341,
0,
0,
0,
0,
0,
0,
0,
0,
284,
0,
0,
0,
0,
0,
0,
0,
225,
0,
0,
0,
0,
0,
0,
0,
237,
0,
0,
0,
0,
0,
0,
0,
172,
0,
0,
0,
0,
0,
0,
0,
0,
159,
0,
0,
0,
0,
0,
0,
0,
105,
0,
0,
0,
0,
0,
0,
0,
824,
],
)
assert hist == exp_hist, 'did not get expected histogram.'
assert hist == exp_hist, "did not get expected histogram."
ds = None
gdal.Unlink('data/utmsmall.tif.aux.xml')
gdal.Unlink("data/utmsmall.tif.aux.xml")
###############################################################################
# Test GetDefaultHistogram( force = 0 ) on a JPG file (#3304)
@ -124,12 +664,12 @@ def test_histogram_5():
def test_histogram_6():
shutil.copy('../gdrivers/data/jpeg/albania.jpg', 'tmp/albania.jpg')
ds = gdal.Open('tmp/albania.jpg')
shutil.copy("../gdrivers/data/jpeg/albania.jpg", "tmp/albania.jpg")
ds = gdal.Open("tmp/albania.jpg")
hist = ds.GetRasterBand(1).GetDefaultHistogram(force=0)
assert hist is None, 'did not get expected histogram.'
assert hist is None, "did not get expected histogram."
ds = None
os.unlink('tmp/albania.jpg')
os.unlink("tmp/albania.jpg")
###############################################################################
@ -138,18 +678,18 @@ def test_histogram_6():
def test_histogram_inf_values():
ds = gdal.GetDriverByName('MEM').Create('', 1, 1, 1, gdal.GDT_Float64)
ds.WriteRaster(0, 0, 1, 1, struct.pack('d', math.inf))
hist = ds.GetRasterBand(1).GetHistogram(buckets=2, min=-0.5, max=1.5,
include_out_of_range=1,
approx_ok=0)
ds = gdal.GetDriverByName("MEM").Create("", 1, 1, 1, gdal.GDT_Float64)
ds.WriteRaster(0, 0, 1, 1, struct.pack("d", math.inf))
hist = ds.GetRasterBand(1).GetHistogram(
buckets=2, min=-0.5, max=1.5, include_out_of_range=1, approx_ok=0
)
assert hist == [0, 1]
ds = gdal.GetDriverByName('MEM').Create('', 1, 1, 1, gdal.GDT_Float64)
ds.WriteRaster(0, 0, 1, 1, struct.pack('d', -math.inf))
hist = ds.GetRasterBand(1).GetHistogram(buckets=2, min=-0.5, max=1.5,
include_out_of_range=1,
approx_ok=0)
ds = gdal.GetDriverByName("MEM").Create("", 1, 1, 1, gdal.GDT_Float64)
ds.WriteRaster(0, 0, 1, 1, struct.pack("d", -math.inf))
hist = ds.GetRasterBand(1).GetHistogram(
buckets=2, min=-0.5, max=1.5, include_out_of_range=1, approx_ok=0
)
assert hist == [1, 0]
@ -159,39 +699,44 @@ def test_histogram_inf_values():
def test_histogram_errors():
ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
# Invalid bucket count
with pytest.raises(Exception):
ds.GetRasterBand(1).GetHistogram(buckets=0, min=-0.5, max=1.5,
include_out_of_range=1,
approx_ok=0)
ds.GetRasterBand(1).GetHistogram(
buckets=0, min=-0.5, max=1.5, include_out_of_range=1, approx_ok=0
)
###############################################################################
# Test GetHistogram() invalid min max bounds
@pytest.mark.parametrize('min,max', [[math.nan, 1.5],
[-math.inf, 1.5],
[math.inf, 1.5],
[-0.5,math.nan],
[-0.5,-math.inf],
[-0.5,math.inf],
[-math.inf,math.inf],
[-sys.float_info.max,sys.float_info.max], # leads to dfScale == 0
[0,0]])
@pytest.mark.parametrize(
"min,max",
[
[math.nan, 1.5],
[-math.inf, 1.5],
[math.inf, 1.5],
[-0.5, math.nan],
[-0.5, -math.inf],
[-0.5, math.inf],
[-math.inf, math.inf],
[-sys.float_info.max, sys.float_info.max], # leads to dfScale == 0
[0, 0],
],
)
def test_histogram_invalid_min_max(min, max):
ds = gdal.GetDriverByName('MEM').Create('', 1, 1)
ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
with gdaltest.error_handler():
gdal.ErrorReset()
ret = ds.GetRasterBand(1).GetHistogram(buckets=2, min=min, max=max,
include_out_of_range=1,
approx_ok=0)
ret = ds.GetRasterBand(1).GetHistogram(
buckets=2, min=min, max=max, include_out_of_range=1, approx_ok=0
)
if (min, max) == (-sys.float_info.max, sys.float_info.max) and ret == [1, 0]:
# Happens on i386 since 2. / (sys.float_info.max - -sys.float_info.max) == 5.56268464626800346e-309
# when using i387 coprocessor (long double)
pass
else:
ret == [0, 0]
assert gdal.GetLastErrorMsg() != ''
assert gdal.GetLastErrorMsg() != ""

View File

@ -29,7 +29,6 @@
###############################################################################
from osgeo import gdal
###############################################################################
@ -38,11 +37,13 @@ from osgeo import gdal
def test_identify_1():
file_list = gdal.ReadDir('data')
file_list = gdal.ReadDir("data")
dr = gdal.IdentifyDriver("data/byte.tif", file_list)
assert (
dr is not None and dr.GetDescription() == "GTiff"
), "Got wrong driver for byte.tif"
dr = gdal.IdentifyDriver('data/byte.tif', file_list)
assert dr is not None and dr.GetDescription() == 'GTiff', \
'Got wrong driver for byte.tif'
###############################################################################
# Test a file that won't be recognised.
@ -50,10 +51,11 @@ def test_identify_1():
def test_identify_2():
file_list = gdal.ReadDir('data')
file_list = gdal.ReadDir("data")
dr = gdal.IdentifyDriver("data/byte.pnm.aux.xml", file_list)
assert dr is None, "Got a driver for byte.pnm.aux.xml!"
dr = gdal.IdentifyDriver('data/byte.pnm.aux.xml', file_list)
assert dr is None, 'Got a driver for byte.pnm.aux.xml!'
###############################################################################
# Try identify on a directory.
@ -61,8 +63,9 @@ def test_identify_2():
def test_identify_3():
dr = gdal.IdentifyDriver('data')
assert dr is None, 'Got a driver for data directory!'
dr = gdal.IdentifyDriver("data")
assert dr is None, "Got a driver for data directory!"
###############################################################################
# Try IdentifyDriverEx
@ -70,25 +73,28 @@ def test_identify_3():
def test_identify_4():
dr = gdal.IdentifyDriverEx('data/byte.tif')
assert dr is not None and dr.GetDescription() == 'GTiff', \
'Got wrong driver for byte.tif'
dr = gdal.IdentifyDriverEx("data/byte.tif")
assert (
dr is not None and dr.GetDescription() == "GTiff"
), "Got wrong driver for byte.tif"
dr = gdal.IdentifyDriverEx('data/byte.tif', gdal.OF_RASTER)
assert dr is not None and dr.GetDescription() == 'GTiff', \
'Got wrong driver for byte.tif'
dr = gdal.IdentifyDriverEx("data/byte.tif", gdal.OF_RASTER)
assert (
dr is not None and dr.GetDescription() == "GTiff"
), "Got wrong driver for byte.tif"
dr = gdal.IdentifyDriverEx('data/byte.tif', gdal.OF_VECTOR)
assert dr is None, 'Got wrong driver for byte.tif'
dr = gdal.IdentifyDriverEx('data/byte.tif', allowed_drivers=['HFA'])
assert dr is None, 'Got wrong driver for byte.tif'
dr = gdal.IdentifyDriverEx('../gdrivers/data/envi/aea.dat', sibling_files=['aea.dat'])
assert dr is None, 'Got a driver, which was not expected!'
dr = gdal.IdentifyDriverEx('../gdrivers/data/envi/aea.dat', sibling_files=['aea.dat', 'aea.hdr'])
assert dr is not None, 'Did not get a driver!'
dr = gdal.IdentifyDriverEx("data/byte.tif", gdal.OF_VECTOR)
assert dr is None, "Got wrong driver for byte.tif"
dr = gdal.IdentifyDriverEx("data/byte.tif", allowed_drivers=["HFA"])
assert dr is None, "Got wrong driver for byte.tif"
dr = gdal.IdentifyDriverEx(
"../gdrivers/data/envi/aea.dat", sibling_files=["aea.dat"]
)
assert dr is None, "Got a driver, which was not expected!"
dr = gdal.IdentifyDriverEx(
"../gdrivers/data/envi/aea.dat", sibling_files=["aea.dat", "aea.hdr"]
)
assert dr is not None, "Did not get a driver!"

File diff suppressed because it is too large Load Diff

View File

@ -30,8 +30,8 @@
###############################################################################
import gdaltest
from osgeo import gdal
###############################################################################
@ -42,42 +42,43 @@ def test_minixml_1():
tree = gdal.ParseXMLString('<TestDoc style="123"><sub1/><sub2>abc</sub2></TestDoc>')
assert tree[0] == gdal.CXT_Element, 'wrong node type.'
assert tree[0] == gdal.CXT_Element, "wrong node type."
assert tree[1] == 'TestDoc', 'Wrong element name'
assert tree[1] == "TestDoc", "Wrong element name"
assert len(tree) == 5, 'Wrong number of children.'
assert len(tree) == 5, "Wrong number of children."
# Check style attribute
node = tree[2]
assert node[0] == gdal.CXT_Attribute, 'wrong node type.'
assert node[0] == gdal.CXT_Attribute, "wrong node type."
assert node[1] == 'style', 'Wrong element name'
assert node[1] == "style", "Wrong element name"
assert len(node) == 3, 'Wrong number of children.'
assert len(node) == 3, "Wrong number of children."
assert node[2][1] == '123', 'Wrong element content.'
assert node[2][1] == "123", "Wrong element content."
# Check <sub1> element
node = tree[3]
assert node[0] == gdal.CXT_Element, 'wrong node type.'
assert node[0] == gdal.CXT_Element, "wrong node type."
assert node[1] == 'sub1', 'Wrong element name'
assert node[1] == "sub1", "Wrong element name"
assert len(node) == 2, 'Wrong number of children.'
assert len(node) == 2, "Wrong number of children."
# Check <sub2> element
node = tree[4]
assert node[0] == gdal.CXT_Element, 'wrong node type.'
assert node[0] == gdal.CXT_Element, "wrong node type."
assert node[1] == 'sub2', 'Wrong element name'
assert node[1] == "sub2", "Wrong element name"
assert len(node) == 3, 'Wrong number of children.'
assert len(node) == 3, "Wrong number of children."
assert node[2][1] == "abc", "Wrong element content."
assert node[2][1] == 'abc', 'Wrong element content.'
###############################################################################
# Serialize an XML Tree
@ -85,10 +86,17 @@ def test_minixml_1():
def test_minixml_2():
tree = [0, 'TestDoc', [2, 'style', [1, '123']], [0, 'sub1'], [0, 'sub2', [1, 'abc']]]
tree = [
0,
"TestDoc",
[2, "style", [1, "123"]],
[0, "sub1"],
[0, "sub2", [1, "abc"]],
]
doc_target = '<TestDoc style="123">\n <sub1 />\n <sub2>abc</sub2>\n</TestDoc>\n'
doc_got = gdal.SerializeXMLTree(tree)
assert doc_got == doc_target, 'serialize xml tree failed.'
assert doc_got == doc_target, "serialize xml tree failed."
###############################################################################
# Read XML document with complex DOCTYPE element.
@ -96,40 +104,41 @@ def test_minixml_2():
def test_minixml_3():
fp = open('data/doctype.xml', 'r')
fp = open("data/doctype.xml", "r")
text = fp.read()
tree = gdal.ParseXMLString(text)
assert tree[0] == gdal.CXT_Element, 'wrong node type.'
assert tree[0] == gdal.CXT_Element, "wrong node type."
# Check <chapter> element
node = tree[6]
assert node[0] == gdal.CXT_Element, 'wrong node type.'
assert node[0] == gdal.CXT_Element, "wrong node type."
assert node[1] == 'chapter', 'Wrong element name'
assert node[1] == "chapter", "Wrong element name"
assert len(node) == 7, 'Wrong number of children.'
assert len(node) == 7, "Wrong number of children."
# Check <chapter><title> subelement
subnode = node[2]
assert subnode[0] == gdal.CXT_Element, 'wrong node type.'
assert subnode[0] == gdal.CXT_Element, "wrong node type."
assert subnode[1] == 'title', 'Wrong element name'
assert subnode[1] == "title", "Wrong element name"
assert len(subnode) == 3, 'Wrong number of children.'
assert len(subnode) == 3, "Wrong number of children."
assert subnode[2][1] == 'Chapter 1', 'Wrong element content.'
assert subnode[2][1] == "Chapter 1", "Wrong element content."
# Check fist <chapter><para> subelement
subnode = node[3]
assert subnode[0] == gdal.CXT_Element, 'wrong node type.'
assert subnode[0] == gdal.CXT_Element, "wrong node type."
assert subnode[1] == 'para', 'Wrong element name'
assert subnode[1] == "para", "Wrong element name"
assert len(subnode) == 3, "Wrong number of children."
assert len(subnode) == 3, 'Wrong number of children.'
###############################################################################
# Parse and serialize an XML Tree with a <?xml> prolog
@ -139,7 +148,8 @@ def test_minixml_4():
xml = """<?xml encoding="utf-8"?>\n<foo />\n"""
got_xml = gdal.SerializeXMLTree(gdal.ParseXMLString(xml))
assert xml == got_xml, 'serialize xml tree failed.'
assert xml == got_xml, "serialize xml tree failed."
###############################################################################
# Parse malformed XML. Complains, but still makes a tree.
@ -148,8 +158,8 @@ def test_minixml_4():
def test_minixml_5():
test_pairs = (
('<a></A>', 'case'),
('<a b=c></a>', 'quoted'),
("<a></A>", "case"),
("<a b=c></a>", "quoted"),
)
for xml_str, expect in test_pairs:
@ -157,11 +167,13 @@ def test_minixml_5():
tree = gdal.ParseXMLString(xml_str)
found = gdal.GetLastErrorMsg()
assert expect in found, ('Did not find expected error message: "%s" '
'Found: "%s" '
'For test string: "%s""' % (expect, found, xml_str))
assert expect in found, (
'Did not find expected error message: "%s" '
'Found: "%s" '
'For test string: "%s""' % (expect, found, xml_str)
)
assert tree is not None, ('Tree is None: "%s"' % tree)
assert tree is not None, 'Tree is None: "%s"' % tree
###############################################################################
@ -171,18 +183,18 @@ def test_minixml_5():
def test_minixml_6():
test_pairs = (
('<', 'element token after open angle bracket'),
('<a>', 'not all elements have been closed'),
('<a><b>', 'not all elements have been closed'),
('<a><b></a></b>', 'have matching'),
('<a foo=></a>', 'attribute value'),
('<></>', 'element token'),
('<&></&>', 'matching'),
('<a></a', 'Missing close angle'),
('<a foo=2\'> foo=2\'>', 'unexpected token'),
('<a?>', 'without matching'),
('<a/.', 'for value of attribute '),
('<a\'>', 'reached EOF before closing quote'),
("<", "element token after open angle bracket"),
("<a>", "not all elements have been closed"),
("<a><b>", "not all elements have been closed"),
("<a><b></a></b>", "have matching"),
("<a foo=></a>", "attribute value"),
("<></>", "element token"),
("<&></&>", "matching"),
("<a></a", "Missing close angle"),
("<a foo=2'> foo=2'>", "unexpected token"),
("<a?>", "without matching"),
("<a/.", "for value of attribute "),
("<a'>", "reached EOF before closing quote"),
)
for xml_str, expect in test_pairs:
@ -190,11 +202,13 @@ def test_minixml_6():
tree = gdal.ParseXMLString(xml_str)
found = gdal.GetLastErrorMsg()
assert expect in found, ('Did not find expected error message: "%s" '
'Found: "%s" '
'For test string: "%s""' % (expect, found, xml_str))
assert expect in found, (
'Did not find expected error message: "%s" '
'Found: "%s" '
'For test string: "%s""' % (expect, found, xml_str)
)
assert tree is None, ('Tree is not None: "%s"' % tree)
assert tree is None, 'Tree is not None: "%s"' % tree
###############################################################################
@ -204,10 +218,10 @@ def test_minixml_6():
def test_minixml_7():
test_strings = (
'<1></1>',
'<-></->',
'<.></.>',
'<![CDATA[',
"<1></1>",
"<-></->",
"<.></.>",
"<![CDATA[",
)
for xml_str in test_strings:
@ -215,9 +229,9 @@ def test_minixml_7():
tree = gdal.ParseXMLString(xml_str)
found = gdal.GetLastErrorMsg()
assert found == '', ('Unexpected msg "%s"' % found)
assert found == "", 'Unexpected msg "%s"' % found
assert tree is not None, ('Tree is None: "%s"' % tree)
assert tree is not None, 'Tree is None: "%s"' % tree
###############################################################################
@ -226,14 +240,14 @@ def test_minixml_7():
def test_minixml_8():
xml_str = '<a>' * 10001
xml_str += '</a>' * 10001
xml_str = "<a>" * 10001
xml_str += "</a>" * 10001
gdal.ErrorReset()
with gdaltest.error_handler():
tree = gdal.ParseXMLString(xml_str)
assert tree is None, 'expected None tree'
assert gdal.GetLastErrorMsg() != '', 'expected error message'
assert tree is None, "expected None tree"
assert gdal.GetLastErrorMsg() != "", "expected error message"
###############################################################################
@ -244,12 +258,12 @@ def test_minixml_processing_instruction():
xml = """<?a b c d?>\n<foo />\n"""
got_xml = gdal.SerializeXMLTree(gdal.ParseXMLString(xml))
assert xml == got_xml, 'serialize xml tree failed.'
assert xml == got_xml, "serialize xml tree failed."
###############################################################################
# Cleanup
def test_minixml_cleanup():
pass

View File

@ -31,26 +31,27 @@
import os
import shutil
from osgeo import gdal
import gdaltest
import pytest
from osgeo import gdal
###############################################################################
# Test that the constructor of GDALDataset() behaves well with a big number of
# opened/created datasets
def test_misc_1():
tab_ds = [None] * 5000
drv = gdal.GetDriverByName('MEM')
drv = gdal.GetDriverByName("MEM")
for i, _ in enumerate(tab_ds):
name = 'mem_%d' % i
name = "mem_%d" % i
tab_ds[i] = drv.Create(name, 1, 1, 1)
assert tab_ds[i] is not None
###############################################################################
# Test that OpenShared() works as expected by opening a big number of times
# the same dataset with it. If it did not work, that would exhaust the system
@ -61,7 +62,7 @@ def test_misc_2():
tab_ds = [None for i in range(5000)]
for i, _ in enumerate(tab_ds):
tab_ds[i] = gdal.OpenShared('data/byte.tif')
tab_ds[i] = gdal.OpenShared("data/byte.tif")
assert tab_ds[i] is not None
@ -72,12 +73,12 @@ def test_misc_2():
def test_misc_3():
with gdaltest.error_handler():
ds = gdal.OpenShared('../gdrivers/data/paux/small16.aux')
ds = gdal.OpenShared("../gdrivers/data/paux/small16.aux")
ds.GetRasterBand(1).Checksum()
cache_size = gdal.GetCacheUsed()
with gdaltest.error_handler():
ds2 = gdal.OpenShared('../gdrivers/data/paux/small16.aux')
ds2 = gdal.OpenShared("../gdrivers/data/paux/small16.aux")
ds2.GetRasterBand(1).Checksum()
cache_size2 = gdal.GetCacheUsed()
@ -87,20 +88,21 @@ def test_misc_3():
ds = None
ds2 = None
###############################################################################
# Test Create() with invalid arguments
def test_misc_4():
gdal.PushErrorHandler('CPLQuietErrorHandler')
gdal.PushErrorHandler("CPLQuietErrorHandler")
# Test a few invalid argument
drv = gdal.GetDriverByName('GTiff')
drv.Create('tmp/foo', 0, 100, 1)
drv.Create('tmp/foo', 100, 1, 1)
drv.Create('tmp/foo', 100, 100, -1)
drv.Delete('tmp/foo')
drv = gdal.GetDriverByName("GTiff")
drv.Create("tmp/foo", 0, 100, 1)
drv.Create("tmp/foo", 100, 1, 1)
drv.Create("tmp/foo", 100, 100, -1)
drv.Delete("tmp/foo")
gdal.PopErrorHandler()
@ -108,33 +110,38 @@ def test_misc_4():
###############################################################################
def get_filename(drv, dirname):
filename = '%s/foo' % dirname
if drv.ShortName == 'GTX':
filename += '.gtx'
elif drv.ShortName == 'RST':
filename += '.rst'
elif drv.ShortName == 'SAGA':
filename += '.sdat'
elif drv.ShortName == 'ADRG':
filename = '%s/ABCDEF01.GEN' % dirname
elif drv.ShortName == 'SRTMHGT':
filename = '%s/N48E002.HGT' % dirname
elif drv.ShortName == 'ECW':
filename += '.ecw'
elif drv.ShortName == 'KMLSUPEROVERLAY':
filename += '.kmz'
elif drv.ShortName == 'RRASTER':
filename += '.grd'
filename = "%s/foo" % dirname
if drv.ShortName == "GTX":
filename += ".gtx"
elif drv.ShortName == "RST":
filename += ".rst"
elif drv.ShortName == "SAGA":
filename += ".sdat"
elif drv.ShortName == "ADRG":
filename = "%s/ABCDEF01.GEN" % dirname
elif drv.ShortName == "SRTMHGT":
filename = "%s/N48E002.HGT" % dirname
elif drv.ShortName == "ECW":
filename += ".ecw"
elif drv.ShortName == "KMLSUPEROVERLAY":
filename += ".kmz"
elif drv.ShortName == "RRASTER":
filename += ".grd"
return filename
###############################################################################
# Test Create() with various band numbers (including 0) and datatype
def _misc_5_internal(drv, datatype, nBands):
dirname = 'tmp/tmp/tmp_%s_%d_%s' % (drv.ShortName, nBands, gdal.GetDataTypeName(datatype))
dirname = "tmp/tmp/tmp_%s_%d_%s" % (
drv.ShortName,
nBands,
gdal.GetDataTypeName(datatype),
)
# print('drv = %s, nBands = %d, datatype = %s' % (drv.ShortName, nBands, gdal.GetDataTypeName(datatype)))
try:
os.mkdir(dirname)
@ -144,22 +151,27 @@ def _misc_5_internal(drv, datatype, nBands):
# Hum the directory already exists... Not expected, but let's try to go on
except OSError:
pytest.fail(
'Cannot create %s for drv = %s, nBands = %d, datatype = %s' % (dirname, drv.ShortName, nBands, gdal.GetDataTypeName(datatype))
"Cannot create %s for drv = %s, nBands = %d, datatype = %s"
% (dirname, drv.ShortName, nBands, gdal.GetDataTypeName(datatype))
)
filename = get_filename(drv, dirname)
ds = drv.Create(filename, 100, 100, nBands, datatype)
if ds is not None and not (drv.ShortName == 'GPKG' and nBands == 0):
if ds is not None and not (drv.ShortName == "GPKG" and nBands == 0):
set_gt = (2, 1.0 / 10, 0, 49, 0, -1.0 / 10)
ds.SetGeoTransform(set_gt)
ds.SetProjection('GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.01745329251994328]]')
ds.SetProjection(
'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.01745329251994328]]'
)
# PNM and MFF have no SetGeoTransform() method implemented
if drv.ShortName not in ['PNM', 'MFF', 'NULL']:
if drv.ShortName not in ["PNM", "MFF", "NULL"]:
got_gt = ds.GetGeoTransform()
for i in range(6):
assert got_gt[i] == pytest.approx(set_gt[i], abs=1e-10), \
'Did not get expected GT for drv = %s, nBands = %d, datatype = %s' % (drv.ShortName, nBands, gdal.GetDataTypeName(datatype))
assert got_gt[i] == pytest.approx(set_gt[i], abs=1e-10), (
"Did not get expected GT for drv = %s, nBands = %d, datatype = %s"
% (drv.ShortName, nBands, gdal.GetDataTypeName(datatype))
)
# if ds.RasterCount > 0:
# ds.GetRasterBand(1).Fill(255)
@ -179,59 +191,62 @@ def _misc_5_internal(drv, datatype, nBands):
shutil.rmtree(dirname)
except OSError:
pytest.fail(
'Cannot remove %s for drv = %s, nBands = %d, datatype = %s' % (dirname, drv.ShortName, nBands, gdal.GetDataTypeName(datatype))
"Cannot remove %s for drv = %s, nBands = %d, datatype = %s"
% (dirname, drv.ShortName, nBands, gdal.GetDataTypeName(datatype))
)
def test_misc_5():
gdal.PushErrorHandler('CPLQuietErrorHandler')
gdal.PushErrorHandler("CPLQuietErrorHandler")
try:
shutil.rmtree('tmp/tmp')
shutil.rmtree("tmp/tmp")
except OSError:
pass
try:
os.mkdir('tmp/tmp')
os.mkdir("tmp/tmp")
except OSError:
try:
os.stat('tmp/tmp')
os.stat("tmp/tmp")
# Hum the directory already exists... Not expected, but let's try to go on
except OSError:
pytest.fail('Cannot create tmp/tmp')
pytest.fail("Cannot create tmp/tmp")
# This is to speed-up the runtime of tests on EXT4 filesystems
# Do not use this for production environment if you care about data safety
# w.r.t system/OS crashes, unless you know what you are doing.
gdal.SetConfigOption('OGR_SQLITE_SYNCHRONOUS', 'OFF')
gdal.SetConfigOption("OGR_SQLITE_SYNCHRONOUS", "OFF")
# Test Create() with various band numbers, including 0
for i in range(gdal.GetDriverCount()):
drv = gdal.GetDriver(i)
md = drv.GetMetadata()
if drv.ShortName == 'PDF':
if drv.ShortName == "PDF":
# PDF Create() is vector-only
continue
if drv.ShortName == 'MBTiles':
if drv.ShortName == "MBTiles":
# MBTiles only support some precise resolutions
continue
if 'DCAP_CREATE' in md and 'DCAP_RASTER' in md:
if "DCAP_CREATE" in md and "DCAP_RASTER" in md:
datatype = gdal.GDT_Byte
for nBands in range(6):
_misc_5_internal(drv, datatype, nBands)
for nBands in [1, 3]:
for datatype in (gdal.GDT_UInt16,
gdal.GDT_Int16,
gdal.GDT_UInt32,
gdal.GDT_Int32,
gdal.GDT_Float32,
gdal.GDT_Float64,
gdal.GDT_CInt16,
gdal.GDT_CInt32,
gdal.GDT_CFloat32,
gdal.GDT_CFloat64):
for datatype in (
gdal.GDT_UInt16,
gdal.GDT_Int16,
gdal.GDT_UInt32,
gdal.GDT_Int32,
gdal.GDT_Float32,
gdal.GDT_Float64,
gdal.GDT_CInt16,
gdal.GDT_CInt32,
gdal.GDT_CFloat32,
gdal.GDT_CFloat64,
):
_misc_5_internal(drv, datatype, nBands)
gdal.PopErrorHandler()
@ -246,34 +261,44 @@ class misc_6_interrupt_callback_class(object):
# pylint: disable=unused-argument
return pct <= 0.5
###############################################################################
# Test CreateCopy() with a source dataset with various band numbers (including 0) and datatype
def misc_6_internal(datatype, nBands, setDriversDone):
ds = gdal.GetDriverByName('MEM').Create('', 10, 10, nBands, datatype)
ds = gdal.GetDriverByName("MEM").Create("", 10, 10, nBands, datatype)
if nBands > 0:
ds.GetRasterBand(1).Fill(255)
ds.SetGeoTransform([2, 1.0 / 10, 0, 49, 0, -1.0 / 10])
ds.SetProjection('GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.01745329251994328]]')
ds.SetMetadata(['a'])
ds.SetProjection(
'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.01745329251994328]]'
)
ds.SetMetadata(["a"])
for i in range(gdal.GetDriverCount()):
drv = gdal.GetDriver(i)
md = drv.GetMetadata()
if ('DCAP_CREATECOPY' in md or 'DCAP_CREATE' in md) and 'DCAP_RASTER' in md:
if ("DCAP_CREATECOPY" in md or "DCAP_CREATE" in md) and "DCAP_RASTER" in md:
# print ('drv = %s, nBands = %d, datatype = %s' % (drv.ShortName, nBands, gdal.GetDataTypeName(datatype)))
skip = False
# FIXME: A few cases that crashes and should be investigated
if drv.ShortName == 'JPEG2000':
if (nBands == 2 or nBands >= 5) or \
not (datatype == gdal.GDT_Byte or datatype == gdal.GDT_Int16 or datatype == gdal.GDT_UInt16):
if drv.ShortName == "JPEG2000":
if (nBands == 2 or nBands >= 5) or not (
datatype == gdal.GDT_Byte
or datatype == gdal.GDT_Int16
or datatype == gdal.GDT_UInt16
):
skip = True
if skip is False:
dirname = 'tmp/tmp/tmp_%s_%d_%s' % (drv.ShortName, nBands, gdal.GetDataTypeName(datatype))
dirname = "tmp/tmp/tmp_%s_%d_%s" % (
drv.ShortName,
nBands,
gdal.GetDataTypeName(datatype),
)
try:
os.mkdir(dirname)
except OSError:
@ -281,7 +306,15 @@ def misc_6_internal(datatype, nBands, setDriversDone):
os.stat(dirname)
# Hum the directory already exists... Not expected, but let's try to go on
except OSError:
reason = 'Cannot create %s before drv = %s, nBands = %d, datatype = %s' % (dirname, drv.ShortName, nBands, gdal.GetDataTypeName(datatype))
reason = (
"Cannot create %s before drv = %s, nBands = %d, datatype = %s"
% (
dirname,
drv.ShortName,
nBands,
gdal.GetDataTypeName(datatype),
)
)
pytest.fail(reason)
filename = get_filename(drv, dirname)
@ -291,7 +324,7 @@ def misc_6_internal(datatype, nBands, setDriversDone):
if dst_ds:
# check that domain == None doesn't crash
dst_ds.GetMetadata(None)
dst_ds.GetMetadataItem('', None)
dst_ds.GetMetadataItem("", None)
dst_ds = None
size = 0
@ -302,7 +335,15 @@ def misc_6_internal(datatype, nBands, setDriversDone):
try:
shutil.rmtree(dirname)
except OSError:
reason = 'Cannot remove %s after drv = %s, nBands = %d, datatype = %s' % (dirname, drv.ShortName, nBands, gdal.GetDataTypeName(datatype))
reason = (
"Cannot remove %s after drv = %s, nBands = %d, datatype = %s"
% (
dirname,
drv.ShortName,
nBands,
gdal.GetDataTypeName(datatype),
)
)
pytest.fail(reason)
if has_succeeded and drv.ShortName not in setDriversDone and nBands > 0:
@ -310,13 +351,20 @@ def misc_6_internal(datatype, nBands, setDriversDone):
# The first list of drivers fail to detect short writing
# The second one is because they are verbose in stderr
if 'DCAP_VIRTUALIO' in md and size != 0 and \
drv.ShortName not in ['JPEG2000', 'KMLSUPEROVERLAY', 'HF2', 'ZMap', 'DDS'] and \
drv.ShortName not in ['GIF', 'JP2ECW', 'JP2Lura']:
if (
"DCAP_VIRTUALIO" in md
and size != 0
and drv.ShortName
not in ["JPEG2000", "KMLSUPEROVERLAY", "HF2", "ZMap", "DDS"]
and drv.ShortName not in ["GIF", "JP2ECW", "JP2Lura"]
):
for j in range(10):
truncated_size = (size * j) / 10
vsimem_filename = ('/vsimem/test_truncate/||maxlength=%d||' % truncated_size) + get_filename(drv, '')[1:]
vsimem_filename = (
"/vsimem/test_truncate/||maxlength=%d||"
% truncated_size
) + get_filename(drv, "")[1:]
# print('drv = %s, nBands = %d, datatype = %s, truncated_size = %d' % (drv.ShortName, nBands, gdal.GetDataTypeName(datatype), truncated_size))
dst_ds = drv.CreateCopy(vsimem_filename, ds)
error_detected = False
@ -325,23 +373,45 @@ def misc_6_internal(datatype, nBands, setDriversDone):
else:
gdal.ErrorReset()
dst_ds = None
if gdal.GetLastErrorMsg() != '':
if gdal.GetLastErrorMsg() != "":
error_detected = True
if not error_detected:
msg = 'write error not decteded with with drv = %s, nBands = %d, datatype = %s, truncated_size = %d' % (drv.ShortName, nBands, gdal.GetDataTypeName(datatype), truncated_size)
msg = (
"write error not decteded with with drv = %s, nBands = %d, datatype = %s, truncated_size = %d"
% (
drv.ShortName,
nBands,
gdal.GetDataTypeName(datatype),
truncated_size,
)
)
print(msg)
gdaltest.post_reason(msg)
fl = gdal.ReadDirRecursive('/vsimem/test_truncate')
fl = gdal.ReadDirRecursive("/vsimem/test_truncate")
if fl is not None:
for myf in fl:
gdal.Unlink('/vsimem/test_truncate/' + myf)
fl = gdal.ReadDirRecursive('/vsimem/test_truncate')
gdal.Unlink("/vsimem/test_truncate/" + myf)
fl = gdal.ReadDirRecursive("/vsimem/test_truncate")
if fl is not None:
print(fl)
if drv.ShortName not in ['ECW', 'JP2ECW', 'VRT', 'XPM', 'JPEG2000', 'FIT', 'RST', 'INGR', 'USGSDEM', 'KMLSUPEROVERLAY', 'GMT']:
dst_ds = drv.CreateCopy(filename, ds, callback=misc_6_interrupt_callback_class().cbk)
if drv.ShortName not in [
"ECW",
"JP2ECW",
"VRT",
"XPM",
"JPEG2000",
"FIT",
"RST",
"INGR",
"USGSDEM",
"KMLSUPEROVERLAY",
"GMT",
]:
dst_ds = drv.CreateCopy(
filename, ds, callback=misc_6_interrupt_callback_class().cbk
)
if dst_ds is not None:
dst_ds = None
@ -350,7 +420,14 @@ def misc_6_internal(datatype, nBands, setDriversDone):
except OSError:
pass
pytest.fail('interruption did not work with drv = %s, nBands = %d, datatype = %s' % (drv.ShortName, nBands, gdal.GetDataTypeName(datatype)))
pytest.fail(
"interruption did not work with drv = %s, nBands = %d, datatype = %s"
% (
drv.ShortName,
nBands,
gdal.GetDataTypeName(datatype),
)
)
dst_ds = None
@ -361,60 +438,71 @@ def misc_6_internal(datatype, nBands, setDriversDone):
try:
os.mkdir(dirname)
except OSError:
reason = 'Cannot create %s before drv = %s, nBands = %d, datatype = %s' % (dirname, drv.ShortName, nBands, gdal.GetDataTypeName(datatype))
reason = (
"Cannot create %s before drv = %s, nBands = %d, datatype = %s"
% (
dirname,
drv.ShortName,
nBands,
gdal.GetDataTypeName(datatype),
)
)
pytest.fail(reason)
ds = None
def test_misc_6():
gdal.PushErrorHandler('CPLQuietErrorHandler')
gdal.PushErrorHandler("CPLQuietErrorHandler")
try:
shutil.rmtree('tmp/tmp')
shutil.rmtree("tmp/tmp")
except OSError:
pass
try:
os.mkdir('tmp/tmp')
os.mkdir("tmp/tmp")
except OSError:
try:
os.stat('tmp/tmp')
os.stat("tmp/tmp")
# Hum the directory already exists... Not expected, but let's try to go on
except OSError:
pytest.fail('Cannot create tmp/tmp')
pytest.fail("Cannot create tmp/tmp")
# This is to speed-up the runtime of tests on EXT4 filesystems
# Do not use this for production environment if you care about data safety
# w.r.t system/OS crashes, unless you know what you are doing.
gdal.SetConfigOption('OGR_SQLITE_SYNCHRONOUS', 'OFF')
gdal.SetConfigOption("OGR_SQLITE_SYNCHRONOUS", "OFF")
datatype = gdal.GDT_Byte
setDriversDone = set()
for nBands in range(6):
ret = misc_6_internal(datatype, nBands, setDriversDone)
if ret != 'success':
if ret != "success":
gdal.PopErrorHandler()
return ret
nBands = 1
for datatype in (gdal.GDT_UInt16,
gdal.GDT_Int16,
gdal.GDT_UInt32,
gdal.GDT_Int32,
gdal.GDT_Float32,
gdal.GDT_Float64,
gdal.GDT_CInt16,
gdal.GDT_CInt32,
gdal.GDT_CFloat32,
gdal.GDT_CFloat64):
for datatype in (
gdal.GDT_UInt16,
gdal.GDT_Int16,
gdal.GDT_UInt32,
gdal.GDT_Int32,
gdal.GDT_Float32,
gdal.GDT_Float64,
gdal.GDT_CInt16,
gdal.GDT_CInt32,
gdal.GDT_CFloat32,
gdal.GDT_CFloat64,
):
ret = misc_6_internal(datatype, nBands, setDriversDone)
if ret != 'success':
if ret != "success":
gdal.PopErrorHandler()
return ret
gdal.PopErrorHandler()
###############################################################################
# Test gdal.InvGeoTransform()
@ -442,14 +530,21 @@ def test_misc_7():
# Test fix for #1615
gt = (-2, 1e-8, 1e-9, 52, 1e-9, -1e-8)
res = gdal.InvGeoTransform(gt)
expected_inv_gt = (-316831683.16831684, 99009900.990099, 9900990.099009901,
5168316831.683168, 9900990.099009901, -99009900.990099)
expected_inv_gt = (
-316831683.16831684,
99009900.990099,
9900990.099009901,
5168316831.683168,
9900990.099009901,
-99009900.990099,
)
for i in range(6):
assert res[i] == pytest.approx(expected_inv_gt[i], abs=1e-6), res
res2 = gdal.InvGeoTransform(res)
for i in range(6):
assert res2[i] == pytest.approx(gt[i], abs=1e-6), res2
###############################################################################
# Test gdal.ApplyGeoTransform()
@ -465,6 +560,7 @@ def test_misc_8():
res = gdal.ApplyGeoTransform(gt, 10, 1)
assert res == [11.0, 19.0]
###############################################################################
# Test setting and retrieving > 2 GB values for GDAL max cache (#3689)
@ -476,20 +572,21 @@ def test_misc_9():
ret_val = gdal.GetCacheMax()
gdal.SetCacheMax(old_val)
assert ret_val == 3000000000, 'did not get expected value'
assert ret_val == 3000000000, "did not get expected value"
###############################################################################
# Test VSIBufferedReaderHandle (fix done in r21358)
def test_misc_10():
try:
os.remove('data/byte.tif.gz.properties')
os.remove("data/byte.tif.gz.properties")
except OSError:
pass
f = gdal.VSIFOpenL('/vsigzip/./data/byte.tif.gz', 'rb')
f = gdal.VSIFOpenL("/vsigzip/./data/byte.tif.gz", "rb")
gdal.VSIFReadL(1, 1, f)
gdal.VSIFSeekL(f, 0, 2)
gdal.VSIFSeekL(f, 0, 0)
@ -497,38 +594,40 @@ def test_misc_10():
gdal.VSIFCloseL(f)
import struct
ar = struct.unpack('B' * 4, data)
ar = struct.unpack("B" * 4, data)
assert ar == (73, 73, 42, 0)
try:
os.remove('data/byte.tif.gz.properties')
os.remove("data/byte.tif.gz.properties")
except OSError:
pass
###############################################################################
# Test that we can open a symlink whose pointed filename isn't a real
# file, but a filename that GDAL recognizes
def test_misc_11():
if not gdaltest.support_symlink():
pytest.skip()
gdal.Unlink('tmp/symlink.tif')
os.symlink('GTIFF_DIR:1:data/byte.tif', 'tmp/symlink.tif')
gdal.Unlink("tmp/symlink.tif")
os.symlink("GTIFF_DIR:1:data/byte.tif", "tmp/symlink.tif")
ds = gdal.Open('tmp/symlink.tif')
ds = gdal.Open("tmp/symlink.tif")
if ds is None:
os.remove('tmp/symlink.tif')
os.remove("tmp/symlink.tif")
pytest.fail()
desc = ds.GetDescription()
ds = None
os.remove('tmp/symlink.tif')
os.remove("tmp/symlink.tif")
assert desc == "GTIFF_DIR:1:data/byte.tif", "did not get expected description"
assert desc == 'GTIFF_DIR:1:data/byte.tif', 'did not get expected description'
###############################################################################
# Test CreateCopy() with a target filename in a non-existing dir
@ -536,64 +635,88 @@ def test_misc_11():
def test_misc_12():
if int(gdal.VersionInfo('VERSION_NUM')) < 1900:
pytest.skip('would crash')
if int(gdal.VersionInfo("VERSION_NUM")) < 1900:
pytest.skip("would crash")
import test_cli_utilities
gdal_translate_path = test_cli_utilities.get_gdal_translate_path()
for i in range(gdal.GetDriverCount()):
drv = gdal.GetDriver(i)
md = drv.GetMetadata()
if ('DCAP_CREATECOPY' in md or 'DCAP_CREATE' in md) and 'DCAP_RASTER' in md:
if ("DCAP_CREATECOPY" in md or "DCAP_CREATE" in md) and "DCAP_RASTER" in md:
nbands = 1
if drv.ShortName == 'WEBP' or drv.ShortName == 'ADRG':
if drv.ShortName == "WEBP" or drv.ShortName == "ADRG":
nbands = 3
datatype = gdal.GDT_Byte
if drv.ShortName == 'BT' or drv.ShortName == 'BLX':
if drv.ShortName == "BT" or drv.ShortName == "BLX":
datatype = gdal.GDT_Int16
elif drv.ShortName == 'GTX' or drv.ShortName == 'NTv2' or drv.ShortName == 'Leveller':
elif (
drv.ShortName == "GTX"
or drv.ShortName == "NTv2"
or drv.ShortName == "Leveller"
):
datatype = gdal.GDT_Float32
size = 1201
if drv.ShortName == 'BLX':
if drv.ShortName == "BLX":
size = 128
src_ds = gdal.GetDriverByName('GTiff').Create('/vsimem/misc_12_src.tif', size, size, nbands, datatype)
src_ds = gdal.GetDriverByName("GTiff").Create(
"/vsimem/misc_12_src.tif", size, size, nbands, datatype
)
set_gt = (2, 1.0 / size, 0, 49, 0, -1.0 / size)
src_ds.SetGeoTransform(set_gt)
src_ds.SetProjection('GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.01745329251994328]]')
src_ds.SetProjection(
'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["degree",0.01745329251994328]]'
)
# Test to detect crashes
gdal.PushErrorHandler('CPLQuietErrorHandler')
ds = drv.CreateCopy('/nonexistingpath' + get_filename(drv, ''), src_ds)
gdal.PushErrorHandler("CPLQuietErrorHandler")
ds = drv.CreateCopy("/nonexistingpath" + get_filename(drv, ""), src_ds)
gdal.PopErrorHandler()
if ds is None and gdal.GetLastErrorMsg() == '':
gdal.Unlink('/vsimem/misc_12_src.tif')
pytest.fail('CreateCopy() into non existing dir fails without error message for driver %s' % drv.ShortName)
if ds is None and gdal.GetLastErrorMsg() == "":
gdal.Unlink("/vsimem/misc_12_src.tif")
pytest.fail(
"CreateCopy() into non existing dir fails without error message for driver %s"
% drv.ShortName
)
ds = None
if gdal_translate_path is not None:
# Test to detect memleaks
ds = gdal.GetDriverByName('VRT').CreateCopy('tmp/misc_12.vrt', src_ds)
(out, _) = gdaltest.runexternal_out_and_err(gdal_translate_path + ' -of ' + drv.ShortName + ' tmp/misc_12.vrt /nonexistingpath/' + get_filename(drv, ''), check_memleak=False)
ds = gdal.GetDriverByName("VRT").CreateCopy("tmp/misc_12.vrt", src_ds)
(out, _) = gdaltest.runexternal_out_and_err(
gdal_translate_path
+ " -of "
+ drv.ShortName
+ " tmp/misc_12.vrt /nonexistingpath/"
+ get_filename(drv, ""),
check_memleak=False,
)
del ds
gdal.Unlink('tmp/misc_12.vrt')
gdal.Unlink("tmp/misc_12.vrt")
# If DEBUG_VSIMALLOC_STATS is defined, this is an easy way
# to catch some memory leaks
if out.find('VSIMalloc + VSICalloc - VSIFree') != -1 and \
out.find('VSIMalloc + VSICalloc - VSIFree : 0') == -1:
if drv.ShortName == 'Rasterlite' and out.find('VSIMalloc + VSICalloc - VSIFree : 1') != -1:
if (
out.find("VSIMalloc + VSICalloc - VSIFree") != -1
and out.find("VSIMalloc + VSICalloc - VSIFree : 0") == -1
):
if (
drv.ShortName == "Rasterlite"
and out.find("VSIMalloc + VSICalloc - VSIFree : 1") != -1
):
pass
else:
print('memleak detected for driver %s' % drv.ShortName)
print("memleak detected for driver %s" % drv.ShortName)
src_ds = None
gdal.Unlink('/vsimem/misc_12_src.tif')
gdal.Unlink("/vsimem/misc_12_src.tif")
###############################################################################
@ -603,22 +726,24 @@ def test_misc_12():
def test_misc_13():
# Raster-only -> vector-only
ds = gdal.Open('data/byte.tif')
ds = gdal.Open("data/byte.tif")
gdal.PushErrorHandler()
out_ds = gdal.GetDriverByName('ESRI Shapefile').CreateCopy('/vsimem/out.shp', ds)
out_ds = gdal.GetDriverByName("ESRI Shapefile").CreateCopy("/vsimem/out.shp", ds)
gdal.PopErrorHandler()
assert out_ds is None
# Raster-only -> vector-only
ds = gdal.OpenEx('../ogr/data/poly.shp', gdal.OF_VECTOR)
ds = gdal.OpenEx("../ogr/data/poly.shp", gdal.OF_VECTOR)
gdal.PushErrorHandler()
out_ds = gdal.GetDriverByName('GTiff').CreateCopy('/vsimem/out.tif', ds)
out_ds = gdal.GetDriverByName("GTiff").CreateCopy("/vsimem/out.tif", ds)
gdal.PopErrorHandler()
assert out_ds is None
###############################################################################
# Test ConfigureLogging()
def test_misc_14():
import collections
import logging
@ -631,8 +756,7 @@ def test_misc_14():
def emit(self, record):
self.messages[record.levelname].append(record.getMessage())
logger = logging.getLogger('gdal_logging_test')
logger = logging.getLogger("gdal_logging_test")
logger.setLevel(logging.DEBUG)
logger.propagate = False
handler = MockLoggingHandler(level=logging.DEBUG)
@ -640,7 +764,7 @@ def test_misc_14():
prev_debug = gdal.GetConfigOption("CPL_DEBUG")
try:
gdal.ConfigurePythonLogging(logger_name='gdal_logging_test', enable_debug=True)
gdal.ConfigurePythonLogging(logger_name="gdal_logging_test", enable_debug=True)
assert gdal.GetConfigOption("CPL_DEBUG") == "ON", "should have enabled debug"
@ -651,62 +775,65 @@ def test_misc_14():
gdal.Error(gdal.CE_Failure, 99999, "error1")
expected = {
'DEBUG': ["test1: debug1", "FileIO: debug2"],
'INFO': ["AppDefined: info1"],
'WARNING': ["AssertionFailed: warning1"],
'ERROR': ["99999: error1"],
"DEBUG": ["test1: debug1", "FileIO: debug2"],
"INFO": ["AppDefined: info1"],
"WARNING": ["AssertionFailed: warning1"],
"ERROR": ["99999: error1"],
}
assert handler.messages == expected, "missing log messages"
gdal.SetErrorHandler('CPLDefaultErrorHandler')
gdal.SetErrorHandler("CPLDefaultErrorHandler")
handler.messages.clear()
gdal.SetConfigOption('CPL_DEBUG', "OFF")
gdal.SetConfigOption("CPL_DEBUG", "OFF")
gdal.ConfigurePythonLogging(logger_name='gdal_logging_test')
gdal.ConfigurePythonLogging(logger_name="gdal_logging_test")
assert gdal.GetConfigOption("CPL_DEBUG") == "OFF", \
"shouldn't have enabled debug"
assert (
gdal.GetConfigOption("CPL_DEBUG") == "OFF"
), "shouldn't have enabled debug"
# these get suppressed by CPL_DEBUG
gdal.Debug("test1", "debug3")
# these don't
gdal.Error(gdal.CE_Debug, gdal.CPLE_None, "debug4")
assert handler.messages['DEBUG'] == ['debug4'], "unexpected log messages"
assert handler.messages["DEBUG"] == ["debug4"], "unexpected log messages"
finally:
gdal.SetErrorHandler('CPLDefaultErrorHandler')
gdal.SetConfigOption('CPL_DEBUG', prev_debug)
gdal.SetErrorHandler("CPLDefaultErrorHandler")
gdal.SetConfigOption("CPL_DEBUG", prev_debug)
logger.removeHandler(handler)
###############################################################################
# Test SetErrorHandler
def test_misc_15():
messages0 = []
def handle0(ecls, ecode, emsg):
messages0.append((ecls, ecode, emsg))
messages1 = []
def handle1(ecls, ecode, emsg):
messages1.append((ecls, ecode, emsg))
prev_debug = gdal.GetConfigOption("CPL_DEBUG")
try:
gdal.SetErrorHandler(handle0)
gdal.SetConfigOption('CPL_DEBUG', "ON")
gdal.SetConfigOption("CPL_DEBUG", "ON")
gdal.Debug('foo', 'bar')
gdal.Debug("foo", "bar")
gdal.Error(gdal.CE_Debug, gdal.CPLE_FileIO, "debug2")
gdal.Error(gdal.CE_None, gdal.CPLE_AppDefined, "info1")
gdal.Error(gdal.CE_Warning, gdal.CPLE_AssertionFailed, "warning1")
gdal.Error(gdal.CE_Failure, 99999, "error1")
expected0 = [
(gdal.CE_Debug, 0, 'foo: bar'),
(gdal.CE_Debug, 0, "foo: bar"),
(gdal.CE_Debug, gdal.CPLE_FileIO, "debug2"),
(gdal.CE_None, gdal.CPLE_AppDefined, "info1"),
(gdal.CE_Warning, gdal.CPLE_AssertionFailed, "warning1"),
@ -738,8 +865,8 @@ def test_misc_15():
assert len(messages1) == 0, "PopErrorHandler: unexpected log messages"
finally:
gdal.SetErrorHandler('CPLDefaultErrorHandler')
gdal.SetConfigOption('CPL_DEBUG', prev_debug)
gdal.SetErrorHandler("CPLDefaultErrorHandler")
gdal.SetConfigOption("CPL_DEBUG", prev_debug)
###############################################################################
@ -748,6 +875,6 @@ def test_misc_15():
def test_misc_cleanup():
try:
shutil.rmtree('tmp/tmp')
shutil.rmtree("tmp/tmp")
except OSError:
pass

View File

@ -29,15 +29,16 @@
###############################################################################
import array
from osgeo import gdal
from osgeo import osr
import gdaltest
import pytest
from osgeo import gdal, osr
def test_multidim_asarray_epsg_4326():
ds = gdal.Open('../gdrivers/data/small_world.tif')
ds = gdal.Open("../gdrivers/data/small_world.tif")
srs_ds = ds.GetSpatialRef()
assert srs_ds.GetDataAxisToSRSAxisMapping() == [2, 1]
band = ds.GetRasterBand(1)
@ -67,7 +68,7 @@ def test_multidim_asarray_epsg_4326():
def test_multidim_asarray_epsg_26711():
ds = gdal.Open('data/byte.tif')
ds = gdal.Open("data/byte.tif")
srs_ds = ds.GetSpatialRef()
assert srs_ds.GetDataAxisToSRSAxisMapping() == [1, 2]
band = ds.GetRasterBand(1)
@ -95,18 +96,23 @@ def test_multidim_asarray_epsg_26711():
assert ds2.ReadRaster() == ds.GetRasterBand(1).ReadRaster()
@pytest.mark.parametrize("resampling",[gdal.GRIORA_NearestNeighbour,
gdal.GRIORA_Bilinear,
gdal.GRIORA_Cubic,
gdal.GRIORA_CubicSpline,
gdal.GRIORA_Lanczos,
gdal.GRIORA_Average,
gdal.GRIORA_Mode,
gdal.GRIORA_Gauss, #unsupported
gdal.GRIORA_RMS])
@pytest.mark.parametrize(
"resampling",
[
gdal.GRIORA_NearestNeighbour,
gdal.GRIORA_Bilinear,
gdal.GRIORA_Cubic,
gdal.GRIORA_CubicSpline,
gdal.GRIORA_Lanczos,
gdal.GRIORA_Average,
gdal.GRIORA_Mode,
gdal.GRIORA_Gauss, # unsupported
gdal.GRIORA_RMS,
],
)
def test_multidim_getresampled(resampling):
ds = gdal.Open('../gdrivers/data/small_world.tif')
ds = gdal.Open("../gdrivers/data/small_world.tif")
srs_ds = ds.GetSpatialRef()
band = ds.GetRasterBand(1)
ar = band.AsMDArray()
@ -114,7 +120,9 @@ def test_multidim_getresampled(resampling):
if resampling == gdal.GRIORA_Gauss:
with gdaltest.error_handler():
resampled_ar = ar.GetResampled([None] * ar.GetDimensionCount(), resampling, None)
resampled_ar = ar.GetResampled(
[None] * ar.GetDimensionCount(), resampling, None
)
assert resampled_ar is None
return
@ -126,12 +134,15 @@ def test_multidim_getresampled(resampling):
assert srs.GetAuthorityCode(None) == srs_ds.GetAuthorityCode(None)
dims = resampled_ar.GetDimensions()
assert len(dims) == 2
assert dims[0].GetName() == 'dimY'
assert dims[0].GetName() == "dimY"
assert dims[0].GetSize() == ds.RasterYSize
assert dims[1].GetName() == 'dimX'
assert dims[1].GetName() == "dimX"
assert dims[1].GetSize() == ds.RasterXSize
assert resampled_ar.Read(buffer_datatype = gdal.ExtendedDataType.CreateString()) != gdal.CE_None
assert (
resampled_ar.Read(buffer_datatype=gdal.ExtendedDataType.CreateString())
!= gdal.CE_None
)
ixdim = 1
iydim = 0
@ -146,42 +157,56 @@ def test_multidim_getresampled(resampling):
assert ds2.ReadRaster() != ds.GetRasterBand(1).ReadRaster()
else:
assert ds2.ReadRaster() == ds.GetRasterBand(1).ReadRaster()
assert ds2.ReadRaster(buf_type = gdal.GDT_UInt16) == ds.GetRasterBand(1).ReadRaster(buf_type = gdal.GDT_UInt16)
assert ds2.ReadRaster(buf_type=gdal.GDT_UInt16) == ds.GetRasterBand(
1
).ReadRaster(buf_type=gdal.GDT_UInt16)
@pytest.mark.parametrize("with_dim_x,with_var_x,with_dim_y,with_var_y",
[[True, False, True, False],
[True, False, False, False],
[False, False, True, False],
[True, True, True, True]])
def test_multidim_getresampled_new_dims_with_variables(with_dim_x,with_var_x,with_dim_y,with_var_y):
@pytest.mark.parametrize(
"with_dim_x,with_var_x,with_dim_y,with_var_y",
[
[True, False, True, False],
[True, False, False, False],
[False, False, True, False],
[True, True, True, True],
],
)
def test_multidim_getresampled_new_dims_with_variables(
with_dim_x, with_var_x, with_dim_y, with_var_y
):
ds = gdal.Open('../gdrivers/data/small_world.tif')
ds = gdal.Open("../gdrivers/data/small_world.tif")
srs_ds = ds.GetSpatialRef()
band = ds.GetRasterBand(1)
ar = band.AsMDArray()
assert ar
drv = gdal.GetDriverByName('MEM')
mem_ds = drv.CreateMultiDimensional('myds')
drv = gdal.GetDriverByName("MEM")
mem_ds = drv.CreateMultiDimensional("myds")
rg = mem_ds.GetRootGroup()
dimY = None
if with_dim_y:
dimY = rg.CreateDimension('dimY', None, None, ds.RasterYSize // 2)
dimY = rg.CreateDimension("dimY", None, None, ds.RasterYSize // 2)
if with_var_y:
varY = rg.CreateMDArray(dimY.GetName(), [dimY],
gdal.ExtendedDataType.Create(gdal.GDT_Float64))
varY.Write(array.array('d', [ 90 - 0.9 - 1.8 * i for i in range(dimY.GetSize()) ]))
varY = rg.CreateMDArray(
dimY.GetName(), [dimY], gdal.ExtendedDataType.Create(gdal.GDT_Float64)
)
varY.Write(
array.array("d", [90 - 0.9 - 1.8 * i for i in range(dimY.GetSize())])
)
dimY.SetIndexingVariable(varY)
dimX = None
if with_dim_x:
dimX = rg.CreateDimension('dimX', None, None, ds.RasterXSize // 2)
dimX = rg.CreateDimension("dimX", None, None, ds.RasterXSize // 2)
if with_var_x:
varX = rg.CreateMDArray(dimX.GetName(), [dimX],
gdal.ExtendedDataType.Create(gdal.GDT_Float64))
varX.Write(array.array('d', [ -180 + 0.9 + 1.8 * i for i in range(dimX.GetSize()) ]))
varX = rg.CreateMDArray(
dimX.GetName(), [dimX], gdal.ExtendedDataType.Create(gdal.GDT_Float64)
)
varX.Write(
array.array("d", [-180 + 0.9 + 1.8 * i for i in range(dimX.GetSize())])
)
dimX.SetIndexingVariable(varX)
resampled_ar = ar.GetResampled([dimY, dimX], gdal.GRIORA_Cubic, None)
@ -194,27 +219,29 @@ def test_multidim_getresampled_new_dims_with_variables(with_dim_x,with_var_x,wit
assert dims[0].GetSize() == ds.RasterYSize // 2
assert dims[1].GetSize() == ds.RasterXSize // 2
expected_ds = gdal.Warp('', ds, options = '-of MEM -ts 200 100 -r cubic')
expected_ds = gdal.Warp("", ds, options="-of MEM -ts 200 100 -r cubic")
assert expected_ds.GetRasterBand(1).ReadRaster() == resampled_ar.Read()
def test_multidim_getresampled_with_srs():
ds = gdal.Open('data/byte.tif')
ds = gdal.Open("data/byte.tif")
band = ds.GetRasterBand(1)
ar = band.AsMDArray()
assert ar
srs = osr.SpatialReference()
srs.ImportFromEPSG(4267)
resampled_ar = ar.GetResampled([None] * ar.GetDimensionCount(), gdal.GRIORA_NearestNeighbour, srs)
resampled_ar = ar.GetResampled(
[None] * ar.GetDimensionCount(), gdal.GRIORA_NearestNeighbour, srs
)
assert resampled_ar
got_srs = resampled_ar.GetSpatialRef()
assert got_srs is not None
assert got_srs.GetAuthorityCode(None) == srs.GetAuthorityCode(None)
dims = resampled_ar.GetDimensions()
expected_ds = gdal.Warp('', ds, options = '-of MEM -t_srs EPSG:4267 -r nearest')
expected_ds = gdal.Warp("", ds, options="-of MEM -t_srs EPSG:4267 -r nearest")
assert expected_ds.RasterXSize == dims[1].GetSize()
assert expected_ds.RasterYSize == dims[0].GetSize()
assert expected_ds.GetRasterBand(1).ReadRaster() == resampled_ar.Read()
@ -227,36 +254,40 @@ def test_multidim_getresampled_with_srs():
def test_multidim_getresampled_3d():
ds = gdal.Open('../gdrivers/data/small_world.tif')
ds = gdal.Open("../gdrivers/data/small_world.tif")
ar_b1 = ds.GetRasterBand(1).AsMDArray()
drv = gdal.GetDriverByName('MEM')
mem_ds = drv.CreateMultiDimensional('myds')
drv = gdal.GetDriverByName("MEM")
mem_ds = drv.CreateMultiDimensional("myds")
rg = mem_ds.GetRootGroup()
dimBand = rg.CreateDimension('dimBand', None, None, ds.RasterCount)
dimBand = rg.CreateDimension("dimBand", None, None, ds.RasterCount)
dimY = ar_b1.GetDimensions()[0]
dimX = ar_b1.GetDimensions()[1]
ar = rg.CreateMDArray("ar", [dimBand, dimY, dimX],
gdal.ExtendedDataType.Create(gdal.GDT_Byte))
ar = rg.CreateMDArray(
"ar", [dimBand, dimY, dimX], gdal.ExtendedDataType.Create(gdal.GDT_Byte)
)
ar.SetOffset(1.5)
ar.SetScale(2.5)
ar.SetUnit('foo')
ar.SetUnit("foo")
ar.SetNoDataValueDouble(-0.5)
attr = ar.CreateAttribute('attr', [],
gdal.ExtendedDataType.Create(gdal.GDT_Float64))
attr = ar.CreateAttribute(
"attr", [], gdal.ExtendedDataType.Create(gdal.GDT_Float64)
)
assert attr.Write(1) == gdal.CE_None
srs = ds.GetSpatialRef().Clone()
srs.SetDataAxisToSRSAxisMapping([1, 2])
ar.SetSpatialRef(srs)
for i in range(ds.RasterCount):
ar[i].Write(ds.GetRasterBand(i+1).ReadRaster())
ar[i].Write(ds.GetRasterBand(i + 1).ReadRaster())
resampled_ar = ar.GetResampled([None] * ar.GetDimensionCount(), gdal.GRIORA_NearestNeighbour, None)
resampled_ar = ar.GetResampled(
[None] * ar.GetDimensionCount(), gdal.GRIORA_NearestNeighbour, None
)
assert resampled_ar
dims = resampled_ar.GetDimensions()
assert len(dims) == 3
assert len(dims) == 3
assert dims[0].GetName() == dimBand.GetName()
assert dims[0].GetSize() == dimBand.GetSize()
assert dims[1].GetSize() == dimY.GetSize()
@ -271,21 +302,19 @@ def test_multidim_getresampled_3d():
assert block_size[0] == 0
assert block_size[1] != 0
assert block_size[2] != 0
assert resampled_ar.GetAttribute('attr') is not None
assert resampled_ar.GetAttribute("attr") is not None
assert len(resampled_ar.GetAttributes()) == 1
assert resampled_ar.Read() == ds.ReadRaster()
def test_multidim_getresampled_error_single_dim():
drv = gdal.GetDriverByName('MEM')
mem_ds = drv.CreateMultiDimensional('myds')
drv = gdal.GetDriverByName("MEM")
mem_ds = drv.CreateMultiDimensional("myds")
rg = mem_ds.GetRootGroup()
dimX = rg.CreateDimension('X', None, None, 3)
ar = rg.CreateMDArray("ar", [dimX],
gdal.ExtendedDataType.Create(gdal.GDT_Byte))
dimX = rg.CreateDimension("X", None, None, 3)
ar = rg.CreateMDArray("ar", [dimX], gdal.ExtendedDataType.Create(gdal.GDT_Byte))
with gdaltest.error_handler():
resampled_ar = ar.GetResampled([None], gdal.GRIORA_NearestNeighbour, None)
assert resampled_ar is None
@ -293,43 +322,50 @@ def test_multidim_getresampled_error_single_dim():
def test_multidim_getresampled_error_too_large_y():
drv = gdal.GetDriverByName('MEM')
mem_ds = drv.CreateMultiDimensional('myds')
drv = gdal.GetDriverByName("MEM")
mem_ds = drv.CreateMultiDimensional("myds")
rg = mem_ds.GetRootGroup()
dimY = rg.CreateDimension('Y', None, None, 4)
dimX = rg.CreateDimension('X', None, None, 3)
ar = rg.CreateMDArray("ar", [dimY, dimX],
gdal.ExtendedDataType.Create(gdal.GDT_Byte))
new_dimY = rg.CreateDimension('Y', None, None, 4 * 1000 * 1000 * 1000)
dimY = rg.CreateDimension("Y", None, None, 4)
dimX = rg.CreateDimension("X", None, None, 3)
ar = rg.CreateMDArray(
"ar", [dimY, dimX], gdal.ExtendedDataType.Create(gdal.GDT_Byte)
)
new_dimY = rg.CreateDimension("Y", None, None, 4 * 1000 * 1000 * 1000)
with gdaltest.error_handler():
resampled_ar = ar.GetResampled([new_dimY, None], gdal.GRIORA_NearestNeighbour, None)
resampled_ar = ar.GetResampled(
[new_dimY, None], gdal.GRIORA_NearestNeighbour, None
)
assert resampled_ar is None
def test_multidim_getresampled_error_too_large_x():
drv = gdal.GetDriverByName('MEM')
mem_ds = drv.CreateMultiDimensional('myds')
drv = gdal.GetDriverByName("MEM")
mem_ds = drv.CreateMultiDimensional("myds")
rg = mem_ds.GetRootGroup()
dimY = rg.CreateDimension('Y', None, None, 4)
dimX = rg.CreateDimension('X', None, None, 3)
ar = rg.CreateMDArray("ar", [dimY, dimX],
gdal.ExtendedDataType.Create(gdal.GDT_Byte))
new_dimX = rg.CreateDimension('Y', None, None, 4 * 1000 * 1000 * 1000)
dimY = rg.CreateDimension("Y", None, None, 4)
dimX = rg.CreateDimension("X", None, None, 3)
ar = rg.CreateMDArray(
"ar", [dimY, dimX], gdal.ExtendedDataType.Create(gdal.GDT_Byte)
)
new_dimX = rg.CreateDimension("Y", None, None, 4 * 1000 * 1000 * 1000)
with gdaltest.error_handler():
resampled_ar = ar.GetResampled([None, new_dimX], gdal.GRIORA_NearestNeighbour, None)
resampled_ar = ar.GetResampled(
[None, new_dimX], gdal.GRIORA_NearestNeighbour, None
)
assert resampled_ar is None
def test_multidim_getresampled_error_no_geotransform():
drv = gdal.GetDriverByName('MEM')
mem_ds = drv.CreateMultiDimensional('myds')
drv = gdal.GetDriverByName("MEM")
mem_ds = drv.CreateMultiDimensional("myds")
rg = mem_ds.GetRootGroup()
dimY = rg.CreateDimension('Y', None, None, 2)
dimX = rg.CreateDimension('X', None, None, 3)
ar = rg.CreateMDArray("ar", [dimY, dimX],
gdal.ExtendedDataType.Create(gdal.GDT_Byte))
dimY = rg.CreateDimension("Y", None, None, 2)
dimX = rg.CreateDimension("X", None, None, 3)
ar = rg.CreateMDArray(
"ar", [dimY, dimX], gdal.ExtendedDataType.Create(gdal.GDT_Byte)
)
with gdaltest.error_handler():
resampled_ar = ar.GetResampled([None, None], gdal.GRIORA_NearestNeighbour, None)
assert resampled_ar is None
@ -337,27 +373,30 @@ def test_multidim_getresampled_error_no_geotransform():
def test_multidim_getresampled_error_extra_dim_not_same():
ds = gdal.Open('../gdrivers/data/small_world.tif')
ds = gdal.Open("../gdrivers/data/small_world.tif")
ar_b1 = ds.GetRasterBand(1).AsMDArray()
drv = gdal.GetDriverByName('MEM')
mem_ds = drv.CreateMultiDimensional('myds')
drv = gdal.GetDriverByName("MEM")
mem_ds = drv.CreateMultiDimensional("myds")
rg = mem_ds.GetRootGroup()
dimOther = rg.CreateDimension('other', None, None, 2)
dimOther = rg.CreateDimension("other", None, None, 2)
dimY = ar_b1.GetDimensions()[0]
dimX = ar_b1.GetDimensions()[1]
ar = rg.CreateMDArray("ar", [dimOther, dimY, dimX],
gdal.ExtendedDataType.Create(gdal.GDT_Byte))
ar = rg.CreateMDArray(
"ar", [dimOther, dimY, dimX], gdal.ExtendedDataType.Create(gdal.GDT_Byte)
)
dimOtherNew = rg.CreateDimension('otherNew', None, None, 1)
dimOtherNew = rg.CreateDimension("otherNew", None, None, 1)
with gdaltest.error_handler():
resampled_ar = ar.GetResampled([dimOtherNew, None, None], gdal.GRIORA_NearestNeighbour, None)
resampled_ar = ar.GetResampled(
[dimOtherNew, None, None], gdal.GRIORA_NearestNeighbour, None
)
assert resampled_ar is None
def test_multidim_getresampled_bad_input_dim_count():
ds = gdal.Open('data/byte.tif')
ds = gdal.Open("data/byte.tif")
band = ds.GetRasterBand(1)
ar = band.AsMDArray()
assert ar
@ -367,6 +406,7 @@ def test_multidim_getresampled_bad_input_dim_count():
assert resampled_ar is None
with gdaltest.error_handler():
resampled_ar = ar.GetResampled([None, None, None], gdal.GRIORA_NearestNeighbour, None)
resampled_ar = ar.GetResampled(
[None, None, None], gdal.GRIORA_NearestNeighbour, None
)
assert resampled_ar is None

View File

@ -36,53 +36,104 @@ from osgeo import gdal
def test_nodatamaskband_1():
for (dt, struct_type, v) in [(gdal.GDT_Byte, 'B', 255),
(gdal.GDT_Int16, 'h', 32767),
(gdal.GDT_UInt16, 'H', 65535),
(gdal.GDT_Int32, 'i', 0x7FFFFFFF),
(gdal.GDT_UInt32, 'I', 0xFFFFFFFF),
(gdal.GDT_Float32, 'f', 1.25),
(gdal.GDT_Float32, 'f', float('nan')),
(gdal.GDT_Float64, 'd', 1.2345678),
(gdal.GDT_Float64, 'd', float('nan'))]:
for (dt, struct_type, v) in [
(gdal.GDT_Byte, "B", 255),
(gdal.GDT_Int16, "h", 32767),
(gdal.GDT_UInt16, "H", 65535),
(gdal.GDT_Int32, "i", 0x7FFFFFFF),
(gdal.GDT_UInt32, "I", 0xFFFFFFFF),
(gdal.GDT_Float32, "f", 1.25),
(gdal.GDT_Float32, "f", float("nan")),
(gdal.GDT_Float64, "d", 1.2345678),
(gdal.GDT_Float64, "d", float("nan")),
]:
ds = gdal.GetDriverByName('MEM').Create('', 6, 4, 1, dt)
ds = gdal.GetDriverByName("MEM").Create("", 6, 4, 1, dt)
ds.GetRasterBand(1).SetNoDataValue(v)
ds.GetRasterBand(1).WriteRaster(0, 0, 6, 4,
struct.pack(struct_type * 6 * 4,
v, 1, 1, v, v, 0,
v, 1, 1, v, v, 0,
v, 1, 1, v, v, 0,
0, v, v, 0, 0, 0))
ds.GetRasterBand(1).WriteRaster(
0,
0,
6,
4,
struct.pack(
struct_type * 6 * 4,
v,
1,
1,
v,
v,
0,
v,
1,
1,
v,
v,
0,
v,
1,
1,
v,
v,
0,
0,
v,
v,
0,
0,
0,
),
)
data = ds.GetRasterBand(1).GetMaskBand().ReadRaster()
data_ar = struct.unpack('B' * 6 * 4, data)
expected_ar = (0, 255, 255, 0, 0, 255,
0, 255, 255, 0, 0, 255,
0, 255, 255, 0, 0, 255,
255, 0, 0, 255, 255, 255)
data_ar = struct.unpack("B" * 6 * 4, data)
expected_ar = (
0,
255,
255,
0,
0,
255,
0,
255,
255,
0,
0,
255,
0,
255,
255,
0,
0,
255,
255,
0,
0,
255,
255,
255,
)
assert data_ar == expected_ar, dt
data = ds.GetRasterBand(1).GetMaskBand().ReadBlock(0, 0)
data_ar = struct.unpack('B' * 6 * 1, data)
data_ar = struct.unpack("B" * 6 * 1, data)
expected_ar = (0, 255, 255, 0, 0, 255)
assert data_ar == expected_ar, dt
data = ds.GetRasterBand(1).GetMaskBand().ReadBlock(0, 3)
data_ar = struct.unpack('B' * 6 * 1, data)
data_ar = struct.unpack("B" * 6 * 1, data)
expected_ar = (255, 0, 0, 255, 255, 255)
assert data_ar == expected_ar, dt
data = ds.GetRasterBand(1).GetMaskBand().ReadRaster(
buf_xsize=3, buf_ysize=2)
data_ar = struct.unpack('B' * 3 * 2, data)
expected_ar = (255, 0, 255,
0, 255, 255)
data = ds.GetRasterBand(1).GetMaskBand().ReadRaster(buf_xsize=3, buf_ysize=2)
data_ar = struct.unpack("B" * 3 * 2, data)
expected_ar = (255, 0, 255, 0, 255, 255)
assert data_ar == expected_ar, dt
data = ds.GetRasterBand(1).GetMaskBand().ReadRaster(
buf_type=gdal.GDT_UInt16, buf_xsize=3, buf_ysize=2)
data_ar = struct.unpack('H' * 3 * 2, data)
expected_ar = (255, 0, 255,
0, 255, 255)
data = (
ds.GetRasterBand(1)
.GetMaskBand()
.ReadRaster(buf_type=gdal.GDT_UInt16, buf_xsize=3, buf_ysize=2)
)
data_ar = struct.unpack("H" * 3 * 2, data)
expected_ar = (255, 0, 255, 0, 255, 255)
assert data_ar == expected_ar, dt

File diff suppressed because it is too large Load Diff

View File

@ -29,11 +29,12 @@
###############################################################################
import struct
import gdaltest
from osgeo import gdal
import pytest
import struct
from osgeo import gdal
###############################################################################
# verify that we can load Numeric python, and find the Numpy driver.
@ -43,12 +44,13 @@ def test_numpy_rw_multidim_init():
gdaltest.numpy_drv = None
# importing gdal_array will allow numpy driver registration
pytest.importorskip('osgeo.gdal_array')
pytest.importorskip("osgeo.gdal_array")
gdal.AllRegister()
gdaltest.numpy_drv = gdal.GetDriverByName('NUMPY')
assert gdaltest.numpy_drv is not None, 'NUMPY driver not found!'
gdaltest.numpy_drv = gdal.GetDriverByName("NUMPY")
assert gdaltest.numpy_drv is not None, "NUMPY driver not found!"
###############################################################################
@ -59,23 +61,39 @@ def test_numpy_rw_multidim_readasarray_writearray():
pytest.skip()
import numpy as np
drv = gdal.GetDriverByName('MEM')
ds = drv.CreateMultiDimensional('myds')
drv = gdal.GetDriverByName("MEM")
ds = drv.CreateMultiDimensional("myds")
rg = ds.GetRootGroup()
dim0 = rg.CreateDimension("dim0", None, None, 2)
dim1 = rg.CreateDimension("dim1", None, None, 3)
myarray = rg.CreateMDArray("myarray", [ dim0, dim1 ],
gdal.ExtendedDataType.Create(gdal.GDT_Byte))
myarray = rg.CreateMDArray(
"myarray", [dim0, dim1], gdal.ExtendedDataType.Create(gdal.GDT_Byte)
)
assert myarray
ar = np.array([[1,2,3], [4,5,6]], dtype=np.uint8)
ar = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint8)
assert myarray.WriteArray(ar) == gdal.CE_None
got_ar = myarray.ReadAsArray()
assert got_ar.shape == (2, 3)
assert np.array_equal(got_ar, ar)
# Check algo with non-numpy method so as to detect issues with buffer striding
assert struct.unpack('B' * 6, myarray.Read()) == (1, 2, 3, 4, 5, 6)
assert struct.unpack('B' * 6, myarray.Read(buffer_stride = [3, 1])) == (1, 2, 3, 4, 5, 6)
assert struct.unpack('B' * 6, myarray.Read(buffer_stride = [1, 2])) == (1, 4, 2, 5, 3, 6)
assert struct.unpack("B" * 6, myarray.Read()) == (1, 2, 3, 4, 5, 6)
assert struct.unpack("B" * 6, myarray.Read(buffer_stride=[3, 1])) == (
1,
2,
3,
4,
5,
6,
)
assert struct.unpack("B" * 6, myarray.Read(buffer_stride=[1, 2])) == (
1,
4,
2,
5,
3,
6,
)
###############################################################################
@ -84,19 +102,32 @@ def test_numpy_rw_multidim_numpy_array_as_dataset():
if gdaltest.numpy_drv is None:
pytest.skip()
from osgeo import gdal_array
import numpy as np
for typ in (np.int8, np.uint8, np.uint16, np.int16, np.uint32, np.int32, np.float32, np.float64, np.cfloat, np.cdouble):
ar = np.array([[1,2,3], [4,5,6]], dtype=typ)
from osgeo import gdal_array
for typ in (
np.int8,
np.uint8,
np.uint16,
np.int16,
np.uint32,
np.int32,
np.float32,
np.float64,
np.cfloat,
np.cdouble,
):
ar = np.array([[1, 2, 3], [4, 5, 6]], dtype=typ)
ds = gdal_array.OpenMultiDimensionalNumPyArray(ar)
assert ds
rg = ds.GetRootGroup()
assert rg
myarray = rg.OpenMDArray('array')
myarray = rg.OpenMDArray("array")
assert myarray
assert np.array_equal(myarray.ReadAsArray(), ar)
###############################################################################
@ -106,24 +137,40 @@ def test_numpy_rw_multidim_readasarray_writearray_negative_strides():
pytest.skip()
import numpy as np
drv = gdal.GetDriverByName('MEM')
ds = drv.CreateMultiDimensional('myds')
drv = gdal.GetDriverByName("MEM")
ds = drv.CreateMultiDimensional("myds")
rg = ds.GetRootGroup()
dim0 = rg.CreateDimension("dim0", None, None, 2)
dim1 = rg.CreateDimension("dim1", None, None, 3)
myarray = rg.CreateMDArray("myarray", [ dim0, dim1 ],
gdal.ExtendedDataType.Create(gdal.GDT_Byte))
myarray = rg.CreateMDArray(
"myarray", [dim0, dim1], gdal.ExtendedDataType.Create(gdal.GDT_Byte)
)
assert myarray
ar = np.array([[1,2,3], [4,5,6]], dtype=np.uint8)
ar = ar[::-1,::-1] # Test negative strides
ar = np.array([[1, 2, 3], [4, 5, 6]], dtype=np.uint8)
ar = ar[::-1, ::-1] # Test negative strides
assert myarray.WriteArray(ar) == gdal.CE_None
got_ar = myarray.ReadAsArray()
assert got_ar.shape == (2, 3)
assert np.array_equal(got_ar, ar)
# Check algo with non-numpy method so as to detect issues with buffer striding
assert struct.unpack('B' * 6, myarray.Read()) == (6, 5, 4, 3, 2, 1)
assert struct.unpack('B' * 6, myarray.Read(buffer_stride = [3, 1])) == (6, 5, 4, 3, 2, 1)
assert struct.unpack('B' * 6, myarray.Read(buffer_stride = [1, 2])) == (6, 3, 5, 2, 4, 1)
assert struct.unpack("B" * 6, myarray.Read()) == (6, 5, 4, 3, 2, 1)
assert struct.unpack("B" * 6, myarray.Read(buffer_stride=[3, 1])) == (
6,
5,
4,
3,
2,
1,
)
assert struct.unpack("B" * 6, myarray.Read(buffer_stride=[1, 2])) == (
6,
3,
5,
2,
4,
1,
)
###############################################################################
@ -132,20 +179,33 @@ def test_numpy_rw_multidim_numpy_array_as_dataset_negative_strides():
if gdaltest.numpy_drv is None:
pytest.skip()
from osgeo import gdal_array
import numpy as np
for typ in (np.int8, np.uint8, np.uint16, np.int16, np.uint32, np.int32, np.float32, np.float64, np.cfloat, np.cdouble):
ar = np.array([[1,2,3], [4,5,6]], dtype=typ)
ar = ar[::-1,::-1] # Test negative strides
from osgeo import gdal_array
for typ in (
np.int8,
np.uint8,
np.uint16,
np.int16,
np.uint32,
np.int32,
np.float32,
np.float64,
np.cfloat,
np.cdouble,
):
ar = np.array([[1, 2, 3], [4, 5, 6]], dtype=typ)
ar = ar[::-1, ::-1] # Test negative strides
ds = gdal_array.OpenMultiDimensionalNumPyArray(ar)
assert ds
rg = ds.GetRootGroup()
assert rg
myarray = rg.OpenMDArray('array')
myarray = rg.OpenMDArray("array")
assert myarray
assert np.array_equal(myarray.ReadAsArray(), ar)
###############################################################################
@ -153,63 +213,82 @@ def test_numpy_rw_multidim_compound_datatype():
if gdaltest.numpy_drv is None:
pytest.skip()
from osgeo import gdal_array
import numpy as np
drv = gdal.GetDriverByName('MEM')
ds = drv.CreateMultiDimensional('myds')
from osgeo import gdal_array
drv = gdal.GetDriverByName("MEM")
ds = drv.CreateMultiDimensional("myds")
rg = ds.GetRootGroup()
dim = rg.CreateDimension("dim0", None, None, 2)
comp0 = gdal.EDTComponent.Create('x', 0, gdal.ExtendedDataType.Create(gdal.GDT_Int16))
comp1 = gdal.EDTComponent.Create('y', 4, gdal.ExtendedDataType.Create(gdal.GDT_Int32))
comp0 = gdal.EDTComponent.Create(
"x", 0, gdal.ExtendedDataType.Create(gdal.GDT_Int16)
)
comp1 = gdal.EDTComponent.Create(
"y", 4, gdal.ExtendedDataType.Create(gdal.GDT_Int32)
)
dt = gdal.ExtendedDataType.CreateCompound("mytype", 8, [comp0, comp1])
myarray = rg.CreateMDArray("myarray", [ dim ], dt)
myarray = rg.CreateMDArray("myarray", [dim], dt)
assert myarray
numpydt = gdal_array.ExtendedDataTypeToNumPyDataType(dt)
assert numpydt.itemsize == 8
assert numpydt.names == ('x', 'y')
assert numpydt.fields['x'] == (np.int16, 0)
assert numpydt.fields['y'] == (np.int32, 4)
assert numpydt.names == ("x", "y")
assert numpydt.fields["x"] == (np.int16, 0)
assert numpydt.fields["y"] == (np.int32, 4)
assert myarray.Write(struct.pack('hi' * 2, 32767, 1000000, -32768, -1000000)) == gdal.CE_None
assert (
myarray.Write(struct.pack("hi" * 2, 32767, 1000000, -32768, -1000000))
== gdal.CE_None
)
res = myarray.ReadAsArray()
assert res.dtype == numpydt
assert np.array_equal(res, np.array([( 32767, 1000000), (-32768, -1000000)], dtype = res.dtype))
assert np.array_equal(
res, np.array([(32767, 1000000), (-32768, -1000000)], dtype=res.dtype)
)
ar = np.array([( -32768, -1000000), (-32767, 1000000)], dtype = numpydt)
ar = np.array([(-32768, -1000000), (-32767, 1000000)], dtype=numpydt)
assert myarray.WriteArray(ar) == gdal.CE_None
res = myarray.ReadAsArray()
assert np.array_equal(res, ar)
###############################################################################
@pytest.mark.parametrize("datatype", [gdal.GDT_Byte,
gdal.GDT_Int16,
gdal.GDT_UInt16,
gdal.GDT_Int32,
gdal.GDT_UInt32,
gdal.GDT_Float32,
gdal.GDT_Float64,
gdal.GDT_CInt16,
gdal.GDT_CInt32,
gdal.GDT_CFloat32,
gdal.GDT_CFloat64, ], ids=gdal.GetDataTypeName)
@pytest.mark.parametrize(
"datatype",
[
gdal.GDT_Byte,
gdal.GDT_Int16,
gdal.GDT_UInt16,
gdal.GDT_Int32,
gdal.GDT_UInt32,
gdal.GDT_Float32,
gdal.GDT_Float64,
gdal.GDT_CInt16,
gdal.GDT_CInt32,
gdal.GDT_CFloat32,
gdal.GDT_CFloat64,
],
ids=gdal.GetDataTypeName,
)
def test_numpy_rw_multidim_datatype(datatype):
if gdaltest.numpy_drv is None:
pytest.skip()
import numpy as np
drv = gdal.GetDriverByName('MEM')
ds = drv.CreateMultiDimensional('myds')
drv = gdal.GetDriverByName("MEM")
ds = drv.CreateMultiDimensional("myds")
rg = ds.GetRootGroup()
dim = rg.CreateDimension("dim0", None, None, 2)
myarray = rg.CreateMDArray("myarray", [ dim ], gdal.ExtendedDataType.Create(datatype))
myarray = rg.CreateMDArray("myarray", [dim], gdal.ExtendedDataType.Create(datatype))
assert myarray
numpy_ar = np.reshape(np.arange(0, 2, dtype=np.uint16), (2,))
assert myarray.WriteArray(numpy_ar) == gdal.CE_None
got = myarray.ReadAsArray()
assert np.array_equal(got, numpy_ar)
assert np.array_equal(myarray.ReadAsArray(buf_obj = np.zeros(got.shape, got.dtype)), numpy_ar)
assert np.array_equal(
myarray.ReadAsArray(buf_obj=np.zeros(got.shape, got.dtype)), numpy_ar
)

View File

@ -29,57 +29,66 @@
# DEALINGS IN THE SOFTWARE.
###############################################################################
import gdaltest
import os
import shutil
import struct
from osgeo import gdal
import gdaltest
import pytest
from osgeo import gdal
###############################################################################
# Error cases
def test_overviewds_1():
ds = gdal.OpenEx('data/byte.tif', open_options=['OVERVIEW_LEVEL=0'])
ds = gdal.OpenEx("data/byte.tif", open_options=["OVERVIEW_LEVEL=0"])
assert ds is None
###############################################################################
# Nominal cases
@pytest.mark.parametrize("externalOverviews", [True, False])
def test_overviewds_2(tmp_path, externalOverviews):
shutil.copy('data/byte.tif', tmp_path)
tmpfilename = str(tmp_path.joinpath('byte.tif'))
shutil.copy("data/byte.tif", tmp_path)
tmpfilename = str(tmp_path.joinpath("byte.tif"))
if externalOverviews:
ds = gdal.Open(tmpfilename)
ds.BuildOverviews('NEAR', overviewlist=[2, 4])
ds.BuildOverviews("NEAR", overviewlist=[2, 4])
ds = None
ds = gdal.Open(tmpfilename, gdal.GA_Update)
else:
ds = gdal.Open(tmpfilename, gdal.GA_Update)
ds.BuildOverviews('NEAR', overviewlist=[2, 4])
ds.GetRasterBand(1).WriteRaster(2, 2, 5, 5, b'\0' * 25)
ds.GetRasterBand(1).WriteRaster(2, 2, 1, 1, b'\0')
ds.GetRasterBand(1).GetOverview(1).WriteRaster(2, 2, 1, 1, b'\0')
ds.BuildOverviews("NEAR", overviewlist=[2, 4])
ds.GetRasterBand(1).WriteRaster(2, 2, 5, 5, b"\0" * 25)
ds.GetRasterBand(1).WriteRaster(2, 2, 1, 1, b"\0")
ds.GetRasterBand(1).GetOverview(1).WriteRaster(2, 2, 1, 1, b"\0")
ds = None
src_ds = gdal.Open(tmpfilename)
ds = gdal.OpenEx(tmpfilename, open_options=['OVERVIEW_LEVEL=NONE'])
ds = gdal.OpenEx(tmpfilename, open_options=["OVERVIEW_LEVEL=NONE"])
assert ds.RasterXSize == 20 and ds.RasterYSize == 20 and ds.RasterCount == 1
assert ds.GetRasterBand(1).GetOverviewCount() == 0
assert ds.GetProjectionRef() == src_ds.GetProjectionRef()
assert ds.GetGeoTransform() == src_ds.GetGeoTransform()
assert ds.ReadRaster() == src_ds.ReadRaster()
# Check that subsampled request doesn't use source overviews
assert ds.ReadRaster(0, 0, 20, 20, 10, 10) != src_ds.GetRasterBand(1).GetOverview(0).ReadRaster()
assert ds.GetRasterBand(1).ReadRaster(0, 0, 20, 20, 10, 10) != src_ds.GetRasterBand(1).GetOverview(0).ReadRaster()
assert (
ds.ReadRaster(0, 0, 20, 20, 10, 10)
!= src_ds.GetRasterBand(1).GetOverview(0).ReadRaster()
)
assert (
ds.GetRasterBand(1).ReadRaster(0, 0, 20, 20, 10, 10)
!= src_ds.GetRasterBand(1).GetOverview(0).ReadRaster()
)
ds = None
ds = gdal.OpenEx(tmpfilename, open_options=['OVERVIEW_LEVEL=0only'])
ds = gdal.OpenEx(tmpfilename, open_options=["OVERVIEW_LEVEL=0only"])
assert ds.RasterXSize == 10 and ds.RasterYSize == 10 and ds.RasterCount == 1
assert ds.GetRasterBand(1).GetOverviewCount() == 0
expected_data = src_ds.GetRasterBand(1).GetOverview(1).ReadRaster()
@ -91,16 +100,27 @@ def test_overviewds_2(tmp_path, externalOverviews):
assert got_data != expected_data
ds = None
ds = gdal.OpenEx(tmpfilename, open_options=['OVERVIEW_LEVEL=0'])
ds = gdal.OpenEx(tmpfilename, open_options=["OVERVIEW_LEVEL=0"])
assert ds is not None
assert ds.RasterXSize == 10 and ds.RasterYSize == 10 and ds.RasterCount == 1
assert ds.GetProjectionRef() == src_ds.GetProjectionRef()
src_gt = src_ds.GetGeoTransform()
expected_gt = (src_gt[0], src_gt[1] * 2, src_gt[2], src_gt[3], src_gt[4], src_gt[5] * 2)
expected_gt = (
src_gt[0],
src_gt[1] * 2,
src_gt[2],
src_gt[3],
src_gt[4],
src_gt[5] * 2,
)
gt = ds.GetGeoTransform()
for i in range(6):
assert expected_gt[i] == pytest.approx(gt[i], abs=1e-5)
assert ds.GetGCPCount() == 0 and ds.GetGCPProjection() == src_ds.GetGCPProjection() and not ds.GetGCPs()
assert (
ds.GetGCPCount() == 0
and ds.GetGCPProjection() == src_ds.GetGCPProjection()
and not ds.GetGCPs()
)
expected_data = src_ds.GetRasterBand(1).GetOverview(0).ReadRaster()
got_data = ds.ReadRaster()
assert expected_data == got_data
@ -117,20 +137,23 @@ def test_overviewds_2(tmp_path, externalOverviews):
assert ds.GetRasterBand(1).GetMaskFlags() == gdal.GMF_ALL_VALID
assert ds.GetRasterBand(1).GetMaskBand()
assert ds.GetMetadata() == src_ds.GetMetadata()
assert ds.GetMetadataItem('AREA_OR_POINT') == src_ds.GetMetadataItem('AREA_OR_POINT')
assert not ds.GetMetadata('RPC')
assert not ds.GetMetadata('GEOLOCATION')
assert ds.GetMetadataItem('RPC', 'FOO') is None
assert ds.GetMetadataItem("AREA_OR_POINT") == src_ds.GetMetadataItem(
"AREA_OR_POINT"
)
assert not ds.GetMetadata("RPC")
assert not ds.GetMetadata("GEOLOCATION")
assert ds.GetMetadataItem("RPC", "FOO") is None
ds = None
###############################################################################
# Test GCP
def test_overviewds_3():
src_ds = gdal.Open('data/byte.tif')
ds = gdal.GetDriverByName('GTiff').CreateCopy('tmp/byte.tif', src_ds)
src_ds = gdal.Open("data/byte.tif")
ds = gdal.GetDriverByName("GTiff").CreateCopy("tmp/byte.tif", src_ds)
ds.SetGeoTransform([0, 1, 0, 0, 0, 1]) # cancel geotransform
gcp1 = gdal.GCP()
gcp1.GCPPixel = 0
@ -150,32 +173,37 @@ def test_overviewds_3():
src_gcps = (gcp1, gcp2, gcp3)
ds.SetGCPs(src_gcps, src_ds.GetProjectionRef())
tr = gdal.Transformer(ds, None, ['METHOD=GCP_POLYNOMIAL'])
tr = gdal.Transformer(ds, None, ["METHOD=GCP_POLYNOMIAL"])
(_, ref_pnt) = tr.TransformPoint(0, 20, 10)
ds.BuildOverviews('NEAR', overviewlist=[2, 4])
ds.BuildOverviews("NEAR", overviewlist=[2, 4])
ds = None
ds = gdal.OpenEx('tmp/byte.tif', open_options=['OVERVIEW_LEVEL=0'])
ds = gdal.OpenEx("tmp/byte.tif", open_options=["OVERVIEW_LEVEL=0"])
gcps = ds.GetGCPs()
for i in range(3):
assert (gcps[i].GCPPixel == src_gcps[i].GCPPixel / 2 and gcps[i].GCPLine == src_gcps[i].GCPLine / 2 and \
gcps[i].GCPX == src_gcps[i].GCPX and gcps[i].GCPY == src_gcps[i].GCPY)
assert (
gcps[i].GCPPixel == src_gcps[i].GCPPixel / 2
and gcps[i].GCPLine == src_gcps[i].GCPLine / 2
and gcps[i].GCPX == src_gcps[i].GCPX
and gcps[i].GCPY == src_gcps[i].GCPY
)
# Really check that the transformer works
tr = gdal.Transformer(ds, None, ['METHOD=GCP_POLYNOMIAL'])
tr = gdal.Transformer(ds, None, ["METHOD=GCP_POLYNOMIAL"])
(_, pnt) = tr.TransformPoint(0, 20 / 2.0, 10 / 2.0)
for i in range(3):
assert ref_pnt[i] == pytest.approx(pnt[i], abs=1e-5)
ds = None
###############################################################################
# Test RPC
def myfloat(s):
p = s.rfind(' ')
p = s.rfind(" ")
if p >= 0:
s = s[0:p]
return float(s)
@ -183,23 +211,28 @@ def myfloat(s):
def test_overviewds_4():
shutil.copy('data/byte.tif', 'tmp/byte.tif')
shutil.copy('data/test_rpc.txt', 'tmp/byte_rpc.txt')
ds = gdal.Open('tmp/byte.tif')
rpc_md = ds.GetMetadata('RPC')
shutil.copy("data/byte.tif", "tmp/byte.tif")
shutil.copy("data/test_rpc.txt", "tmp/byte_rpc.txt")
ds = gdal.Open("tmp/byte.tif")
rpc_md = ds.GetMetadata("RPC")
tr = gdal.Transformer(ds, None, ['METHOD=RPC'])
tr = gdal.Transformer(ds, None, ["METHOD=RPC"])
(_, ref_pnt) = tr.TransformPoint(0, 20, 10)
ds.BuildOverviews('NEAR', overviewlist=[2, 4])
ds.BuildOverviews("NEAR", overviewlist=[2, 4])
ds = None
ds = gdal.OpenEx('tmp/byte.tif', open_options=['OVERVIEW_LEVEL=0'])
got_md = ds.GetMetadata('RPC')
ds = gdal.OpenEx("tmp/byte.tif", open_options=["OVERVIEW_LEVEL=0"])
got_md = ds.GetMetadata("RPC")
for key in rpc_md:
assert ds.GetMetadataItem(key, 'RPC') == got_md[key]
if key == 'LINE_SCALE' or key == 'SAMP_SCALE' or key == 'LINE_OFF' or key == 'SAMP_OFF':
assert ds.GetMetadataItem(key, "RPC") == got_md[key]
if (
key == "LINE_SCALE"
or key == "SAMP_SCALE"
or key == "LINE_OFF"
or key == "SAMP_OFF"
):
assert float(got_md[key]) == myfloat(rpc_md[key]) / 2
elif got_md[key] != rpc_md[key]:
print(got_md[key])
@ -207,7 +240,7 @@ def test_overviewds_4():
pytest.fail(key)
# Really check that the transformer works
tr = gdal.Transformer(ds, None, ['METHOD=RPC'])
tr = gdal.Transformer(ds, None, ["METHOD=RPC"])
(_, pnt) = tr.TransformPoint(0, 20 / 2.0, 10 / 2.0)
for i in range(3):
@ -216,7 +249,7 @@ def test_overviewds_4():
ds = None
try:
os.remove('tmp/byte_rpc.txt')
os.remove("tmp/byte_rpc.txt")
except OSError:
pass
@ -227,34 +260,38 @@ def test_overviewds_4():
def test_overviewds_5():
shutil.copy('data/sstgeo.tif', 'tmp/sstgeo.tif')
shutil.copy('data/sstgeo.vrt', 'tmp/sstgeo.vrt')
shutil.copy("data/sstgeo.tif", "tmp/sstgeo.tif")
shutil.copy("data/sstgeo.vrt", "tmp/sstgeo.vrt")
ds = gdal.Open('tmp/sstgeo.vrt')
geoloc_md = ds.GetMetadata('GEOLOCATION')
ds = gdal.Open("tmp/sstgeo.vrt")
geoloc_md = ds.GetMetadata("GEOLOCATION")
tr = gdal.Transformer(ds, None, ['METHOD=GEOLOC_ARRAY'])
tr = gdal.Transformer(ds, None, ["METHOD=GEOLOC_ARRAY"])
(_, ref_pnt) = tr.TransformPoint(0, 20, 10)
ds.BuildOverviews('NEAR', overviewlist=[2, 4])
ds.BuildOverviews("NEAR", overviewlist=[2, 4])
ds = None
ds = gdal.OpenEx('tmp/sstgeo.vrt', open_options=['OVERVIEW_LEVEL=0'])
got_md = ds.GetMetadata('GEOLOCATION')
ds = gdal.OpenEx("tmp/sstgeo.vrt", open_options=["OVERVIEW_LEVEL=0"])
got_md = ds.GetMetadata("GEOLOCATION")
for key in geoloc_md:
assert ds.GetMetadataItem(key, 'GEOLOCATION') == got_md[key]
if key == 'PIXEL_OFFSET' or key == 'LINE_OFFSET':
assert float(got_md[key]) == pytest.approx(myfloat(geoloc_md[key]) * 2, abs=1e-1)
elif key == 'PIXEL_STEP' or key == 'LINE_STEP':
assert float(got_md[key]) == pytest.approx(myfloat(geoloc_md[key]) / 2, abs=1e-1)
assert ds.GetMetadataItem(key, "GEOLOCATION") == got_md[key]
if key == "PIXEL_OFFSET" or key == "LINE_OFFSET":
assert float(got_md[key]) == pytest.approx(
myfloat(geoloc_md[key]) * 2, abs=1e-1
)
elif key == "PIXEL_STEP" or key == "LINE_STEP":
assert float(got_md[key]) == pytest.approx(
myfloat(geoloc_md[key]) / 2, abs=1e-1
)
elif got_md[key] != geoloc_md[key]:
print(got_md[key])
print(geoloc_md[key])
pytest.fail(key)
# Really check that the transformer works
tr = gdal.Transformer(ds, None, ['METHOD=GEOLOC_ARRAY'])
tr = gdal.Transformer(ds, None, ["METHOD=GEOLOC_ARRAY"])
expected_xyz = (20.0 / 2, 10.0 / 2, 0)
(_, pnt) = tr.TransformPoint(1, ref_pnt[0], ref_pnt[1])
@ -262,59 +299,67 @@ def test_overviewds_5():
assert pnt[i] == pytest.approx(expected_xyz[i], abs=0.5)
ds = None
###############################################################################
# Test VRT
def test_overviewds_6():
shutil.copy('data/byte.tif', 'tmp')
ds = gdal.Open('tmp/byte.tif')
ds.BuildOverviews('NEAR', overviewlist=[2, 4])
shutil.copy("data/byte.tif", "tmp")
ds = gdal.Open("tmp/byte.tif")
ds.BuildOverviews("NEAR", overviewlist=[2, 4])
ds = None
src_ds = gdal.OpenEx('tmp/byte.tif', open_options=['OVERVIEW_LEVEL=0'])
src_ds = gdal.OpenEx("tmp/byte.tif", open_options=["OVERVIEW_LEVEL=0"])
expected_cs = src_ds.GetRasterBand(1).Checksum()
ds = gdal.GetDriverByName('VRT').CreateCopy('tmp/byte.vrt', src_ds)
ds = gdal.GetDriverByName("VRT").CreateCopy("tmp/byte.vrt", src_ds)
ds = None
src_ds = None
ds = gdal.Open('tmp/byte.vrt')
ds = gdal.Open("tmp/byte.vrt")
assert ds.RasterXSize == 10 and ds.RasterYSize == 10 and ds.RasterCount == 1
got_cs = ds.GetRasterBand(1).Checksum()
assert got_cs == expected_cs
ds = None
###############################################################################
# Dataset with a mask
def test_overviewds_mask():
with gdaltest.config_option('GDAL_TIFF_INTERNAL_MASK', 'YES'):
src_ds = gdal.GetDriverByName('GTiff').Create('/vsimem/test.tif', 4, 4)
with gdaltest.config_option("GDAL_TIFF_INTERNAL_MASK", "YES"):
src_ds = gdal.GetDriverByName("GTiff").Create("/vsimem/test.tif", 4, 4)
src_ds.CreateMaskBand(gdal.GMF_PER_DATASET)
src_ds.GetRasterBand(1).GetMaskBand().WriteRaster(0, 0, 2, 4, b'\xFF' * 8)
src_ds.BuildOverviews('NEAR', [2, 4])
src_ds.GetRasterBand(1).GetMaskBand().WriteRaster(0, 0, 2, 4, b"\xFF" * 8)
src_ds.BuildOverviews("NEAR", [2, 4])
src_ds = None
ovr_ds = gdal.OpenEx('/vsimem/test.tif', open_options=['OVERVIEW_LEVEL=0'])
ovr_ds = gdal.OpenEx("/vsimem/test.tif", open_options=["OVERVIEW_LEVEL=0"])
assert ovr_ds
assert ovr_ds.GetRasterBand(1).GetMaskFlags() == gdal.GMF_PER_DATASET
ovrmaskband = ovr_ds.GetRasterBand(1).GetMaskBand()
assert struct.unpack('B' * 4, ovrmaskband.ReadRaster()) == (255, 0, 255, 0)
assert struct.unpack("B" * 4, ovrmaskband.ReadRaster()) == (255, 0, 255, 0)
# Mask of mask
assert ovrmaskband.GetMaskFlags() == gdal.GMF_ALL_VALID
assert struct.unpack('B' * 4, ovrmaskband.GetMaskBand().ReadRaster()) == (255, 255, 255, 255)
assert struct.unpack("B" * 4, ovrmaskband.GetMaskBand().ReadRaster()) == (
255,
255,
255,
255,
)
# Overview of overview of mask
assert ovrmaskband.GetOverviewCount() == 1
ovrofovrmaskband = ovrmaskband.GetOverview(0)
assert struct.unpack('B' * 1, ovrofovrmaskband.ReadRaster()) == (255,)
assert struct.unpack("B" * 1, ovrofovrmaskband.ReadRaster()) == (255,)
ovr_ds = None
gdal.GetDriverByName('GTiff').Delete('/vsimem/test.tif')
gdal.GetDriverByName("GTiff").Delete("/vsimem/test.tif")
###############################################################################
@ -323,22 +368,18 @@ def test_overviewds_mask():
def test_overviewds_cleanup():
gdal.GetDriverByName('GTiff').Delete('tmp/byte.tif')
gdal.GetDriverByName("GTiff").Delete("tmp/byte.tif")
try:
os.remove('tmp/byte_rpc.txt')
os.remove("tmp/byte_rpc.txt")
except OSError:
pass
try:
os.remove('tmp/sstgeo.tif')
os.remove('tmp/sstgeo.vrt')
os.remove('tmp/sstgeo.vrt.ovr')
os.remove("tmp/sstgeo.tif")
os.remove("tmp/sstgeo.vrt")
os.remove("tmp/sstgeo.vrt.ovr")
except OSError:
pass
try:
os.remove('tmp/byte.vrt')
os.remove("tmp/byte.vrt")
except OSError:
pass

View File

@ -34,28 +34,29 @@ import os
import shutil
import stat
import gdaltest
from osgeo import gdal
from osgeo import osr
import pytest
from osgeo import gdal, osr
###############################################################################
@pytest.fixture(autouse=True, scope='module')
@pytest.fixture(autouse=True, scope="module")
def startup_and_cleanup():
with gdaltest.config_option('GDAL_PAM_ENABLED', 'YES'):
with gdaltest.config_option("GDAL_PAM_ENABLED", "YES"):
yield
try:
os.chmod('tmpdirreadonly', stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
shutil.rmtree('tmpdirreadonly')
os.chmod("tmpdirreadonly", stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
shutil.rmtree("tmpdirreadonly")
except OSError:
pass
try:
shutil.rmtree('tmppamproxydir')
shutil.rmtree("tmppamproxydir")
except OSError:
pass
###############################################################################
# Check that we can read PAM metadata for existing PNM file.
@ -65,20 +66,22 @@ def test_pam_1():
ds = gdal.Open("data/byte.pnm")
base_md = ds.GetMetadata()
assert len(base_md) == 2 and base_md['other'] == 'red' and base_md['key'] == 'value', \
'Default domain metadata missing'
assert (
len(base_md) == 2 and base_md["other"] == "red" and base_md["key"] == "value"
), "Default domain metadata missing"
xml_md = ds.GetMetadata('xml:test')
xml_md = ds.GetMetadata("xml:test")
assert len(xml_md) == 1, 'xml:test metadata missing'
assert len(xml_md) == 1, "xml:test metadata missing"
assert isinstance(xml_md, list), 'xml:test metadata not returned as list.'
assert isinstance(xml_md, list), "xml:test metadata not returned as list."
expected_xml = """<?xml version="2.0"?>
<TestXML>Value</TestXML>
"""
assert xml_md[0] == expected_xml, 'xml does not match'
assert xml_md[0] == expected_xml, "xml does not match"
###############################################################################
# Verify that we can write XML to a new file.
@ -86,22 +89,23 @@ def test_pam_1():
def test_pam_2():
driver = gdal.GetDriverByName('PNM')
ds = driver.Create('tmp/pam.pgm', 10, 10)
driver = gdal.GetDriverByName("PNM")
ds = driver.Create("tmp/pam.pgm", 10, 10)
band = ds.GetRasterBand(1)
band.SetMetadata({'other': 'red', 'key': 'value'})
band.SetMetadata({"other": "red", "key": "value"})
expected_xml = """<?xml version="2.0"?>
<TestXML>Value</TestXML>
"""
band.SetMetadata([expected_xml], 'xml:test')
band.SetMetadata([expected_xml], "xml:test")
band.SetNoDataValue(100)
ds = None
###############################################################################
# Check that we can read PAM metadata for existing PNM file.
@ -112,31 +116,34 @@ def test_pam_3():
band = ds.GetRasterBand(1)
base_md = band.GetMetadata()
assert len(base_md) == 2 and base_md['other'] == 'red' and base_md['key'] == 'value', \
'Default domain metadata missing'
assert (
len(base_md) == 2 and base_md["other"] == "red" and base_md["key"] == "value"
), "Default domain metadata missing"
xml_md = band.GetMetadata('xml:test')
xml_md = band.GetMetadata("xml:test")
assert len(xml_md) == 1, 'xml:test metadata missing'
assert len(xml_md) == 1, "xml:test metadata missing"
assert isinstance(xml_md, list), 'xml:test metadata not returned as list.'
assert isinstance(xml_md, list), "xml:test metadata not returned as list."
expected_xml = """<?xml version="2.0"?>
<TestXML>Value</TestXML>
"""
assert xml_md[0] == expected_xml, 'xml does not match'
assert xml_md[0] == expected_xml, "xml does not match"
assert band.GetNoDataValue() == 100, 'nodata not saved via pam'
assert band.GetNoDataValue() == 100, "nodata not saved via pam"
ds = None
ds = gdal.Open('tmp/pam.pgm', gdal.GA_Update)
ds = gdal.Open("tmp/pam.pgm", gdal.GA_Update)
assert ds.GetRasterBand(1).DeleteNoDataValue() == 0
ds = None
ds = gdal.Open('tmp/pam.pgm')
assert ds.GetRasterBand(1).GetNoDataValue() is None, \
'got nodata value whereas none was expected'
ds = gdal.Open("tmp/pam.pgm")
assert (
ds.GetRasterBand(1).GetNoDataValue() is None
), "got nodata value whereas none was expected"
###############################################################################
# Check that PAM binary encoded nodata values work properly.
@ -147,15 +154,17 @@ def test_pam_4():
# Copy test dataset to tmp directory so that the .aux.xml file
# won't be rewritten with the statistics in the master dataset.
shutil.copyfile('data/mfftest.hdr.aux.xml', 'tmp/mfftest.hdr.aux.xml')
shutil.copyfile('data/mfftest.hdr', 'tmp/mfftest.hdr')
shutil.copyfile('data/mfftest.r00', 'tmp/mfftest.r00')
shutil.copyfile("data/mfftest.hdr.aux.xml", "tmp/mfftest.hdr.aux.xml")
shutil.copyfile("data/mfftest.hdr", "tmp/mfftest.hdr")
shutil.copyfile("data/mfftest.r00", "tmp/mfftest.r00")
ds = gdal.Open('tmp/mfftest.hdr')
ds = gdal.Open("tmp/mfftest.hdr")
stats = ds.GetRasterBand(1).GetStatistics(0, 1)
assert stats[0] == 0 and stats[1] == 4, \
'Got wrong min/max, likely nodata not working?'
assert (
stats[0] == 0 and stats[1] == 4
), "Got wrong min/max, likely nodata not working?"
###############################################################################
# Verify that .aux files that don't match the configuration of the
@ -165,11 +174,12 @@ def test_pam_4():
def test_pam_5():
ds = gdal.Open('data/sasha.tif')
ds = gdal.Open("data/sasha.tif")
filelist = ds.GetFileList()
ds = None
assert len(filelist) == 1, 'did not get expected file list.'
assert len(filelist) == 1, "did not get expected file list."
###############################################################################
# Verify we can read nodata values from .aux files (#2505)
@ -178,13 +188,16 @@ def test_pam_5():
def test_pam_6():
ds = gdal.Open('data/f2r23.tif')
assert ds.GetRasterBand(1).GetNoDataValue() == 0, \
'did not get expected .aux sourced nodata.'
ds = gdal.Open("data/f2r23.tif")
assert (
ds.GetRasterBand(1).GetNoDataValue() == 0
), "did not get expected .aux sourced nodata."
ds = None
assert not os.path.exists('data/f2r23.tif.aux.xml'), \
'did not expect .aux.xml to be created.'
assert not os.path.exists(
"data/f2r23.tif.aux.xml"
), "did not expect .aux.xml to be created."
###############################################################################
# Verify we can create overviews on PNG with PAM disabled (#3693)
@ -193,22 +206,23 @@ def test_pam_6():
def test_pam_7():
with gdaltest.config_option('GDAL_PAM_ENABLED', 'NO'):
with gdaltest.config_option("GDAL_PAM_ENABLED", "NO"):
shutil.copyfile('data/stefan_full_rgba.png', 'tmp/stefan_full_rgba.png')
ds = gdal.Open('tmp/stefan_full_rgba.png')
ds.BuildOverviews('NEAR', [2])
shutil.copyfile("data/stefan_full_rgba.png", "tmp/stefan_full_rgba.png")
ds = gdal.Open("tmp/stefan_full_rgba.png")
ds.BuildOverviews("NEAR", [2])
ds = None
ds = gdal.Open('tmp/stefan_full_rgba.png')
ds = gdal.Open("tmp/stefan_full_rgba.png")
ovr_count = ds.GetRasterBand(1).GetOverviewCount()
ds = None
os.remove('tmp/stefan_full_rgba.png')
os.remove('tmp/stefan_full_rgba.png.ovr')
os.remove("tmp/stefan_full_rgba.png")
os.remove("tmp/stefan_full_rgba.png.ovr")
assert ovr_count == 1
###############################################################################
# Test that Band.SetDescription() goes through PAM (#3780)
#
@ -216,19 +230,20 @@ def test_pam_7():
def test_pam_8():
with gdaltest.config_option('GDAL_PAM_ENABLED', 'YES'):
with gdaltest.config_option("GDAL_PAM_ENABLED", "YES"):
ds = gdal.GetDriverByName('GTiff').Create('/vsimem/pam_8.tif', 1, 1, 1)
ds.GetRasterBand(1).SetDescription('foo')
ds = gdal.GetDriverByName("GTiff").Create("/vsimem/pam_8.tif", 1, 1, 1)
ds.GetRasterBand(1).SetDescription("foo")
ds = None
ds = gdal.Open('/vsimem/pam_8.tif')
ds = gdal.Open("/vsimem/pam_8.tif")
desc = ds.GetRasterBand(1).GetDescription()
ds = None
gdal.GetDriverByName('GTiff').Delete('/vsimem/pam_8.tif')
gdal.GetDriverByName("GTiff").Delete("/vsimem/pam_8.tif")
assert desc == "foo"
assert desc == 'foo'
###############################################################################
# Test that we can retrieve projection from xml:ESRI domain
@ -237,10 +252,10 @@ def test_pam_8():
def test_pam_9():
ds = gdal.GetDriverByName('GTiff').Create('/vsimem/pam_9.tif', 1, 1, 1)
ds = gdal.GetDriverByName("GTiff").Create("/vsimem/pam_9.tif", 1, 1, 1)
ds = None
f = gdal.VSIFOpenL('/vsimem/pam_9.tif.aux.xml', 'wb')
f = gdal.VSIFOpenL("/vsimem/pam_9.tif.aux.xml", "wb")
content = """<PAMDataset>
<Metadata domain="xml:ESRI" format="xml">
<GeodataXform xsi:type="typens:IdentityXform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:typens="http://www.esri.com/schemas/ArcGIS/9.2">
@ -254,44 +269,55 @@ def test_pam_9():
gdal.VSIFWriteL(content, 1, len(content), f)
gdal.VSIFCloseL(f)
ds = gdal.Open('/vsimem/pam_9.tif')
ds = gdal.Open("/vsimem/pam_9.tif")
wkt = ds.GetProjectionRef()
ds = None
gdal.GetDriverByName('GTiff').Delete('/vsimem/pam_9.tif')
gdal.GetDriverByName("GTiff").Delete("/vsimem/pam_9.tif")
expected_wkt = """PROJCS["NAD83 / UTM zone 14N",GEOGCS["NAD83",DATUM["North_American_Datum_1983",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],AUTHORITY["EPSG","6269"]],PRIMEM["Greenwich",0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-99],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH]]"""
assert wkt == expected_wkt
###############################################################################
# Test serializing and deserializing of various band metadata
def test_pam_10():
src_ds = gdal.Open('data/testserialization.asc')
ds = gdal.GetDriverByName('AAIGRID').CreateCopy('/vsimem/pam_10.asc', src_ds)
src_ds = gdal.Open("data/testserialization.asc")
ds = gdal.GetDriverByName("AAIGRID").CreateCopy("/vsimem/pam_10.asc", src_ds)
src_ds = None
ds = None
ds = gdal.Open('/vsimem/pam_10.asc')
ds = gdal.Open("/vsimem/pam_10.asc")
gcps = ds.GetGCPs()
assert len(gcps) == 2 and ds.GetGCPCount() == 2
assert ds.GetGCPProjection().find("WGS 84") != -1
assert (gcps[0].GCPPixel == 0 and gcps[0].GCPLine == 1 and \
gcps[0].GCPX == 2 and gcps[0].GCPY == 3 and gcps[0].GCPZ == 4)
assert (
gcps[0].GCPPixel == 0
and gcps[0].GCPLine == 1
and gcps[0].GCPX == 2
and gcps[0].GCPY == 3
and gcps[0].GCPZ == 4
)
assert (gcps[1].GCPPixel == 1 and gcps[1].GCPLine == 2 and \
gcps[1].GCPX == 3 and gcps[1].GCPY == 4 and gcps[1].GCPZ == 5)
assert (
gcps[1].GCPPixel == 1
and gcps[1].GCPLine == 2
and gcps[1].GCPX == 3
and gcps[1].GCPY == 4
and gcps[1].GCPZ == 5
)
band = ds.GetRasterBand(1)
assert band.GetDescription() == 'MyDescription'
assert band.GetDescription() == "MyDescription"
assert band.GetUnitType() == 'MyUnit'
assert band.GetUnitType() == "MyUnit"
assert band.GetOffset() == 1
@ -299,7 +325,7 @@ def test_pam_10():
assert band.GetRasterColorInterpretation() == gdal.GCI_PaletteIndex
assert band.GetCategoryNames() == ['Cat1', 'Cat2']
assert band.GetCategoryNames() == ["Cat1", "Cat2"]
ct = band.GetColorTable()
assert ct.GetColorEntry(0) == (0, 0, 0, 255)
@ -309,12 +335,18 @@ def test_pam_10():
assert band.GetMinimum() == 2
assert band.GetMetadata() == {'STATISTICS_MEAN': '1', 'STATISTICS_MINIMUM': '2', 'STATISTICS_MAXIMUM': '0', 'STATISTICS_STDDEV': '3'}
assert band.GetMetadata() == {
"STATISTICS_MEAN": "1",
"STATISTICS_MINIMUM": "2",
"STATISTICS_MAXIMUM": "0",
"STATISTICS_STDDEV": "3",
}
ds = None
gdal.Unlink('/vsimem/pam_10.asc')
gdal.Unlink('/vsimem/pam_10.asc.aux.xml')
gdal.Unlink("/vsimem/pam_10.asc")
gdal.Unlink("/vsimem/pam_10.asc.aux.xml")
###############################################################################
# Test PamProxyDb mechanism
@ -324,20 +356,20 @@ def test_pam_11():
# Create a read-only directory
try:
os.chmod('tmpdirreadonly', stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
shutil.rmtree('tmpdirreadonly')
os.chmod("tmpdirreadonly", stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR)
shutil.rmtree("tmpdirreadonly")
except OSError:
pass
os.mkdir('tmpdirreadonly')
shutil.copy('data/byte.tif', 'tmpdirreadonly/byte.tif')
os.mkdir("tmpdirreadonly")
shutil.copy("data/byte.tif", "tmpdirreadonly/byte.tif")
# FIXME: how do we create a read-only dir on windows ?
# The following has no effect
os.chmod('tmpdirreadonly', stat.S_IRUSR | stat.S_IXUSR)
os.chmod("tmpdirreadonly", stat.S_IRUSR | stat.S_IXUSR)
# Test that the directory is really read-only
try:
f = open('tmpdirreadonly/test', 'w')
f = open("tmpdirreadonly/test", "w")
if f is not None:
f.close()
pytest.skip()
@ -345,31 +377,34 @@ def test_pam_11():
pass
# Compute statistics --> the saving as .aux.xml should fail
ds = gdal.Open('tmpdirreadonly/byte.tif')
ds = gdal.Open("tmpdirreadonly/byte.tif")
stats = ds.GetRasterBand(1).ComputeStatistics(False)
assert stats[0] == 74, 'did not get expected minimum'
assert stats[0] == 74, "did not get expected minimum"
gdal.ErrorReset()
with gdaltest.error_handler():
ds = None
error_msg = gdal.GetLastErrorMsg()
assert error_msg.startswith('Unable to save auxiliary information'), \
'warning was expected at that point'
assert error_msg.startswith(
"Unable to save auxiliary information"
), "warning was expected at that point"
# Check that we actually have no saved statistics
ds = gdal.Open('tmpdirreadonly/byte.tif')
ds = gdal.Open("tmpdirreadonly/byte.tif")
stats = ds.GetRasterBand(1).GetStatistics(False, False)
assert stats[3] == -1, 'did not expected to have stats at that point'
assert stats[3] == -1, "did not expected to have stats at that point"
ds = None
# This must be run as an external process so we can override GDAL_PAM_PROXY_DIR
# at the beginning of the process
import test_py_scripts
ret = test_py_scripts.run_py_script_as_external_script('.', 'pamproxydb', '-test1')
assert ret.find('success') != -1, ('pamproxydb.py -test1 failed %s' % ret)
ret = test_py_scripts.run_py_script_as_external_script(".", "pamproxydb", "-test1")
assert ret.find("success") != -1, "pamproxydb.py -test1 failed %s" % ret
# Test loading an existing proxydb
ret = test_py_scripts.run_py_script_as_external_script('.', 'pamproxydb', '-test2')
assert ret.find('success') != -1, ('pamproxydb.py -test2 failed %s' % ret)
ret = test_py_scripts.run_py_script_as_external_script(".", "pamproxydb", "-test2")
assert ret.find("success") != -1, "pamproxydb.py -test2 failed %s" % ret
###############################################################################
# Test histogram with 64bit counts
@ -377,8 +412,9 @@ def test_pam_11():
def test_pam_12():
shutil.copy('data/byte.tif', 'tmp')
open('tmp/byte.tif.aux.xml', 'wt').write("""<PAMDataset>
shutil.copy("data/byte.tif", "tmp")
open("tmp/byte.tif.aux.xml", "wt").write(
"""<PAMDataset>
<PAMRasterBand band="1">
<Histograms>
<HistItem>
@ -391,21 +427,23 @@ def test_pam_12():
</HistItem>
</Histograms>
</PAMRasterBand>
</PAMDataset>""")
</PAMDataset>"""
)
ds = gdal.Open('tmp/byte.tif')
ds = gdal.Open("tmp/byte.tif")
(mini, maxi, _, hist1) = ds.GetRasterBand(1).GetDefaultHistogram()
hist2 = ds.GetRasterBand(1).GetHistogram(include_out_of_range=1, approx_ok=0)
ds.SetMetadataItem('FOO', 'BAR')
ds.SetMetadataItem("FOO", "BAR")
ds.GetRasterBand(1).SetDefaultHistogram(mini, maxi, hist1)
ds = None
aux_xml = open('tmp/byte.tif.aux.xml', 'rt').read()
gdal.Unlink('tmp/byte.tif')
gdal.Unlink('tmp/byte.tif.aux.xml')
aux_xml = open("tmp/byte.tif.aux.xml", "rt").read()
gdal.Unlink("tmp/byte.tif")
gdal.Unlink("tmp/byte.tif.aux.xml")
assert hist1 == hist2
assert hist1[0] == 6000000000
assert '<HistCounts>6000000000|' in aux_xml
assert "<HistCounts>6000000000|" in aux_xml
###############################################################################
# Test various stuff with PAM disabled
@ -414,14 +452,14 @@ def test_pam_12():
def test_pam_13():
with gdaltest.config_option('GDAL_PAM_ENABLED', 'NO'):
with gdaltest.config_option("GDAL_PAM_ENABLED", "NO"):
tmpfilename = '/vsimem/tmp.pgm'
ds = gdal.GetDriverByName('PNM').Create(tmpfilename, 1, 1)
tmpfilename = "/vsimem/tmp.pgm"
ds = gdal.GetDriverByName("PNM").Create(tmpfilename, 1, 1)
ds.GetRasterBand(1).SetNoDataValue(0)
ds = None
assert gdal.VSIStatL(tmpfilename + '.aux.xml') is None
assert gdal.VSIStatL(tmpfilename + ".aux.xml") is None
gdal.Unlink(tmpfilename)
@ -433,30 +471,35 @@ def test_pam_13():
def test_pam_metadata_preserved():
tmpfilename = '/vsimem/tmp.pgm'
ds = gdal.GetDriverByName('PNM').Create(tmpfilename, 1, 1)
ds.SetMetadataItem('foo', 'bar')
tmpfilename = "/vsimem/tmp.pgm"
ds = gdal.GetDriverByName("PNM").Create(tmpfilename, 1, 1)
ds.SetMetadataItem("foo", "bar")
ds = None
ds = gdal.Open(tmpfilename)
ds.GetRasterBand(1).SetMetadataItem('bar', 'baz')
ds.GetRasterBand(1).SetMetadataItem("bar", "baz")
ds = None
ds = gdal.Open(tmpfilename)
assert ds.GetMetadataItem('foo') == 'bar'
assert ds.GetRasterBand(1).GetMetadataItem('bar') == 'baz'
assert ds.GetMetadataItem("foo") == "bar"
assert ds.GetRasterBand(1).GetMetadataItem("bar") == "baz"
ds = None
gdal.GetDriverByName('PNM').Delete(tmpfilename)
gdal.GetDriverByName("PNM").Delete(tmpfilename)
###############################################################################
# Test that we can retrieve GCPs from xml:ESRI domain
#
def test_pam_esri_GeodataXform_gcp():
ds = gdal.GetDriverByName('GTiff').Create('/vsimem/test_pam_esri_GeodataXform_gcp.tif', 20, 20, 1)
ds = gdal.GetDriverByName("GTiff").Create(
"/vsimem/test_pam_esri_GeodataXform_gcp.tif", 20, 20, 1
)
ds = None
gdal.FileFromMemBuffer('/vsimem/test_pam_esri_GeodataXform_gcp.tif.aux.xml',
"""<PAMDataset>
gdal.FileFromMemBuffer(
"/vsimem/test_pam_esri_GeodataXform_gcp.tif.aux.xml",
"""<PAMDataset>
<Metadata domain="xml:ESRI" format="xml">
<GeodataXform xsi:type="typens:PolynomialXform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:typens="http://www.esri.com/schemas/ArcGIS/10.3">
<PolynomialOrder>1</PolynomialOrder>
@ -492,14 +535,15 @@ def test_pam_esri_GeodataXform_gcp():
</TargetGCPs>
</GeodataXform>
</Metadata>
</PAMDataset>""")
</PAMDataset>""",
)
ds = gdal.Open('/vsimem/test_pam_esri_GeodataXform_gcp.tif')
ds = gdal.Open("/vsimem/test_pam_esri_GeodataXform_gcp.tif")
gcps = ds.GetGCPs()
sr_gt = ds.GetSpatialRef()
sr_gcp = ds.GetGCPSpatialRef()
gdal.GetDriverByName('GTiff').Delete('/vsimem/test_pam_esri_GeodataXform_gcp.tif')
gdal.GetDriverByName("GTiff").Delete("/vsimem/test_pam_esri_GeodataXform_gcp.tif")
assert len(gcps) == 3
assert gcps[0].GCPPixel == 1
@ -517,13 +561,14 @@ def test_pam_esri_GeodataXform_gcp():
ds = None
###############################################################################
def test_pam_metadata_coordinate_epoch():
tmpfilename = '/vsimem/tmp.pgm'
ds = gdal.GetDriverByName('PNM').Create(tmpfilename, 1, 1)
tmpfilename = "/vsimem/tmp.pgm"
ds = gdal.GetDriverByName("PNM").Create(tmpfilename, 1, 1)
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)
srs.SetCoordinateEpoch(2021.3)
@ -535,19 +580,19 @@ def test_pam_metadata_coordinate_epoch():
assert srs.GetCoordinateEpoch() == 2021.3
ds = None
gdal.GetDriverByName('PNM').Delete(tmpfilename)
gdal.GetDriverByName("PNM").Delete(tmpfilename)
###############################################################################
# Check that PAM handles correctly equality of NaN nodata values (#4847)
def test_pam_nodata_nan():
outfilename = '/vsimem/test_pam_nodata_nan.tif'
src_ds = gdal.GetDriverByName('MEM').Create('', 1, 1, 1, gdal.GDT_Float32)
src_ds.GetRasterBand(1).SetNoDataValue(float('nan'))
gdal.GetDriverByName('GTiff').CreateCopy(outfilename, src_ds)
outfilename = "/vsimem/test_pam_nodata_nan.tif"
src_ds = gdal.GetDriverByName("MEM").Create("", 1, 1, 1, gdal.GDT_Float32)
src_ds.GetRasterBand(1).SetNoDataValue(float("nan"))
gdal.GetDriverByName("GTiff").CreateCopy(outfilename, src_ds)
# Check that no PAM file is generated
assert gdal.VSIStatL(outfilename + '.aux.xml') is None
gdal.GetDriverByName('GTiff').Delete(outfilename)
assert gdal.VSIStatL(outfilename + ".aux.xml") is None
gdal.GetDriverByName("GTiff").Delete(outfilename)

View File

@ -28,47 +28,50 @@
# DEALINGS IN THE SOFTWARE.
###############################################################################
import sys
import os
import sys
try:
os.putenv('CPL_SHOW_MEM_STATS', '')
os.putenv("CPL_SHOW_MEM_STATS", "")
except OSError:
pass
# Must to be launched from pam.py/pam_11()
# Test creating a new proxydb
if len(sys.argv) == 2 and sys.argv[1] == '-test1':
if len(sys.argv) == 2 and sys.argv[1] == "-test1":
from osgeo import gdal
import shutil
from osgeo import gdal
try:
shutil.rmtree('tmppamproxydir')
shutil.rmtree("tmppamproxydir")
except OSError:
pass
os.mkdir('tmppamproxydir')
os.mkdir("tmppamproxydir")
gdal.SetConfigOption('GDAL_PAM_PROXY_DIR', 'tmppamproxydir')
gdal.SetConfigOption("GDAL_PAM_PROXY_DIR", "tmppamproxydir")
# Compute statistics. They should be saved in the .aux.xml in the proxyDB
ds = gdal.Open('tmpdirreadonly/byte.tif')
ds = gdal.Open("tmpdirreadonly/byte.tif")
stats = ds.GetRasterBand(1).ComputeStatistics(False)
gdal.ErrorReset()
ds = None
error_msg = gdal.GetLastErrorMsg()
if error_msg != '':
print('did not expected error message')
if error_msg != "":
print("did not expected error message")
sys.exit(1)
# Check that the .aux.xml in the proxyDB exists
filelist = gdal.ReadDir('tmppamproxydir')
if '000000_tmpdirreadonly_byte.tif.aux.xml' not in filelist:
print('did not get find 000000_tmpdirreadonly_byte.tif.aux.xml on filesystem')
filelist = gdal.ReadDir("tmppamproxydir")
if "000000_tmpdirreadonly_byte.tif.aux.xml" not in filelist:
print("did not get find 000000_tmpdirreadonly_byte.tif.aux.xml on filesystem")
sys.exit(1)
# Test altering a value to check that the file will be used
f = open('tmppamproxydir/000000_tmpdirreadonly_byte.tif.aux.xml', 'w')
f.write("""<PAMDataset>
f = open("tmppamproxydir/000000_tmpdirreadonly_byte.tif.aux.xml", "w")
f.write(
"""<PAMDataset>
<PAMRasterBand band="1">
<Metadata>
<MDI key="STATISTICS_MAXIMUM">255</MDI>
@ -77,78 +80,85 @@ if len(sys.argv) == 2 and sys.argv[1] == '-test1':
<MDI key="STATISTICS_STDDEV">22.928470838676</MDI>
</Metadata>
</PAMRasterBand>
</PAMDataset>""")
</PAMDataset>"""
)
f.close()
ds = gdal.Open('tmpdirreadonly/byte.tif')
ds = gdal.Open("tmpdirreadonly/byte.tif")
filelist = ds.GetFileList()
if len(filelist) != 2:
print('did not get find 000000_tmpdirreadonly_byte.tif.aux.xml in dataset GetFileList()')
print(
"did not get find 000000_tmpdirreadonly_byte.tif.aux.xml in dataset GetFileList()"
)
print(filelist)
sys.exit(1)
stats = ds.GetRasterBand(1).GetStatistics(False, False)
if stats[0] != -9999:
print('did not get expected minimum')
print("did not get expected minimum")
sys.exit(1)
ds = None
# Check that proxy overviews work
ds = gdal.Open('tmpdirreadonly/byte.tif')
ds = gdal.Open("tmpdirreadonly/byte.tif")
gdal.PushErrorHandler()
assert ds.BuildOverviews('NEAR', overviewlist=[2]) == gdal.CE_None
assert ds.BuildOverviews("NEAR", overviewlist=[2]) == gdal.CE_None
gdal.PopErrorHandler()
ds = None
filelist = gdal.ReadDir('tmppamproxydir')
if '000001_tmpdirreadonly_byte.tif.ovr' not in filelist:
print('did not get find 000001_tmpdirreadonly_byte.tif.ovr')
filelist = gdal.ReadDir("tmppamproxydir")
if "000001_tmpdirreadonly_byte.tif.ovr" not in filelist:
print("did not get find 000001_tmpdirreadonly_byte.tif.ovr")
sys.exit(1)
ds = gdal.Open('tmpdirreadonly/byte.tif')
ds = gdal.Open("tmpdirreadonly/byte.tif")
filelist = ds.GetFileList()
if len(filelist) != 3:
print('did not get find 000001_tmpdirreadonly_byte.tif.ovr in dataset GetFileList()')
print(
"did not get find 000001_tmpdirreadonly_byte.tif.ovr in dataset GetFileList()"
)
print(filelist)
sys.exit(1)
nb_ovr = ds.GetRasterBand(1).GetOverviewCount()
ds = None
if nb_ovr != 1:
print('did not get expected overview count')
print("did not get expected overview count")
sys.exit(1)
print('success')
print("success")
sys.exit(0)
# Must to be launched from pam.py/pam_11()
# Test loading an existing proxydb
if len(sys.argv) == 2 and sys.argv[1] == '-test2':
if len(sys.argv) == 2 and sys.argv[1] == "-test2":
from osgeo import gdal
gdal.SetConfigOption('GDAL_PAM_PROXY_DIR', 'tmppamproxydir')
gdal.SetConfigOption("GDAL_PAM_PROXY_DIR", "tmppamproxydir")
ds = gdal.Open('tmpdirreadonly/byte.tif')
ds = gdal.Open("tmpdirreadonly/byte.tif")
filelist = ds.GetFileList()
if len(filelist) != 3:
print('did not get find 000000_tmpdirreadonly_byte.tif.aux.xml and/or 000001_tmpdirreadonly_byte.tif.ovr in dataset GetFileList()')
print(
"did not get find 000000_tmpdirreadonly_byte.tif.aux.xml and/or 000001_tmpdirreadonly_byte.tif.ovr in dataset GetFileList()"
)
print(filelist)
sys.exit(1)
stats = ds.GetRasterBand(1).GetStatistics(False, False)
if stats[0] != -9999:
print('did not get expected minimum')
print("did not get expected minimum")
sys.exit(1)
nb_ovr = ds.GetRasterBand(1).GetOverviewCount()
ds = None
if nb_ovr != 1:
print('did not get expected overview count')
print("did not get expected overview count")
sys.exit(1)
print('success')
print("success")
sys.exit(0)

File diff suppressed because it is too large Load Diff

View File

@ -28,24 +28,21 @@
# DEALINGS IN THE SOFTWARE.
###############################################################################
import pytest
import gdaltest
import pytest
###############################################################################
# When imported build a list of units based on the files available.
init_list = [
('byte.pnm', 4672),
('uint16.pnm', 4672)]
init_list = [("byte.pnm", 4672), ("uint16.pnm", 4672)]
@pytest.mark.parametrize(
'filename,checksum',
"filename,checksum",
init_list,
ids=[tup[0].split('.')[0] for tup in init_list],
ids=[tup[0].split(".")[0] for tup in init_list],
)
@pytest.mark.require_driver('PNM')
@pytest.mark.require_driver("PNM")
def test_pnm_open(filename, checksum):
ut = gdaltest.GDALTest('PNM', filename, 1, checksum)
ut = gdaltest.GDALTest("PNM", filename, 1, checksum)
ut.testOpen()

View File

@ -25,27 +25,25 @@
# Boston, MA 02111-1307, USA.
###############################################################################
import gdaltest
import pytest
import gdaltest
init_list = [
('byte.tif', 4672),
('uint16.tif', 4672)]
init_list = [("byte.tif", 4672), ("uint16.tif", 4672)]
@pytest.mark.parametrize(
'filename,checksum',
"filename,checksum",
init_list,
ids=[tup[0].split('.')[0] for tup in init_list],
ids=[tup[0].split(".")[0] for tup in init_list],
)
@pytest.mark.parametrize(
'testfunction', [
'testCreateCopy',
'testCreate',
]
"testfunction",
[
"testCreateCopy",
"testCreate",
],
)
@pytest.mark.require_driver('PNM')
@pytest.mark.require_driver("PNM")
def test_pnm_create(filename, checksum, testfunction):
ut = gdaltest.GDALTest('PNM', filename, 1, checksum)
ut = gdaltest.GDALTest("PNM", filename, 1, checksum)
getattr(ut, testfunction)()

File diff suppressed because it is too large Load Diff

View File

@ -32,9 +32,10 @@
import gdaltest
from osgeo import gdal
import pytest
from osgeo import gdal
###############################################################################
# Create a raster attribute table.
@ -48,8 +49,8 @@ def test_rat_1():
except:
pytest.skip()
rat.CreateColumn('Value', gdal.GFT_Integer, gdal.GFU_MinMax)
rat.CreateColumn('Count', gdal.GFT_Integer, gdal.GFU_PixelCount)
rat.CreateColumn("Value", gdal.GFT_Integer, gdal.GFU_MinMax)
rat.CreateColumn("Count", gdal.GFT_Integer, gdal.GFU_PixelCount)
rat.SetRowCount(3)
rat.SetValueAsInt(0, 0, 10)
@ -61,22 +62,23 @@ def test_rat_1():
rat2 = rat.Clone()
assert rat2.GetColumnCount() == 2, 'wrong column count'
assert rat2.GetColumnCount() == 2, "wrong column count"
assert rat2.GetRowCount() == 3, 'wrong row count'
assert rat2.GetRowCount() == 3, "wrong row count"
assert rat2.GetNameOfCol(1) == 'Count', 'wrong column name'
assert rat2.GetNameOfCol(1) == "Count", "wrong column name"
assert rat2.GetUsageOfCol(1) == gdal.GFU_PixelCount, 'wrong column usage'
assert rat2.GetUsageOfCol(1) == gdal.GFU_PixelCount, "wrong column usage"
assert rat2.GetTypeOfCol(1) == gdal.GFT_Integer, 'wrong column type'
assert rat2.GetTypeOfCol(1) == gdal.GFT_Integer, "wrong column type"
assert rat2.GetRowOfValue(11.0) == 1, 'wrong row for value'
assert rat2.GetRowOfValue(11.0) == 1, "wrong row for value"
assert rat2.GetValueAsInt(1, 1) == 200, 'wrong field value.'
assert rat2.GetValueAsInt(1, 1) == 200, "wrong field value."
gdaltest.saved_rat = rat
###############################################################################
# Save a RAT in a file, written to .aux.xml, read it back and check it.
@ -86,54 +88,55 @@ def test_rat_2():
if gdaltest.saved_rat is None:
pytest.skip()
ds = gdal.GetDriverByName('PNM').Create('tmp/rat_2.pnm', 100, 90, 1,
gdal.GDT_Byte)
ds = gdal.GetDriverByName("PNM").Create("tmp/rat_2.pnm", 100, 90, 1, gdal.GDT_Byte)
ds.GetRasterBand(1).SetDefaultRAT(gdaltest.saved_rat)
ds = None
ds = gdal.Open('tmp/rat_2.pnm', gdal.GA_Update)
ds = gdal.Open("tmp/rat_2.pnm", gdal.GA_Update)
rat2 = ds.GetRasterBand(1).GetDefaultRAT()
assert rat2.GetColumnCount() == 2, 'wrong column count'
assert rat2.GetColumnCount() == 2, "wrong column count"
assert rat2.GetRowCount() == 3, 'wrong row count'
assert rat2.GetRowCount() == 3, "wrong row count"
assert rat2.GetNameOfCol(1) == 'Count', 'wrong column name'
assert rat2.GetNameOfCol(1) == "Count", "wrong column name"
assert rat2.GetUsageOfCol(1) == gdal.GFU_PixelCount, 'wrong column usage'
assert rat2.GetUsageOfCol(1) == gdal.GFU_PixelCount, "wrong column usage"
assert rat2.GetTypeOfCol(1) == gdal.GFT_Integer, 'wrong column type'
assert rat2.GetTypeOfCol(1) == gdal.GFT_Integer, "wrong column type"
assert rat2.GetRowOfValue(11.0) == 1, 'wrong row for value'
assert rat2.GetRowOfValue(11.0) == 1, "wrong row for value"
assert rat2.GetValueAsInt(1, 1) == 200, 'wrong field value.'
assert rat2.GetValueAsInt(1, 1) == 200, "wrong field value."
# unset the RAT
ds.GetRasterBand(1).SetDefaultRAT(None)
ds = None
ds = gdal.Open('tmp/rat_2.pnm')
ds = gdal.Open("tmp/rat_2.pnm")
rat = ds.GetRasterBand(1).GetDefaultRAT()
ds = None
assert rat is None, 'expected a NULL RAT.'
assert rat is None, "expected a NULL RAT."
gdal.GetDriverByName('PNM').Delete('tmp/rat_2.pnm')
gdal.GetDriverByName("PNM").Delete("tmp/rat_2.pnm")
gdaltest.saved_rat = None
###############################################################################
# Save an empty RAT (#5451)
def test_rat_3():
ds = gdal.GetDriverByName('GTiff').Create('/vsimem/rat_3.tif', 1, 1)
ds = gdal.GetDriverByName("GTiff").Create("/vsimem/rat_3.tif", 1, 1)
ds.GetRasterBand(1).SetDefaultRAT(gdal.RasterAttributeTable())
ds = None
gdal.GetDriverByName('GTiff').Delete('/vsimem/rat_3.tif')
gdal.GetDriverByName("GTiff").Delete("/vsimem/rat_3.tif")
###############################################################################
# Edit an existing RAT (#3783)
@ -142,17 +145,17 @@ def test_rat_3():
def test_rat_4():
# Create test RAT
ds = gdal.GetDriverByName('GTiff').Create('/vsimem/rat_4.tif', 1, 1)
ds = gdal.GetDriverByName("GTiff").Create("/vsimem/rat_4.tif", 1, 1)
rat = gdal.RasterAttributeTable()
rat.CreateColumn('VALUE', gdal.GFT_Integer, gdal.GFU_MinMax)
rat.CreateColumn('CLASS', gdal.GFT_String, gdal.GFU_Name)
rat.CreateColumn("VALUE", gdal.GFT_Integer, gdal.GFU_MinMax)
rat.CreateColumn("CLASS", gdal.GFT_String, gdal.GFU_Name)
rat.SetValueAsInt(0, 0, 111)
rat.SetValueAsString(0, 1, 'Class1')
rat.SetValueAsString(0, 1, "Class1")
ds.GetRasterBand(1).SetDefaultRAT(rat)
ds = None
# Verify
ds = gdal.OpenEx('/vsimem/rat_4.tif')
ds = gdal.OpenEx("/vsimem/rat_4.tif")
gdal_band = ds.GetRasterBand(1)
rat = gdal_band.GetDefaultRAT()
assert rat.GetValueAsInt(0, 0) == 111
@ -160,23 +163,24 @@ def test_rat_4():
# Replace existing RAT
rat = gdal.RasterAttributeTable()
rat.CreateColumn('VALUE', gdal.GFT_Integer, gdal.GFU_MinMax)
rat.CreateColumn('CLASS', gdal.GFT_String, gdal.GFU_Name)
rat.CreateColumn("VALUE", gdal.GFT_Integer, gdal.GFU_MinMax)
rat.CreateColumn("CLASS", gdal.GFT_String, gdal.GFU_Name)
rat.SetValueAsInt(0, 0, 222)
rat.SetValueAsString(0, 1, 'Class1')
ds = gdal.OpenEx('/vsimem/rat_4.tif', gdal.OF_RASTER | gdal.OF_UPDATE)
rat.SetValueAsString(0, 1, "Class1")
ds = gdal.OpenEx("/vsimem/rat_4.tif", gdal.OF_RASTER | gdal.OF_UPDATE)
gdal_band = ds.GetRasterBand(1)
gdal_band.SetDefaultRAT(rat)
ds = None
# Verify
ds = gdal.OpenEx('/vsimem/rat_4.tif')
ds = gdal.OpenEx("/vsimem/rat_4.tif")
gdal_band = ds.GetRasterBand(1)
rat = gdal_band.GetDefaultRAT()
assert rat is not None
assert rat.GetValueAsInt(0, 0) == 222
ds = None
gdal.GetDriverByName('GTiff').Delete('/vsimem/rat_4.tif')
gdal.GetDriverByName("GTiff").Delete("/vsimem/rat_4.tif")
##############################################################################

View File

@ -28,57 +28,61 @@
# DEALINGS IN THE SOFTWARE.
###############################################################################
from osgeo import gdal
import pytest
from osgeo import gdal
def test_gdal_relationship():
with pytest.raises(ValueError):
gdal.Relationship(None, None, None, gdal.GRC_ONE_TO_ONE)
relationship = gdal.Relationship('name', 'left_table', 'right_table', gdal.GRC_MANY_TO_ONE)
assert relationship.GetName() == 'name'
relationship = gdal.Relationship(
"name", "left_table", "right_table", gdal.GRC_MANY_TO_ONE
)
assert relationship.GetName() == "name"
assert relationship.GetCardinality() == gdal.GRC_MANY_TO_ONE
assert relationship.GetLeftTableName() == 'left_table'
assert relationship.GetRightTableName() == 'right_table'
assert relationship.GetLeftTableName() == "left_table"
assert relationship.GetRightTableName() == "right_table"
relationship = gdal.Relationship('name', 'left_table', 'right_table', gdal.GRC_ONE_TO_ONE)
relationship = gdal.Relationship(
"name", "left_table", "right_table", gdal.GRC_ONE_TO_ONE
)
assert relationship.GetCardinality() == gdal.GRC_ONE_TO_ONE
assert relationship.GetMappingTableName() == ''
relationship.SetMappingTableName('mapping_table')
assert relationship.GetMappingTableName() == 'mapping_table'
assert relationship.GetMappingTableName() == ""
relationship.SetMappingTableName("mapping_table")
assert relationship.GetMappingTableName() == "mapping_table"
assert relationship.GetLeftTableFields() is None
relationship.SetLeftTableFields(['a_field'])
assert relationship.GetLeftTableFields() == ['a_field']
relationship.SetLeftTableFields(["a_field"])
assert relationship.GetLeftTableFields() == ["a_field"]
assert relationship.GetRightTableFields() is None
relationship.SetRightTableFields(['b_field', 'c_field'])
assert relationship.GetRightTableFields() == ['b_field', 'c_field']
relationship.SetRightTableFields(["b_field", "c_field"])
assert relationship.GetRightTableFields() == ["b_field", "c_field"]
assert relationship.GetLeftMappingTableFields() is None
relationship.SetLeftMappingTableFields(['a_field2'])
assert relationship.GetLeftMappingTableFields() == ['a_field2']
relationship.SetLeftMappingTableFields(["a_field2"])
assert relationship.GetLeftMappingTableFields() == ["a_field2"]
assert relationship.GetRightMappingTableFields() is None
relationship.SetRightMappingTableFields(['b_field2', 'c_field2'])
assert relationship.GetRightMappingTableFields() == ['b_field2', 'c_field2']
relationship.SetRightMappingTableFields(["b_field2", "c_field2"])
assert relationship.GetRightMappingTableFields() == ["b_field2", "c_field2"]
assert relationship.GetType() == gdal.GRT_ASSOCIATION
relationship.SetType(gdal.GRT_AGGREGATION)
assert relationship.GetType() == gdal.GRT_AGGREGATION
assert relationship.GetForwardPathLabel() == ''
relationship.SetForwardPathLabel('forward path')
assert relationship.GetForwardPathLabel() == 'forward path'
assert relationship.GetForwardPathLabel() == ""
relationship.SetForwardPathLabel("forward path")
assert relationship.GetForwardPathLabel() == "forward path"
assert relationship.GetBackwardPathLabel() == ''
relationship.SetBackwardPathLabel('backward path')
assert relationship.GetBackwardPathLabel() == 'backward path'
assert relationship.GetBackwardPathLabel() == ""
relationship.SetBackwardPathLabel("backward path")
assert relationship.GetBackwardPathLabel() == "backward path"
assert relationship.GetRelatedTableType() == ''
relationship.SetRelatedTableType('media')
assert relationship.GetRelatedTableType() == 'media'
assert relationship.GetRelatedTableType() == ""
relationship.SetRelatedTableType("media")
assert relationship.GetRelatedTableType() == "media"

View File

@ -31,36 +31,39 @@
import urllib.parse
from osgeo import gdal
import gdaltest
import pytest
from osgeo import gdal
###############################################################################
# Try opening a file with a Chinese name using the Python UTF-8 string.
def test_rfc30_1():
filename = 'xx\u4E2D\u6587.\u4E2D\u6587'
filename = "xx\u4E2D\u6587.\u4E2D\u6587"
filename_escaped = urllib.parse.quote(filename)
if not gdaltest.download_file('http://download.osgeo.org/gdal/data/gtiff/' + filename_escaped, filename):
if not gdaltest.download_file(
"http://download.osgeo.org/gdal/data/gtiff/" + filename_escaped, filename
):
pytest.skip()
filename = 'tmp/cache/' + filename
filename = "tmp/cache/" + filename
ds = gdal.Open(filename)
file_list = ds.GetFileList()
assert ds is not None, 'failed to open utf filename.'
assert ds is not None, "failed to open utf filename."
ds = None
ds = gdal.Open(file_list[0])
assert ds is not None, 'failed to open utf filename (2).'
assert ds is not None, "failed to open utf filename (2)."
###############################################################################
# Try creating, then renaming a utf-8 named file.
@ -68,29 +71,29 @@ def test_rfc30_1():
def test_rfc30_2():
filename = 'tmp/yy\u4E2D\u6587.\u4E2D\u6587'
fd = gdal.VSIFOpenL(filename, 'w')
assert fd is not None, 'failed to create utf-8 named file.'
filename = "tmp/yy\u4E2D\u6587.\u4E2D\u6587"
fd = gdal.VSIFOpenL(filename, "w")
assert fd is not None, "failed to create utf-8 named file."
gdal.VSIFWriteL('abc', 3, 1, fd)
gdal.VSIFWriteL("abc", 3, 1, fd)
gdal.VSIFCloseL(fd)
# rename
new_filename = 'tmp/yy\u4E2D\u6587.\u4E2D\u6587'
new_filename = "tmp/yy\u4E2D\u6587.\u4E2D\u6587"
filename_for_rename = filename
assert gdal.Rename(filename_for_rename, new_filename) == 0, 'utf-8 rename failed.'
assert gdal.Rename(filename_for_rename, new_filename) == 0, "utf-8 rename failed."
fd = gdal.VSIFOpenL(new_filename, 'r')
assert fd is not None, 'reopen failed with utf8'
fd = gdal.VSIFOpenL(new_filename, "r")
assert fd is not None, "reopen failed with utf8"
data = gdal.VSIFReadL(3, 1, fd)
gdal.VSIFCloseL(fd)
assert data == b'abc'
assert data == b"abc"
gdal.Unlink(new_filename)
fd = gdal.VSIFOpenL(new_filename, 'r')
assert fd is None, 'did unlink fail on utf8 filename?'
fd = gdal.VSIFOpenL(new_filename, "r")
assert fd is None, "did unlink fail on utf8 filename?"

View File

@ -30,18 +30,32 @@
import pytest
from osgeo import gdal
from lxml import etree
all_driver_names = [gdal.GetDriver(i).GetDescription() for i in range(gdal.GetDriverCount())]
ogr_driver_names = [driver_name for driver_name in all_driver_names
if gdal.GetDriverByName(driver_name).GetMetadataItem('DCAP_VECTOR') == 'YES']
gdal_driver_names = [driver_name for driver_name in all_driver_names
if gdal.GetDriverByName(driver_name).GetMetadataItem('DCAP_RASTER') == 'YES']
multidim_driver_name = [driver_name for driver_name in gdal_driver_names
if gdal.GetDriverByName(driver_name).GetMetadataItem('DCAP_MULTIDIM_RASTER') == 'YES']
from osgeo import gdal
schema_openoptionslist = etree.XML(r"""
all_driver_names = [
gdal.GetDriver(i).GetDescription() for i in range(gdal.GetDriverCount())
]
ogr_driver_names = [
driver_name
for driver_name in all_driver_names
if gdal.GetDriverByName(driver_name).GetMetadataItem("DCAP_VECTOR") == "YES"
]
gdal_driver_names = [
driver_name
for driver_name in all_driver_names
if gdal.GetDriverByName(driver_name).GetMetadataItem("DCAP_RASTER") == "YES"
]
multidim_driver_name = [
driver_name
for driver_name in gdal_driver_names
if gdal.GetDriverByName(driver_name).GetMetadataItem("DCAP_MULTIDIM_RASTER")
== "YES"
]
schema_openoptionslist = etree.XML(
r"""
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Value">
<xs:complexType>
@ -106,9 +120,11 @@ schema_openoptionslist = etree.XML(r"""
</xs:complexType>
</xs:element>
</xs:schema>
""")
"""
)
schema_creationoptionslist_xml = etree.XML(r"""
schema_creationoptionslist_xml = etree.XML(
r"""
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Value">
<xs:complexType>
@ -173,9 +189,11 @@ schema_creationoptionslist_xml = etree.XML(r"""
</xs:complexType>
</xs:element>
</xs:schema>
""")
"""
)
schema_layer_creationoptionslist_xml = etree.XML(r"""
schema_layer_creationoptionslist_xml = etree.XML(
r"""
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Value">
<xs:complexType>
@ -240,10 +258,12 @@ schema_layer_creationoptionslist_xml = etree.XML(r"""
</xs:complexType>
</xs:element>
</xs:schema>
""")
"""
)
schema_multidim_array_creationoptionslist_xml =etree.XML(r"""
schema_multidim_array_creationoptionslist_xml = etree.XML(
r"""
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Value">
<xs:complexType>
@ -276,10 +296,12 @@ schema_multidim_array_creationoptionslist_xml =etree.XML(r"""
</xs:complexType>
</xs:element>
</xs:schema>
""")
"""
)
schema_multidim_attribute_creationoptionslist_xml = etree.XML(r"""
schema_multidim_attribute_creationoptionslist_xml = etree.XML(
r"""
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Value" type="xs:string"/>
<xs:element name="Option">
@ -299,10 +321,12 @@ schema_multidim_attribute_creationoptionslist_xml = etree.XML(r"""
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>""")
</xs:schema>"""
)
schema_multidim_dataset_creationoptionslist_xml = etree.XML(r"""
schema_multidim_dataset_creationoptionslist_xml = etree.XML(
r"""
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Value" type="xs:string"/>
<xs:element name="Option">
@ -323,10 +347,12 @@ schema_multidim_dataset_creationoptionslist_xml = etree.XML(r"""
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>""")
</xs:schema>"""
)
schema_multidim_dimension_creationoptionslist_xml = etree.XML(r"""
schema_multidim_dimension_creationoptionslist_xml = etree.XML(
r"""
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Option">
<xs:complexType>
@ -347,17 +373,18 @@ schema_multidim_dimension_creationoptionslist_xml = etree.XML(r"""
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>""")
</xs:schema>"""
)
@pytest.mark.parametrize('driver_name', all_driver_names)
@pytest.mark.parametrize("driver_name", all_driver_names)
def test_metadata_openoptionlist(driver_name):
""" Test if DMD_OPENOPTIONLIST metadataitem is present and can be parsed """
"""Test if DMD_OPENOPTIONLIST metadataitem is present and can be parsed"""
schema = etree.XMLSchema(schema_openoptionslist)
driver = gdal.GetDriverByName(driver_name)
openoptionlist_xml = driver.GetMetadataItem('DMD_OPENOPTIONLIST')
openoptionlist_xml = driver.GetMetadataItem("DMD_OPENOPTIONLIST")
if openoptionlist_xml is not None and len(openoptionlist_xml) > 0:
# do not fail
@ -369,14 +396,14 @@ def test_metadata_openoptionlist(driver_name):
raise
@pytest.mark.parametrize('driver_name', all_driver_names)
@pytest.mark.parametrize("driver_name", all_driver_names)
def test_metadata_creationoptionslist(driver_name):
""" Test if DMD_CREATIONOPTIONLIST metadataitem is present and can be parsed """
"""Test if DMD_CREATIONOPTIONLIST metadataitem is present and can be parsed"""
schema = etree.XMLSchema(schema_creationoptionslist_xml)
driver = gdal.GetDriverByName(driver_name)
creationoptionslist_xml = driver.GetMetadataItem('DMD_CREATIONOPTIONLIST')
creationoptionslist_xml = driver.GetMetadataItem("DMD_CREATIONOPTIONLIST")
if creationoptionslist_xml is not None and len(creationoptionslist_xml) > 0:
# do not fail
@ -388,14 +415,14 @@ def test_metadata_creationoptionslist(driver_name):
raise
@pytest.mark.parametrize('driver_name', ogr_driver_names)
@pytest.mark.parametrize("driver_name", ogr_driver_names)
def test_metadata_layer_creationoptionslist(driver_name):
""" Test if DS_LAYER_CREATIONOPTIONLIST metadataitem is present and can be parsed """
"""Test if DS_LAYER_CREATIONOPTIONLIST metadataitem is present and can be parsed"""
schema = etree.XMLSchema(schema_layer_creationoptionslist_xml)
driver = gdal.GetDriverByName(driver_name)
creationoptionslist_xml = driver.GetMetadataItem('DS_LAYER_CREATIONOPTIONLIST')
creationoptionslist_xml = driver.GetMetadataItem("DS_LAYER_CREATIONOPTIONLIST")
if creationoptionslist_xml is not None and len(creationoptionslist_xml) > 0:
# do not fail
@ -407,14 +434,14 @@ def test_metadata_layer_creationoptionslist(driver_name):
raise
@pytest.mark.parametrize('driver_name', multidim_driver_name)
@pytest.mark.parametrize("driver_name", multidim_driver_name)
def test_metadata_multidim_array_creationoptionslist(driver_name):
""" Test if DMD_MULTIDIM_ARRAY_CREATIONOPTIONLIST metadataitem is present and can be parsed """
"""Test if DMD_MULTIDIM_ARRAY_CREATIONOPTIONLIST metadataitem is present and can be parsed"""
schema = etree.XMLSchema(schema_multidim_array_creationoptionslist_xml)
driver = gdal.GetDriverByName(driver_name)
xml = driver.GetMetadataItem('DMD_MULTIDIM_ARRAY_CREATIONOPTIONLIST')
xml = driver.GetMetadataItem("DMD_MULTIDIM_ARRAY_CREATIONOPTIONLIST")
if xml is not None and len(xml) > 0:
# do not fail
@ -426,14 +453,14 @@ def test_metadata_multidim_array_creationoptionslist(driver_name):
raise
@pytest.mark.parametrize('driver_name', multidim_driver_name)
@pytest.mark.parametrize("driver_name", multidim_driver_name)
def test_metadata_multidim_attribute_creationoptionslist(driver_name):
""" Test if DMD_MULTIDIM_ATTRIBUTE_CREATIONOPTIONLIST metadataitem is present and can be parsed """
"""Test if DMD_MULTIDIM_ATTRIBUTE_CREATIONOPTIONLIST metadataitem is present and can be parsed"""
schema = etree.XMLSchema(schema_multidim_attribute_creationoptionslist_xml)
driver = gdal.GetDriverByName(driver_name)
xml = driver.GetMetadataItem('DMD_MULTIDIM_ATTRIBUTE_CREATIONOPTIONLIST')
xml = driver.GetMetadataItem("DMD_MULTIDIM_ATTRIBUTE_CREATIONOPTIONLIST")
if xml is not None and len(xml) > 0:
# do not fail
@ -445,14 +472,14 @@ def test_metadata_multidim_attribute_creationoptionslist(driver_name):
raise
@pytest.mark.parametrize('driver_name', multidim_driver_name)
@pytest.mark.parametrize("driver_name", multidim_driver_name)
def test_metadata_multidim_dataset_creationoptionslist(driver_name):
""" Test if DMD_MULTIDIM_DATASET_CREATIONOPTIONLIST metadataitem is present and can be parsed """
"""Test if DMD_MULTIDIM_DATASET_CREATIONOPTIONLIST metadataitem is present and can be parsed"""
schema = etree.XMLSchema(schema_multidim_dataset_creationoptionslist_xml)
driver = gdal.GetDriverByName(driver_name)
xml = driver.GetMetadataItem('DMD_MULTIDIM_DATASET_CREATIONOPTIONLIST')
xml = driver.GetMetadataItem("DMD_MULTIDIM_DATASET_CREATIONOPTIONLIST")
if xml is not None and len(xml) > 0:
# do not fail
@ -464,14 +491,14 @@ def test_metadata_multidim_dataset_creationoptionslist(driver_name):
raise
@pytest.mark.parametrize('driver_name', multidim_driver_name)
@pytest.mark.parametrize("driver_name", multidim_driver_name)
def test_metadata_multidim_dimension_creationoptionslist(driver_name):
""" Test if DMD_MULTIDIM_DIMENSION_CREATIONOPTIONLIST metadataitem is present and can be parsed """
"""Test if DMD_MULTIDIM_DIMENSION_CREATIONOPTIONLIST metadataitem is present and can be parsed"""
schema = etree.XMLSchema(schema_multidim_dimension_creationoptionslist_xml)
driver = gdal.GetDriverByName(driver_name)
xml = driver.GetMetadataItem('DMD_MULTIDIM_DIMENSION_CREATIONOPTIONLIST')
xml = driver.GetMetadataItem("DMD_MULTIDIM_DIMENSION_CREATIONOPTIONLIST")
if xml is not None and len(xml) > 0:
# do not fail
@ -483,15 +510,15 @@ def test_metadata_multidim_dimension_creationoptionslist(driver_name):
raise
@pytest.mark.parametrize('driver_name', multidim_driver_name)
@pytest.mark.parametrize("driver_name", multidim_driver_name)
def test_metadata_multidim_group_creationoptionslist(driver_name):
""" Test if DMD_MULTIDIM_GROUP_CREATIONOPTIONLIST metadataitem is present and can be parsed """
"""Test if DMD_MULTIDIM_GROUP_CREATIONOPTIONLIST metadataitem is present and can be parsed"""
# TODO: create schema if xml is available
# schema = etree.XMLSchema(schema_multidim_group_creationoptionslist_xml)
driver = gdal.GetDriverByName(driver_name)
xml = driver.GetMetadataItem('DMD_MULTIDIM_GROUP_CREATIONOPTIONLIST')
xml = driver.GetMetadataItem("DMD_MULTIDIM_GROUP_CREATIONOPTIONLIST")
if xml is not None and len(xml) > 0:
# do not fail
@ -504,41 +531,53 @@ def test_metadata_multidim_group_creationoptionslist(driver_name):
raise
@pytest.mark.parametrize('driver_name', all_driver_names)
@pytest.mark.parametrize("driver_name", all_driver_names)
def test_metadata_creation_field_datatypes(driver_name):
""" Test if DMD_CREATIONFIELDDATATYPES metadataitem returns valid datatypes """
"""Test if DMD_CREATIONFIELDDATATYPES metadataitem returns valid datatypes"""
valid_datatypes = {'Integer', 'Integer64', 'Real', 'String', 'Date', 'DateTime', 'Time', 'IntegerList',
'Integer64List', 'RealList', 'StringList', 'Binary'}
valid_datatypes = {
"Integer",
"Integer64",
"Real",
"String",
"Date",
"DateTime",
"Time",
"IntegerList",
"Integer64List",
"RealList",
"StringList",
"Binary",
}
driver = gdal.GetDriverByName(driver_name)
datatypes_str = driver.GetMetadataItem('DMD_CREATIONFIELDDATATYPES')
datatypes_str = driver.GetMetadataItem("DMD_CREATIONFIELDDATATYPES")
if datatypes_str is not None:
for datatype in datatypes_str.split(' '):
for datatype in datatypes_str.split(" "):
assert datatype in valid_datatypes
@pytest.mark.parametrize('driver_name', all_driver_names)
@pytest.mark.parametrize("driver_name", all_driver_names)
def test_metadata_creation_sub_field_datatypes(driver_name):
""" Test if DMD_CREATIONFIELDDATASUBTYPES metadataitem returns valid data subtypes """
"""Test if DMD_CREATIONFIELDDATASUBTYPES metadataitem returns valid data subtypes"""
valid_datatypes = {'Boolean', 'Float32', 'Int16', 'JSON', 'UUID'}
valid_datatypes = {"Boolean", "Float32", "Int16", "JSON", "UUID"}
driver = gdal.GetDriverByName(driver_name)
datatypes_str = driver.GetMetadataItem('DMD_CREATIONFIELDDATASUBTYPES')
datatypes_str = driver.GetMetadataItem("DMD_CREATIONFIELDDATASUBTYPES")
if datatypes_str is not None:
for datatype in datatypes_str.split(' '):
for datatype in datatypes_str.split(" "):
assert datatype in valid_datatypes
@pytest.mark.parametrize('driver_name', ogr_driver_names)
@pytest.mark.parametrize("driver_name", ogr_driver_names)
def test_metadata_alter_geom_field_defn_flags(driver_name):
""" Test if GDAL_DMD_ALTER_GEOM_FIELD_DEFN_FLAGS metadataitem returns valid flags """
"""Test if GDAL_DMD_ALTER_GEOM_FIELD_DEFN_FLAGS metadataitem returns valid flags"""
supported_flags = {'Name', 'Type', 'Nullable', 'SRS', 'CoordinateEpoch'}
supported_flags = {"Name", "Type", "Nullable", "SRS", "CoordinateEpoch"}
driver = gdal.GetDriverByName(driver_name)
flags_str = driver.GetMetadataItem(gdal.DMD_ALTER_GEOM_FIELD_DEFN_FLAGS)
if flags_str is not None:
for flag in flags_str.split(' '):
for flag in flags_str.split(" "):
assert flag in supported_flags

View File

@ -31,10 +31,10 @@
import ctypes
from osgeo import gdal
import gdaltest
import pytest
import gdaltest
from osgeo import gdal
gdal_handle_init = False
gdal_handle = None
@ -45,13 +45,14 @@ gdal_handle_stdcall = None
def find_libgdal():
return gdaltest.find_lib('gdal')
return gdaltest.find_lib("gdal")
###############################################################################
# Init
@pytest.fixture(scope='module', autouse=True)
@pytest.fixture(scope="module", autouse=True)
def setup():
global gdal_handle_init, gdal_handle, gdal_handle_stdcall
@ -66,7 +67,7 @@ def setup():
if name is None:
pytest.skip()
print('Found libgdal we are running against : %s' % name)
print("Found libgdal we are running against : %s" % name)
static_version = gdal.VersionInfo(None)
# short_static_version = static_version[0:2]
@ -82,17 +83,20 @@ def setup():
gdal_handle_stdcall.GDALVersionInfo.restype = ctypes.c_char_p
dynamic_version = gdal_handle_stdcall.GDALVersionInfo(None)
dynamic_version = dynamic_version.decode('utf-8')
dynamic_version = dynamic_version.decode("utf-8")
if dynamic_version != static_version:
gdal_handle = None
gdal_handle_stdcall = None
pytest.skip(f'dynamic version({dynamic_version}) does not match '
f'static version ({static_version})')
pytest.skip(
f"dynamic version({dynamic_version}) does not match "
f"static version ({static_version})"
)
return gdal_handle
except Exception:
pytest.skip('cannot find gdal shared object')
pytest.skip("cannot find gdal shared object")
###############################################################################
# Call GDALDestroyDriverManager()
@ -105,6 +109,7 @@ def GDALDestroyDriverManager():
gdal_handle_stdcall.GDALDestroyDriverManager()
###############################################################################
# Call OGRCleanupAll()
@ -116,6 +121,7 @@ def OGRCleanupAll():
gdal_handle_stdcall.OGRCleanupAll()
###############################################################################
# Call OSRCleanup()
@ -127,13 +133,14 @@ def OSRCleanup():
gdal_handle.OSRCleanup()
###############################################################################
# Test GDALSimpleImageWarp
def test_testnonboundtoswig_GDALSimpleImageWarp():
src_ds = gdal.Open('data/byte.tif')
src_ds = gdal.Open("data/byte.tif")
gt = src_ds.GetGeoTransform()
wkt = src_ds.GetProjectionRef()
src_ds = None
@ -145,31 +152,57 @@ def test_testnonboundtoswig_GDALSimpleImageWarp():
gdal_handle_stdcall.GDALClose.restype = None
gdal_handle.GDALCreateGenImgProjTransformer2.restype = ctypes.c_void_p
gdal_handle.GDALCreateGenImgProjTransformer2.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p]
gdal_handle.GDALCreateGenImgProjTransformer2.argtypes = [
ctypes.c_void_p,
ctypes.c_void_p,
ctypes.c_void_p,
]
gdal_handle_stdcall.GDALSimpleImageWarp.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p]
gdal_handle_stdcall.GDALSimpleImageWarp.argtypes = [
ctypes.c_void_p,
ctypes.c_void_p,
ctypes.c_int,
ctypes.c_void_p,
ctypes.c_void_p,
ctypes.c_void_p,
ctypes.c_void_p,
ctypes.c_void_p,
ctypes.c_void_p,
]
gdal_handle_stdcall.GDALSimpleImageWarp.restype = ctypes.c_int
gdal_handle.GDALDestroyGenImgProjTransformer.argtypes = [ctypes.c_void_p]
gdal_handle.GDALDestroyGenImgProjTransformer.restype = None
out_ds = gdal.GetDriverByName('GTiff').Create('/vsimem/out.tif', 20, 20, 1)
out_ds = gdal.GetDriverByName("GTiff").Create("/vsimem/out.tif", 20, 20, 1)
out_ds.SetGeoTransform(gt)
out_ds.SetProjection(wkt)
out_ds = None
filename = b'data/byte.tif'
filename = b"data/byte.tif"
native_in_ds = gdal_handle_stdcall.GDALOpen(filename, gdal.GA_ReadOnly)
assert native_in_ds is not None
filename = b'/vsimem/out.tif'
filename = b"/vsimem/out.tif"
native_out_ds = gdal_handle_stdcall.GDALOpen(filename, gdal.GA_Update)
assert native_out_ds is not None
pTransformerArg = gdal_handle.GDALCreateGenImgProjTransformer2(native_in_ds, native_out_ds, None)
pTransformerArg = gdal_handle.GDALCreateGenImgProjTransformer2(
native_in_ds, native_out_ds, None
)
assert pTransformerArg is not None
ret = gdal_handle_stdcall.GDALSimpleImageWarp(native_in_ds, native_out_ds, 0, None, gdal_handle_stdcall.GDALGenImgProjTransform, pTransformerArg, None, None, None)
ret = gdal_handle_stdcall.GDALSimpleImageWarp(
native_in_ds,
native_out_ds,
0,
None,
gdal_handle_stdcall.GDALGenImgProjTransform,
pTransformerArg,
None,
None,
None,
)
assert ret == 1
gdal_handle.GDALDestroyGenImgProjTransformer(pTransformerArg)
@ -177,14 +210,15 @@ def test_testnonboundtoswig_GDALSimpleImageWarp():
gdal_handle_stdcall.GDALClose(native_in_ds)
gdal_handle_stdcall.GDALClose(native_out_ds)
ds = gdal.Open('/vsimem/out.tif')
ds = gdal.Open("/vsimem/out.tif")
cs = ds.GetRasterBand(1).Checksum()
ds = None
gdal.Unlink('/vsimem/out.tif')
gdal.Unlink("/vsimem/out.tif")
assert cs == 4672
###############################################################################
# Test VRT derived bands with callback functions implemented in Python!
@ -208,32 +242,42 @@ def GDALTypeToCTypes(gdaltype):
return None
def my_pyDerivedPixelFunc(papoSources, nSources, pData, nBufXSize, nBufYSize, eSrcType, eBufType, nPixelSpace, nLineSpace):
def my_pyDerivedPixelFunc(
papoSources,
nSources,
pData,
nBufXSize,
nBufYSize,
eSrcType,
eBufType,
nPixelSpace,
nLineSpace,
):
if nSources != 1:
print(nSources)
print('did not get expected nSources')
print("did not get expected nSources")
return 1
srcctype = GDALTypeToCTypes(eSrcType)
if srcctype is None:
print(eSrcType)
print('did not get expected eSrcType')
print("did not get expected eSrcType")
return 1
dstctype = GDALTypeToCTypes(eBufType)
if dstctype is None:
print(eBufType)
print('did not get expected eBufType')
print("did not get expected eBufType")
return 1
if nPixelSpace != gdal.GetDataTypeSize(eBufType) / 8:
print(nPixelSpace)
print('did not get expected nPixelSpace')
print("did not get expected nPixelSpace")
return 1
if (nLineSpace % nPixelSpace) != 0:
print(nLineSpace)
print('did not get expected nLineSpace')
print("did not get expected nLineSpace")
return 1
nLineStride = (int)(nLineSpace / nPixelSpace)
@ -249,27 +293,34 @@ def my_pyDerivedPixelFunc(papoSources, nSources, pData, nBufXSize, nBufYSize, eS
def test_testnonboundtoswig_VRTDerivedBands():
DerivedPixelFuncType = ctypes.CFUNCTYPE(ctypes.c_int, # ret CPLErr
ctypes.POINTER(ctypes.c_void_p), # void **papoSources
ctypes.c_int, # int nSources
ctypes.c_void_p, # void *pData
ctypes.c_int, # int nBufXSize
ctypes.c_int, # int nBufYSize
ctypes.c_int, # GDALDataType eSrcType
ctypes.c_int, # GDALDataType eBufType
ctypes.c_int, # int nPixelSpace
ctypes.c_int) # int nLineSpace
DerivedPixelFuncType = ctypes.CFUNCTYPE(
ctypes.c_int, # ret CPLErr
ctypes.POINTER(ctypes.c_void_p), # void **papoSources
ctypes.c_int, # int nSources
ctypes.c_void_p, # void *pData
ctypes.c_int, # int nBufXSize
ctypes.c_int, # int nBufYSize
ctypes.c_int, # GDALDataType eSrcType
ctypes.c_int, # GDALDataType eBufType
ctypes.c_int, # int nPixelSpace
ctypes.c_int,
) # int nLineSpace
my_cDerivedPixelFunc = DerivedPixelFuncType(my_pyDerivedPixelFunc)
# CPLErr CPL_DLL CPL_STDCALL GDALAddDerivedBandPixelFunc( const char *pszName,
# GDALDerivedPixelFunc pfnPixelFunc );
gdal_handle_stdcall.GDALAddDerivedBandPixelFunc.argtypes = [ctypes.c_char_p, DerivedPixelFuncType]
gdal_handle_stdcall.GDALAddDerivedBandPixelFunc.argtypes = [
ctypes.c_char_p,
DerivedPixelFuncType,
]
gdal_handle_stdcall.GDALAddDerivedBandPixelFunc.restype = ctypes.c_int
funcName = b"pyDerivedPixelFunc"
ret = gdal_handle_stdcall.GDALAddDerivedBandPixelFunc(funcName, my_cDerivedPixelFunc)
ret = gdal_handle_stdcall.GDALAddDerivedBandPixelFunc(
funcName, my_cDerivedPixelFunc
)
assert ret == 0
vrt_xml = """<VRTDataset rasterXSize="20" rasterYSize="20">
@ -285,7 +336,7 @@ def test_testnonboundtoswig_VRTDerivedBands():
</VRTRasterBand>
</VRTDataset>"""
src_ds = gdal.Open('data/byte.tif')
src_ds = gdal.Open("data/byte.tif")
ref_cs = src_ds.GetRasterBand(1).Checksum()
ref_data = src_ds.GetRasterBand(1).ReadRaster(0, 0, 20, 20)
src_ds = None
@ -295,6 +346,6 @@ def test_testnonboundtoswig_VRTDerivedBands():
got_data = ds.GetRasterBand(1).ReadRaster(0, 0, 20, 20)
ds = None
assert ref_cs == got_cs, 'wrong checksum'
assert ref_cs == got_cs, "wrong checksum"
assert ref_data == got_data

View File

@ -31,9 +31,10 @@
import threading
from osgeo import gdal
import pytest
from osgeo import gdal
def my_error_handler(err_type, err_no, err_msg):
# pylint: disable=unused-argument
@ -42,39 +43,35 @@ def my_error_handler(err_type, err_no, err_msg):
def thread_test_1_worker(args_dict):
for i in range(1000):
ds = gdal.Open('data/byte.tif')
ds = gdal.Open("data/byte.tif")
if (i % 2) == 0:
if ds.GetRasterBand(1).Checksum() != 4672:
args_dict['ret'] = False
args_dict["ret"] = False
else:
ds.GetRasterBand(1).ReadAsArray()
for i in range(1000):
gdal.PushErrorHandler(my_error_handler)
ds = gdal.Open('i_dont_exist')
ds = gdal.Open("i_dont_exist")
gdal.PopErrorHandler()
def test_thread_test_1():
pytest.importorskip('numpy')
pytest.importorskip("numpy")
threads = []
args_array = []
for i in range(4):
args_dict = {'ret': True}
args_dict = {"ret": True}
t = threading.Thread(target=thread_test_1_worker, args=(args_dict,))
args_array.append(args_dict)
threads.append(t)
t.start()
ret = 'success'
ret = "success"
for i in range(4):
threads[i].join()
if not args_array[i]:
ret = 'fail'
ret = "fail"
return ret

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -33,14 +33,13 @@ import struct
from osgeo import gdal
###############################################################################
# Test absolute/offset && index directory access
def test_tiff_read_subifds():
ds = gdal.Open('data/tiff_with_subifds.tif')
ds = gdal.Open("data/tiff_with_subifds.tif")
assert ds.GetRasterBand(1).Checksum() == 35731
sds = ds.GetSubDatasets()
@ -48,26 +47,44 @@ def test_tiff_read_subifds():
assert sds[0][0] == "GTIFF_DIR:1:data/tiff_with_subifds.tif"
assert sds[1][0] == "GTIFF_DIR:2:data/tiff_with_subifds.tif"
data = ds.GetRasterBand(1).ReadRaster(buf_xsize=1,buf_ysize=1, xsize=1, ysize=1,buf_type=gdal.GDT_Int16)
assert struct.unpack('H', data)[0] == 220
data = ds.GetRasterBand(1).ReadRaster(
buf_xsize=1, buf_ysize=1, xsize=1, ysize=1, buf_type=gdal.GDT_Int16
)
assert struct.unpack("H", data)[0] == 220
data = ds.GetRasterBand(1).GetOverview(1).ReadRaster(buf_xsize=1,buf_ysize=1, xsize=1, ysize=1,buf_type=gdal.GDT_Int16)
assert struct.unpack('H', data)[0] == 12
data = (
ds.GetRasterBand(1)
.GetOverview(1)
.ReadRaster(buf_xsize=1, buf_ysize=1, xsize=1, ysize=1, buf_type=gdal.GDT_Int16)
)
assert struct.unpack("H", data)[0] == 12
ds = gdal.Open('GTIFF_DIR:1:data/tiff_with_subifds.tif')
ds = gdal.Open("GTIFF_DIR:1:data/tiff_with_subifds.tif")
assert ds.GetRasterBand(1).Checksum() == 35731
data = ds.GetRasterBand(1).ReadRaster(buf_xsize=1,buf_ysize=1, xsize=1, ysize=1,buf_type=gdal.GDT_Int16)
assert struct.unpack('H', data)[0] == 220
data = ds.GetRasterBand(1).ReadRaster(
buf_xsize=1, buf_ysize=1, xsize=1, ysize=1, buf_type=gdal.GDT_Int16
)
assert struct.unpack("H", data)[0] == 220
data = ds.GetRasterBand(1).GetOverview(1).ReadRaster(buf_xsize=1,buf_ysize=1, xsize=1, ysize=1,buf_type=gdal.GDT_Int16)
assert struct.unpack('H', data)[0] == 12
data = (
ds.GetRasterBand(1)
.GetOverview(1)
.ReadRaster(buf_xsize=1, buf_ysize=1, xsize=1, ysize=1, buf_type=gdal.GDT_Int16)
)
assert struct.unpack("H", data)[0] == 12
ds = gdal.Open('GTIFF_DIR:2:data/tiff_with_subifds.tif')
ds = gdal.Open("GTIFF_DIR:2:data/tiff_with_subifds.tif")
assert ds.GetRasterBand(1).Checksum() == 0
data = ds.GetRasterBand(1).ReadRaster(buf_xsize=1,buf_ysize=1, xsize=1, ysize=1,buf_type=gdal.GDT_Int16)
assert struct.unpack('H', data)[0] == 0
data = ds.GetRasterBand(1).ReadRaster(
buf_xsize=1, buf_ysize=1, xsize=1, ysize=1, buf_type=gdal.GDT_Int16
)
assert struct.unpack("H", data)[0] == 0
data = ds.GetRasterBand(1).GetOverview(1).ReadRaster(buf_xsize=1,buf_ysize=1, xsize=1, ysize=1,buf_type=gdal.GDT_Int16)
assert struct.unpack('H', data)[0] == 128
data = (
ds.GetRasterBand(1)
.GetOverview(1)
.ReadRaster(buf_xsize=1, buf_ysize=1, xsize=1, ysize=1, buf_type=gdal.GDT_Int16)
)
assert struct.unpack("H", data)[0] == 128

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -31,20 +31,22 @@
import sys
from osgeo import gdal
import pytest
from osgeo import gdal
# All tests will be skipped if numpy unavailable or SKIP_VIRTUALMEM is set.
numpy = pytest.importorskip('numpy')
pytestmark = pytest.mark.skipif(gdal.GetConfigOption('SKIP_VIRTUALMEM'),
reason='SKIP_VIRTUALMEM is set in config')
numpy = pytest.importorskip("numpy")
pytestmark = pytest.mark.skipif(
gdal.GetConfigOption("SKIP_VIRTUALMEM"), reason="SKIP_VIRTUALMEM is set in config"
)
###############################################################################
# Test linear and tiled virtual mem interfaces in read-only mode
def test_virtualmem_1():
ds = gdal.Open('../gdrivers/data/small_world.tif')
ds = gdal.Open("../gdrivers/data/small_world.tif")
bufxsize = 400
bufysize = 128
tilexsize = 128
@ -53,22 +55,115 @@ def test_virtualmem_1():
ar = ds.ReadAsArray(0, 0, bufxsize, bufysize)
try:
ar_flat_bsq = ds.GetVirtualMemArray(gdal.GF_Read, 0, 0, bufxsize, bufysize, bufxsize, bufysize, gdal.GDT_Int16, [1, 2, 3], 1, 1024 * 1024, 0)
ar_flat_bsq = ds.GetVirtualMemArray(
gdal.GF_Read,
0,
0,
bufxsize,
bufysize,
bufxsize,
bufysize,
gdal.GDT_Int16,
[1, 2, 3],
1,
1024 * 1024,
0,
)
except:
if not sys.platform.startswith('linux'):
if not sys.platform.startswith("linux"):
# Also try GetTiledVirtualMemArray() robustness (#5728)
try:
ar_tiled_band1 = ds.GetRasterBand(1).GetTiledVirtualMemArray(gdal.GF_Read, 0, 0, bufxsize, bufysize, tilexsize, tileysize, gdal.GDT_Int16, 1024 * 1024)
ar_tiled_band1 = ds.GetRasterBand(1).GetTiledVirtualMemArray(
gdal.GF_Read,
0,
0,
bufxsize,
bufysize,
tilexsize,
tileysize,
gdal.GDT_Int16,
1024 * 1024,
)
except:
pass
pytest.skip()
ar_flat_band1 = ds.GetRasterBand(1).GetVirtualMemArray(gdal.GF_Read, 0, 0, bufxsize, bufysize, bufxsize, bufysize, gdal.GDT_Int16, 1024 * 1024, 0)
ar_flat_bip = ds.GetVirtualMemArray(gdal.GF_Read, 0, 0, bufxsize, bufysize, bufxsize, bufysize, gdal.GDT_Int16, [1, 2, 3], 0, 1024 * 1024, 0)
ar_tiled_band1 = ds.GetRasterBand(1).GetTiledVirtualMemArray(gdal.GF_Read, 0, 0, bufxsize, bufysize, tilexsize, tileysize, gdal.GDT_Int16, 1024 * 1024)
ar_tip = ds.GetTiledVirtualMemArray(gdal.GF_Read, 0, 0, bufxsize, bufysize, tilexsize, tileysize, gdal.GDT_Int16, [1, 2, 3], gdal.GTO_TIP, 1024 * 1024)
ar_bit = ds.GetTiledVirtualMemArray(gdal.GF_Read, 0, 0, bufxsize, bufysize, tilexsize, tileysize, gdal.GDT_Int16, [1, 2, 3], gdal.GTO_BIT, 1024 * 1024)
ar_bsq = ds.GetTiledVirtualMemArray(gdal.GF_Read, 0, 0, bufxsize, bufysize, tilexsize, tileysize, gdal.GDT_Int16, [1, 2, 3], gdal.GTO_BSQ, 1024 * 1024)
ar_flat_band1 = ds.GetRasterBand(1).GetVirtualMemArray(
gdal.GF_Read,
0,
0,
bufxsize,
bufysize,
bufxsize,
bufysize,
gdal.GDT_Int16,
1024 * 1024,
0,
)
ar_flat_bip = ds.GetVirtualMemArray(
gdal.GF_Read,
0,
0,
bufxsize,
bufysize,
bufxsize,
bufysize,
gdal.GDT_Int16,
[1, 2, 3],
0,
1024 * 1024,
0,
)
ar_tiled_band1 = ds.GetRasterBand(1).GetTiledVirtualMemArray(
gdal.GF_Read,
0,
0,
bufxsize,
bufysize,
tilexsize,
tileysize,
gdal.GDT_Int16,
1024 * 1024,
)
ar_tip = ds.GetTiledVirtualMemArray(
gdal.GF_Read,
0,
0,
bufxsize,
bufysize,
tilexsize,
tileysize,
gdal.GDT_Int16,
[1, 2, 3],
gdal.GTO_TIP,
1024 * 1024,
)
ar_bit = ds.GetTiledVirtualMemArray(
gdal.GF_Read,
0,
0,
bufxsize,
bufysize,
tilexsize,
tileysize,
gdal.GDT_Int16,
[1, 2, 3],
gdal.GTO_BIT,
1024 * 1024,
)
ar_bsq = ds.GetTiledVirtualMemArray(
gdal.GF_Read,
0,
0,
bufxsize,
bufysize,
tilexsize,
tileysize,
gdal.GDT_Int16,
[1, 2, 3],
gdal.GTO_BSQ,
1024 * 1024,
)
tilepercol = int((bufysize + tileysize - 1) / tileysize)
tileperrow = int((bufxsize + tilexsize - 1) / tilexsize)
@ -83,14 +178,45 @@ def test_virtualmem_1():
for y in range(reqysize):
for x in range(reqxsize):
for band in range(3):
assert ar_tip[tiley][tilex][y][x][band] == ar[band][tiley * tileysize + y][tilex * tilexsize + x]
assert ar_tip[tiley][tilex][y][x][band] == ar_flat_bsq[band][tiley * tileysize + y][tilex * tilexsize + x]
assert ar_tip[tiley][tilex][y][x][band] == ar_flat_bip[tiley * tileysize + y][tilex * tilexsize + x][band]
assert ar_tip[tiley][tilex][y][x][band] == ar_bsq[band][tiley][tilex][y][x]
assert ar_tip[tiley][tilex][y][x][band] == ar_bit[tiley][tilex][band][y][x]
assert (
ar_tip[tiley][tilex][y][x][band]
== ar[band][tiley * tileysize + y][tilex * tilexsize + x]
)
assert (
ar_tip[tiley][tilex][y][x][band]
== ar_flat_bsq[band][tiley * tileysize + y][
tilex * tilexsize + x
]
)
assert (
ar_tip[tiley][tilex][y][x][band]
== ar_flat_bip[tiley * tileysize + y][
tilex * tilexsize + x
][band]
)
assert (
ar_tip[tiley][tilex][y][x][band]
== ar_bsq[band][tiley][tilex][y][x]
)
assert (
ar_tip[tiley][tilex][y][x][band]
== ar_bit[tiley][tilex][band][y][x]
)
if band == 0:
assert ar_flat_band1[tiley * tileysize + y][tilex * tilexsize + x] == ar_flat_bip[tiley * tileysize + y][tilex * tilexsize + x][0]
assert ar_tiled_band1[tiley][tilex][y][x] == ar_flat_bip[tiley * tileysize + y][tilex * tilexsize + x][0]
assert (
ar_flat_band1[tiley * tileysize + y][
tilex * tilexsize + x
]
== ar_flat_bip[tiley * tileysize + y][
tilex * tilexsize + x
][0]
)
assert (
ar_tiled_band1[tiley][tilex][y][x]
== ar_flat_bip[tiley * tileysize + y][
tilex * tilexsize + x
][0]
)
# We need to destroy the array before dataset destruction
ar_flat_band1 = None
@ -104,9 +230,9 @@ def test_virtualmem_1():
###############################################################################
# Test write mode
@pytest.mark.skipif(sys.platform != 'linux', reason='Incorrect platform')
@pytest.mark.skipif(sys.platform != "linux", reason="Incorrect platform")
def test_virtualmem_2():
ds = gdal.GetDriverByName('MEM').Create('', 100, 100, 1)
ds = gdal.GetDriverByName("MEM").Create("", 100, 100, 1)
ar = ds.GetVirtualMemArray(gdal.GF_Write)
ar.fill(255)
ar = None
@ -120,10 +246,10 @@ def test_virtualmem_2():
###############################################################################
# Test virtual mem auto with a raw driver
@pytest.mark.skipif(sys.platform != 'linux', reason='Incorrect platform')
@pytest.mark.skipif(sys.platform != "linux", reason="Incorrect platform")
def test_virtualmem_3():
for tmpfile in ['tmp/virtualmem_3.img', '/vsimem/virtualmem_3.img']:
ds = gdal.GetDriverByName('EHdr').Create(tmpfile, 400, 300, 2)
for tmpfile in ["tmp/virtualmem_3.img", "/vsimem/virtualmem_3.img"]:
ds = gdal.GetDriverByName("EHdr").Create(tmpfile, 400, 300, 2)
ar1 = ds.GetRasterBand(1).GetVirtualMemAutoArray(gdal.GF_Write)
ar2 = ds.GetRasterBand(2).GetVirtualMemAutoArray(gdal.GF_Write)
for y in range(ds.RasterYSize):
@ -149,19 +275,21 @@ def test_virtualmem_3():
ar2 = None
ds = None
gdal.GetDriverByName('EHdr').Delete(tmpfile)
gdal.GetDriverByName("EHdr").Delete(tmpfile)
###############################################################################
# Test virtual mem auto with GTiff
@pytest.mark.skipif(sys.platform != 'linux', reason='Incorrect platform')
@pytest.mark.skipif(sys.platform != "linux", reason="Incorrect platform")
def test_virtualmem_4():
tmpfile = 'tmp/virtualmem_4.tif'
for option in ['INTERLEAVE=PIXEL', 'INTERLEAVE=BAND']:
tmpfile = "tmp/virtualmem_4.tif"
for option in ["INTERLEAVE=PIXEL", "INTERLEAVE=BAND"]:
gdal.Unlink(tmpfile)
ds = gdal.GetDriverByName('GTiff').Create(tmpfile, 400, 301, 2, options=[option])
ds = gdal.GetDriverByName("GTiff").Create(
tmpfile, 400, 301, 2, options=[option]
)
ar1 = ds.GetRasterBand(1).GetVirtualMemAutoArray(gdal.GF_Write)
if gdal.GetLastErrorMsg().find('mmap() failed') >= 0:
if gdal.GetLastErrorMsg().find("mmap() failed") >= 0:
ar1 = None
ds = None
pytest.skip()
@ -204,4 +332,4 @@ def test_virtualmem_4():
ar2 = None
ds = None
gdal.GetDriverByName('GTiff').Delete(tmpfile)
gdal.GetDriverByName("GTiff").Delete(tmpfile)

File diff suppressed because it is too large Load Diff

View File

@ -29,13 +29,13 @@
# DEALINGS IN THE SOFTWARE.
###############################################################################
import gdaltest
import os
import shutil
import sys
from osgeo import gdal
from osgeo import osr
import gdaltest
from osgeo import gdal, osr
###############################################################################
# Test linear scaling
@ -43,11 +43,12 @@ from osgeo import osr
def test_vrtmisc_1():
ds = gdal.Translate('', 'data/byte.tif', options='-of MEM -scale 74 255 0 255')
ds = gdal.Translate("", "data/byte.tif", options="-of MEM -scale 74 255 0 255")
cs = ds.GetRasterBand(1).Checksum()
ds = None
assert cs == 4323, 'did not get expected checksum'
assert cs == 4323, "did not get expected checksum"
###############################################################################
# Test power scaling
@ -55,11 +56,14 @@ def test_vrtmisc_1():
def test_vrtmisc_2():
ds = gdal.Translate('', 'data/byte.tif', options='-of MEM -scale 74 255 0 255 -exponent 2.2')
ds = gdal.Translate(
"", "data/byte.tif", options="-of MEM -scale 74 255 0 255 -exponent 2.2"
)
cs = ds.GetRasterBand(1).Checksum()
ds = None
assert cs == 4159, 'did not get expected checksum'
assert cs == 4159, "did not get expected checksum"
###############################################################################
# Test power scaling (not <SrcMin> <SrcMax> in VRT file)
@ -67,7 +71,8 @@ def test_vrtmisc_2():
def test_vrtmisc_3():
ds = gdal.Open("""<VRTDataset rasterXSize="20" rasterYSize="20">
ds = gdal.Open(
"""<VRTDataset rasterXSize="20" rasterYSize="20">
<VRTRasterBand dataType="Byte" band="1">
<ComplexSource>
<SourceFilename relativeToVRT="0">data/byte.tif</SourceFilename>
@ -77,11 +82,13 @@ def test_vrtmisc_3():
<DstMax>255</DstMax>
</ComplexSource>
</VRTRasterBand>
</VRTDataset>""")
</VRTDataset>"""
)
cs = ds.GetRasterBand(1).Checksum()
ds = None
assert cs == 4159, 'did not get expected checksum'
assert cs == 4159, "did not get expected checksum"
###############################################################################
# Test multi-band linear scaling with a single -scale occurrence.
@ -90,13 +97,16 @@ def test_vrtmisc_3():
def test_vrtmisc_4():
# -scale specified once applies to all bands
ds = gdal.Translate('', 'data/byte.tif', options='-of MEM -scale 74 255 0 255 -b 1 -b 1')
ds = gdal.Translate(
"", "data/byte.tif", options="-of MEM -scale 74 255 0 255 -b 1 -b 1"
)
cs1 = ds.GetRasterBand(1).Checksum()
cs2 = ds.GetRasterBand(2).Checksum()
ds = None
assert cs1 == 4323, 'did not get expected checksum'
assert cs2 == 4323, 'did not get expected checksum'
assert cs1 == 4323, "did not get expected checksum"
assert cs2 == 4323, "did not get expected checksum"
###############################################################################
# Test multi-band linear scaling with -scale_XX syntax
@ -105,13 +115,16 @@ def test_vrtmisc_4():
def test_vrtmisc_5():
# -scale_2 applies to band 2 only
ds = gdal.Translate('', 'data/byte.tif', options='-of MEM -scale_2 74 255 0 255 -b 1 -b 1')
ds = gdal.Translate(
"", "data/byte.tif", options="-of MEM -scale_2 74 255 0 255 -b 1 -b 1"
)
cs1 = ds.GetRasterBand(1).Checksum()
cs2 = ds.GetRasterBand(2).Checksum()
ds = None
assert cs1 == 4672, 'did not get expected checksum'
assert cs2 == 4323, 'did not get expected checksum'
assert cs1 == 4672, "did not get expected checksum"
assert cs2 == 4323, "did not get expected checksum"
###############################################################################
# Test multi-band linear scaling with repeated -scale syntax
@ -120,13 +133,18 @@ def test_vrtmisc_5():
def test_vrtmisc_6():
# -scale repeated as many times as output band number
ds = gdal.Translate('', 'data/byte.tif', options='-of MEM -scale 0 255 0 255 -scale 74 255 0 255 -b 1 -b 1')
ds = gdal.Translate(
"",
"data/byte.tif",
options="-of MEM -scale 0 255 0 255 -scale 74 255 0 255 -b 1 -b 1",
)
cs1 = ds.GetRasterBand(1).Checksum()
cs2 = ds.GetRasterBand(2).Checksum()
ds = None
assert cs1 == 4672, 'did not get expected checksum'
assert cs2 == 4323, 'did not get expected checksum'
assert cs1 == 4672, "did not get expected checksum"
assert cs2 == 4323, "did not get expected checksum"
###############################################################################
# Test multi-band power scaling with a single -scale and -exponent occurrence.
@ -135,13 +153,18 @@ def test_vrtmisc_6():
def test_vrtmisc_7():
# -scale and -exponent, specified once, apply to all bands
ds = gdal.Translate('', 'data/byte.tif', options='-of MEM -scale 74 255 0 255 -exponent 2.2 -b 1 -b 1')
ds = gdal.Translate(
"",
"data/byte.tif",
options="-of MEM -scale 74 255 0 255 -exponent 2.2 -b 1 -b 1",
)
cs1 = ds.GetRasterBand(1).Checksum()
cs2 = ds.GetRasterBand(2).Checksum()
ds = None
assert cs1 == 4159, 'did not get expected checksum'
assert cs2 == 4159, 'did not get expected checksum'
assert cs1 == 4159, "did not get expected checksum"
assert cs2 == 4159, "did not get expected checksum"
###############################################################################
# Test multi-band power scaling with -scale_XX and -exponent_XX syntax
@ -150,13 +173,18 @@ def test_vrtmisc_7():
def test_vrtmisc_8():
# -scale_2 and -exponent_2 apply to band 2 only
ds = gdal.Translate('', 'data/byte.tif', options='-of MEM -scale_2 74 255 0 255 -exponent_2 2.2 -b 1 -b 1')
ds = gdal.Translate(
"",
"data/byte.tif",
options="-of MEM -scale_2 74 255 0 255 -exponent_2 2.2 -b 1 -b 1",
)
cs1 = ds.GetRasterBand(1).Checksum()
cs2 = ds.GetRasterBand(2).Checksum()
ds = None
assert cs1 == 4672, 'did not get expected checksum'
assert cs2 == 4159, 'did not get expected checksum'
assert cs1 == 4672, "did not get expected checksum"
assert cs2 == 4159, "did not get expected checksum"
###############################################################################
# Test multi-band linear scaling with repeated -scale and -exponent syntax
@ -165,13 +193,18 @@ def test_vrtmisc_8():
def test_vrtmisc_9():
# -scale and -exponent repeated as many times as output band number
ds = gdal.Translate('', 'data/byte.tif', options='-of MEM -scale 0 255 0 255 -scale 74 255 0 255 -exponent 1 -exponent 2.2 -b 1 -b 1')
ds = gdal.Translate(
"",
"data/byte.tif",
options="-of MEM -scale 0 255 0 255 -scale 74 255 0 255 -exponent 1 -exponent 2.2 -b 1 -b 1",
)
cs1 = ds.GetRasterBand(1).Checksum()
cs2 = ds.GetRasterBand(2).Checksum()
ds = None
assert cs1 == 4672, 'did not get expected checksum'
assert cs2 == 4159, 'did not get expected checksum'
assert cs1 == 4672, "did not get expected checksum"
assert cs2 == 4159, "did not get expected checksum"
###############################################################################
# Test metadata serialization (#5944)
@ -179,8 +212,9 @@ def test_vrtmisc_9():
def test_vrtmisc_10():
gdal.FileFromMemBuffer("/vsimem/vrtmisc_10.vrt",
"""<VRTDataset rasterXSize="1" rasterYSize="1">
gdal.FileFromMemBuffer(
"/vsimem/vrtmisc_10.vrt",
"""<VRTDataset rasterXSize="1" rasterYSize="1">
<Metadata>
<MDI key="foo">bar</MDI>
</Metadata>
@ -200,7 +234,8 @@ def test_vrtmisc_10():
</SimpleSource>
</VRTRasterBand>
</VRTDataset>
""")
""",
)
ds = gdal.Open("/vsimem/vrtmisc_10.vrt", gdal.GA_Update)
# to trigger a flush
@ -208,28 +243,29 @@ def test_vrtmisc_10():
ds = None
ds = gdal.Open("/vsimem/vrtmisc_10.vrt", gdal.GA_Update)
assert ds.GetMetadata() == {'foo': 'bar'}
assert ds.GetMetadata('some_domain') == {'bar': 'baz'}
assert ds.GetMetadata_List('xml:a_xml_domain')[0] == '<some_xml />\n'
assert ds.GetMetadata() == {"foo": "bar"}
assert ds.GetMetadata("some_domain") == {"bar": "baz"}
assert ds.GetMetadata_List("xml:a_xml_domain")[0] == "<some_xml />\n"
# Empty default domain
ds.SetMetadata({})
ds = None
ds = gdal.Open("/vsimem/vrtmisc_10.vrt")
assert ds.GetMetadata() == {}
assert ds.GetMetadata('some_domain') == {'bar': 'baz'}
assert ds.GetMetadata_List('xml:a_xml_domain')[0] == '<some_xml />\n'
assert ds.GetMetadata("some_domain") == {"bar": "baz"}
assert ds.GetMetadata_List("xml:a_xml_domain")[0] == "<some_xml />\n"
ds = None
gdal.Unlink("/vsimem/vrtmisc_10.vrt")
###############################################################################
# Test relativeToVRT is preserved during re-serialization (#5985)
def test_vrtmisc_11():
f = open('tmp/vrtmisc_11.vrt', 'wt')
f = open("tmp/vrtmisc_11.vrt", "wt")
f.write(
"""<VRTDataset rasterXSize="1" rasterYSize="1">
<VRTRasterBand dataType="Byte" band="1">
@ -242,7 +278,8 @@ def test_vrtmisc_11():
</SimpleSource>
</VRTRasterBand>
</VRTDataset>
""")
"""
)
f.close()
ds = gdal.Open("tmp/vrtmisc_11.vrt", gdal.GA_Update)
@ -250,20 +287,22 @@ def test_vrtmisc_11():
ds.SetMetadata(ds.GetMetadata())
ds = None
data = open('tmp/vrtmisc_11.vrt', 'rt').read()
data = open("tmp/vrtmisc_11.vrt", "rt").read()
gdal.Unlink("tmp/vrtmisc_11.vrt")
assert '<SourceFilename relativeToVRT="1">../data/byte.tif</SourceFilename>' in data
###############################################################################
# Test set/delete nodata
def test_vrtmisc_12():
gdal.FileFromMemBuffer("/vsimem/vrtmisc_12.vrt",
"""<VRTDataset rasterXSize="1" rasterYSize="1">
gdal.FileFromMemBuffer(
"/vsimem/vrtmisc_12.vrt",
"""<VRTDataset rasterXSize="1" rasterYSize="1">
<VRTRasterBand dataType="Byte" band="1">
<SimpleSource>
<SourceFilename relativeToVRT="0">foo.tif</SourceFilename>
@ -274,7 +313,8 @@ def test_vrtmisc_12():
</SimpleSource>
</VRTRasterBand>
</VRTDataset>
""")
""",
)
ds = gdal.Open("/vsimem/vrtmisc_12.vrt", gdal.GA_Update)
ds.GetRasterBand(1).SetNoDataValue(123)
@ -291,15 +331,17 @@ def test_vrtmisc_12():
gdal.Unlink("/vsimem/vrtmisc_12.vrt")
###############################################################################
# Test CreateCopy() preserve NBITS
def test_vrtmisc_13():
ds = gdal.Open('data/oddsize1bit.tif')
out_ds = gdal.GetDriverByName('VRT').CreateCopy('', ds)
assert out_ds.GetRasterBand(1).GetMetadataItem('NBITS', 'IMAGE_STRUCTURE') == '1'
ds = gdal.Open("data/oddsize1bit.tif")
out_ds = gdal.GetDriverByName("VRT").CreateCopy("", ds)
assert out_ds.GetRasterBand(1).GetMetadataItem("NBITS", "IMAGE_STRUCTURE") == "1"
###############################################################################
# Test SrcRect/DstRect are serialized as integers
@ -307,31 +349,46 @@ def test_vrtmisc_13():
def test_vrtmisc_14():
src_ds = gdal.GetDriverByName('GTiff').Create('/vsimem/vrtmisc_14_src.tif', 123456789, 1, options=['SPARSE_OK=YES', 'TILED=YES'])
gdal.GetDriverByName('VRT').CreateCopy('/vsimem/vrtmisc_14.vrt', src_ds)
src_ds = gdal.GetDriverByName("GTiff").Create(
"/vsimem/vrtmisc_14_src.tif",
123456789,
1,
options=["SPARSE_OK=YES", "TILED=YES"],
)
gdal.GetDriverByName("VRT").CreateCopy("/vsimem/vrtmisc_14.vrt", src_ds)
src_ds = None
fp = gdal.VSIFOpenL('/vsimem/vrtmisc_14.vrt', 'rb')
content = gdal.VSIFReadL(1, 10000, fp).decode('latin1')
fp = gdal.VSIFOpenL("/vsimem/vrtmisc_14.vrt", "rb")
content = gdal.VSIFReadL(1, 10000, fp).decode("latin1")
gdal.VSIFCloseL(fp)
gdal.Unlink("/vsimem/vrtmisc_14_src.tif")
gdal.Unlink("/vsimem/vrtmisc_14.vrt")
assert ('<SrcRect xOff="0" yOff="0" xSize="123456789" ySize="1"' in content and \
'<DstRect xOff="0" yOff="0" xSize="123456789" ySize="1"' in content)
assert (
'<SrcRect xOff="0" yOff="0" xSize="123456789" ySize="1"' in content
and '<DstRect xOff="0" yOff="0" xSize="123456789" ySize="1"' in content
)
src_ds = gdal.GetDriverByName('GTiff').Create('/vsimem/vrtmisc_14_src.tif', 1, 123456789, options=['SPARSE_OK=YES', 'TILED=YES'])
gdal.GetDriverByName('VRT').CreateCopy('/vsimem/vrtmisc_14.vrt', src_ds)
src_ds = gdal.GetDriverByName("GTiff").Create(
"/vsimem/vrtmisc_14_src.tif",
1,
123456789,
options=["SPARSE_OK=YES", "TILED=YES"],
)
gdal.GetDriverByName("VRT").CreateCopy("/vsimem/vrtmisc_14.vrt", src_ds)
src_ds = None
fp = gdal.VSIFOpenL('/vsimem/vrtmisc_14.vrt', 'rb')
content = gdal.VSIFReadL(1, 10000, fp).decode('latin1')
fp = gdal.VSIFOpenL("/vsimem/vrtmisc_14.vrt", "rb")
content = gdal.VSIFReadL(1, 10000, fp).decode("latin1")
gdal.VSIFCloseL(fp)
gdal.Unlink("/vsimem/vrtmisc_14_src.tif")
gdal.Unlink("/vsimem/vrtmisc_14.vrt")
assert ('<SrcRect xOff="0" yOff="0" xSize="1" ySize="123456789"' in content and \
'<DstRect xOff="0" yOff="0" xSize="1" ySize="123456789"' in content)
assert (
'<SrcRect xOff="0" yOff="0" xSize="1" ySize="123456789"' in content
and '<DstRect xOff="0" yOff="0" xSize="1" ySize="123456789"' in content
)
###############################################################################
# Test CreateCopy() preserve SIGNEDBYTE
@ -339,11 +396,17 @@ def test_vrtmisc_14():
def test_vrtmisc_15():
ds = gdal.GetDriverByName('GTiff').Create('/vsimem/vrtmisc_15.tif', 1, 1, options=['PIXELTYPE=SIGNEDBYTE'])
out_ds = gdal.GetDriverByName('VRT').CreateCopy('', ds)
assert out_ds.GetRasterBand(1).GetMetadataItem('PIXELTYPE', 'IMAGE_STRUCTURE') == 'SIGNEDBYTE'
ds = gdal.GetDriverByName("GTiff").Create(
"/vsimem/vrtmisc_15.tif", 1, 1, options=["PIXELTYPE=SIGNEDBYTE"]
)
out_ds = gdal.GetDriverByName("VRT").CreateCopy("", ds)
assert (
out_ds.GetRasterBand(1).GetMetadataItem("PIXELTYPE", "IMAGE_STRUCTURE")
== "SIGNEDBYTE"
)
ds = None
gdal.Unlink('/vsimem/vrtmisc_15.tif')
gdal.Unlink("/vsimem/vrtmisc_15.tif")
###############################################################################
# Test rounding to closest int for coordinates
@ -351,9 +414,12 @@ def test_vrtmisc_15():
def test_vrtmisc_16():
gdal.BuildVRT('/vsimem/vrtmisc_16.vrt', ['data/vrtmisc16_tile1.tif', 'data/vrtmisc16_tile2.tif'])
fp = gdal.VSIFOpenL('/vsimem/vrtmisc_16.vrt', 'rb')
content = gdal.VSIFReadL(1, 100000, fp).decode('latin1')
gdal.BuildVRT(
"/vsimem/vrtmisc_16.vrt",
["data/vrtmisc16_tile1.tif", "data/vrtmisc16_tile2.tif"],
)
fp = gdal.VSIFOpenL("/vsimem/vrtmisc_16.vrt", "rb")
content = gdal.VSIFReadL(1, 100000, fp).decode("latin1")
gdal.VSIFCloseL(fp)
assert '<SrcRect xOff="0" yOff="0" xSize="952" ySize="1189"' in content
@ -361,14 +427,18 @@ def test_vrtmisc_16():
assert '<SrcRect xOff="0" yOff="0" xSize="494" ySize="893"' in content
assert '<DstRect xOff="1680" yOff="5922" xSize="494" ySize="893"' in content
gdal.GetDriverByName('GTiff').CreateCopy('/vsimem/vrtmisc_16.tif', gdal.Open('/vsimem/vrtmisc_16.vrt'))
ds = gdal.Open('/vsimem/vrtmisc_16.tif')
gdal.GetDriverByName("GTiff").CreateCopy(
"/vsimem/vrtmisc_16.tif", gdal.Open("/vsimem/vrtmisc_16.vrt")
)
ds = gdal.Open("/vsimem/vrtmisc_16.tif")
cs = ds.GetRasterBand(1).Checksum()
assert cs == 206
gdal.Unlink('/vsimem/vrtmisc_16.tif')
gdal.Unlink('/vsimem/vrtmisc_16.vrt')
gdal.Unlink("/vsimem/vrtmisc_16.tif")
gdal.Unlink("/vsimem/vrtmisc_16.vrt")
gdal.FileFromMemBuffer('/vsimem/vrtmisc_16.vrt', """<VRTDataset rasterXSize="2174" rasterYSize="6815">
gdal.FileFromMemBuffer(
"/vsimem/vrtmisc_16.vrt",
"""<VRTDataset rasterXSize="2174" rasterYSize="6815">
<VRTRasterBand dataType="Byte" band="1">
<NoDataValue>0</NoDataValue>
<ComplexSource>
@ -388,13 +458,17 @@ def test_vrtmisc_16():
<NODATA>0</NODATA>
</ComplexSource>
</VRTRasterBand>
</VRTDataset>""")
gdal.GetDriverByName('GTiff').CreateCopy('/vsimem/vrtmisc_16.tif', gdal.Open('/vsimem/vrtmisc_16.vrt'))
ds = gdal.Open('/vsimem/vrtmisc_16.tif')
</VRTDataset>""",
)
gdal.GetDriverByName("GTiff").CreateCopy(
"/vsimem/vrtmisc_16.tif", gdal.Open("/vsimem/vrtmisc_16.vrt")
)
ds = gdal.Open("/vsimem/vrtmisc_16.tif")
cs = ds.GetRasterBand(1).Checksum()
assert cs == 206
gdal.Unlink('/vsimem/vrtmisc_16.tif')
gdal.Unlink('/vsimem/vrtmisc_16.vrt')
gdal.Unlink("/vsimem/vrtmisc_16.tif")
gdal.Unlink("/vsimem/vrtmisc_16.vrt")
###############################################################################
# Check that the serialized xml:VRT doesn't include itself (#6767)
@ -402,14 +476,15 @@ def test_vrtmisc_16():
def test_vrtmisc_17():
ds = gdal.Open('data/byte.tif')
vrt_ds = gdal.GetDriverByName('VRT').CreateCopy('/vsimem/vrtmisc_17.vrt', ds)
xml_vrt = vrt_ds.GetMetadata('xml:VRT')[0]
ds = gdal.Open("data/byte.tif")
vrt_ds = gdal.GetDriverByName("VRT").CreateCopy("/vsimem/vrtmisc_17.vrt", ds)
xml_vrt = vrt_ds.GetMetadata("xml:VRT")[0]
vrt_ds = None
gdal.Unlink('/vsimem/vrtmisc_17.vrt')
gdal.Unlink("/vsimem/vrtmisc_17.vrt")
assert "xml:VRT" not in xml_vrt
assert 'xml:VRT' not in xml_vrt
###############################################################################
# Check GetMetadata('xml:VRT') behaviour on a in-memory VRT copied from a VRT
@ -417,14 +492,18 @@ def test_vrtmisc_17():
def test_vrtmisc_18():
ds = gdal.Open('data/byte.vrt')
vrt_ds = gdal.GetDriverByName('VRT').CreateCopy('', ds)
xml_vrt = vrt_ds.GetMetadata('xml:VRT')[0]
assert gdal.GetLastErrorMsg() == ''
ds = gdal.Open("data/byte.vrt")
vrt_ds = gdal.GetDriverByName("VRT").CreateCopy("", ds)
xml_vrt = vrt_ds.GetMetadata("xml:VRT")[0]
assert gdal.GetLastErrorMsg() == ""
vrt_ds = None
assert ('<SourceFilename relativeToVRT="1">data/byte.tif</SourceFilename>' in xml_vrt or \
'<SourceFilename relativeToVRT="1">data\\byte.tif</SourceFilename>' in xml_vrt)
assert (
'<SourceFilename relativeToVRT="1">data/byte.tif</SourceFilename>' in xml_vrt
or '<SourceFilename relativeToVRT="1">data\\byte.tif</SourceFilename>'
in xml_vrt
)
###############################################################################
# Check RAT support
@ -432,30 +511,32 @@ def test_vrtmisc_18():
def test_vrtmisc_rat():
ds = gdal.Translate('/vsimem/vrtmisc_rat.tif', 'data/byte.tif', format='MEM')
ds = gdal.Translate("/vsimem/vrtmisc_rat.tif", "data/byte.tif", format="MEM")
rat = gdal.RasterAttributeTable()
rat.CreateColumn("Ints", gdal.GFT_Integer, gdal.GFU_Generic)
ds.GetRasterBand(1).SetDefaultRAT(rat)
vrt_ds = gdal.GetDriverByName('VRT').CreateCopy('/vsimem/vrtmisc_rat.vrt', ds)
vrt_ds = gdal.GetDriverByName("VRT").CreateCopy("/vsimem/vrtmisc_rat.vrt", ds)
xml_vrt = vrt_ds.GetMetadata('xml:VRT')[0]
assert gdal.GetLastErrorMsg() == ''
xml_vrt = vrt_ds.GetMetadata("xml:VRT")[0]
assert gdal.GetLastErrorMsg() == ""
vrt_ds = None
assert '<GDALRasterAttributeTable tableType="thematic">' in xml_vrt
vrt_ds = gdal.Translate('/vsimem/vrtmisc_rat.vrt', ds, format='VRT', srcWin=[0, 0, 1, 1])
vrt_ds = gdal.Translate(
"/vsimem/vrtmisc_rat.vrt", ds, format="VRT", srcWin=[0, 0, 1, 1]
)
xml_vrt = vrt_ds.GetMetadata('xml:VRT')[0]
assert gdal.GetLastErrorMsg() == ''
xml_vrt = vrt_ds.GetMetadata("xml:VRT")[0]
assert gdal.GetLastErrorMsg() == ""
vrt_ds = None
assert '<GDALRasterAttributeTable tableType="thematic">' in xml_vrt
ds = None
vrt_ds = gdal.Open('/vsimem/vrtmisc_rat.vrt', gdal.GA_Update)
vrt_ds = gdal.Open("/vsimem/vrtmisc_rat.vrt", gdal.GA_Update)
rat = vrt_ds.GetRasterBand(1).GetDefaultRAT()
assert rat is not None and rat.GetColumnCount() == 1
vrt_ds.GetRasterBand(1).SetDefaultRAT(None)
@ -464,8 +545,9 @@ def test_vrtmisc_rat():
ds = None
gdal.Unlink('/vsimem/vrtmisc_rat.vrt')
gdal.Unlink('/vsimem/vrtmisc_rat.tif')
gdal.Unlink("/vsimem/vrtmisc_rat.vrt")
gdal.Unlink("/vsimem/vrtmisc_rat.tif")
###############################################################################
# Check ColorTable support
@ -473,7 +555,7 @@ def test_vrtmisc_rat():
def test_vrtmisc_colortable():
ds = gdal.Translate('', 'data/byte.tif', format='VRT')
ds = gdal.Translate("", "data/byte.tif", format="VRT")
ct = gdal.ColorTable()
ct.SetColorEntry(0, (255, 255, 255, 255))
ds.GetRasterBand(1).SetColorTable(ct)
@ -481,14 +563,15 @@ def test_vrtmisc_colortable():
ds.GetRasterBand(1).SetColorTable(None)
assert ds.GetRasterBand(1).GetColorTable() is None
###############################################################################
# Check histogram support
def test_vrtmisc_histogram():
tmpfile = '/vsimem/vrtmisc_histogram.vrt'
ds = gdal.Translate(tmpfile, 'data/byte.tif', format='VRT')
tmpfile = "/vsimem/vrtmisc_histogram.vrt"
ds = gdal.Translate(tmpfile, "data/byte.tif", format="VRT")
ds.GetRasterBand(1).SetDefaultHistogram(1, 2, [3000000000, 4])
ds = None
@ -500,14 +583,15 @@ def test_vrtmisc_histogram():
gdal.Unlink(tmpfile)
###############################################################################
# write SRS with unusual data axis to SRS axis mapping
def test_vrtmisc_write_srs():
tmpfile = '/vsimem/test_vrtmisc_write_srs.vrt'
ds = gdal.Translate(tmpfile, 'data/byte.tif', format='VRT')
tmpfile = "/vsimem/test_vrtmisc_write_srs.vrt"
ds = gdal.Translate(tmpfile, "data/byte.tif", format="VRT")
sr = osr.SpatialReference()
sr.SetAxisMappingStrategy(osr.OAMS_AUTHORITY_COMPLIANT)
sr.ImportFromEPSG(4326)
@ -515,37 +599,45 @@ def test_vrtmisc_write_srs():
ds = None
ds = gdal.Open(tmpfile)
assert ds.GetSpatialRef().GetDataAxisToSRSAxisMapping() == [1,2]
assert ds.GetSpatialRef().GetDataAxisToSRSAxisMapping() == [1, 2]
ds = None
gdal.Unlink(tmpfile)
###############################################################################
# complex scenario involving masks and implicit overviews
def test_vrtmisc_mask_implicit_overviews():
with gdaltest.config_option('GDAL_TIFF_INTERNAL_MASK', 'YES'):
ds = gdal.Translate('/vsimem/cog.tif', 'data/stefan_full_rgba.tif', options = '-outsize 2048 0 -b 1 -b 2 -b 3 -mask 4')
ds.BuildOverviews('NEAR', [2, 4])
with gdaltest.config_option("GDAL_TIFF_INTERNAL_MASK", "YES"):
ds = gdal.Translate(
"/vsimem/cog.tif",
"data/stefan_full_rgba.tif",
options="-outsize 2048 0 -b 1 -b 2 -b 3 -mask 4",
)
ds.BuildOverviews("NEAR", [2, 4])
ds = None
gdal.Translate('/vsimem/cog.vrt', '/vsimem/cog.tif')
ds = gdal.Open('/vsimem/cog.vrt')
gdal.Translate("/vsimem/cog.vrt", "/vsimem/cog.tif")
ds = gdal.Open("/vsimem/cog.vrt")
assert ds.GetRasterBand(1).GetOverview(0).GetMaskFlags() == gdal.GMF_PER_DATASET
assert ds.GetRasterBand(1).GetMaskBand().IsMaskBand()
assert ds.GetRasterBand(1).GetOverview(0).GetMaskBand().IsMaskBand()
ds = None
gdal.Translate('/vsimem/out.tif', '/vsimem/cog.vrt', options = '-b mask -outsize 10% 0')
gdal.GetDriverByName('GTiff').Delete('/vsimem/cog.tif')
gdal.Unlink('/vsimem/cog.vrt')
ds = gdal.Open('/vsimem/out.tif')
gdal.Translate(
"/vsimem/out.tif", "/vsimem/cog.vrt", options="-b mask -outsize 10% 0"
)
gdal.GetDriverByName("GTiff").Delete("/vsimem/cog.tif")
gdal.Unlink("/vsimem/cog.vrt")
ds = gdal.Open("/vsimem/out.tif")
histo = ds.GetRasterBand(1).GetDefaultHistogram()[3]
# Check that there are only 0 and 255 in the histogram
assert histo[0] + histo[255] == ds.RasterXSize * ds.RasterYSize, histo
assert ds.GetRasterBand(1).Checksum() == 46885
ds = None
gdal.Unlink('/vsimem/out.tif')
gdal.Unlink("/vsimem/out.tif")
###############################################################################
@ -553,13 +645,9 @@ def test_vrtmisc_mask_implicit_overviews():
def test_vrtmisc_blocksize():
filename = '/vsimem/test_vrtmisc_blocksize.vrt'
vrt_ds = gdal.GetDriverByName('VRT').Create(filename, 50, 50, 0)
options = [
'subClass=VRTSourcedRasterBand',
'blockXSize=32',
'blockYSize=48'
]
filename = "/vsimem/test_vrtmisc_blocksize.vrt"
vrt_ds = gdal.GetDriverByName("VRT").Create(filename, 50, 50, 0)
options = ["subClass=VRTSourcedRasterBand", "blockXSize=32", "blockYSize=48"]
vrt_ds.AddBand(gdal.GDT_Byte, options)
vrt_ds = None
@ -578,8 +666,8 @@ def test_vrtmisc_blocksize():
def test_vrtmisc_coordinate_epoch():
filename = '/vsimem/temp.vrt'
gdal.Translate(filename, 'data/byte.tif', options='-a_coord_epoch 2021.3')
filename = "/vsimem/temp.vrt"
gdal.Translate(filename, "data/byte.tif", options="-a_coord_epoch 2021.3")
ds = gdal.Open(filename)
srs = ds.GetSpatialRef()
assert srs.GetCoordinateEpoch() == 2021.3
@ -591,79 +679,96 @@ def test_vrtmisc_coordinate_epoch():
###############################################################################
# Test the relativeToVRT attribute of SourceFilename
def test_vrtmisc_sourcefilename_all_relatives():
shutil.copy('data/byte.tif', 'tmp')
shutil.copy("data/byte.tif", "tmp")
try:
src_ds = gdal.Open(os.path.join('tmp', 'byte.tif'))
ds = gdal.GetDriverByName('VRT').CreateCopy('', src_ds)
ds.SetDescription(os.path.join('tmp', 'byte.vrt'))
src_ds = gdal.Open(os.path.join("tmp", "byte.tif"))
ds = gdal.GetDriverByName("VRT").CreateCopy("", src_ds)
ds.SetDescription(os.path.join("tmp", "byte.vrt"))
ds = None
assert '<SourceFilename relativeToVRT="1">byte.tif<' in open('tmp/byte.vrt', 'rt').read()
assert (
'<SourceFilename relativeToVRT="1">byte.tif<'
in open("tmp/byte.vrt", "rt").read()
)
finally:
gdal.Unlink('tmp/byte.tif')
gdal.Unlink('tmp/byte.vrt')
gdal.Unlink("tmp/byte.tif")
gdal.Unlink("tmp/byte.vrt")
###############################################################################
# Test the relativeToVRT attribute of SourceFilename
def test_vrtmisc_sourcefilename_source_relative_dest_absolute():
shutil.copy('data/byte.tif', 'tmp')
shutil.copy("data/byte.tif", "tmp")
try:
src_ds = gdal.Open(os.path.join('tmp', 'byte.tif'))
ds = gdal.GetDriverByName('VRT').CreateCopy('', src_ds)
path = os.path.join(os.getcwd(), 'tmp', 'byte.vrt')
if sys.platform == 'win32':
path = path.replace('/', '\\')
src_ds = gdal.Open(os.path.join("tmp", "byte.tif"))
ds = gdal.GetDriverByName("VRT").CreateCopy("", src_ds)
path = os.path.join(os.getcwd(), "tmp", "byte.vrt")
if sys.platform == "win32":
path = path.replace("/", "\\")
ds.SetDescription(path)
ds = None
assert '<SourceFilename relativeToVRT="1">byte.tif<' in open('tmp/byte.vrt', 'rt').read()
assert (
'<SourceFilename relativeToVRT="1">byte.tif<'
in open("tmp/byte.vrt", "rt").read()
)
finally:
gdal.Unlink('tmp/byte.tif')
gdal.Unlink('tmp/byte.vrt')
gdal.Unlink("tmp/byte.tif")
gdal.Unlink("tmp/byte.vrt")
###############################################################################
# Test the relativeToVRT attribute of SourceFilename
def test_vrtmisc_sourcefilename_source_absolute_dest_absolute():
shutil.copy('data/byte.tif', 'tmp')
shutil.copy("data/byte.tif", "tmp")
try:
src_ds = gdal.Open(os.path.join(os.getcwd(), 'tmp', 'byte.tif'))
ds = gdal.GetDriverByName('VRT').CreateCopy('', src_ds)
ds.SetDescription(os.path.join(os.getcwd(), 'tmp', 'byte.vrt'))
src_ds = gdal.Open(os.path.join(os.getcwd(), "tmp", "byte.tif"))
ds = gdal.GetDriverByName("VRT").CreateCopy("", src_ds)
ds.SetDescription(os.path.join(os.getcwd(), "tmp", "byte.vrt"))
ds = None
assert '<SourceFilename relativeToVRT="1">byte.tif<' in open('tmp/byte.vrt', 'rt').read()
assert (
'<SourceFilename relativeToVRT="1">byte.tif<'
in open("tmp/byte.vrt", "rt").read()
)
finally:
gdal.Unlink('tmp/byte.tif')
gdal.Unlink('tmp/byte.vrt')
gdal.Unlink("tmp/byte.tif")
gdal.Unlink("tmp/byte.vrt")
###############################################################################
# Test the relativeToVRT attribute of SourceFilename
def test_vrtmisc_sourcefilename_source_absolute_dest_relative():
shutil.copy('data/byte.tif', 'tmp')
shutil.copy("data/byte.tif", "tmp")
try:
path = os.path.join(os.getcwd(), 'tmp', 'byte.tif')
if sys.platform == 'win32':
path = path.replace('/', '\\')
path = os.path.join(os.getcwd(), "tmp", "byte.tif")
if sys.platform == "win32":
path = path.replace("/", "\\")
src_ds = gdal.Open(path)
ds = gdal.GetDriverByName('VRT').CreateCopy('', src_ds)
ds.SetDescription(os.path.join('tmp', 'byte.vrt'))
ds = gdal.GetDriverByName("VRT").CreateCopy("", src_ds)
ds.SetDescription(os.path.join("tmp", "byte.vrt"))
ds = None
assert '<SourceFilename relativeToVRT="1">byte.tif<' in open('tmp/byte.vrt', 'rt').read()
assert (
'<SourceFilename relativeToVRT="1">byte.tif<'
in open("tmp/byte.vrt", "rt").read()
)
finally:
gdal.Unlink('tmp/byte.tif')
gdal.Unlink('tmp/byte.vrt')
gdal.Unlink("tmp/byte.tif")
gdal.Unlink("tmp/byte.vrt")
###############################################################################
# Test Int64 nodata
@ -671,8 +776,10 @@ def test_vrtmisc_sourcefilename_source_absolute_dest_relative():
def test_vrtmisc_nodata_int64():
filename = '/vsimem/temp.vrt'
ds = gdal.Translate(filename, 'data/byte.tif', format = 'VRT', outputType = gdal.GDT_Int64)
filename = "/vsimem/temp.vrt"
ds = gdal.Translate(
filename, "data/byte.tif", format="VRT", outputType=gdal.GDT_Int64
)
val = -(1 << 63)
assert ds.GetRasterBand(1).SetNoDataValue(val) == gdal.CE_None
assert ds.GetRasterBand(1).GetNoDataValue() == val
@ -691,8 +798,10 @@ def test_vrtmisc_nodata_int64():
def test_vrtmisc_nodata_uint64():
filename = '/vsimem/temp.vrt'
ds = gdal.Translate(filename, 'data/byte.tif', format = 'VRT', outputType = gdal.GDT_UInt64)
filename = "/vsimem/temp.vrt"
ds = gdal.Translate(
filename, "data/byte.tif", format="VRT", outputType=gdal.GDT_UInt64
)
val = (1 << 64) - 1
assert ds.GetRasterBand(1).SetNoDataValue(val) == gdal.CE_None
assert ds.GetRasterBand(1).GetNoDataValue() == val
@ -711,8 +820,8 @@ def test_vrtmisc_nodata_uint64():
def test_vrtmisc_alpha_ismaskband():
src_ds = gdal.GetDriverByName('MEM').Create('', 1, 1, 2)
src_ds = gdal.GetDriverByName("MEM").Create("", 1, 1, 2)
src_ds.GetRasterBand(2).SetColorInterpretation(gdal.GCI_AlphaBand)
ds = gdal.GetDriverByName('VRT').CreateCopy('', src_ds)
ds = gdal.GetDriverByName("VRT").CreateCopy("", src_ds)
assert not ds.GetRasterBand(1).IsMaskBand()
assert ds.GetRasterBand(2).IsMaskBand()

File diff suppressed because it is too large Load Diff

View File

@ -30,20 +30,22 @@
import base64
import stat
from osgeo import gdal
import gdaltest
import pytest
pytestmark = pytest.mark.skipif(not gdaltest.built_against_curl(), reason="GDAL not built against curl")
from osgeo import gdal
pytestmark = pytest.mark.skipif(
not gdaltest.built_against_curl(), reason="GDAL not built against curl"
)
def open_for_read(uri):
"""
Opens a test file for reading.
"""
return gdal.VSIFOpenExL(uri, 'rb', 1)
return gdal.VSIFOpenExL(uri, "rb", 1)
###############################################################################
@ -52,132 +54,152 @@ def open_for_read(uri):
def test_vsiadls_real_instance_tests():
adls_resource = gdal.GetConfigOption('ADLS_RESOURCE')
adls_resource = gdal.GetConfigOption("ADLS_RESOURCE")
if adls_resource is None:
pytest.skip('Missing ADLS_RESOURCE')
pytest.skip("Missing ADLS_RESOURCE")
if '/' not in adls_resource:
path = '/vsiadls/' + adls_resource
if "/" not in adls_resource:
path = "/vsiadls/" + adls_resource
try:
statres = gdal.VSIStatL(path)
assert statres is not None and stat.S_ISDIR(statres.mode), \
('%s is not a valid bucket' % path)
assert statres is not None and stat.S_ISDIR(statres.mode), (
"%s is not a valid bucket" % path
)
readdir = gdal.ReadDir(path)
assert readdir is not None, 'ReadDir() should not return empty list'
assert readdir is not None, "ReadDir() should not return empty list"
for filename in readdir:
if filename != '.':
subpath = path + '/' + filename
assert gdal.VSIStatL(subpath) is not None, \
('Stat(%s) should not return an error' % subpath)
if filename != ".":
subpath = path + "/" + filename
assert gdal.VSIStatL(subpath) is not None, (
"Stat(%s) should not return an error" % subpath
)
unique_id = 'vsiadls_test'
subpath = path + '/' + unique_id
unique_id = "vsiadls_test"
subpath = path + "/" + unique_id
ret = gdal.Mkdir(subpath, 0)
assert ret >= 0, ('Mkdir(%s) should not return an error' % subpath)
assert ret >= 0, "Mkdir(%s) should not return an error" % subpath
readdir = gdal.ReadDir(path)
assert unique_id in readdir, \
('ReadDir(%s) should contain %s' % (path, unique_id))
assert unique_id in readdir, "ReadDir(%s) should contain %s" % (
path,
unique_id,
)
ret = gdal.Mkdir(subpath, 0)
assert ret != 0, ('Mkdir(%s) repeated should return an error' % subpath)
assert ret != 0, "Mkdir(%s) repeated should return an error" % subpath
ret = gdal.Rmdir(subpath)
assert ret >= 0, ('Rmdir(%s) should not return an error' % subpath)
assert ret >= 0, "Rmdir(%s) should not return an error" % subpath
readdir = gdal.ReadDir(path)
assert unique_id not in readdir, \
('ReadDir(%s) should not contain %s' % (path, unique_id))
assert unique_id not in readdir, "ReadDir(%s) should not contain %s" % (
path,
unique_id,
)
ret = gdal.Rmdir(subpath)
assert ret != 0, ('Rmdir(%s) repeated should return an error' % subpath)
assert ret != 0, "Rmdir(%s) repeated should return an error" % subpath
ret = gdal.Mkdir(subpath, 0)
assert ret >= 0, ('Mkdir(%s) should not return an error' % subpath)
assert ret >= 0, "Mkdir(%s) should not return an error" % subpath
f = gdal.VSIFOpenL(subpath + '/test.txt', 'wb')
f = gdal.VSIFOpenL(subpath + "/test.txt", "wb")
assert f is not None
gdal.VSIFWriteL('hello', 1, 5, f)
gdal.VSIFWriteL("hello", 1, 5, f)
gdal.VSIFCloseL(f)
ret = gdal.Rmdir(subpath)
assert ret != 0, \
('Rmdir(%s) on non empty directory should return an error' % subpath)
assert ret != 0, (
"Rmdir(%s) on non empty directory should return an error" % subpath
)
f = gdal.VSIFOpenL(subpath + '/test.txt', 'rb')
f = gdal.VSIFOpenL(subpath + "/test.txt", "rb")
assert f is not None
data = gdal.VSIFReadL(1, 5, f).decode('utf-8')
assert data == 'hello'
data = gdal.VSIFReadL(1, 5, f).decode("utf-8")
assert data == "hello"
gdal.VSIFCloseL(f)
assert gdal.VSIStatL(subpath + '/test.txt') is not None
assert gdal.VSIStatL(subpath + "/test.txt") is not None
md = gdal.GetFileMetadata(subpath + '/test.txt', 'HEADERS')
assert 'x-ms-properties' in md
md = gdal.GetFileMetadata(subpath + "/test.txt", "HEADERS")
assert "x-ms-properties" in md
md = gdal.GetFileMetadata(subpath + '/test.txt', 'STATUS')
assert 'x-ms-resource-type' in md
assert 'x-ms-properties' not in md
md = gdal.GetFileMetadata(subpath + "/test.txt", "STATUS")
assert "x-ms-resource-type" in md
assert "x-ms-properties" not in md
md = gdal.GetFileMetadata(subpath + '/test.txt', 'ACL')
assert 'x-ms-acl' in md
assert 'x-ms-permissions' in md
md = gdal.GetFileMetadata(subpath + "/test.txt", "ACL")
assert "x-ms-acl" in md
assert "x-ms-permissions" in md
# Change properties
properties_foo_bar = 'foo=' + base64.b64encode('bar')
assert gdal.SetFileMetadata(subpath + '/test.txt', {'x-ms-properties': properties_foo_bar}, 'PROPERTIES')
properties_foo_bar = "foo=" + base64.b64encode("bar")
assert gdal.SetFileMetadata(
subpath + "/test.txt",
{"x-ms-properties": properties_foo_bar},
"PROPERTIES",
)
md = gdal.GetFileMetadata(subpath + '/test.txt', 'HEADERS')
assert 'x-ms-properties' in md
assert md['x-ms-properties'] == properties_foo_bar
md = gdal.GetFileMetadata(subpath + "/test.txt", "HEADERS")
assert "x-ms-properties" in md
assert md["x-ms-properties"] == properties_foo_bar
# Change ACL
assert gdal.SetFileMetadata(subpath + '/test.txt', {'x-ms-permissions': '0777'}, 'ACL')
assert gdal.SetFileMetadata(
subpath + "/test.txt", {"x-ms-permissions": "0777"}, "ACL"
)
md = gdal.GetFileMetadata(subpath + '/test.txt', 'ACL')
assert 'x-ms-permissions' in md
assert md['x-ms-permissions'] == 'rwxrwxrwx'
md = gdal.GetFileMetadata(subpath + "/test.txt", "ACL")
assert "x-ms-permissions" in md
assert md["x-ms-permissions"] == "rwxrwxrwx"
# Change ACL recursively
md = gdal.GetFileMetadata(subpath, 'ACL')
assert 'x-ms-acl' in md
assert gdal.SetFileMetadata(subpath + '/test.txt', {'x-ms-acl': md['x-ms-acl']}, 'ACL', ['RECURSIVE=YES', 'MODE=set'])
md = gdal.GetFileMetadata(subpath, "ACL")
assert "x-ms-acl" in md
assert gdal.SetFileMetadata(
subpath + "/test.txt",
{"x-ms-acl": md["x-ms-acl"]},
"ACL",
["RECURSIVE=YES", "MODE=set"],
)
assert gdal.Rename(subpath + "/test.txt", subpath + "/test2.txt") == 0
assert gdal.Rename(subpath + '/test.txt', subpath + '/test2.txt') == 0
assert gdal.VSIStatL(subpath + "/test.txt") is None
assert gdal.VSIStatL(subpath + '/test.txt') is None
assert gdal.VSIStatL(subpath + "/test2.txt") is not None
assert gdal.VSIStatL(subpath + '/test2.txt') is not None
f = gdal.VSIFOpenL(subpath + '/test2.txt', 'rb')
f = gdal.VSIFOpenL(subpath + "/test2.txt", "rb")
assert f is not None
data = gdal.VSIFReadL(1, 5, f).decode('utf-8')
assert data == 'hello'
data = gdal.VSIFReadL(1, 5, f).decode("utf-8")
assert data == "hello"
gdal.VSIFCloseL(f)
ret = gdal.Unlink(subpath + '/test2.txt')
assert ret >= 0, \
('Unlink(%s) should not return an error' % (subpath + '/test2.txt'))
ret = gdal.Unlink(subpath + "/test2.txt")
assert ret >= 0, "Unlink(%s) should not return an error" % (
subpath + "/test2.txt"
)
assert gdal.VSIStatL(subpath + '/test2.txt') is None
assert gdal.VSIStatL(subpath + "/test2.txt") is None
assert gdal.Unlink(subpath + '/test2.txt') != 0, "Unlink on a deleted file should return an error"
assert (
gdal.Unlink(subpath + "/test2.txt") != 0
), "Unlink on a deleted file should return an error"
f = gdal.VSIFOpenL(subpath + '/test2.txt', 'wb')
f = gdal.VSIFOpenL(subpath + "/test2.txt", "wb")
assert f is not None
gdal.VSIFCloseL(f)
assert gdal.VSIStatL(subpath + '/test2.txt') is not None
assert gdal.VSIStatL(subpath + "/test2.txt") is not None
finally:
assert gdal.RmdirRecursive(subpath) == 0
return
f = open_for_read('/vsiadls/' + adls_resource)
f = open_for_read("/vsiadls/" + adls_resource)
assert f is not None
ret = gdal.VSIFReadL(1, 1, f)
gdal.VSIFCloseL(f)
@ -185,25 +207,27 @@ def test_vsiadls_real_instance_tests():
assert len(ret) == 1
# Test GetSignedURL()
signed_url = gdal.GetSignedURL('/vsiadls/' + adls_resource)
f = open_for_read('/vsicurl_streaming/' + signed_url)
signed_url = gdal.GetSignedURL("/vsiadls/" + adls_resource)
f = open_for_read("/vsicurl_streaming/" + signed_url)
assert f is not None
ret = gdal.VSIFReadL(1, 1, f)
gdal.VSIFCloseL(f)
assert len(ret) == 1
###############################################################################
# Nominal cases (require valid credentials)
# Note: that test must be run with a delay > 30 seconds due to such a delay
# for re-creating a filesystem of the same name of one that has been destroyed
def test_vsiadls_real_instance_filesystem_tests():
if gdal.GetConfigOption('ADLS_ALLOW_FILESYSTEM_TESTS') is None:
pytest.skip('Missing ADLS_ALLOW_FILESYSTEM_TESTS')
if gdal.GetConfigOption("ADLS_ALLOW_FILESYSTEM_TESTS") is None:
pytest.skip("Missing ADLS_ALLOW_FILESYSTEM_TESTS")
fspath = '/vsiadls/test-vsiadls-filesystem-tests'
fspath = "/vsiadls/test-vsiadls-filesystem-tests"
try:
assert gdal.VSIStatL(fspath) is None
@ -213,13 +237,13 @@ def test_vsiadls_real_instance_filesystem_tests():
statres = gdal.VSIStatL(fspath)
assert statres is not None and stat.S_ISDIR(statres.mode)
assert gdal.ReadDir(fspath) == [ "." ]
assert gdal.ReadDir(fspath) == ["."]
assert gdal.Mkdir(fspath, 0) != 0
assert gdal.Mkdir(fspath + '/subdir', 0) == 0
assert gdal.Mkdir(fspath + "/subdir", 0) == 0
statres = gdal.VSIStatL(fspath + '/subdir')
statres = gdal.VSIStatL(fspath + "/subdir")
assert statres is not None and stat.S_ISDIR(statres.mode)
assert gdal.Rmdir(fspath) != 0

File diff suppressed because it is too large Load Diff

View File

@ -28,46 +28,52 @@
# DEALINGS IN THE SOFTWARE.
###############################################################################
from osgeo import gdal
import gdaltest
import os
import pytest
import stat
import sys
pytestmark = pytest.mark.skipif(not gdaltest.built_against_curl(), reason="GDAL not built against curl")
import gdaltest
import pytest
from osgeo import gdal
pytestmark = pytest.mark.skipif(
not gdaltest.built_against_curl(), reason="GDAL not built against curl"
)
def open_for_read(uri):
"""
Opens a test file for reading.
"""
return gdal.VSIFOpenExL(uri, 'rb', 1)
return gdal.VSIFOpenExL(uri, "rb", 1)
###############################################################################
@pytest.fixture(autouse=True, scope='module')
@pytest.fixture(autouse=True, scope="module")
def startup_and_cleanup():
az_vars = {}
for var, reset_val in (
('AZURE_STORAGE_CONNECTION_STRING', None),
('AZURE_STORAGE_ACCOUNT', None),
('AZURE_STORAGE_ACCESS_KEY', None),
('AZURE_STORAGE_SAS_TOKEN', None),
('AZURE_NO_SIGN_REQUEST', None),
('AZURE_CONFIG_DIR', ''),
('AZURE_STORAGE_ACCESS_TOKEN', '')):
("AZURE_STORAGE_CONNECTION_STRING", None),
("AZURE_STORAGE_ACCOUNT", None),
("AZURE_STORAGE_ACCESS_KEY", None),
("AZURE_STORAGE_SAS_TOKEN", None),
("AZURE_NO_SIGN_REQUEST", None),
("AZURE_CONFIG_DIR", ""),
("AZURE_STORAGE_ACCESS_TOKEN", ""),
):
az_vars[var] = gdal.GetConfigOption(var)
gdal.SetConfigOption(var, reset_val)
assert gdal.GetSignedURL('/vsiaz/foo/bar') is None
assert gdal.GetSignedURL("/vsiaz/foo/bar") is None
yield
for var in az_vars:
gdal.SetConfigOption(var, az_vars[var])
###############################################################################
# Error cases
@ -80,45 +86,56 @@ def test_vsiaz_real_server_errors():
# Missing AZURE_STORAGE_ACCOUNT
gdal.ErrorReset()
with gdaltest.error_handler():
f = open_for_read('/vsiaz/foo/bar')
assert f is None and gdal.VSIGetLastErrorMsg().find('AZURE_STORAGE_ACCOUNT') >= 0
f = open_for_read("/vsiaz/foo/bar")
assert f is None and gdal.VSIGetLastErrorMsg().find("AZURE_STORAGE_ACCOUNT") >= 0
gdal.ErrorReset()
with gdaltest.error_handler():
f = open_for_read('/vsiaz_streaming/foo/bar')
assert f is None and gdal.VSIGetLastErrorMsg().find('AZURE_STORAGE_ACCOUNT') >= 0
f = open_for_read("/vsiaz_streaming/foo/bar")
assert f is None and gdal.VSIGetLastErrorMsg().find("AZURE_STORAGE_ACCOUNT") >= 0
# Invalid AZURE_STORAGE_CONNECTION_STRING
with gdaltest.config_option('AZURE_STORAGE_CONNECTION_STRING', 'invalid'):
with gdaltest.config_option("AZURE_STORAGE_CONNECTION_STRING", "invalid"):
gdal.ErrorReset()
with gdaltest.error_handler():
f = open_for_read('/vsiaz/foo/bar')
f = open_for_read("/vsiaz/foo/bar")
assert f is None
# Missing AZURE_STORAGE_ACCESS_KEY
gdal.ErrorReset()
with gdaltest.config_options({'AZURE_STORAGE_ACCOUNT': 'AZURE_STORAGE_ACCOUNT',
'CPL_AZURE_VM_API_ROOT_URL': 'disabled'}):
with gdaltest.config_options(
{
"AZURE_STORAGE_ACCOUNT": "AZURE_STORAGE_ACCOUNT",
"CPL_AZURE_VM_API_ROOT_URL": "disabled",
}
):
with gdaltest.error_handler():
f = open_for_read('/vsiaz/foo/bar')
assert f is None and gdal.VSIGetLastErrorMsg().find('AZURE_STORAGE_ACCESS_KEY') >= 0
f = open_for_read("/vsiaz/foo/bar")
assert (
f is None
and gdal.VSIGetLastErrorMsg().find("AZURE_STORAGE_ACCESS_KEY") >= 0
)
# AZURE_STORAGE_ACCOUNT and AZURE_STORAGE_ACCESS_KEY but invalid
gdal.ErrorReset()
with gdaltest.config_options({'AZURE_STORAGE_ACCOUNT': 'AZURE_STORAGE_ACCOUNT',
'AZURE_STORAGE_ACCESS_KEY': 'AZURE_STORAGE_ACCESS_KEY'}):
with gdaltest.config_options(
{
"AZURE_STORAGE_ACCOUNT": "AZURE_STORAGE_ACCOUNT",
"AZURE_STORAGE_ACCESS_KEY": "AZURE_STORAGE_ACCESS_KEY",
}
):
with gdaltest.error_handler():
f = open_for_read('/vsiaz/foo/bar.baz')
f = open_for_read("/vsiaz/foo/bar.baz")
if f is not None:
if f is not None:
gdal.VSIFCloseL(f)
if gdal.GetConfigOption('APPVEYOR') is not None:
if gdal.GetConfigOption("APPVEYOR") is not None:
return
pytest.fail(gdal.VSIGetLastErrorMsg())
gdal.ErrorReset()
with gdaltest.error_handler():
f = open_for_read('/vsiaz_streaming/foo/bar.baz')
f = open_for_read("/vsiaz_streaming/foo/bar.baz")
assert f is None, gdal.VSIGetLastErrorMsg()
@ -127,6 +144,7 @@ def test_vsiaz_real_server_errors():
# FIXME: this resource is no longer accessible through no sign request...
# Find another one.
def test_vsiaz_no_sign_request():
if not gdaltest.built_against_curl():
@ -134,42 +152,64 @@ def test_vsiaz_no_sign_request():
gdal.VSICurlClearCache()
with gdaltest.config_options({ 'AZURE_STORAGE_ACCOUNT': 'naipblobs', 'AZURE_NO_SIGN_REQUEST': 'YES'}):
actual_url = gdal.GetActualURL('/vsiaz/naip/v002/al/2015/al_100cm_2015/30086/m_3008601_ne_16_1_20150804.tif')
assert actual_url == 'https://naipblobs.blob.core.windows.net/naip/v002/al/2015/al_100cm_2015/30086/m_3008601_ne_16_1_20150804.tif'
assert actual_url == gdal.GetSignedURL('/vsiaz/naip/v002/al/2015/al_100cm_2015/30086/m_3008601_ne_16_1_20150804.tif')
with gdaltest.config_options(
{"AZURE_STORAGE_ACCOUNT": "naipblobs", "AZURE_NO_SIGN_REQUEST": "YES"}
):
actual_url = gdal.GetActualURL(
"/vsiaz/naip/v002/al/2015/al_100cm_2015/30086/m_3008601_ne_16_1_20150804.tif"
)
assert (
actual_url
== "https://naipblobs.blob.core.windows.net/naip/v002/al/2015/al_100cm_2015/30086/m_3008601_ne_16_1_20150804.tif"
)
assert actual_url == gdal.GetSignedURL(
"/vsiaz/naip/v002/al/2015/al_100cm_2015/30086/m_3008601_ne_16_1_20150804.tif"
)
f = open_for_read('/vsiaz/naip/v002/al/2015/al_100cm_2015/30086/m_3008601_ne_16_1_20150804.tif')
f = open_for_read(
"/vsiaz/naip/v002/al/2015/al_100cm_2015/30086/m_3008601_ne_16_1_20150804.tif"
)
if f is None:
if gdaltest.gdalurlopen('https://naipblobs.blob.core.windows.net/naip/v002/al/2015/al_100cm_2015/30086/m_3008601_ne_16_1_20150804.tif') is None:
pytest.skip('cannot open URL')
if (
gdaltest.gdalurlopen(
"https://naipblobs.blob.core.windows.net/naip/v002/al/2015/al_100cm_2015/30086/m_3008601_ne_16_1_20150804.tif"
)
is None
):
pytest.skip("cannot open URL")
pytest.fail()
gdal.VSIFCloseL(f)
assert 'm_3008601_ne_16_1_20150804.tif' in gdal.ReadDir('/vsiaz/naip/v002/al/2015/al_100cm_2015/30086/')
assert "m_3008601_ne_16_1_20150804.tif" in gdal.ReadDir(
"/vsiaz/naip/v002/al/2015/al_100cm_2015/30086/"
)
gdal.VSICurlClearCache()
assert stat.S_ISDIR(gdal.VSIStatL('/vsiaz/naip').mode)
assert stat.S_ISDIR(gdal.VSIStatL("/vsiaz/naip").mode)
gdal.VSICurlClearCache()
assert stat.S_ISDIR(gdal.VSIStatL('/vsiaz/naip/').mode)
assert stat.S_ISDIR(gdal.VSIStatL("/vsiaz/naip/").mode)
gdal.VSICurlClearCache()
assert gdal.VSIStatL('/vsiaz/naip_i_dont_exist') is None
assert gdal.VSIStatL("/vsiaz/naip_i_dont_exist") is None
gdal.VSICurlClearCache()
assert stat.S_ISDIR(gdal.VSIStatL('/vsiaz/naip/v002').mode)
assert stat.S_ISDIR(gdal.VSIStatL("/vsiaz/naip/v002").mode)
###############################################################################
# Test AZURE_SAS option
@pytest.mark.skipif(sys.platform == 'darwin' and 'CI' in os.environ, reason='Randomly fails on MacOSX. Not sure why.')
@pytest.mark.skipif(
sys.platform == "darwin" and "CI" in os.environ,
reason="Randomly fails on MacOSX. Not sure why.",
)
def test_vsiaz_sas():
if not gdaltest.built_against_curl():
@ -178,33 +218,57 @@ def test_vsiaz_sas():
gdal.VSICurlClearCache()
# See https://azure.microsoft.com/en-us/services/open-datasets/catalog/naip/ for the value of AZURE_SAS
with gdaltest.config_options({ 'AZURE_STORAGE_ACCOUNT': 'naipblobs', 'AZURE_SAS': 'st=2019-07-18T03%3A53%3A22Z&se=2035-07-19T03%3A53%3A00Z&sp=rl&sv=2018-03-28&sr=c&sig=2RIXmLbLbiagYnUd49rgx2kOXKyILrJOgafmkODhRAQ%3D'}):
actual_url = gdal.GetActualURL('/vsiaz/naip/v002/al/2015/al_100cm_2015/30086/m_3008601_ne_16_1_20150804.tif')
assert actual_url == 'https://naipblobs.blob.core.windows.net/naip/v002/al/2015/al_100cm_2015/30086/m_3008601_ne_16_1_20150804.tif'
assert gdal.GetSignedURL('/vsiaz/naip/v002/al/2015/al_100cm_2015/30086/m_3008601_ne_16_1_20150804.tif') == 'https://naipblobs.blob.core.windows.net/naip/v002/al/2015/al_100cm_2015/30086/m_3008601_ne_16_1_20150804.tif?st=2019-07-18T03%3A53%3A22Z&se=2035-07-19T03%3A53%3A00Z&sp=rl&sv=2018-03-28&sr=c&sig=2RIXmLbLbiagYnUd49rgx2kOXKyILrJOgafmkODhRAQ%3D'
with gdaltest.config_options(
{
"AZURE_STORAGE_ACCOUNT": "naipblobs",
"AZURE_SAS": "st=2019-07-18T03%3A53%3A22Z&se=2035-07-19T03%3A53%3A00Z&sp=rl&sv=2018-03-28&sr=c&sig=2RIXmLbLbiagYnUd49rgx2kOXKyILrJOgafmkODhRAQ%3D",
}
):
actual_url = gdal.GetActualURL(
"/vsiaz/naip/v002/al/2015/al_100cm_2015/30086/m_3008601_ne_16_1_20150804.tif"
)
assert (
actual_url
== "https://naipblobs.blob.core.windows.net/naip/v002/al/2015/al_100cm_2015/30086/m_3008601_ne_16_1_20150804.tif"
)
assert (
gdal.GetSignedURL(
"/vsiaz/naip/v002/al/2015/al_100cm_2015/30086/m_3008601_ne_16_1_20150804.tif"
)
== "https://naipblobs.blob.core.windows.net/naip/v002/al/2015/al_100cm_2015/30086/m_3008601_ne_16_1_20150804.tif?st=2019-07-18T03%3A53%3A22Z&se=2035-07-19T03%3A53%3A00Z&sp=rl&sv=2018-03-28&sr=c&sig=2RIXmLbLbiagYnUd49rgx2kOXKyILrJOgafmkODhRAQ%3D"
)
f = open_for_read('/vsiaz/naip/v002/al/2015/al_100cm_2015/30086/m_3008601_ne_16_1_20150804.tif')
f = open_for_read(
"/vsiaz/naip/v002/al/2015/al_100cm_2015/30086/m_3008601_ne_16_1_20150804.tif"
)
if f is None:
if gdaltest.gdalurlopen('https://naipblobs.blob.core.windows.net/naip/v002/al/2015/al_100cm_2015/30086/m_3008601_ne_16_1_20150804.tif') is None:
pytest.skip('cannot open URL')
if (
gdaltest.gdalurlopen(
"https://naipblobs.blob.core.windows.net/naip/v002/al/2015/al_100cm_2015/30086/m_3008601_ne_16_1_20150804.tif"
)
is None
):
pytest.skip("cannot open URL")
pytest.fail()
gdal.VSIFCloseL(f)
assert 'm_3008601_ne_16_1_20150804.tif' in gdal.ReadDir('/vsiaz/naip/v002/al/2015/al_100cm_2015/30086/')
assert "m_3008601_ne_16_1_20150804.tif" in gdal.ReadDir(
"/vsiaz/naip/v002/al/2015/al_100cm_2015/30086/"
)
gdal.VSICurlClearCache()
assert stat.S_ISDIR(gdal.VSIStatL('/vsiaz/naip').mode)
assert stat.S_ISDIR(gdal.VSIStatL("/vsiaz/naip").mode)
gdal.VSICurlClearCache()
assert stat.S_ISDIR(gdal.VSIStatL('/vsiaz/naip/').mode)
assert stat.S_ISDIR(gdal.VSIStatL("/vsiaz/naip/").mode)
gdal.VSICurlClearCache()
assert gdal.VSIStatL('/vsiaz/naip_i_dont_exist') is None
assert gdal.VSIStatL("/vsiaz/naip_i_dont_exist") is None
gdal.VSICurlClearCache()
assert stat.S_ISDIR(gdal.VSIStatL('/vsiaz/naip/v002').mode)
assert stat.S_ISDIR(gdal.VSIStatL("/vsiaz/naip/v002").mode)

View File

@ -29,20 +29,23 @@
###############################################################################
import stat
from osgeo import gdal
import gdaltest
import pytest
pytestmark = pytest.mark.skipif(not gdaltest.built_against_curl(), reason="GDAL not built against curl")
from osgeo import gdal
pytestmark = pytest.mark.skipif(
not gdaltest.built_against_curl(), reason="GDAL not built against curl"
)
def open_for_read(uri):
"""
Opens a test file for reading.
"""
return gdal.VSIFOpenExL(uri, 'rb', 1)
return gdal.VSIFOpenExL(uri, "rb", 1)
###############################################################################
# Nominal cases (require valid credentials)
@ -53,105 +56,118 @@ def test_vsiaz_extra_1():
if not gdaltest.built_against_curl():
pytest.skip()
az_resource = gdal.GetConfigOption('AZ_RESOURCE')
az_resource = gdal.GetConfigOption("AZ_RESOURCE")
if az_resource is None:
pytest.skip('Missing AZ_RESOURCE')
pytest.skip("Missing AZ_RESOURCE")
if '/' not in az_resource:
path = '/vsiaz/' + az_resource
if "/" not in az_resource:
path = "/vsiaz/" + az_resource
statres = gdal.VSIStatL(path)
assert statres is not None and stat.S_ISDIR(statres.mode), \
('%s is not a valid bucket' % path)
assert statres is not None and stat.S_ISDIR(statres.mode), (
"%s is not a valid bucket" % path
)
readdir = gdal.ReadDir(path)
assert readdir is not None, 'ReadDir() should not return empty list'
assert readdir is not None, "ReadDir() should not return empty list"
for filename in readdir:
if filename != '.':
subpath = path + '/' + filename
assert gdal.VSIStatL(subpath) is not None, \
('Stat(%s) should not return an error' % subpath)
if filename != ".":
subpath = path + "/" + filename
assert gdal.VSIStatL(subpath) is not None, (
"Stat(%s) should not return an error" % subpath
)
unique_id = 'vsiaz_test'
subpath = path + '/' + unique_id
unique_id = "vsiaz_test"
subpath = path + "/" + unique_id
ret = gdal.Mkdir(subpath, 0)
assert ret >= 0, ('Mkdir(%s) should not return an error' % subpath)
assert ret >= 0, "Mkdir(%s) should not return an error" % subpath
readdir = gdal.ReadDir(path)
assert unique_id in readdir, \
('ReadDir(%s) should contain %s' % (path, unique_id))
assert unique_id in readdir, "ReadDir(%s) should contain %s" % (path, unique_id)
ret = gdal.Mkdir(subpath, 0)
assert ret != 0, ('Mkdir(%s) repeated should return an error' % subpath)
assert ret != 0, "Mkdir(%s) repeated should return an error" % subpath
ret = gdal.Rmdir(subpath)
assert ret >= 0, ('Rmdir(%s) should not return an error' % subpath)
assert ret >= 0, "Rmdir(%s) should not return an error" % subpath
readdir = gdal.ReadDir(path)
assert unique_id not in readdir, \
('ReadDir(%s) should not contain %s' % (path, unique_id))
assert unique_id not in readdir, "ReadDir(%s) should not contain %s" % (
path,
unique_id,
)
ret = gdal.Mkdir(subpath, 0)
assert ret >= 0, ('Mkdir(%s) should not return an error' % subpath)
assert ret >= 0, "Mkdir(%s) should not return an error" % subpath
f = gdal.VSIFOpenL(subpath + '/test.txt', 'wb')
f = gdal.VSIFOpenL(subpath + "/test.txt", "wb")
assert f is not None
gdal.VSIFWriteL('hello', 1, 5, f)
gdal.VSIFWriteL("hello", 1, 5, f)
gdal.VSIFCloseL(f)
ret = gdal.Rmdir(subpath)
assert ret != 0, \
('Rmdir(%s) on non empty directory should return an error' % subpath)
assert ret != 0, (
"Rmdir(%s) on non empty directory should return an error" % subpath
)
f = gdal.VSIFOpenL(subpath + '/test.txt', 'rb')
f = gdal.VSIFOpenL(subpath + "/test.txt", "rb")
assert f is not None
data = gdal.VSIFReadL(1, 5, f).decode('utf-8')
assert data == 'hello'
data = gdal.VSIFReadL(1, 5, f).decode("utf-8")
assert data == "hello"
gdal.VSIFCloseL(f)
md = gdal.GetFileMetadata(subpath + '/test.txt', 'HEADERS')
assert 'x-ms-blob-type' in md
md = gdal.GetFileMetadata(subpath + "/test.txt", "HEADERS")
assert "x-ms-blob-type" in md
metadata_md = gdal.GetFileMetadata(subpath + '/test.txt', 'METADATA')
assert 'ETag' in metadata_md or 'etag' in metadata_md
metadata_md = gdal.GetFileMetadata(subpath + "/test.txt", "METADATA")
assert "ETag" in metadata_md or "etag" in metadata_md
assert metadata_md != md
md = gdal.GetFileMetadata(subpath + '/test.txt', 'TAGS')
md = gdal.GetFileMetadata(subpath + "/test.txt", "TAGS")
assert md == {}
# Change properties
assert gdal.SetFileMetadata(subpath + '/test.txt', {'x-ms-blob-content-type' : 'foo'}, 'PROPERTIES')
md = gdal.GetFileMetadata(subpath + '/test.txt', 'HEADERS')
assert md.get('Content-Type', '') == 'foo' or md.get('content-type', '') == 'foo', md
assert gdal.SetFileMetadata(
subpath + "/test.txt", {"x-ms-blob-content-type": "foo"}, "PROPERTIES"
)
md = gdal.GetFileMetadata(subpath + "/test.txt", "HEADERS")
assert (
md.get("Content-Type", "") == "foo" or md.get("content-type", "") == "foo"
), md
# Change metadata
assert gdal.SetFileMetadata(subpath + '/test.txt', {'x-ms-meta-FOO' : 'BAR'}, 'METADATA')
md = gdal.GetFileMetadata(subpath + '/test.txt', 'METADATA')
assert md['x-ms-meta-FOO'] == 'BAR'
assert gdal.SetFileMetadata(
subpath + "/test.txt", {"x-ms-meta-FOO": "BAR"}, "METADATA"
)
md = gdal.GetFileMetadata(subpath + "/test.txt", "METADATA")
assert md["x-ms-meta-FOO"] == "BAR"
# Change tags (doesn't seem to work with Azurite)
if ':10000/devstoreaccount1' not in gdal.GetConfigOption('AZURE_STORAGE_CONNECTION_STRING', ''):
assert gdal.SetFileMetadata(subpath + '/test.txt', {'BAR' : 'BAZ'}, 'TAGS')
md = gdal.GetFileMetadata(subpath + '/test.txt', 'TAGS')
assert md['BAR'] == 'BAZ'
if ":10000/devstoreaccount1" not in gdal.GetConfigOption(
"AZURE_STORAGE_CONNECTION_STRING", ""
):
assert gdal.SetFileMetadata(subpath + "/test.txt", {"BAR": "BAZ"}, "TAGS")
md = gdal.GetFileMetadata(subpath + "/test.txt", "TAGS")
assert md["BAR"] == "BAZ"
assert gdal.Rename(subpath + '/test.txt', subpath + '/test2.txt') == 0
assert gdal.Rename(subpath + "/test.txt", subpath + "/test2.txt") == 0
f = gdal.VSIFOpenL(subpath + '/test2.txt', 'rb')
f = gdal.VSIFOpenL(subpath + "/test2.txt", "rb")
assert f is not None
data = gdal.VSIFReadL(1, 5, f).decode('utf-8')
assert data == 'hello'
data = gdal.VSIFReadL(1, 5, f).decode("utf-8")
assert data == "hello"
gdal.VSIFCloseL(f)
ret = gdal.Unlink(subpath + '/test2.txt')
assert ret >= 0, \
('Unlink(%s) should not return an error' % (subpath + '/test2.txt'))
ret = gdal.Unlink(subpath + "/test2.txt")
assert ret >= 0, "Unlink(%s) should not return an error" % (
subpath + "/test2.txt"
)
ret = gdal.Rmdir(subpath)
assert ret >= 0, ('Rmdir(%s) should not return an error' % subpath)
assert ret >= 0, "Rmdir(%s) should not return an error" % subpath
return
f = open_for_read('/vsiaz/' + az_resource)
f = open_for_read("/vsiaz/" + az_resource)
assert f is not None
ret = gdal.VSIFReadL(1, 1, f)
gdal.VSIFCloseL(f)
@ -159,7 +175,7 @@ def test_vsiaz_extra_1():
assert len(ret) == 1
# Same with /vsiaz_streaming/
f = open_for_read('/vsiaz_streaming/' + az_resource)
f = open_for_read("/vsiaz_streaming/" + az_resource)
assert f is not None
ret = gdal.VSIFReadL(1, 1, f)
gdal.VSIFCloseL(f)
@ -170,20 +186,20 @@ def test_vsiaz_extra_1():
# we actually try to read at read() time and bSetError = false
# Invalid bucket : "The specified bucket does not exist"
gdal.ErrorReset()
f = open_for_read('/vsiaz/not_existing_bucket/foo')
f = open_for_read("/vsiaz/not_existing_bucket/foo")
with gdaltest.error_handler():
gdal.VSIFReadL(1, 1, f)
gdal.VSIFCloseL(f)
assert gdal.VSIGetLastErrorMsg() != ''
assert gdal.VSIGetLastErrorMsg() != ""
# Invalid resource
gdal.ErrorReset()
f = open_for_read('/vsiaz_streaming/' + az_resource + '/invalid_resource.baz')
f = open_for_read("/vsiaz_streaming/" + az_resource + "/invalid_resource.baz")
assert f is None, gdal.VSIGetLastErrorMsg()
# Test GetSignedURL()
signed_url = gdal.GetSignedURL('/vsiaz/' + az_resource)
f = open_for_read('/vsicurl_streaming/' + signed_url)
signed_url = gdal.GetSignedURL("/vsiaz/" + az_resource)
f = open_for_read("/vsicurl_streaming/" + signed_url)
assert f is not None
ret = gdal.VSIFReadL(1, 1, f)
gdal.VSIFCloseL(f)

View File

@ -28,43 +28,44 @@
# DEALINGS IN THE SOFTWARE.
###############################################################################
import pytest
from osgeo import gdal
import pytest
def test_vsicredential():
with pytest.raises(Exception):
assert gdal.GetCredential(None, 'key')
assert gdal.GetCredential(None, "key")
with pytest.raises(Exception):
assert gdal.GetCredential('prefix', None)
assert gdal.GetCredential("prefix", None)
assert gdal.GetCredential('prefix', 'key') is None
assert gdal.GetCredential("prefix", "key") is None
assert gdal.GetCredential('prefix', 'key', 'default') == 'default'
assert gdal.GetCredential("prefix", "key", "default") == "default"
with pytest.raises(Exception):
gdal.SetCredential(None, 'key', 'value')
gdal.SetCredential(None, "key", "value")
with pytest.raises(Exception):
gdal.SetCredential('prefix', None, 'value')
gdal.SetCredential("prefix", None, "value")
gdal.SetCredential('prefix', 'key', 'value')
assert gdal.GetCredential('prefix', 'key') == 'value'
assert gdal.GetCredential('prefix/object', 'key') == 'value'
assert gdal.GetCredential('prefix', 'key', 'default') == 'value'
assert gdal.GetCredential('another_prefix', 'key') is None
gdal.SetCredential("prefix", "key", "value")
assert gdal.GetCredential("prefix", "key") == "value"
assert gdal.GetCredential("prefix/object", "key") == "value"
assert gdal.GetCredential("prefix", "key", "default") == "value"
assert gdal.GetCredential("another_prefix", "key") is None
gdal.SetCredential('prefix', 'key', None)
assert gdal.GetCredential('prefix', 'key') is None
gdal.SetCredential("prefix", "key", None)
assert gdal.GetCredential("prefix", "key") is None
gdal.SetCredential('prefix', 'key', 'value')
gdal.ClearCredentials('prefix')
assert gdal.GetCredential('prefix', 'key') is None
gdal.SetCredential("prefix", "key", "value")
gdal.ClearCredentials("prefix")
assert gdal.GetCredential("prefix", "key") is None
gdal.SetCredential('prefix', 'key', 'value')
gdal.ClearCredentials('another_prefix')
assert gdal.GetCredential('prefix', 'key') == 'value'
gdal.SetCredential("prefix", "key", "value")
gdal.ClearCredentials("another_prefix")
assert gdal.GetCredential("prefix", "key") == "value"
gdal.ClearCredentials()
assert gdal.GetCredential('prefix', 'key') is None
assert gdal.GetCredential("prefix", "key") is None

View File

@ -33,13 +33,14 @@ import ctypes
import os
import struct
from osgeo import gdal
import gdaltest
import pytest
from gcore.testnonboundtoswig import setup as testnonboundtoswig_setup # noqa
testnonboundtoswig_setup; # to please pyflakes
from osgeo import gdal
testnonboundtoswig_setup
# to please pyflakes
###############################################################################
# Use common test for /vsicrypt
@ -48,15 +49,19 @@ testnonboundtoswig_setup; # to please pyflakes
def test_vsicrypt_1():
gdaltest.has_vsicrypt = False
fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin', 'wb+')
fp = gdal.VSIFOpenL("/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin", "wb+")
if fp is None:
pytest.skip()
gdal.VSIFCloseL(fp)
gdal.Unlink('/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin')
gdal.Unlink("/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin")
gdaltest.has_vsicrypt = True
import vsifile
return vsifile.vsifile_generic('/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin')
return vsifile.vsifile_generic(
"/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin"
)
###############################################################################
# Test various error cases
@ -69,49 +74,59 @@ def test_vsicrypt_2():
# Missing key
with gdaltest.error_handler():
fp = gdal.VSIFOpenL('/vsicrypt//vsimem/file.bin', 'wb+')
fp = gdal.VSIFOpenL("/vsicrypt//vsimem/file.bin", "wb+")
assert fp is None
# Invalid file
with gdaltest.error_handler():
fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,file=/not_existing/not_existing', 'wb')
fp = gdal.VSIFOpenL(
"/vsicrypt/key=DONT_USE_IN_PROD,file=/not_existing/not_existing", "wb"
)
assert fp is None
# Invalid file
with gdaltest.error_handler():
fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,file=/not_existing/not_existing', 'rb')
fp = gdal.VSIFOpenL(
"/vsicrypt/key=DONT_USE_IN_PROD,file=/not_existing/not_existing", "rb"
)
assert fp is None
# Invalid file
with gdaltest.error_handler():
fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,file=/not_existing/not_existing', 'ab')
fp = gdal.VSIFOpenL(
"/vsicrypt/key=DONT_USE_IN_PROD,file=/not_existing/not_existing", "ab"
)
assert fp is None
# Invalid access
with gdaltest.error_handler():
fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,file=/not_existing/not_existing', 'foo')
fp = gdal.VSIFOpenL(
"/vsicrypt/key=DONT_USE_IN_PROD,file=/not_existing/not_existing", "foo"
)
assert fp is None
# Key to short
with gdaltest.error_handler():
fp = gdal.VSIFOpenL('/vsicrypt/key=a,file=/vsimem/file.bin', 'wb+')
fp = gdal.VSIFOpenL("/vsicrypt/key=a,file=/vsimem/file.bin", "wb+")
assert fp is None
# Invalid signature
gdal.FileFromMemBuffer('/vsimem/file.bin', 'foo')
gdal.FileFromMemBuffer("/vsimem/file.bin", "foo")
with gdaltest.error_handler():
fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin', 'rb')
fp = gdal.VSIFOpenL(
"/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin", "rb"
)
assert fp is None
# Generate empty file
fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin', 'wb')
fp = gdal.VSIFOpenL("/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin", "wb")
gdal.VSIFCloseL(fp)
fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin', 'rb')
fp = gdal.VSIFOpenL("/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin", "rb")
assert fp is not None
gdal.VSIFCloseL(fp)
fp = gdal.VSIFOpenL('/vsimem/file.bin', 'rb')
fp = gdal.VSIFOpenL("/vsimem/file.bin", "rb")
header = gdal.VSIFReadL(1, 1000, fp)
gdal.VSIFCloseL(fp)
@ -119,233 +134,353 @@ def test_vsicrypt_2():
# Test shortening header
for i in range(46):
fp = gdal.VSIFOpenL('/vsimem/file.bin', 'wb')
fp = gdal.VSIFOpenL("/vsimem/file.bin", "wb")
gdal.VSIFWriteL(header, 1, 46 - 1 - i, fp)
gdal.VSIFCloseL(fp)
with gdaltest.error_handler():
fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin', 'rb')
fp = gdal.VSIFOpenL(
"/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin", "rb"
)
assert fp is None
# Test corrupting all bytes of header
for i in range(46):
for val in (0, 127, 255):
fp = gdal.VSIFOpenL('/vsimem/file.bin', 'wb')
fp = gdal.VSIFOpenL("/vsimem/file.bin", "wb")
try:
new_byte = chr(val).encode('latin1')
new_byte = chr(val).encode("latin1")
except (UnicodeDecodeError, UnicodeEncodeError):
new_byte = chr(val)
header_new = header[0:i] + new_byte + header[i + 1:]
header_new = header[0:i] + new_byte + header[i + 1 :]
gdal.VSIFWriteL(header_new, 1, 46, fp)
gdal.VSIFCloseL(fp)
with gdaltest.error_handler():
fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,file='
'/vsimem/file.bin', 'rb')
fp = gdal.VSIFOpenL(
"/vsicrypt/key=DONT_USE_IN_PROD,file=" "/vsimem/file.bin", "rb"
)
if fp is not None:
gdal.VSIFCloseL(fp)
gdal.SetConfigOption('VSICRYPT_IV', 'TOO_SHORT')
gdal.SetConfigOption("VSICRYPT_IV", "TOO_SHORT")
with gdaltest.error_handler():
fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,file='
'/vsimem/file.bin', 'wb')
gdal.SetConfigOption('VSICRYPT_IV', None)
fp = gdal.VSIFOpenL(
"/vsicrypt/key=DONT_USE_IN_PROD,file=" "/vsimem/file.bin", "wb"
)
gdal.SetConfigOption("VSICRYPT_IV", None)
if fp is not None:
gdal.VSIFCloseL(fp)
# Inconsistent initial vector.
header = struct.pack('B' * 38,
86, 83, 73, 67, 82, 89, 80, 84, # signature
38, 0, # header size
1, # major
0, # minor
0, 2, # sector size
0, # alg
0, # mode
8, # size of IV (should be 16)
32, 13, 169, 71, 154, 208, 22, 32, # IV
0, 0, # size of free text
0, # size of key check
0, 0, 0, 0, 0, 0, 0, 0, # size of unencrypted file
0, 0 # size of extra content
)
fp = gdal.VSIFOpenL('/vsimem/file.bin', 'wb')
header = struct.pack(
"B" * 38,
86,
83,
73,
67,
82,
89,
80,
84, # signature
38,
0, # header size
1, # major
0, # minor
0,
2, # sector size
0, # alg
0, # mode
8, # size of IV (should be 16)
32,
13,
169,
71,
154,
208,
22,
32, # IV
0,
0, # size of free text
0, # size of key check
0,
0,
0,
0,
0,
0,
0,
0, # size of unencrypted file
0,
0, # size of extra content
)
fp = gdal.VSIFOpenL("/vsimem/file.bin", "wb")
gdal.VSIFWriteL(header, 1, len(header), fp)
gdal.VSIFCloseL(fp)
with gdaltest.error_handler():
fp = gdal.VSIFOpenL(
'/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin', 'rb')
"/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin", "rb"
)
assert fp is None
# Inconsistent initial vector with key check.
header = struct.pack('B' * 39,
86, 83, 73, 67, 82, 89, 80, 84, # signature
39, 0, # header size
1, # major
0, # minor
0, 2, # sector size
0, # alg
0, # mode
8, # size of IV (should be 16)
32, 13, 169, 71, 154, 208, 22, 32, # IV
0, 0, # size of free text
1, # size of key check
0, # key check
0, 0, 0, 0, 0, 0, 0, 0, # size of unencrypted file
0, 0 # size of extra content
)
fp = gdal.VSIFOpenL('/vsimem/file.bin', 'wb')
header = struct.pack(
"B" * 39,
86,
83,
73,
67,
82,
89,
80,
84, # signature
39,
0, # header size
1, # major
0, # minor
0,
2, # sector size
0, # alg
0, # mode
8, # size of IV (should be 16)
32,
13,
169,
71,
154,
208,
22,
32, # IV
0,
0, # size of free text
1, # size of key check
0, # key check
0,
0,
0,
0,
0,
0,
0,
0, # size of unencrypted file
0,
0, # size of extra content
)
fp = gdal.VSIFOpenL("/vsimem/file.bin", "wb")
gdal.VSIFWriteL(header, 1, len(header), fp)
gdal.VSIFCloseL(fp)
with gdaltest.error_handler():
fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin', 'rb')
fp = gdal.VSIFOpenL(
"/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin", "rb"
)
assert fp is None
# Test reading with wrong key
fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin', 'wb')
gdal.VSIFWriteL('hello', 1, 5, fp)
fp = gdal.VSIFOpenL("/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin", "wb")
gdal.VSIFWriteL("hello", 1, 5, fp)
gdal.VSIFCloseL(fp)
fp = gdal.VSIFOpenL('/vsicrypt/key=dont_use_in_prod,file=/vsimem/file.bin', 'rb')
content = gdal.VSIFReadL(1, 5, fp).decode('latin1')
fp = gdal.VSIFOpenL("/vsicrypt/key=dont_use_in_prod,file=/vsimem/file.bin", "rb")
content = gdal.VSIFReadL(1, 5, fp).decode("latin1")
gdal.VSIFCloseL(fp)
assert content != 'hello'
assert content != "hello"
with gdaltest.error_handler():
fp = gdal.VSIFOpenL('/vsicrypt/key=short_key,file=/vsimem/file.bin', 'ab')
fp = gdal.VSIFOpenL("/vsicrypt/key=short_key,file=/vsimem/file.bin", "ab")
assert fp is None
# Test reading with wrong key with add_key_check
fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,add_key_check=yes,file=/vsimem/file.bin', 'wb')
gdal.VSIFWriteL('hello', 1, 5, fp)
fp = gdal.VSIFOpenL(
"/vsicrypt/key=DONT_USE_IN_PROD,add_key_check=yes,file=/vsimem/file.bin", "wb"
)
gdal.VSIFWriteL("hello", 1, 5, fp)
gdal.VSIFCloseL(fp)
with gdaltest.error_handler():
fp = gdal.VSIFOpenL('/vsicrypt/key=dont_use_in_prod,file=/vsimem/file.bin', 'rb')
fp = gdal.VSIFOpenL(
"/vsicrypt/key=dont_use_in_prod,file=/vsimem/file.bin", "rb"
)
assert fp is None
with gdaltest.error_handler():
fp = gdal.VSIFOpenL('/vsicrypt/key=short_key,file=/vsimem/file.bin', 'ab')
fp = gdal.VSIFOpenL("/vsicrypt/key=short_key,file=/vsimem/file.bin", "ab")
assert fp is None
with gdaltest.error_handler():
fp = gdal.VSIFOpenL('/vsicrypt/key=dont_use_in_prod,file=/vsimem/file.bin', 'ab')
fp = gdal.VSIFOpenL(
"/vsicrypt/key=dont_use_in_prod,file=/vsimem/file.bin", "ab"
)
assert fp is None
# Test creating with potentially not built-in alg:
with gdaltest.error_handler():
fp = gdal.VSIFOpenL('/vsicrypt/alg=blowfish,key=DONT_USE_IN_PROD,file=/vsimem/file.bin', 'wb')
fp = gdal.VSIFOpenL(
"/vsicrypt/alg=blowfish,key=DONT_USE_IN_PROD,file=/vsimem/file.bin", "wb"
)
if fp is not None:
gdal.VSIFCloseL(fp)
# Invalid sector_size
with gdaltest.error_handler():
fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,sector_size=1,file=/vsimem/file.bin', 'wb')
fp = gdal.VSIFOpenL(
"/vsicrypt/key=DONT_USE_IN_PROD,sector_size=1,file=/vsimem/file.bin", "wb"
)
assert fp is None
# Sector size (16) should be at least twice larger than the block size (16) in CBC_CTS
with gdaltest.error_handler():
fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,sector_size=16,mode=CBC_CTS,file=/vsimem/file.bin', 'wb')
fp = gdal.VSIFOpenL(
"/vsicrypt/key=DONT_USE_IN_PROD,sector_size=16,mode=CBC_CTS,file=/vsimem/file.bin",
"wb",
)
assert fp is None
gdal.Unlink('/vsimem/file.bin')
gdal.Unlink("/vsimem/file.bin")
###############################################################################
# Test various options
@pytest.mark.skipif(
os.environ.get('BUILD_NAME', '') == 's390x',
reason='Fails randomly on that platform'
os.environ.get("BUILD_NAME", "") == "s390x",
reason="Fails randomly on that platform",
)
def test_vsicrypt_3():
if not gdaltest.has_vsicrypt:
pytest.skip()
for options in ['sector_size=16', 'alg=AES', 'alg=DES_EDE2', 'alg=DES_EDE3', 'alg=SKIPJACK', 'alg=invalid',
'mode=CBC', 'mode=CFB', 'mode=OFB', 'mode=CTR', 'mode=CBC_CTS', 'mode=invalid',
'freetext=my_free_text',
'add_key_check=yes']:
for options in [
"sector_size=16",
"alg=AES",
"alg=DES_EDE2",
"alg=DES_EDE3",
"alg=SKIPJACK",
"alg=invalid",
"mode=CBC",
"mode=CFB",
"mode=OFB",
"mode=CTR",
"mode=CBC_CTS",
"mode=invalid",
"freetext=my_free_text",
"add_key_check=yes",
]:
gdal.Unlink('/vsimem/file.bin')
gdal.Unlink("/vsimem/file.bin")
if options == 'alg=invalid' or options == 'mode=invalid':
if options == "alg=invalid" or options == "mode=invalid":
with gdaltest.error_handler():
fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PRODDONT_USE_IN_PROD,%s,file=/vsimem/file.bin' % options, 'wb')
fp = gdal.VSIFOpenL(
"/vsicrypt/key=DONT_USE_IN_PRODDONT_USE_IN_PROD,%s,file=/vsimem/file.bin"
% options,
"wb",
)
else:
fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PRODDONT_USE_IN_PROD,%s,file=/vsimem/file.bin' % options, 'wb')
fp = gdal.VSIFOpenL(
"/vsicrypt/key=DONT_USE_IN_PRODDONT_USE_IN_PROD,%s,file=/vsimem/file.bin"
% options,
"wb",
)
assert fp is not None, options
gdal.VSIFWriteL('hello', 1, 5, fp)
gdal.VSIFWriteL("hello", 1, 5, fp)
gdal.VSIFCloseL(fp)
fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PRODDONT_USE_IN_PROD,file=/vsimem/file.bin', 'r')
content = gdal.VSIFReadL(1, 5, fp).decode('latin1')
fp = gdal.VSIFOpenL(
"/vsicrypt/key=DONT_USE_IN_PRODDONT_USE_IN_PROD,file=/vsimem/file.bin", "r"
)
content = gdal.VSIFReadL(1, 5, fp).decode("latin1")
gdal.VSIFCloseL(fp)
assert content == 'hello', options
assert content == "hello", options
# Some of those algs might be missing
for options in ['alg=Blowfish', 'alg=Camellia', 'alg=CAST256', 'alg=MARS', 'alg=IDEA', 'alg=RC5', 'alg=RC6', 'alg=Serpent', 'alg=SHACAL2', 'alg=Twofish', 'alg=XTEA']:
for options in [
"alg=Blowfish",
"alg=Camellia",
"alg=CAST256",
"alg=MARS",
"alg=IDEA",
"alg=RC5",
"alg=RC6",
"alg=Serpent",
"alg=SHACAL2",
"alg=Twofish",
"alg=XTEA",
]:
gdal.Unlink('/vsimem/file.bin')
gdal.Unlink("/vsimem/file.bin")
with gdaltest.error_handler():
fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,%s,file=/vsimem/file.bin' % options, 'wb')
fp = gdal.VSIFOpenL(
"/vsicrypt/key=DONT_USE_IN_PROD,%s,file=/vsimem/file.bin" % options,
"wb",
)
if fp is not None:
gdal.VSIFWriteL('hello', 1, 5, fp)
gdal.VSIFWriteL("hello", 1, 5, fp)
gdal.VSIFCloseL(fp)
fp = gdal.VSIFOpenL('/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin', 'rb')
content = gdal.VSIFReadL(1, 5, fp).decode('latin1')
fp = gdal.VSIFOpenL(
"/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file.bin", "rb"
)
content = gdal.VSIFReadL(1, 5, fp).decode("latin1")
gdal.VSIFCloseL(fp)
assert content == 'hello', options
assert content == "hello", options
# Test key generation
# Do NOT set VSICRYPT_CRYPTO_RANDOM=NO in production. This is just to speed up tests !
gdal.SetConfigOption("VSICRYPT_CRYPTO_RANDOM", "NO")
fp = gdal.VSIFOpenL('/vsicrypt/key=GENERATE_IT,add_key_check=yes,file=/vsimem/file.bin', 'wb')
fp = gdal.VSIFOpenL(
"/vsicrypt/key=GENERATE_IT,add_key_check=yes,file=/vsimem/file.bin", "wb"
)
gdal.SetConfigOption("VSICRYPT_CRYPTO_RANDOM", None)
# Get the generated random key
key_b64 = gdal.GetConfigOption('VSICRYPT_KEY_B64')
key_b64 = gdal.GetConfigOption("VSICRYPT_KEY_B64")
assert key_b64 is not None
gdal.VSIFWriteL('hello', 1, 5, fp)
gdal.VSIFWriteL("hello", 1, 5, fp)
gdal.VSIFCloseL(fp)
fp = gdal.VSIFOpenL('/vsicrypt//vsimem/file.bin', 'rb')
content = gdal.VSIFReadL(1, 5, fp).decode('latin1')
fp = gdal.VSIFOpenL("/vsicrypt//vsimem/file.bin", "rb")
content = gdal.VSIFReadL(1, 5, fp).decode("latin1")
gdal.VSIFCloseL(fp)
assert content == 'hello', options
assert content == "hello", options
gdal.SetConfigOption('VSICRYPT_KEY_B64', None)
gdal.SetConfigOption("VSICRYPT_KEY_B64", None)
fp = gdal.VSIFOpenL('/vsicrypt/key_b64=%s,file=/vsimem/file.bin' % key_b64, 'rb')
content = gdal.VSIFReadL(1, 5, fp).decode('latin1')
fp = gdal.VSIFOpenL("/vsicrypt/key_b64=%s,file=/vsimem/file.bin" % key_b64, "rb")
content = gdal.VSIFReadL(1, 5, fp).decode("latin1")
gdal.VSIFCloseL(fp)
assert content == 'hello', options
assert content == "hello", options
with gdaltest.error_handler():
statRes = gdal.VSIStatL('/vsicrypt//vsimem/file.bin')
statRes = gdal.VSIStatL("/vsicrypt//vsimem/file.bin")
assert statRes is None
ret = gdal.Rename('/vsicrypt//vsimem/file.bin', '/vsicrypt//vsimem/subdir_crypt/file.bin')
ret = gdal.Rename(
"/vsicrypt//vsimem/file.bin", "/vsicrypt//vsimem/subdir_crypt/file.bin"
)
assert ret == 0
ret = gdal.Rename('/vsicrypt//vsimem/subdir_crypt/file.bin', '/vsimem/subdir_crypt/file2.bin')
ret = gdal.Rename(
"/vsicrypt//vsimem/subdir_crypt/file.bin", "/vsimem/subdir_crypt/file2.bin"
)
assert ret == 0
dir_content = gdal.ReadDir('/vsicrypt//vsimem/subdir_crypt')
assert dir_content == ['file2.bin']
dir_content = gdal.ReadDir("/vsicrypt//vsimem/subdir_crypt")
assert dir_content == ["file2.bin"]
gdal.Unlink("/vsimem/subdir_crypt/file2.bin")
gdal.Unlink('/vsimem/subdir_crypt/file2.bin')
###############################################################################
# Test "random" operations against reference filesystem
@ -356,18 +491,21 @@ def test_vsicrypt_4():
if not gdaltest.has_vsicrypt:
pytest.skip()
test_file = '/vsicrypt/key=DONT_USE_IN_PROD,sector_size=32,file=/vsimem/file_enc.bin'
ref_file = '/vsimem/file.bin'
test_file = (
"/vsicrypt/key=DONT_USE_IN_PROD,sector_size=32,file=/vsimem/file_enc.bin"
)
ref_file = "/vsimem/file.bin"
for seed in range(1000):
gdal.Unlink(test_file)
gdal.Unlink(ref_file)
test_f = gdal.VSIFOpenL(test_file, 'wb+')
ref_f = gdal.VSIFOpenL(ref_file, 'wb+')
test_f = gdal.VSIFOpenL(test_file, "wb+")
ref_f = gdal.VSIFOpenL(ref_file, "wb+")
import random
random.seed(seed)
for _ in range(20):
@ -376,7 +514,9 @@ def test_vsicrypt_4():
gdal.VSIFSeekL(ref_f, random_offset, 0)
random_size = random.randint(1, 80)
random_content = ''.join([chr(40 + int(10 * random.random())) for _ in range(random_size)])
random_content = "".join(
[chr(40 + int(10 * random.random())) for _ in range(random_size)]
)
gdal.VSIFWriteL(random_content, 1, random_size, test_f)
gdal.VSIFWriteL(random_content, 1, random_size, ref_f)
@ -390,9 +530,9 @@ def test_vsicrypt_4():
ref_content = gdal.VSIFReadL(1, random_size, ref_f)
if test_content != ref_content:
print(seed)
print('Test content (%d):' % len(test_content))
print('')
pytest.fail('Ref content (%d):' % len(ref_content))
print("Test content (%d):" % len(test_content))
print("")
pytest.fail("Ref content (%d):" % len(ref_content))
gdal.VSIFSeekL(test_f, 0, 0)
gdal.VSIFSeekL(ref_f, 0, 0)
@ -404,13 +544,14 @@ def test_vsicrypt_4():
if test_content != ref_content:
print(seed)
print('Test content (%d):' % len(test_content))
print('')
pytest.fail('Ref content (%d):' % len(ref_content))
print("Test content (%d):" % len(test_content))
print("")
pytest.fail("Ref content (%d):" % len(ref_content))
gdal.Unlink(test_file)
gdal.Unlink(ref_file)
###############################################################################
# Test random filling of last sector
@ -420,51 +561,52 @@ def test_vsicrypt_5():
if not gdaltest.has_vsicrypt:
pytest.skip()
test_file = '/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file_enc.bin'
test_file = "/vsicrypt/key=DONT_USE_IN_PROD,file=/vsimem/file_enc.bin"
f = gdal.VSIFOpenL(test_file, 'wb+')
gdal.VSIFWriteL('ab', 1, 2, f)
f = gdal.VSIFOpenL(test_file, "wb+")
gdal.VSIFWriteL("ab", 1, 2, f)
gdal.VSIFCloseL(f)
f = gdal.VSIFOpenL(test_file, 'rb+')
f = gdal.VSIFOpenL(test_file, "rb+")
gdal.VSIFSeekL(f, 3, 0)
gdal.VSIFWriteL('d', 1, 1, f)
gdal.VSIFWriteL("d", 1, 1, f)
gdal.VSIFCloseL(f)
f = gdal.VSIFOpenL(test_file, 'rb')
f = gdal.VSIFOpenL(test_file, "rb")
content = gdal.VSIFReadL(1, 4, f)
content = struct.unpack('B' * len(content), content)
content = struct.unpack("B" * len(content), content)
gdal.VSIFCloseL(f)
assert content == (97, 98, 0, 100)
f = gdal.VSIFOpenL(test_file, 'rb+')
f = gdal.VSIFOpenL(test_file, "rb+")
gdal.VSIFReadL(1, 1, f)
gdal.VSIFSeekL(f, 5, 0)
gdal.VSIFWriteL('f', 1, 1, f)
gdal.VSIFWriteL("f", 1, 1, f)
gdal.VSIFCloseL(f)
f = gdal.VSIFOpenL(test_file, 'rb')
f = gdal.VSIFOpenL(test_file, "rb")
content = gdal.VSIFReadL(1, 6, f)
content = struct.unpack('B' * len(content), content)
content = struct.unpack("B" * len(content), content)
gdal.VSIFCloseL(f)
assert content == (97, 98, 0, 100, 0, 102)
f = gdal.VSIFOpenL(test_file, 'rb+')
f = gdal.VSIFOpenL(test_file, "rb+")
gdal.VSIFReadL(1, 1, f)
gdal.VSIFSeekL(f, 512, 0)
gdal.VSIFWriteL('Z', 1, 1, f)
gdal.VSIFWriteL("Z", 1, 1, f)
gdal.VSIFSeekL(f, 7, 0)
gdal.VSIFWriteL('h', 1, 1, f)
gdal.VSIFWriteL("h", 1, 1, f)
gdal.VSIFCloseL(f)
f = gdal.VSIFOpenL(test_file, 'rb')
f = gdal.VSIFOpenL(test_file, "rb")
content = gdal.VSIFReadL(1, 8, f)
content = struct.unpack('B' * len(content), content)
content = struct.unpack("B" * len(content), content)
gdal.VSIFCloseL(f)
assert content == (97, 98, 0, 100, 0, 102, 0, 104)
gdal.Unlink(test_file)
###############################################################################
# Test VSISetCryptKey
@ -475,50 +617,47 @@ def test_vsicrypt_6(testnonboundtoswig_setup): # noqa
testnonboundtoswig_setup.VSISetCryptKey.restype = None
# Set a valid key
testnonboundtoswig_setup.VSISetCryptKey('DONT_USE_IN_PROD'.encode('ASCII'), 16)
testnonboundtoswig_setup.VSISetCryptKey("DONT_USE_IN_PROD".encode("ASCII"), 16)
if not gdaltest.has_vsicrypt:
pytest.skip()
fp = gdal.VSIFOpenL('/vsicrypt/add_key_check=yes,file=/vsimem/file.bin', 'wb+')
fp = gdal.VSIFOpenL("/vsicrypt/add_key_check=yes,file=/vsimem/file.bin", "wb+")
assert fp is not None
gdal.VSIFWriteL('hello', 1, 5, fp)
gdal.VSIFWriteL("hello", 1, 5, fp)
gdal.VSIFCloseL(fp)
fp = gdal.VSIFOpenL('/vsicrypt//vsimem/file.bin', 'rb')
content = gdal.VSIFReadL(1, 5, fp).decode('latin1')
fp = gdal.VSIFOpenL("/vsicrypt//vsimem/file.bin", "rb")
content = gdal.VSIFReadL(1, 5, fp).decode("latin1")
gdal.VSIFCloseL(fp)
assert content == 'hello'
assert content == "hello"
fp = gdal.VSIFOpenL('/vsicrypt//vsimem/file.bin', 'wb+')
fp = gdal.VSIFOpenL("/vsicrypt//vsimem/file.bin", "wb+")
assert fp is not None
gdal.VSIFWriteL('hello', 1, 5, fp)
gdal.VSIFWriteL("hello", 1, 5, fp)
gdal.VSIFCloseL(fp)
fp = gdal.VSIFOpenL('/vsicrypt//vsimem/file.bin', 'rb')
content = gdal.VSIFReadL(1, 5, fp).decode('latin1')
fp = gdal.VSIFOpenL("/vsicrypt//vsimem/file.bin", "rb")
content = gdal.VSIFReadL(1, 5, fp).decode("latin1")
gdal.VSIFCloseL(fp)
assert content == 'hello'
assert content == "hello"
# Set a too short key
testnonboundtoswig_setup.VSISetCryptKey('bbc'.encode('ASCII'), 3)
testnonboundtoswig_setup.VSISetCryptKey("bbc".encode("ASCII"), 3)
with gdaltest.error_handler():
fp = gdal.VSIFOpenL('/vsicrypt//vsimem/file.bin', 'rb')
fp = gdal.VSIFOpenL("/vsicrypt//vsimem/file.bin", "rb")
assert fp is None
with gdaltest.error_handler():
fp = gdal.VSIFOpenL('/vsicrypt//vsimem/file.bin', 'wb+')
fp = gdal.VSIFOpenL("/vsicrypt//vsimem/file.bin", "wb+")
assert fp is None
# Erase key
testnonboundtoswig_setup.VSISetCryptKey(None, 0)
with gdaltest.error_handler():
fp = gdal.VSIFOpenL('/vsicrypt//vsimem/file.bin', 'wb+')
fp = gdal.VSIFOpenL("/vsicrypt//vsimem/file.bin", "wb+")
assert fp is None
gdal.Unlink('/vsimem/file.bin')
gdal.Unlink("/vsimem/file.bin")

View File

@ -29,18 +29,17 @@
###############################################################################
import time
from osgeo import gdal
from osgeo import ogr
import gdaltest
import webserver
import pytest
import webserver
from osgeo import gdal, ogr
###############################################################################
#
def test_vsicurl_1():
if not gdaltest.run_slow_tests():
pytest.skip()
@ -48,9 +47,12 @@ def test_vsicurl_1():
if not gdaltest.built_against_curl():
pytest.skip()
ds = ogr.Open('/vsizip/vsicurl/http://publicfiles.dep.state.fl.us/dear/BWR_GIS/2007NWFLULC/NWFWMD2007LULC.zip')
ds = ogr.Open(
"/vsizip/vsicurl/http://publicfiles.dep.state.fl.us/dear/BWR_GIS/2007NWFLULC/NWFWMD2007LULC.zip"
)
assert ds is not None
###############################################################################
#
@ -62,9 +64,12 @@ def vsicurl_2():
if not gdaltest.built_against_curl():
pytest.skip()
ds = gdal.Open('/vsizip//vsicurl/http://eros.usgs.gov/archive/nslrsda/GeoTowns/HongKong/srtm/n22e113.zip/n22e113.bil')
ds = gdal.Open(
"/vsizip//vsicurl/http://eros.usgs.gov/archive/nslrsda/GeoTowns/HongKong/srtm/n22e113.zip/n22e113.bil"
)
assert ds is not None
###############################################################################
# This server doesn't support range downloading
@ -76,9 +81,12 @@ def vsicurl_3():
if not gdaltest.built_against_curl():
pytest.skip()
ds = ogr.Open('/vsizip/vsicurl/http://www.iucnredlist.org/spatial-data/MAMMALS_TERRESTRIAL.zip')
ds = ogr.Open(
"/vsizip/vsicurl/http://www.iucnredlist.org/spatial-data/MAMMALS_TERRESTRIAL.zip"
)
assert ds is None
###############################################################################
# This server doesn't support range downloading
@ -90,9 +98,12 @@ def test_vsicurl_4():
if not gdaltest.built_against_curl():
pytest.skip()
ds = ogr.Open('/vsizip/vsicurl/http://lelserver.env.duke.edu:8080/LandscapeTools/export/49/Downloads/1_Habitats.zip')
ds = ogr.Open(
"/vsizip/vsicurl/http://lelserver.env.duke.edu:8080/LandscapeTools/export/49/Downloads/1_Habitats.zip"
)
assert ds is None
###############################################################################
# Test URL unescaping when reading HTTP file list
@ -104,9 +115,12 @@ def test_vsicurl_5():
if not gdaltest.built_against_curl():
pytest.skip()
ds = gdal.Open('/vsicurl/http://dds.cr.usgs.gov/srtm/SRTM_image_sample/picture%20examples/N34W119_DEM.tif')
ds = gdal.Open(
"/vsicurl/http://dds.cr.usgs.gov/srtm/SRTM_image_sample/picture%20examples/N34W119_DEM.tif"
)
assert ds is not None
###############################################################################
# Test with FTP server that doesn't support EPSV command
@ -118,13 +132,14 @@ def vsicurl_6_disabled():
if not gdaltest.built_against_curl():
pytest.skip()
fl = gdal.ReadDir('/vsicurl/ftp://ftp2.cits.rncan.gc.ca/pub/cantopo/250k_tif')
fl = gdal.ReadDir("/vsicurl/ftp://ftp2.cits.rncan.gc.ca/pub/cantopo/250k_tif")
assert fl
###############################################################################
# Test Microsoft-IIS/6.0 listing
def test_vsicurl_7():
if not gdaltest.run_slow_tests():
pytest.skip()
@ -132,9 +147,10 @@ def test_vsicurl_7():
if not gdaltest.built_against_curl():
pytest.skip()
fl = gdal.ReadDir('/vsicurl/http://ortho.linz.govt.nz/tifs/2005_06')
fl = gdal.ReadDir("/vsicurl/http://ortho.linz.govt.nz/tifs/2005_06")
assert fl
###############################################################################
# Test interleaved reading between 2 datasets
@ -146,11 +162,16 @@ def vsicurl_8():
if not gdaltest.built_against_curl():
pytest.skip()
ds1 = gdal.Open('/vsigzip//vsicurl/http://dds.cr.usgs.gov/pub/data/DEM/250/notavail/C/chipicoten-w.gz')
gdal.Open('/vsizip//vsicurl/http://edcftp.cr.usgs.gov/pub/data/landcover/files/2009/biso/gokn09b_dnbr.zip/nps-serotnbsp-9001-20090321_rd.tif')
ds1 = gdal.Open(
"/vsigzip//vsicurl/http://dds.cr.usgs.gov/pub/data/DEM/250/notavail/C/chipicoten-w.gz"
)
gdal.Open(
"/vsizip//vsicurl/http://edcftp.cr.usgs.gov/pub/data/landcover/files/2009/biso/gokn09b_dnbr.zip/nps-serotnbsp-9001-20090321_rd.tif"
)
cs = ds1.GetRasterBand(1).Checksum()
assert cs == 61342
###############################################################################
# Test reading a file with Chinese characters, but the HTTP file listing
# returns escaped sequences instead of the Chinese characters.
@ -163,10 +184,13 @@ def test_vsicurl_9():
if not gdaltest.built_against_curl():
pytest.skip()
ds = gdal.Open('/vsicurl/http://download.osgeo.org/gdal/data/gtiff/'
'xx\u4E2D\u6587.\u4E2D\u6587')
ds = gdal.Open(
"/vsicurl/http://download.osgeo.org/gdal/data/gtiff/"
"xx\u4E2D\u6587.\u4E2D\u6587"
)
assert ds is not None
###############################################################################
# Test reading a file with escaped Chinese characters.
@ -178,9 +202,12 @@ def test_vsicurl_10():
if not gdaltest.built_against_curl():
pytest.skip()
ds = gdal.Open('/vsicurl/http://download.osgeo.org/gdal/data/gtiff/xx%E4%B8%AD%E6%96%87.%E4%B8%AD%E6%96%87')
ds = gdal.Open(
"/vsicurl/http://download.osgeo.org/gdal/data/gtiff/xx%E4%B8%AD%E6%96%87.%E4%B8%AD%E6%96%87"
)
assert ds is not None
###############################################################################
# Test ReadDir() after reading a file on the same server
@ -192,16 +219,19 @@ def test_vsicurl_11():
if not gdaltest.built_against_curl():
pytest.skip()
f = gdal.VSIFOpenL('/vsicurl/http://download.osgeo.org/gdal/data/bmp/Bug2236.bmp', 'rb')
f = gdal.VSIFOpenL(
"/vsicurl/http://download.osgeo.org/gdal/data/bmp/Bug2236.bmp", "rb"
)
if f is None:
pytest.skip()
gdal.VSIFSeekL(f, 1000000, 0)
gdal.VSIFReadL(1, 1, f)
gdal.VSIFCloseL(f)
filelist = gdal.ReadDir('/vsicurl/http://download.osgeo.org/gdal/data/gtiff')
filelist = gdal.ReadDir("/vsicurl/http://download.osgeo.org/gdal/data/gtiff")
assert filelist is not None and filelist
###############################################################################
@ -213,7 +243,9 @@ def test_vsicurl_start_webserver():
if not gdaltest.built_against_curl():
pytest.skip()
(gdaltest.webserver_process, gdaltest.webserver_port) = webserver.launch(handler=webserver.DispatcherHttpHandler)
(gdaltest.webserver_process, gdaltest.webserver_port) = webserver.launch(
handler=webserver.DispatcherHttpHandler
)
if gdaltest.webserver_port == 0:
pytest.skip()
@ -230,58 +262,84 @@ def test_vsicurl_test_redirect():
gdal.VSICurlClearCache()
handler = webserver.SequentialHandler()
handler.add('GET', '/test_redirect/', 404)
handler.add("GET", "/test_redirect/", 404)
# Simulate a big time difference between server and local machine
current_time = 1500
def method(request):
response = 'HTTP/1.1 302\r\n'
response += 'Server: foo\r\n'
response += 'Date: ' + time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(current_time)) + '\r\n'
response += 'Location: %s\r\n' % ('http://localhost:%d/foo.s3.amazonaws.com/test_redirected/test.bin?Signature=foo&Expires=%d' % (gdaltest.webserver_port, current_time + 30))
response += '\r\n'
request.wfile.write(response.encode('ascii'))
response = "HTTP/1.1 302\r\n"
response += "Server: foo\r\n"
response += (
"Date: "
+ time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(current_time))
+ "\r\n"
)
response += "Location: %s\r\n" % (
"http://localhost:%d/foo.s3.amazonaws.com/test_redirected/test.bin?Signature=foo&Expires=%d"
% (gdaltest.webserver_port, current_time + 30)
)
response += "\r\n"
request.wfile.write(response.encode("ascii"))
handler.add('HEAD', '/test_redirect/test.bin', custom_method=method)
handler.add('HEAD', '/foo.s3.amazonaws.com/test_redirected/test.bin?Signature=foo&Expires=%d' % (current_time + 30), 403,
{'Server': 'foo'}, '')
handler.add("HEAD", "/test_redirect/test.bin", custom_method=method)
handler.add(
"HEAD",
"/foo.s3.amazonaws.com/test_redirected/test.bin?Signature=foo&Expires=%d"
% (current_time + 30),
403,
{"Server": "foo"},
"",
)
def method(request):
if 'Range' in request.headers:
if request.headers['Range'] == 'bytes=0-16383':
request.protocol_version = 'HTTP/1.1'
if "Range" in request.headers:
if request.headers["Range"] == "bytes=0-16383":
request.protocol_version = "HTTP/1.1"
request.send_response(200)
request.send_header('Content-type', 'text/plain')
request.send_header('Content-Range', 'bytes 0-16383/1000000')
request.send_header('Content-Length', 16384)
request.send_header('Connection', 'close')
request.send_header("Content-type", "text/plain")
request.send_header("Content-Range", "bytes 0-16383/1000000")
request.send_header("Content-Length", 16384)
request.send_header("Connection", "close")
request.end_headers()
request.wfile.write(('x' * 16384).encode('ascii'))
elif request.headers['Range'] == 'bytes=16384-49151':
request.wfile.write(("x" * 16384).encode("ascii"))
elif request.headers["Range"] == "bytes=16384-49151":
# Test expiration of the signed URL
request.protocol_version = 'HTTP/1.1'
request.protocol_version = "HTTP/1.1"
request.send_response(403)
request.send_header('Content-Length', 0)
request.send_header("Content-Length", 0)
request.end_headers()
else:
request.send_response(404)
request.send_header('Content-Length', 0)
request.send_header("Content-Length", 0)
request.end_headers()
else:
# After a failed attempt on a HEAD, the client should go there
response = 'HTTP/1.1 200\r\n'
response += 'Server: foo\r\n'
response += 'Date: ' + time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(current_time)) + '\r\n'
response += 'Content-type: text/plain\r\n'
response += 'Content-Length: 1000000\r\n'
response += 'Connection: close\r\n'
response += '\r\n'
request.wfile.write(response.encode('ascii'))
response = "HTTP/1.1 200\r\n"
response += "Server: foo\r\n"
response += (
"Date: "
+ time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(current_time))
+ "\r\n"
)
response += "Content-type: text/plain\r\n"
response += "Content-Length: 1000000\r\n"
response += "Connection: close\r\n"
response += "\r\n"
request.wfile.write(response.encode("ascii"))
handler.add('GET', '/foo.s3.amazonaws.com/test_redirected/test.bin?Signature=foo&Expires=%d' % (current_time + 30), custom_method=method)
handler.add(
"GET",
"/foo.s3.amazonaws.com/test_redirected/test.bin?Signature=foo&Expires=%d"
% (current_time + 30),
custom_method=method,
)
with webserver.install_http_handler(handler):
f = gdal.VSIFOpenL('/vsicurl/http://localhost:%d/test_redirect/test.bin' % gdaltest.webserver_port, 'rb')
f = gdal.VSIFOpenL(
"/vsicurl/http://localhost:%d/test_redirect/test.bin"
% gdaltest.webserver_port,
"rb",
)
assert f is not None
gdal.VSIFSeekL(f, 0, 2)
@ -293,51 +351,75 @@ def test_vsicurl_test_redirect():
gdal.VSIFSeekL(f, 0, 0)
handler = webserver.SequentialHandler()
handler.add('GET', '/foo.s3.amazonaws.com/test_redirected/test.bin?Signature=foo&Expires=%d' % (current_time + 30), custom_method=method)
handler.add('GET', '/foo.s3.amazonaws.com/test_redirected/test.bin?Signature=foo&Expires=%d' % (current_time + 30), custom_method=method)
handler.add(
"GET",
"/foo.s3.amazonaws.com/test_redirected/test.bin?Signature=foo&Expires=%d"
% (current_time + 30),
custom_method=method,
)
handler.add(
"GET",
"/foo.s3.amazonaws.com/test_redirected/test.bin?Signature=foo&Expires=%d"
% (current_time + 30),
custom_method=method,
)
current_time = int(time.time())
def method(request):
# We should go there after expiration of the first signed URL
if 'Range' in request.headers and \
request.headers['Range'] == 'bytes=16384-49151':
request.protocol_version = 'HTTP/1.1'
if (
"Range" in request.headers
and request.headers["Range"] == "bytes=16384-49151"
):
request.protocol_version = "HTTP/1.1"
request.send_response(302)
# Return a new signed URL
request.send_header('Location', 'http://localhost:%d/foo.s3.amazonaws.com/test_redirected2/test.bin?Signature=foo&Expires=%d' % (request.server.port, current_time + 30))
request.send_header('Content-Length', 16384)
request.send_header(
"Location",
"http://localhost:%d/foo.s3.amazonaws.com/test_redirected2/test.bin?Signature=foo&Expires=%d"
% (request.server.port, current_time + 30),
)
request.send_header("Content-Length", 16384)
request.end_headers()
request.wfile.write(('x' * 16384).encode('ascii'))
request.wfile.write(("x" * 16384).encode("ascii"))
handler.add('GET', '/test_redirect/test.bin', custom_method=method)
handler.add("GET", "/test_redirect/test.bin", custom_method=method)
def method(request):
# Second signed URL
if 'Range' in request.headers and \
request.headers['Range'] == 'bytes=16384-49151':
request.protocol_version = 'HTTP/1.1'
if (
"Range" in request.headers
and request.headers["Range"] == "bytes=16384-49151"
):
request.protocol_version = "HTTP/1.1"
request.send_response(200)
request.send_header('Content-type', 'text/plain')
request.send_header('Content-Range', 'bytes 16384-16384/1000000')
request.send_header('Content-Length', 1)
request.send_header("Content-type", "text/plain")
request.send_header("Content-Range", "bytes 16384-16384/1000000")
request.send_header("Content-Length", 1)
request.end_headers()
request.wfile.write('y'.encode('ascii'))
request.wfile.write("y".encode("ascii"))
handler.add('GET', '/foo.s3.amazonaws.com/test_redirected2/test.bin?Signature=foo&Expires=%d' % (current_time + 30), custom_method=method)
handler.add(
"GET",
"/foo.s3.amazonaws.com/test_redirected2/test.bin?Signature=foo&Expires=%d"
% (current_time + 30),
custom_method=method,
)
with webserver.install_http_handler(handler):
content = gdal.VSIFReadL(1, 16383, f).decode('ascii')
if len(content) != 16383 or content[0] != 'x':
content = gdal.VSIFReadL(1, 16383, f).decode("ascii")
if len(content) != 16383 or content[0] != "x":
gdal.VSIFCloseL(f)
pytest.fail(content)
content = gdal.VSIFReadL(1, 2, f).decode('ascii')
if content != 'xy':
content = gdal.VSIFReadL(1, 2, f).decode("ascii")
if content != "xy":
gdal.VSIFCloseL(f)
pytest.fail(content)
gdal.VSIFCloseL(f)
###############################################################################
# Test redirection with X-Amz-Expires= + X-Amz-Date= type of signed URLs
@ -350,58 +432,87 @@ def test_vsicurl_test_redirect_x_amz():
gdal.VSICurlClearCache()
handler = webserver.SequentialHandler()
handler.add('GET', '/test_redirect/', 404)
handler.add("GET", "/test_redirect/", 404)
# Simulate a big time difference between server and local machine
current_time = 1500
def method(request):
response = 'HTTP/1.1 302\r\n'
response += 'Server: foo\r\n'
response += 'Date: ' + time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(current_time)) + '\r\n'
response += 'Location: %s\r\n' % ('http://localhost:%d/foo.s3.amazonaws.com/test_redirected/test.bin?X-Amz-Signature=foo&X-Amz-Expires=30&X-Amz-Date=%s' % (gdaltest.webserver_port, time.strftime("%Y%m%dT%H%M%SZ", time.gmtime(current_time))))
response += '\r\n'
request.wfile.write(response.encode('ascii'))
response = "HTTP/1.1 302\r\n"
response += "Server: foo\r\n"
response += (
"Date: "
+ time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(current_time))
+ "\r\n"
)
response += "Location: %s\r\n" % (
"http://localhost:%d/foo.s3.amazonaws.com/test_redirected/test.bin?X-Amz-Signature=foo&X-Amz-Expires=30&X-Amz-Date=%s"
% (
gdaltest.webserver_port,
time.strftime("%Y%m%dT%H%M%SZ", time.gmtime(current_time)),
)
)
response += "\r\n"
request.wfile.write(response.encode("ascii"))
handler.add('HEAD', '/test_redirect/test.bin', custom_method=method)
handler.add('HEAD', '/foo.s3.amazonaws.com/test_redirected/test.bin?X-Amz-Signature=foo&X-Amz-Expires=30&X-Amz-Date=%s' % time.strftime("%Y%m%dT%H%M%SZ", time.gmtime(current_time)), 403,
{'Server': 'foo'}, '')
handler.add("HEAD", "/test_redirect/test.bin", custom_method=method)
handler.add(
"HEAD",
"/foo.s3.amazonaws.com/test_redirected/test.bin?X-Amz-Signature=foo&X-Amz-Expires=30&X-Amz-Date=%s"
% time.strftime("%Y%m%dT%H%M%SZ", time.gmtime(current_time)),
403,
{"Server": "foo"},
"",
)
def method(request):
if 'Range' in request.headers:
if request.headers['Range'] == 'bytes=0-16383':
request.protocol_version = 'HTTP/1.1'
if "Range" in request.headers:
if request.headers["Range"] == "bytes=0-16383":
request.protocol_version = "HTTP/1.1"
request.send_response(200)
request.send_header('Content-type', 'text/plain')
request.send_header('Content-Range', 'bytes 0-16383/1000000')
request.send_header('Content-Length', 16384)
request.send_header('Connection', 'close')
request.send_header("Content-type", "text/plain")
request.send_header("Content-Range", "bytes 0-16383/1000000")
request.send_header("Content-Length", 16384)
request.send_header("Connection", "close")
request.end_headers()
request.wfile.write(('x' * 16384).encode('ascii'))
elif request.headers['Range'] == 'bytes=16384-49151':
request.wfile.write(("x" * 16384).encode("ascii"))
elif request.headers["Range"] == "bytes=16384-49151":
# Test expiration of the signed URL
request.protocol_version = 'HTTP/1.1'
request.protocol_version = "HTTP/1.1"
request.send_response(403)
request.send_header('Content-Length', 0)
request.send_header("Content-Length", 0)
request.end_headers()
else:
request.send_response(404)
request.send_header('Content-Length', 0)
request.send_header("Content-Length", 0)
request.end_headers()
else:
# After a failed attempt on a HEAD, the client should go there
response = 'HTTP/1.1 200\r\n'
response += 'Server: foo\r\n'
response += 'Date: ' + time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(current_time)) + '\r\n'
response += 'Content-type: text/plain\r\n'
response += 'Content-Length: 1000000\r\n'
response += 'Connection: close\r\n'
response += '\r\n'
request.wfile.write(response.encode('ascii'))
response = "HTTP/1.1 200\r\n"
response += "Server: foo\r\n"
response += (
"Date: "
+ time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(current_time))
+ "\r\n"
)
response += "Content-type: text/plain\r\n"
response += "Content-Length: 1000000\r\n"
response += "Connection: close\r\n"
response += "\r\n"
request.wfile.write(response.encode("ascii"))
handler.add('GET', '/foo.s3.amazonaws.com/test_redirected/test.bin?X-Amz-Signature=foo&X-Amz-Expires=30&X-Amz-Date=%s' % time.strftime("%Y%m%dT%H%M%SZ", time.gmtime(current_time)), custom_method=method)
handler.add(
"GET",
"/foo.s3.amazonaws.com/test_redirected/test.bin?X-Amz-Signature=foo&X-Amz-Expires=30&X-Amz-Date=%s"
% time.strftime("%Y%m%dT%H%M%SZ", time.gmtime(current_time)),
custom_method=method,
)
with webserver.install_http_handler(handler):
f = gdal.VSIFOpenL('/vsicurl/http://localhost:%d/test_redirect/test.bin' % gdaltest.webserver_port, 'rb')
f = gdal.VSIFOpenL(
"/vsicurl/http://localhost:%d/test_redirect/test.bin"
% gdaltest.webserver_port,
"rb",
)
assert f is not None
gdal.VSIFSeekL(f, 0, 2)
@ -413,51 +524,78 @@ def test_vsicurl_test_redirect_x_amz():
gdal.VSIFSeekL(f, 0, 0)
handler = webserver.SequentialHandler()
handler.add('GET', '/foo.s3.amazonaws.com/test_redirected/test.bin?X-Amz-Signature=foo&X-Amz-Expires=30&X-Amz-Date=%s' % time.strftime("%Y%m%dT%H%M%SZ", time.gmtime(current_time)), custom_method=method)
handler.add('GET', '/foo.s3.amazonaws.com/test_redirected/test.bin?X-Amz-Signature=foo&X-Amz-Expires=30&X-Amz-Date=%s' % time.strftime("%Y%m%dT%H%M%SZ", time.gmtime(current_time)), custom_method=method)
handler.add(
"GET",
"/foo.s3.amazonaws.com/test_redirected/test.bin?X-Amz-Signature=foo&X-Amz-Expires=30&X-Amz-Date=%s"
% time.strftime("%Y%m%dT%H%M%SZ", time.gmtime(current_time)),
custom_method=method,
)
handler.add(
"GET",
"/foo.s3.amazonaws.com/test_redirected/test.bin?X-Amz-Signature=foo&X-Amz-Expires=30&X-Amz-Date=%s"
% time.strftime("%Y%m%dT%H%M%SZ", time.gmtime(current_time)),
custom_method=method,
)
current_time = int(time.time())
def method(request):
# We should go there after expiration of the first signed URL
if 'Range' in request.headers and \
request.headers['Range'] == 'bytes=16384-49151':
request.protocol_version = 'HTTP/1.1'
if (
"Range" in request.headers
and request.headers["Range"] == "bytes=16384-49151"
):
request.protocol_version = "HTTP/1.1"
request.send_response(302)
# Return a new signed URL
request.send_header('Location', 'http://localhost:%d/foo.s3.amazonaws.com/test_redirected2/test.bin?X-Amz-Signature=foo&X-Amz-Expires=30&X-Amz-Date=%s' % (request.server.port, time.strftime("%Y%m%dT%H%M%SZ", time.gmtime(current_time))))
request.send_header('Content-Length', 16384)
request.send_header(
"Location",
"http://localhost:%d/foo.s3.amazonaws.com/test_redirected2/test.bin?X-Amz-Signature=foo&X-Amz-Expires=30&X-Amz-Date=%s"
% (
request.server.port,
time.strftime("%Y%m%dT%H%M%SZ", time.gmtime(current_time)),
),
)
request.send_header("Content-Length", 16384)
request.end_headers()
request.wfile.write(('x' * 16384).encode('ascii'))
request.wfile.write(("x" * 16384).encode("ascii"))
handler.add('GET', '/test_redirect/test.bin', custom_method=method)
handler.add("GET", "/test_redirect/test.bin", custom_method=method)
def method(request):
# Second signed URL
if 'Range' in request.headers and \
request.headers['Range'] == 'bytes=16384-49151':
request.protocol_version = 'HTTP/1.1'
if (
"Range" in request.headers
and request.headers["Range"] == "bytes=16384-49151"
):
request.protocol_version = "HTTP/1.1"
request.send_response(200)
request.send_header('Content-type', 'text/plain')
request.send_header('Content-Range', 'bytes 16384-16384/1000000')
request.send_header('Content-Length', 1)
request.send_header("Content-type", "text/plain")
request.send_header("Content-Range", "bytes 16384-16384/1000000")
request.send_header("Content-Length", 1)
request.end_headers()
request.wfile.write('y'.encode('ascii'))
request.wfile.write("y".encode("ascii"))
handler.add('GET', '/foo.s3.amazonaws.com/test_redirected2/test.bin?X-Amz-Signature=foo&X-Amz-Expires=30&X-Amz-Date=%s' % time.strftime("%Y%m%dT%H%M%SZ", time.gmtime(current_time)), custom_method=method)
handler.add(
"GET",
"/foo.s3.amazonaws.com/test_redirected2/test.bin?X-Amz-Signature=foo&X-Amz-Expires=30&X-Amz-Date=%s"
% time.strftime("%Y%m%dT%H%M%SZ", time.gmtime(current_time)),
custom_method=method,
)
with webserver.install_http_handler(handler):
content = gdal.VSIFReadL(1, 16383, f).decode('ascii')
if len(content) != 16383 or content[0] != 'x':
content = gdal.VSIFReadL(1, 16383, f).decode("ascii")
if len(content) != 16383 or content[0] != "x":
gdal.VSIFCloseL(f)
pytest.fail(content)
content = gdal.VSIFReadL(1, 2, f).decode('ascii')
if content != 'xy':
content = gdal.VSIFReadL(1, 2, f).decode("ascii")
if content != "xy":
gdal.VSIFCloseL(f)
pytest.fail(content)
gdal.VSIFCloseL(f)
###############################################################################
# TODO: better testing
@ -467,6 +605,7 @@ def test_vsicurl_test_clear_cache():
gdal.VSICurlClearCache()
gdal.VSICurlClearCache()
###############################################################################
@ -476,11 +615,15 @@ def test_vsicurl_test_retry():
pytest.skip()
handler = webserver.SequentialHandler()
handler.add('GET', '/test_retry/', 404)
handler.add('HEAD', '/test_retry/test.txt', 200, {'Content-Length': '3'})
handler.add('GET', '/test_retry/test.txt', 502)
handler.add("GET", "/test_retry/", 404)
handler.add("HEAD", "/test_retry/test.txt", 200, {"Content-Length": "3"})
handler.add("GET", "/test_retry/test.txt", 502)
with webserver.install_http_handler(handler):
f = gdal.VSIFOpenL('/vsicurl/http://localhost:%d/test_retry/test.txt' % gdaltest.webserver_port, 'rb')
f = gdal.VSIFOpenL(
"/vsicurl/http://localhost:%d/test_retry/test.txt"
% gdaltest.webserver_port,
"rb",
)
data_len = 0
if f:
data_len = len(gdal.VSIFReadL(1, 1, f))
@ -490,21 +633,25 @@ def test_vsicurl_test_retry():
gdal.VSICurlClearCache()
handler = webserver.SequentialHandler()
handler.add('GET', '/test_retry/', 404)
handler.add('HEAD', '/test_retry/test.txt', 200, {'Content-Length': '3'})
handler.add('GET', '/test_retry/test.txt', 502)
handler.add('GET', '/test_retry/test.txt', 429)
handler.add('GET', '/test_retry/test.txt', 200, {}, 'foo')
handler.add("GET", "/test_retry/", 404)
handler.add("HEAD", "/test_retry/test.txt", 200, {"Content-Length": "3"})
handler.add("GET", "/test_retry/test.txt", 502)
handler.add("GET", "/test_retry/test.txt", 429)
handler.add("GET", "/test_retry/test.txt", 200, {}, "foo")
with webserver.install_http_handler(handler):
f = gdal.VSIFOpenL('/vsicurl?max_retry=2&retry_delay=0.01&url=http://localhost:%d/test_retry/test.txt' % gdaltest.webserver_port, 'rb')
f = gdal.VSIFOpenL(
"/vsicurl?max_retry=2&retry_delay=0.01&url=http://localhost:%d/test_retry/test.txt"
% gdaltest.webserver_port,
"rb",
)
assert f is not None
gdal.ErrorReset()
with gdaltest.error_handler():
data = gdal.VSIFReadL(1, 3, f).decode('ascii')
data = gdal.VSIFReadL(1, 3, f).decode("ascii")
error_msg = gdal.GetLastErrorMsg()
gdal.VSIFCloseL(f)
assert data == 'foo'
assert '429' in error_msg
assert data == "foo"
assert "429" in error_msg
###############################################################################
@ -518,14 +665,18 @@ def test_vsicurl_test_fallback_from_head_to_get():
gdal.VSICurlClearCache()
handler = webserver.SequentialHandler()
handler.add('HEAD', '/test_fallback_from_head_to_get', 405)
handler.add('GET', '/test_fallback_from_head_to_get', 200, {}, 'foo')
handler.add("HEAD", "/test_fallback_from_head_to_get", 405)
handler.add("GET", "/test_fallback_from_head_to_get", 200, {}, "foo")
with webserver.install_http_handler(handler):
statres = gdal.VSIStatL('/vsicurl/http://localhost:%d/test_fallback_from_head_to_get' % gdaltest.webserver_port)
statres = gdal.VSIStatL(
"/vsicurl/http://localhost:%d/test_fallback_from_head_to_get"
% gdaltest.webserver_port
)
assert statres.size == 3
gdal.VSICurlClearCache()
###############################################################################
@ -535,7 +686,12 @@ def test_vsicurl_test_parse_html_filelist_apache():
pytest.skip()
handler = webserver.SequentialHandler()
handler.add('GET', '/mydir/', 200, {}, """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
handler.add(
"GET",
"/mydir/",
200,
{},
"""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
<head>
<title>Index of /mydir</title>
@ -548,17 +704,34 @@ def test_vsicurl_test_parse_html_filelist_apache():
<tr><td valign="top"><img src="/icons/image2.gif" alt="[IMG]"></td><td><a href="foo%20with%20space.tif">foo with space.tif</a></td><td align="right">15-Jan-2007 11:02 </td><td align="right">736 </td><td>&nbsp;</td></tr>
<tr><th colspan="5"><hr></th></tr>
</table>
</body></html>""")
</body></html>""",
)
with webserver.install_http_handler(handler):
fl = gdal.ReadDir('/vsicurl/http://localhost:%d/mydir' % gdaltest.webserver_port)
assert fl == ['foo.tif', 'foo%20with%20space.tif']
fl = gdal.ReadDir(
"/vsicurl/http://localhost:%d/mydir" % gdaltest.webserver_port
)
assert fl == ["foo.tif", "foo%20with%20space.tif"]
assert gdal.VSIStatL('/vsicurl/http://localhost:%d/mydir/foo%%20with%%20space.tif' % gdaltest.webserver_port, gdal.VSI_STAT_EXISTS_FLAG) is not None
assert (
gdal.VSIStatL(
"/vsicurl/http://localhost:%d/mydir/foo%%20with%%20space.tif"
% gdaltest.webserver_port,
gdal.VSI_STAT_EXISTS_FLAG,
)
is not None
)
handler = webserver.SequentialHandler()
handler.add('HEAD', '/mydir/i_dont_exist', 404, {})
handler.add("HEAD", "/mydir/i_dont_exist", 404, {})
with webserver.install_http_handler(handler):
assert gdal.VSIStatL('/vsicurl/http://localhost:%d/mydir/i_dont_exist' % gdaltest.webserver_port, gdal.VSI_STAT_EXISTS_FLAG) is None
assert (
gdal.VSIStatL(
"/vsicurl/http://localhost:%d/mydir/i_dont_exist"
% gdaltest.webserver_port,
gdal.VSI_STAT_EXISTS_FLAG,
)
is None
)
###############################################################################
@ -570,12 +743,22 @@ def test_vsicurl_no_size_in_HEAD():
pytest.skip()
handler = webserver.SequentialHandler()
handler.add('HEAD', '/test_vsicurl_no_size_in_HEAD.bin', 200, {}, add_content_length_header=False)
handler.add('GET', '/test_vsicurl_no_size_in_HEAD.bin', 200, {}, 'X' * 10)
handler.add(
"HEAD",
"/test_vsicurl_no_size_in_HEAD.bin",
200,
{},
add_content_length_header=False,
)
handler.add("GET", "/test_vsicurl_no_size_in_HEAD.bin", 200, {}, "X" * 10)
with webserver.install_http_handler(handler):
statres = gdal.VSIStatL('/vsicurl/http://localhost:%d/test_vsicurl_no_size_in_HEAD.bin' % gdaltest.webserver_port)
statres = gdal.VSIStatL(
"/vsicurl/http://localhost:%d/test_vsicurl_no_size_in_HEAD.bin"
% gdaltest.webserver_port
)
assert statres.size == 10
###############################################################################
@ -593,20 +776,25 @@ def test_vsicurl_test_CPL_CURL_VERBOSE():
self.found_CURL_INFO_HEADER_OUT = False
def handler(self, err_type, err_no, err_msg):
if 'CURL_INFO_TEXT:' in err_msg:
if "CURL_INFO_TEXT:" in err_msg:
self.found_CURL_INFO_TEXT = True
if 'CURL_INFO_HEADER_IN:' in err_msg:
if "CURL_INFO_HEADER_IN:" in err_msg:
self.found_CURL_INFO_HEADER_IN = True
if 'CURL_INFO_HEADER_OUT:' in err_msg:
if "CURL_INFO_HEADER_OUT:" in err_msg:
self.found_CURL_INFO_HEADER_OUT = True
handler = webserver.SequentialHandler()
handler.add('HEAD', '/test_vsicurl_test_CPL_CURL_VERBOSE', 200, {'Content-Length': '3'})
handler.add(
"HEAD", "/test_vsicurl_test_CPL_CURL_VERBOSE", 200, {"Content-Length": "3"}
)
my_error_handler = MyHandler()
with gdaltest.config_options({'CPL_CURL_VERBOSE': 'YES', 'CPL_DEBUG': 'ON'}):
with gdaltest.config_options({"CPL_CURL_VERBOSE": "YES", "CPL_DEBUG": "ON"}):
with gdaltest.error_handler(my_error_handler.handler):
with webserver.install_http_handler(handler):
statres = gdal.VSIStatL('/vsicurl/http://localhost:%d/test_vsicurl_test_CPL_CURL_VERBOSE' % gdaltest.webserver_port)
statres = gdal.VSIStatL(
"/vsicurl/http://localhost:%d/test_vsicurl_test_CPL_CURL_VERBOSE"
% gdaltest.webserver_port
)
assert statres.size == 3
assert my_error_handler.found_CURL_INFO_TEXT
@ -615,6 +803,7 @@ def test_vsicurl_test_CPL_CURL_VERBOSE():
gdal.VSICurlClearCache()
###############################################################################
@ -628,6 +817,3 @@ def test_vsicurl_stop_webserver():
gdal.VSICurlClearCache()
webserver.server_stop(gdaltest.webserver_process, gdaltest.webserver_port)

View File

@ -29,35 +29,44 @@
###############################################################################
import time
from osgeo import gdal
import gdaltest
import pytest
from osgeo import gdal
###############################################################################
#
def test_vsicurl_streaming_1():
drv = gdal.GetDriverByName('HTTP')
drv = gdal.GetDriverByName("HTTP")
if drv is None:
pytest.skip()
gdal.SetConfigOption('GDAL_HTTP_CONNECTTIMEOUT', '5')
fp = gdal.VSIFOpenL('/vsicurl_streaming/http://download.osgeo.org/gdal/data/usgsdem/cded/114p01_0100_deme.dem', 'rb')
gdal.SetConfigOption('GDAL_HTTP_CONNECTTIMEOUT', None)
gdal.SetConfigOption("GDAL_HTTP_CONNECTTIMEOUT", "5")
fp = gdal.VSIFOpenL(
"/vsicurl_streaming/http://download.osgeo.org/gdal/data/usgsdem/cded/114p01_0100_deme.dem",
"rb",
)
gdal.SetConfigOption("GDAL_HTTP_CONNECTTIMEOUT", None)
if fp is None:
if gdaltest.gdalurlopen('http://download.osgeo.org/gdal/data/usgsdem/cded/114p01_0100_deme.dem', timeout=4) is None:
pytest.skip('cannot open URL')
if (
gdaltest.gdalurlopen(
"http://download.osgeo.org/gdal/data/usgsdem/cded/114p01_0100_deme.dem",
timeout=4,
)
is None
):
pytest.skip("cannot open URL")
pytest.fail()
if gdal.VSIFTellL(fp) != 0:
gdal.VSIFCloseL(fp)
pytest.fail()
data = gdal.VSIFReadL(1, 50, fp)
if data.decode('ascii') != ' 114p01DEMe Base Ma':
if data.decode("ascii") != " 114p01DEMe Base Ma":
gdal.VSIFCloseL(fp)
pytest.fail()
if gdal.VSIFTellL(fp) != 50:
@ -70,7 +79,7 @@ def test_vsicurl_streaming_1():
gdal.VSIFCloseL(fp)
pytest.fail()
data = gdal.VSIFReadL(1, 50, fp)
if data.decode('ascii') != ' 114p01DEMe Base Ma':
if data.decode("ascii") != " 114p01DEMe Base Ma":
gdal.VSIFCloseL(fp)
pytest.fail()
if gdal.VSIFTellL(fp) != 50:
@ -80,7 +89,7 @@ def test_vsicurl_streaming_1():
time.sleep(0.5)
gdal.VSIFSeekL(fp, 2001, 0)
data_2001 = gdal.VSIFReadL(1, 20, fp)
if data_2001.decode('ascii') != '7-32767-32767-32767-':
if data_2001.decode("ascii") != "7-32767-32767-32767-":
gdal.VSIFCloseL(fp)
pytest.fail(data_2001)
if gdal.VSIFTellL(fp) != 2001 + 20:
@ -109,7 +118,7 @@ def test_vsicurl_streaming_1():
gdal.VSIFSeekL(fp, 1024 * 1024 + 100, 0)
data = gdal.VSIFReadL(1, 20, fp)
if data.decode('ascii') != '67-32767-32767-32767':
if data.decode("ascii") != "67-32767-32767-32767":
gdal.VSIFCloseL(fp)
pytest.fail(data)
if gdal.VSIFTellL(fp) != 1024 * 1024 + 100 + 20:
@ -117,6 +126,3 @@ def test_vsicurl_streaming_1():
pytest.fail()
gdal.VSIFCloseL(fp)

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -31,16 +31,17 @@
###############################################################################
import os
from osgeo import gdal
import gdaltest
import pytest
from osgeo import gdal
# Read test
def test_vsihdfs_1():
filename = '/vsihdfs/file:' + os.getcwd() + '/data/text.txt'
fp = gdal.VSIFOpenL(filename, 'rb')
filename = "/vsihdfs/file:" + os.getcwd() + "/data/text.txt"
fp = gdal.VSIFOpenL(filename, "rb")
if fp is None:
gdaltest.have_vsihdfs = False
pytest.skip()
@ -48,65 +49,69 @@ def test_vsihdfs_1():
gdaltest.have_vsihdfs = True
data = gdal.VSIFReadL(5, 1, fp)
assert data and data.decode('ascii') == 'Lorem'
assert data and data.decode("ascii") == "Lorem"
data = gdal.VSIFReadL(1, 6, fp)
assert data and data.decode('ascii') == ' ipsum'
assert data and data.decode("ascii") == " ipsum"
gdal.VSIFCloseL(fp)
# Seek test
def test_vsihdfs_2():
if gdaltest.have_vsihdfs == False:
pytest.skip()
filename = '/vsihdfs/file:' + os.getcwd() + '/data/text.txt'
fp = gdal.VSIFOpenL(filename, 'rb')
filename = "/vsihdfs/file:" + os.getcwd() + "/data/text.txt"
fp = gdal.VSIFOpenL(filename, "rb")
assert fp is not None
gdal.VSIFSeekL(fp, 2, 0) # From beginning
gdal.VSIFSeekL(fp, 2, 0) # From beginning
gdal.VSIFSeekL(fp, 5, 0)
data = gdal.VSIFReadL(6, 1, fp)
assert data and data.decode('ascii') == ' ipsum'
assert data and data.decode("ascii") == " ipsum"
gdal.VSIFSeekL(fp, 7, 1) # From current
gdal.VSIFSeekL(fp, 7, 1) # From current
data = gdal.VSIFReadL(3, 1, fp)
assert data and data.decode('ascii') == 'sit'
assert data and data.decode("ascii") == "sit"
gdal.VSIFSeekL(fp, 9, 2) # From end
gdal.VSIFSeekL(fp, 9, 2) # From end
data = gdal.VSIFReadL(7, 1, fp)
assert data and data.decode('ascii') == 'laborum'
assert data and data.decode("ascii") == "laborum"
gdal.VSIFCloseL(fp)
# Tell test
def test_vsihdfs_3():
if gdaltest.have_vsihdfs == False:
pytest.skip()
filename = '/vsihdfs/file:' + os.getcwd() + '/data/text.txt'
fp = gdal.VSIFOpenL(filename, 'rb')
filename = "/vsihdfs/file:" + os.getcwd() + "/data/text.txt"
fp = gdal.VSIFOpenL(filename, "rb")
assert fp is not None
data = gdal.VSIFReadL(5, 1, fp)
assert data and data.decode('ascii') == 'Lorem'
assert data and data.decode("ascii") == "Lorem"
offset = gdal.VSIFTellL(fp)
assert offset == 5
gdal.VSIFCloseL(fp)
# Write test
def test_vsihdfs_4():
pytest.skip()
# EOF test
def test_vsihdfs_5():
if gdaltest.have_vsihdfs == False:
pytest.skip()
filename = '/vsihdfs/file:' + os.getcwd() + '/data/text.txt'
fp = gdal.VSIFOpenL(filename, 'rb')
filename = "/vsihdfs/file:" + os.getcwd() + "/data/text.txt"
fp = gdal.VSIFOpenL(filename, "rb")
assert fp is not None
gdal.VSIFReadL(5, 1, fp)
@ -127,27 +132,26 @@ def test_vsihdfs_5():
gdal.VSIFCloseL(fp)
# Stat test
def test_vsihdfs_6():
if gdaltest.have_vsihdfs == False:
pytest.skip()
filename = '/vsihdfs/file:' + os.getcwd() + '/data/text.txt'
filename = "/vsihdfs/file:" + os.getcwd() + "/data/text.txt"
statBuf = gdal.VSIStatL(filename, 0)
assert statBuf
filename = '/vsihdfs/file:' + os.getcwd() + '/data/no-such-file.txt'
filename = "/vsihdfs/file:" + os.getcwd() + "/data/no-such-file.txt"
statBuf = gdal.VSIStatL(filename, 0)
assert not statBuf
# ReadDir test
def test_vsihdfs_7():
if gdaltest.have_vsihdfs == False:
pytest.skip()
dirname = '/vsihdfs/file:' + os.getcwd() + '/data/'
dirname = "/vsihdfs/file:" + os.getcwd() + "/data/"
lst = gdal.ReadDir(dirname)
assert len(lst) >= 360

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -30,11 +30,11 @@
import os
import gdaltest
import pytest
import test_cli_utilities
from osgeo import gdal
import gdaltest
import test_cli_utilities
import pytest
###############################################################################
# Test on a small file
@ -44,14 +44,17 @@ def test_vsistdin_1():
if test_cli_utilities.get_gdal_translate_path() is None:
pytest.skip()
src_ds = gdal.Open('data/byte.tif')
ds = gdal.GetDriverByName('GTiff').CreateCopy('tmp/vsistdin_1_src.tif', src_ds)
src_ds = gdal.Open("data/byte.tif")
ds = gdal.GetDriverByName("GTiff").CreateCopy("tmp/vsistdin_1_src.tif", src_ds)
ds = None
cs = src_ds.GetRasterBand(1).Checksum()
src_ds = None
# Should work on both Unix and Windows
os.system(test_cli_utilities.get_gdal_translate_path() + " /vsistdin/ tmp/vsistdin_1_out.tif -q < tmp/vsistdin_1_src.tif")
os.system(
test_cli_utilities.get_gdal_translate_path()
+ " /vsistdin/ tmp/vsistdin_1_out.tif -q < tmp/vsistdin_1_src.tif"
)
gdal.Unlink("tmp/vsistdin_1_src.tif")
@ -61,6 +64,7 @@ def test_vsistdin_1():
gdal.Unlink("tmp/vsistdin_1_out.tif")
###############################################################################
# Test on a bigger file (> 1 MB)
@ -69,11 +73,14 @@ def test_vsistdin_2():
if test_cli_utilities.get_gdal_translate_path() is None:
pytest.skip()
ds = gdal.GetDriverByName('GTiff').Create('tmp/vsistdin_2_src.tif', 2048, 2048)
ds = gdal.GetDriverByName("GTiff").Create("tmp/vsistdin_2_src.tif", 2048, 2048)
ds = None
# Should work on both Unix and Windows
os.system(test_cli_utilities.get_gdal_translate_path() + " /vsistdin/ tmp/vsistdin_2_out.tif -q < tmp/vsistdin_2_src.tif")
os.system(
test_cli_utilities.get_gdal_translate_path()
+ " /vsistdin/ tmp/vsistdin_2_out.tif -q < tmp/vsistdin_2_src.tif"
)
gdal.Unlink("tmp/vsistdin_2_src.tif")
@ -83,17 +90,19 @@ def test_vsistdin_2():
gdal.Unlink("tmp/vsistdin_2_out.tif")
###############################################################################
# Test opening /vsistdin/ in write mode (failure expected)
def test_vsistdin_3():
gdal.PushErrorHandler('CPLQuietErrorHandler')
f = gdal.VSIFOpenL('/vsistdin/', 'wb')
gdal.PushErrorHandler("CPLQuietErrorHandler")
f = gdal.VSIFOpenL("/vsistdin/", "wb")
gdal.PopErrorHandler()
assert f is None
###############################################################################
# Test fix for #6061
@ -102,8 +111,9 @@ def test_vsistdin_4():
if test_cli_utilities.get_gdal_translate_path() is None:
pytest.skip()
f = open('tmp/vsistdin_4_src.vrt', 'wt')
f.write("""<VRTDataset rasterXSize="20" rasterYSize="20">
f = open("tmp/vsistdin_4_src.vrt", "wt")
f.write(
"""<VRTDataset rasterXSize="20" rasterYSize="20">
%s
<SRS>PROJCS["NAD27 / UTM zone 11N",GEOGCS["NAD27",DATUM["North_American_Datum_1927",SPHEROID["Clarke 1866",6378206.4,294.9786982138982,AUTHORITY["EPSG","7008"]],AUTHORITY["EPSG","6267"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4267"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-117],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","26711"]]</SRS>
<GeoTransform> 4.4072000000000000e+05, 6.0000000000000000e+01, 0.0000000000000000e+00, 3.7513200000000000e+06, 0.0000000000000000e+00, -6.0000000000000000e+01</GeoTransform>
@ -114,11 +124,16 @@ def test_vsistdin_4():
<SourceBand>1</SourceBand>
</SimpleSource>
</VRTRasterBand>
</VRTDataset>""" % (' ' * (2 * 1024 * 1024)))
</VRTDataset>"""
% (" " * (2 * 1024 * 1024))
)
f.close()
# Should work on both Unix and Windows
os.system(test_cli_utilities.get_gdal_translate_path() + " /vsistdin/ tmp/vsistdin_4_out.tif -q < tmp/vsistdin_4_src.vrt")
os.system(
test_cli_utilities.get_gdal_translate_path()
+ " /vsistdin/ tmp/vsistdin_4_out.tif -q < tmp/vsistdin_4_src.vrt"
)
gdal.Unlink("tmp/vsistdin_4_src.vrt")
@ -134,71 +149,87 @@ def test_vsistdin_4():
def test_vsistdin_5():
f = open('tmp/test_vsistdin_5.bin', 'wb')
f.write(b'0123456789' * (1024 * 1024))
f = open("tmp/test_vsistdin_5.bin", "wb")
f.write(b"0123456789" * (1024 * 1024))
f.close()
with gdaltest.config_options({'CPL_VSISTDIN_FILE': 'tmp/test_vsistdin_5.bin',
'CPL_VSISTDIN_RESET_POSITION': 'YES',
'CPL_VSISTDIN_FILE_CLOSE': 'YES'}):
f = gdal.VSIFOpenL('/vsistdin?buffer_limit=10MB', 'rb')
with gdaltest.config_options(
{
"CPL_VSISTDIN_FILE": "tmp/test_vsistdin_5.bin",
"CPL_VSISTDIN_RESET_POSITION": "YES",
"CPL_VSISTDIN_FILE_CLOSE": "YES",
}
):
f = gdal.VSIFOpenL("/vsistdin?buffer_limit=10MB", "rb")
assert f is not None
assert gdal.VSIFEofL(f) == 0
assert gdal.VSIFReadL(10, 1, f) == b'0123456789'
assert gdal.VSIFReadL(10, 1, f) == b"0123456789"
assert gdal.VSIFTellL(f) == 10
assert gdal.VSIFSeekL(f, 5, 0) == 0
assert gdal.VSIFTellL(f) == 5
assert gdal.VSIFReadL(3, 1, f) == b'567'
assert gdal.VSIFReadL(3, 1, f) == b"567"
assert gdal.VSIFTellL(f) == 8
assert gdal.VSIFReadL(4, 1, f) == b'8901'
assert gdal.VSIFReadL(4, 1, f) == b"8901"
assert gdal.VSIFTellL(f) == 12
assert gdal.VSIFSeekL(f, 0, 2) == 0
assert gdal.VSIFTellL(f) == 10*1024 * 1024
assert gdal.VSIFReadL(1, 1, f) == b''
assert gdal.VSIFTellL(f) == 10 * 1024 * 1024
assert gdal.VSIFReadL(1, 1, f) == b""
assert gdal.VSIFEofL(f) == 1
assert gdal.VSIFTellL(f) == 10*1024 * 1024
assert gdal.VSIFTellL(f) == 10 * 1024 * 1024
assert gdal.VSIFSeekL(f, 5, 0) == 0
assert gdal.VSIFTellL(f) == 5
assert gdal.VSIFReadL(3, 1, f) == b'567'
assert gdal.VSIFReadL(3, 1, f) == b"567"
assert gdal.VSIFTellL(f) == 8
assert gdal.VSIFSeekL(f, 10*1024 * 1024 - 10 + 1, 0) == 0
assert gdal.VSIFReadL(3, 1, f) == b'123'
assert gdal.VSIFSeekL(f, 10 * 1024 * 1024 - 10 + 1, 0) == 0
assert gdal.VSIFReadL(3, 1, f) == b"123"
gdal.VSIFCloseL(f)
with gdaltest.config_options({'CPL_VSISTDIN_FILE': 'tmp/test_vsistdin_5.bin',
'CPL_VSISTDIN_RESET_POSITION': 'YES',
'CPL_VSISTDIN_FILE_CLOSE': 'YES'}):
f = gdal.VSIFOpenL('/vsistdin?buffer_limit=-1', 'rb')
with gdaltest.config_options(
{
"CPL_VSISTDIN_FILE": "tmp/test_vsistdin_5.bin",
"CPL_VSISTDIN_RESET_POSITION": "YES",
"CPL_VSISTDIN_FILE_CLOSE": "YES",
}
):
f = gdal.VSIFOpenL("/vsistdin?buffer_limit=-1", "rb")
assert f is not None
assert gdal.VSIFSeekL(f, 0, 2) == 0
assert gdal.VSIFTellL(f) == 10*1024 * 1024
assert gdal.VSIFTellL(f) == 10 * 1024 * 1024
gdal.VSIFCloseL(f)
assert gdal.VSIStatL('/vsistdin?buffer_limit=-1').size == 10*1024 * 1024
assert gdal.VSIStatL("/vsistdin?buffer_limit=-1").size == 10 * 1024 * 1024
with gdaltest.config_options({'CPL_VSISTDIN_FILE': 'tmp/test_vsistdin_5.bin',
'CPL_VSISTDIN_RESET_POSITION': 'YES',
'CPL_VSISTDIN_FILE_CLOSE': 'YES'}):
f = gdal.VSIFOpenL('/vsistdin?buffer_limit=1GB', 'rb')
with gdaltest.config_options(
{
"CPL_VSISTDIN_FILE": "tmp/test_vsistdin_5.bin",
"CPL_VSISTDIN_RESET_POSITION": "YES",
"CPL_VSISTDIN_FILE_CLOSE": "YES",
}
):
f = gdal.VSIFOpenL("/vsistdin?buffer_limit=1GB", "rb")
assert f is not None
assert gdal.VSIFSeekL(f, 0, 2) == 0
assert gdal.VSIFTellL(f) == 10*1024 * 1024
assert gdal.VSIFTellL(f) == 10 * 1024 * 1024
gdal.VSIFCloseL(f)
assert gdal.VSIStatL('/vsistdin?buffer_limit=-1').size == 10*1024 * 1024
assert gdal.VSIStatL("/vsistdin?buffer_limit=-1").size == 10 * 1024 * 1024
with gdaltest.config_options({'CPL_VSISTDIN_FILE': 'tmp/test_vsistdin_5.bin',
'CPL_VSISTDIN_RESET_POSITION': 'YES',
'CPL_VSISTDIN_FILE_CLOSE': 'YES'}):
f = gdal.VSIFOpenL('/vsistdin?buffer_limit=10', 'rb')
with gdaltest.config_options(
{
"CPL_VSISTDIN_FILE": "tmp/test_vsistdin_5.bin",
"CPL_VSISTDIN_RESET_POSITION": "YES",
"CPL_VSISTDIN_FILE_CLOSE": "YES",
}
):
f = gdal.VSIFOpenL("/vsistdin?buffer_limit=10", "rb")
assert f is not None
assert gdal.VSIFSeekL(f, 0, 2) == 0
assert gdal.VSIFTellL(f) == 10*1024 * 1024
assert gdal.VSIFTellL(f) == 10 * 1024 * 1024
assert gdal.VSIFSeekL(f, 0, 0) == 0
assert gdal.VSIFReadL(5, 1, f) == b'01234'
assert gdal.VSIFReadL(3, 1, f) == b'567'
assert gdal.VSIFReadL(5, 1, f) == b"01234"
assert gdal.VSIFReadL(3, 1, f) == b"567"
with gdaltest.error_handler():
assert gdal.VSIFReadL(3, 1, f) == b''
assert gdal.VSIFReadL(3, 1, f) == b""
gdal.VSIFCloseL(f)
os.unlink('tmp/test_vsistdin_5.bin')
os.unlink("tmp/test_vsistdin_5.bin")

File diff suppressed because it is too large Load Diff

View File

@ -30,19 +30,20 @@
import stat
import sys
from osgeo import gdal
import gdaltest
import webserver
import pytest
import webserver
from osgeo import gdal
def open_for_read(uri):
"""
Opens a test file for reading.
"""
return gdal.VSIFOpenExL(uri, 'rb', 1)
return gdal.VSIFOpenExL(uri, "rb", 1)
###############################################################################
@ -50,12 +51,12 @@ def open_for_read(uri):
def test_vsiwebhdfs_init():
gdaltest.webhdfs_vars = {}
for var in ('WEBHDFS_USERNAME', 'WEBHDFS_DELEGATION'):
for var in ("WEBHDFS_USERNAME", "WEBHDFS_DELEGATION"):
gdaltest.webhdfs_vars[var] = gdal.GetConfigOption(var)
if gdaltest.webhdfs_vars[var] is not None:
gdal.SetConfigOption(var, "")
###############################################################################
@ -68,14 +69,18 @@ def test_vsiwebhdfs_start_webserver():
pytest.skip()
(gdaltest.webserver_process, gdaltest.webserver_port) = webserver.launch(
handler=webserver.DispatcherHttpHandler)
handler=webserver.DispatcherHttpHandler
)
if gdaltest.webserver_port == 0:
pytest.skip()
gdaltest.webhdfs_base_connection = '/vsiwebhdfs/http://localhost:' + \
str(gdaltest.webserver_port) + '/webhdfs/v1'
gdaltest.webhdfs_redirected_url = 'http://non_existing_host:' + \
str(gdaltest.webserver_port) + '/redirected'
gdaltest.webhdfs_base_connection = (
"/vsiwebhdfs/http://localhost:" + str(gdaltest.webserver_port) + "/webhdfs/v1"
)
gdaltest.webhdfs_redirected_url = (
"http://non_existing_host:" + str(gdaltest.webserver_port) + "/redirected"
)
###############################################################################
# Test VSIFOpenL()
@ -90,13 +95,18 @@ def test_vsiwebhdfs_open():
# Download without redirect (not nominal)
handler = webserver.SequentialHandler()
handler.add('GET', '/webhdfs/v1/foo/bar?op=OPEN&offset=9999990784&length=16384', 200,
{}, '0123456789data')
handler.add(
"GET",
"/webhdfs/v1/foo/bar?op=OPEN&offset=9999990784&length=16384",
200,
{},
"0123456789data",
)
with webserver.install_http_handler(handler):
f = open_for_read(gdaltest.webhdfs_base_connection + '/foo/bar')
f = open_for_read(gdaltest.webhdfs_base_connection + "/foo/bar")
assert f is not None
gdal.VSIFSeekL(f, 9999990784 + 10, 0)
assert gdal.VSIFReadL(1, 4, f).decode('ascii') == 'data'
assert gdal.VSIFReadL(1, 4, f).decode("ascii") == "data"
gdal.VSIFCloseL(f)
# Download with redirect (nominal) and permissions
@ -104,28 +114,44 @@ def test_vsiwebhdfs_open():
gdal.VSICurlClearCache()
handler = webserver.SequentialHandler()
handler.add('GET', '/webhdfs/v1/foo/bar?op=OPEN&offset=0&length=16384&user.name=root&delegation=token', 307,
{'Location': gdaltest.webhdfs_redirected_url + '/webhdfs/v1/foo/bar?op=OPEN&offset=0&length=16384'})
handler.add('GET', '/redirected/webhdfs/v1/foo/bar?op=OPEN&offset=0&length=16384', 200,
{}, 'yeah')
with gdaltest.config_options({'WEBHDFS_USERNAME': 'root',
'WEBHDFS_DELEGATION': 'token',
'WEBHDFS_DATANODE_HOST': 'localhost'}):
handler.add(
"GET",
"/webhdfs/v1/foo/bar?op=OPEN&offset=0&length=16384&user.name=root&delegation=token",
307,
{
"Location": gdaltest.webhdfs_redirected_url
+ "/webhdfs/v1/foo/bar?op=OPEN&offset=0&length=16384"
},
)
handler.add(
"GET",
"/redirected/webhdfs/v1/foo/bar?op=OPEN&offset=0&length=16384",
200,
{},
"yeah",
)
with gdaltest.config_options(
{
"WEBHDFS_USERNAME": "root",
"WEBHDFS_DELEGATION": "token",
"WEBHDFS_DATANODE_HOST": "localhost",
}
):
with webserver.install_http_handler(handler):
f = open_for_read(gdaltest.webhdfs_base_connection + '/foo/bar')
f = open_for_read(gdaltest.webhdfs_base_connection + "/foo/bar")
assert f is not None
assert gdal.VSIFReadL(1, 4, f).decode('ascii') == 'yeah'
assert gdal.VSIFReadL(1, 4, f).decode("ascii") == "yeah"
gdal.VSIFCloseL(f)
# Test error
gdal.VSICurlClearCache()
f = open_for_read(gdaltest.webhdfs_base_connection + '/foo/bar')
f = open_for_read(gdaltest.webhdfs_base_connection + "/foo/bar")
assert f is not None
handler = webserver.SequentialHandler()
handler.add('GET', '/webhdfs/v1/foo/bar?op=OPEN&offset=0&length=16384', 404)
handler.add("GET", "/webhdfs/v1/foo/bar?op=OPEN&offset=0&length=16384", 404)
with webserver.install_http_handler(handler):
assert len(gdal.VSIFReadL(1, 4, f)) == 0
@ -134,6 +160,7 @@ def test_vsiwebhdfs_open():
gdal.VSIFCloseL(f)
###############################################################################
# Test VSIStatL()
@ -146,10 +173,15 @@ def test_vsiwebhdfs_stat():
gdal.VSICurlClearCache()
handler = webserver.SequentialHandler()
handler.add('GET', '/webhdfs/v1/foo/bar?op=GETFILESTATUS', 200,
{}, '{"FileStatus":{"type":"FILE","length":1000000}}')
handler.add(
"GET",
"/webhdfs/v1/foo/bar?op=GETFILESTATUS",
200,
{},
'{"FileStatus":{"type":"FILE","length":1000000}}',
)
with webserver.install_http_handler(handler):
stat_res = gdal.VSIStatL(gdaltest.webhdfs_base_connection + '/foo/bar')
stat_res = gdal.VSIStatL(gdaltest.webhdfs_base_connection + "/foo/bar")
if stat_res is None or stat_res.size != 1000000:
if stat_res is not None:
print(stat_res.size)
@ -158,18 +190,23 @@ def test_vsiwebhdfs_stat():
pytest.fail()
# Test caching
stat_res = gdal.VSIStatL(gdaltest.webhdfs_base_connection + '/foo/bar')
stat_res = gdal.VSIStatL(gdaltest.webhdfs_base_connection + "/foo/bar")
assert stat_res.size == 1000000
# Test missing file
handler = webserver.SequentialHandler()
handler.add('GET', '/webhdfs/v1/unexisting?op=GETFILESTATUS', 404, {},
'{"RemoteException":{"exception":"FileNotFoundException","javaClassName":"java.io.FileNotFoundException","message":"File does not exist: /unexisting"}}')
handler.add(
"GET",
"/webhdfs/v1/unexisting?op=GETFILESTATUS",
404,
{},
'{"RemoteException":{"exception":"FileNotFoundException","javaClassName":"java.io.FileNotFoundException","message":"File does not exist: /unexisting"}}',
)
with webserver.install_http_handler(handler):
stat_res = gdal.VSIStatL(
gdaltest.webhdfs_base_connection + '/unexisting')
stat_res = gdal.VSIStatL(gdaltest.webhdfs_base_connection + "/unexisting")
assert stat_res is None
###############################################################################
# Test ReadDir()
@ -182,28 +219,34 @@ def test_vsiwebhdfs_readdir():
gdal.VSICurlClearCache()
handler = webserver.SequentialHandler()
handler.add('GET', '/webhdfs/v1/foo/?op=LISTSTATUS', 200,
{}, '{"FileStatuses":{"FileStatus":[{"type":"FILE","modificationTime":1000,"pathSuffix":"bar.baz","length":123456},{"type":"DIRECTORY","pathSuffix":"mysubdir","length":0}]}}')
handler.add(
"GET",
"/webhdfs/v1/foo/?op=LISTSTATUS",
200,
{},
'{"FileStatuses":{"FileStatus":[{"type":"FILE","modificationTime":1000,"pathSuffix":"bar.baz","length":123456},{"type":"DIRECTORY","pathSuffix":"mysubdir","length":0}]}}',
)
with webserver.install_http_handler(handler):
dir_contents = gdal.ReadDir(gdaltest.webhdfs_base_connection + '/foo')
assert dir_contents == ['bar.baz', 'mysubdir']
stat_res = gdal.VSIStatL(gdaltest.webhdfs_base_connection + '/foo/bar.baz')
dir_contents = gdal.ReadDir(gdaltest.webhdfs_base_connection + "/foo")
assert dir_contents == ["bar.baz", "mysubdir"]
stat_res = gdal.VSIStatL(gdaltest.webhdfs_base_connection + "/foo/bar.baz")
assert stat_res.size == 123456
assert stat_res.mtime == 1
# ReadDir on something known to be a file shouldn't cause network access
dir_contents = gdal.ReadDir(
gdaltest.webhdfs_base_connection + '/foo/bar.baz')
dir_contents = gdal.ReadDir(gdaltest.webhdfs_base_connection + "/foo/bar.baz")
assert dir_contents is None
# Test error on ReadDir()
handler = webserver.SequentialHandler()
handler.add('GET', '/webhdfs/v1foo/error_test/?op=LISTSTATUS', 404)
handler.add("GET", "/webhdfs/v1foo/error_test/?op=LISTSTATUS", 404)
with webserver.install_http_handler(handler):
dir_contents = gdal.ReadDir(
gdaltest.webhdfs_base_connection + 'foo/error_test/')
gdaltest.webhdfs_base_connection + "foo/error_test/"
)
assert dir_contents is None
###############################################################################
# Test write
@ -220,20 +263,30 @@ def test_vsiwebhdfs_write():
with webserver.install_http_handler(handler):
# Missing required config options
with gdaltest.error_handler():
f = gdal.VSIFOpenL(
gdaltest.webhdfs_base_connection + '/foo/bar', 'wb')
f = gdal.VSIFOpenL(gdaltest.webhdfs_base_connection + "/foo/bar", "wb")
assert f is None
handler = webserver.SequentialHandler()
handler.add('PUT', '/webhdfs/v1/foo/bar?op=CREATE&overwrite=true&user.name=root', 307,
{'Location': gdaltest.webhdfs_redirected_url + '/webhdfs/v1/foo/bar?op=CREATE&overwrite=true&user.name=root'}, )
handler.add(
'PUT', '/redirected/webhdfs/v1/foo/bar?op=CREATE&overwrite=true&user.name=root', 201)
"PUT",
"/webhdfs/v1/foo/bar?op=CREATE&overwrite=true&user.name=root",
307,
{
"Location": gdaltest.webhdfs_redirected_url
+ "/webhdfs/v1/foo/bar?op=CREATE&overwrite=true&user.name=root"
},
)
handler.add(
"PUT",
"/redirected/webhdfs/v1/foo/bar?op=CREATE&overwrite=true&user.name=root",
201,
)
with gdaltest.config_options({'WEBHDFS_USERNAME': 'root', 'WEBHDFS_DATANODE_HOST': 'localhost'}):
with gdaltest.config_options(
{"WEBHDFS_USERNAME": "root", "WEBHDFS_DATANODE_HOST": "localhost"}
):
with webserver.install_http_handler(handler):
f = gdal.VSIFOpenL(
gdaltest.webhdfs_base_connection + '/foo/bar', 'wb')
f = gdal.VSIFOpenL(gdaltest.webhdfs_base_connection + "/foo/bar", "wb")
assert f is not None
assert gdal.VSIFCloseL(f) == 0
@ -242,39 +295,61 @@ def test_vsiwebhdfs_write():
gdal.VSICurlClearCache()
handler = webserver.SequentialHandler()
handler.add('PUT', '/webhdfs/v1/foo/bar?op=CREATE&overwrite=true&user.name=root', 307,
{'Location': gdaltest.webhdfs_redirected_url + '/webhdfs/v1/foo/bar?op=CREATE&overwrite=true&user.name=root'}, )
handler.add(
'PUT', '/redirected/webhdfs/v1/foo/bar?op=CREATE&overwrite=true&user.name=root', 201)
"PUT",
"/webhdfs/v1/foo/bar?op=CREATE&overwrite=true&user.name=root",
307,
{
"Location": gdaltest.webhdfs_redirected_url
+ "/webhdfs/v1/foo/bar?op=CREATE&overwrite=true&user.name=root"
},
)
handler.add(
"PUT",
"/redirected/webhdfs/v1/foo/bar?op=CREATE&overwrite=true&user.name=root",
201,
)
with gdaltest.config_options({'WEBHDFS_USERNAME': 'root', 'WEBHDFS_DATANODE_HOST': 'localhost'}):
with gdaltest.config_options(
{"WEBHDFS_USERNAME": "root", "WEBHDFS_DATANODE_HOST": "localhost"}
):
with webserver.install_http_handler(handler):
f = gdal.VSIFOpenL(
gdaltest.webhdfs_base_connection + '/foo/bar', 'wb')
f = gdal.VSIFOpenL(gdaltest.webhdfs_base_connection + "/foo/bar", "wb")
assert f is not None
assert gdal.VSIFWriteL('foobar', 1, 6, f) == 6
assert gdal.VSIFWriteL("foobar", 1, 6, f) == 6
handler = webserver.SequentialHandler()
def method(request):
h = request.headers
if 'Content-Length' in h and h['Content-Length'] != 0:
sys.stderr.write('Bad headers: %s\n' % str(request.headers))
if "Content-Length" in h and h["Content-Length"] != 0:
sys.stderr.write("Bad headers: %s\n" % str(request.headers))
request.send_response(403)
request.send_header('Content-Length', 0)
request.send_header("Content-Length", 0)
request.end_headers()
request.protocol_version = 'HTTP/1.1'
request.protocol_version = "HTTP/1.1"
request.send_response(307)
request.send_header('Location', gdaltest.webhdfs_redirected_url +
'/webhdfs/v1/foo/bar?op=APPEND&user.name=root')
request.send_header(
"Location",
gdaltest.webhdfs_redirected_url
+ "/webhdfs/v1/foo/bar?op=APPEND&user.name=root",
)
request.end_headers()
handler.add('POST', '/webhdfs/v1/foo/bar?op=APPEND&user.name=root',
307, custom_method=method)
handler.add('POST', '/redirected/webhdfs/v1/foo/bar?op=APPEND&user.name=root',
200, expected_body='foobar'.encode('ascii'))
handler.add(
"POST",
"/webhdfs/v1/foo/bar?op=APPEND&user.name=root",
307,
custom_method=method,
)
handler.add(
"POST",
"/redirected/webhdfs/v1/foo/bar?op=APPEND&user.name=root",
200,
expected_body="foobar".encode("ascii"),
)
with webserver.install_http_handler(handler):
assert gdal.VSIFCloseL(f) == 0
@ -285,22 +360,30 @@ def test_vsiwebhdfs_write():
handler = webserver.SequentialHandler()
handler.add(
'PUT', '/webhdfs/v1/foo/bar?op=CREATE&overwrite=true&user.name=root', 404)
with gdaltest.config_options({'WEBHDFS_USERNAME': 'root', 'WEBHDFS_DATANODE_HOST': 'localhost'}):
"PUT", "/webhdfs/v1/foo/bar?op=CREATE&overwrite=true&user.name=root", 404
)
with gdaltest.config_options(
{"WEBHDFS_USERNAME": "root", "WEBHDFS_DATANODE_HOST": "localhost"}
):
with webserver.install_http_handler(handler):
with gdaltest.error_handler():
f = gdal.VSIFOpenL(
gdaltest.webhdfs_base_connection + '/foo/bar', 'wb')
f = gdal.VSIFOpenL(gdaltest.webhdfs_base_connection + "/foo/bar", "wb")
assert f is None
handler = webserver.SequentialHandler()
handler.add('PUT', '/webhdfs/v1/foo/bar?op=CREATE&overwrite=true&user.name=root', 307,
{'Location': gdaltest.webhdfs_redirected_url + '/webhdfs/v1/foo/bar?op=CREATE&overwrite=true&user.name=root'}, )
with gdaltest.config_options({'WEBHDFS_USERNAME': 'root'}):
handler.add(
"PUT",
"/webhdfs/v1/foo/bar?op=CREATE&overwrite=true&user.name=root",
307,
{
"Location": gdaltest.webhdfs_redirected_url
+ "/webhdfs/v1/foo/bar?op=CREATE&overwrite=true&user.name=root"
},
)
with gdaltest.config_options({"WEBHDFS_USERNAME": "root"}):
with webserver.install_http_handler(handler):
with gdaltest.error_handler():
f = gdal.VSIFOpenL(
gdaltest.webhdfs_base_connection + '/foo/bar', 'wb')
f = gdal.VSIFOpenL(gdaltest.webhdfs_base_connection + "/foo/bar", "wb")
assert f is None
# Errors during POST
@ -308,30 +391,47 @@ def test_vsiwebhdfs_write():
gdal.VSICurlClearCache()
handler = webserver.SequentialHandler()
handler.add('PUT', '/webhdfs/v1/foo/bar?op=CREATE&overwrite=true&user.name=root', 307,
{'Location': gdaltest.webhdfs_redirected_url + '/webhdfs/v1/foo/bar?op=CREATE&overwrite=true&user.name=root'}, )
handler.add(
'PUT', '/redirected/webhdfs/v1/foo/bar?op=CREATE&overwrite=true&user.name=root', 201)
"PUT",
"/webhdfs/v1/foo/bar?op=CREATE&overwrite=true&user.name=root",
307,
{
"Location": gdaltest.webhdfs_redirected_url
+ "/webhdfs/v1/foo/bar?op=CREATE&overwrite=true&user.name=root"
},
)
handler.add(
"PUT",
"/redirected/webhdfs/v1/foo/bar?op=CREATE&overwrite=true&user.name=root",
201,
)
with gdaltest.config_options({'WEBHDFS_USERNAME': 'root', 'WEBHDFS_DATANODE_HOST': 'localhost'}):
with gdaltest.config_options(
{"WEBHDFS_USERNAME": "root", "WEBHDFS_DATANODE_HOST": "localhost"}
):
with webserver.install_http_handler(handler):
f = gdal.VSIFOpenL(
gdaltest.webhdfs_base_connection + '/foo/bar', 'wb')
f = gdal.VSIFOpenL(gdaltest.webhdfs_base_connection + "/foo/bar", "wb")
assert f is not None
assert gdal.VSIFWriteL('foobar', 1, 6, f) == 6
assert gdal.VSIFWriteL("foobar", 1, 6, f) == 6
handler = webserver.SequentialHandler()
handler.add('POST', '/webhdfs/v1/foo/bar?op=APPEND&user.name=root', 307,
{'Location': gdaltest.webhdfs_redirected_url + '/webhdfs/v1/foo/bar?op=APPEND&user.name=root'})
handler.add(
'POST', '/redirected/webhdfs/v1/foo/bar?op=APPEND&user.name=root', 400)
"POST",
"/webhdfs/v1/foo/bar?op=APPEND&user.name=root",
307,
{
"Location": gdaltest.webhdfs_redirected_url
+ "/webhdfs/v1/foo/bar?op=APPEND&user.name=root"
},
)
handler.add("POST", "/redirected/webhdfs/v1/foo/bar?op=APPEND&user.name=root", 400)
with gdaltest.error_handler():
with webserver.install_http_handler(handler):
assert gdal.VSIFCloseL(f) != 0
###############################################################################
# Test Unlink()
@ -345,48 +445,51 @@ def test_vsiwebhdfs_unlink():
# Success
handler = webserver.SequentialHandler()
handler.add('DELETE', '/webhdfs/v1/foo/bar?op=DELETE', 200,
{}, '{"boolean":true}')
handler.add("DELETE", "/webhdfs/v1/foo/bar?op=DELETE", 200, {}, '{"boolean":true}')
with webserver.install_http_handler(handler):
ret = gdal.Unlink(gdaltest.webhdfs_base_connection + '/foo/bar')
ret = gdal.Unlink(gdaltest.webhdfs_base_connection + "/foo/bar")
assert ret == 0
gdal.VSICurlClearCache()
# With permissions
gdal.VSICurlClearCache()
handler = webserver.SequentialHandler()
handler.add('DELETE', '/webhdfs/v1/foo/bar?op=DELETE&user.name=root&delegation=token', 200,
{}, '{"boolean":true}')
with gdaltest.config_options({'WEBHDFS_USERNAME': 'root',
'WEBHDFS_DELEGATION': 'token'}):
handler.add(
"DELETE",
"/webhdfs/v1/foo/bar?op=DELETE&user.name=root&delegation=token",
200,
{},
'{"boolean":true}',
)
with gdaltest.config_options(
{"WEBHDFS_USERNAME": "root", "WEBHDFS_DELEGATION": "token"}
):
with webserver.install_http_handler(handler):
ret = gdal.Unlink(gdaltest.webhdfs_base_connection + '/foo/bar')
ret = gdal.Unlink(gdaltest.webhdfs_base_connection + "/foo/bar")
assert ret == 0
# Failure
handler = webserver.SequentialHandler()
handler.add('DELETE', '/webhdfs/v1/foo/bar?op=DELETE', 200,
{}, '{"boolean":false}')
handler.add("DELETE", "/webhdfs/v1/foo/bar?op=DELETE", 200, {}, '{"boolean":false}')
with webserver.install_http_handler(handler):
with gdaltest.error_handler():
ret = gdal.Unlink(gdaltest.webhdfs_base_connection + '/foo/bar')
ret = gdal.Unlink(gdaltest.webhdfs_base_connection + "/foo/bar")
assert ret == -1
gdal.VSICurlClearCache()
# Failure
handler = webserver.SequentialHandler()
handler.add('DELETE', '/webhdfs/v1/foo/bar?op=DELETE', 404,
{})
handler.add("DELETE", "/webhdfs/v1/foo/bar?op=DELETE", 404, {})
with webserver.install_http_handler(handler):
with gdaltest.error_handler():
ret = gdal.Unlink(gdaltest.webhdfs_base_connection + '/foo/bar')
ret = gdal.Unlink(gdaltest.webhdfs_base_connection + "/foo/bar")
assert ret == -1
###############################################################################
# Test Mkdir() / Rmdir()
@ -399,60 +502,66 @@ def test_vsiwebhdfs_mkdir_rmdir():
gdal.VSICurlClearCache()
# Invalid name
ret = gdal.Mkdir('/vsiwebhdfs', 0)
ret = gdal.Mkdir("/vsiwebhdfs", 0)
assert ret != 0
# Valid
handler = webserver.SequentialHandler()
handler.add('PUT', '/webhdfs/v1/foo/dir?op=MKDIRS', 200,
{}, '{"boolean":true}')
handler.add("PUT", "/webhdfs/v1/foo/dir?op=MKDIRS", 200, {}, '{"boolean":true}')
with webserver.install_http_handler(handler):
ret = gdal.Mkdir(gdaltest.webhdfs_base_connection + '/foo/dir', 0)
ret = gdal.Mkdir(gdaltest.webhdfs_base_connection + "/foo/dir", 0)
assert ret == 0
# Valid with all options
handler = webserver.SequentialHandler()
handler.add('PUT', '/webhdfs/v1/foo/dir?op=MKDIRS&user.name=root&delegation=token&permission=755', 200,
{}, '{"boolean":true}')
with gdaltest.config_options({'WEBHDFS_USERNAME': 'root', 'WEBHDFS_DELEGATION': 'token'}):
handler.add(
"PUT",
"/webhdfs/v1/foo/dir?op=MKDIRS&user.name=root&delegation=token&permission=755",
200,
{},
'{"boolean":true}',
)
with gdaltest.config_options(
{"WEBHDFS_USERNAME": "root", "WEBHDFS_DELEGATION": "token"}
):
with webserver.install_http_handler(handler):
ret = gdal.Mkdir(gdaltest.webhdfs_base_connection +
'/foo/dir/', 493) # 0755
ret = gdal.Mkdir(
gdaltest.webhdfs_base_connection + "/foo/dir/", 493
) # 0755
assert ret == 0
# Error
handler = webserver.SequentialHandler()
handler.add('PUT', '/webhdfs/v1/foo/dir_error?op=MKDIRS', 404)
handler.add("PUT", "/webhdfs/v1/foo/dir_error?op=MKDIRS", 404)
with webserver.install_http_handler(handler):
ret = gdal.Mkdir(gdaltest.webhdfs_base_connection +
'/foo/dir_error', 0)
ret = gdal.Mkdir(gdaltest.webhdfs_base_connection + "/foo/dir_error", 0)
assert ret != 0
# Root name is invalid
ret = gdal.Mkdir(gdaltest.webhdfs_base_connection + '/', 0)
ret = gdal.Mkdir(gdaltest.webhdfs_base_connection + "/", 0)
assert ret != 0
# Invalid name
ret = gdal.Rmdir('/vsiwebhdfs')
ret = gdal.Rmdir("/vsiwebhdfs")
assert ret != 0
gdal.VSICurlClearCache()
# Valid
handler = webserver.SequentialHandler()
handler.add('DELETE', '/webhdfs/v1/foo/dir?op=DELETE', 200,
{}, '{"boolean":true}')
handler.add("DELETE", "/webhdfs/v1/foo/dir?op=DELETE", 200, {}, '{"boolean":true}')
with webserver.install_http_handler(handler):
ret = gdal.Rmdir(gdaltest.webhdfs_base_connection + '/foo/dir')
ret = gdal.Rmdir(gdaltest.webhdfs_base_connection + "/foo/dir")
assert ret == 0
# Error
handler = webserver.SequentialHandler()
handler.add('DELETE', '/webhdfs/v1/foo/dir_error?op=DELETE', 404)
handler.add("DELETE", "/webhdfs/v1/foo/dir_error?op=DELETE", 404)
with webserver.install_http_handler(handler):
ret = gdal.Rmdir(gdaltest.webhdfs_base_connection + '/foo/dir_error')
ret = gdal.Rmdir(gdaltest.webhdfs_base_connection + "/foo/dir_error")
assert ret != 0
###############################################################################
@ -467,6 +576,7 @@ def test_vsiwebhdfs_stop_webserver():
webserver.server_stop(gdaltest.webserver_process, gdaltest.webserver_port)
###############################################################################
# Nominal cases (require valid credentials)
@ -476,83 +586,89 @@ def test_vsiwebhdfs_extra_1():
if not gdaltest.built_against_curl():
pytest.skip()
webhdfs_url = gdal.GetConfigOption('WEBHDFS_URL')
webhdfs_url = gdal.GetConfigOption("WEBHDFS_URL")
if webhdfs_url is None:
pytest.skip('Missing WEBHDFS_URL')
pytest.skip("Missing WEBHDFS_URL")
if webhdfs_url.endswith('/webhdfs/v1') or webhdfs_url.endswith('/webhdfs/v1/'):
path = '/vsiwebhdfs/' + webhdfs_url
if webhdfs_url.endswith("/webhdfs/v1") or webhdfs_url.endswith("/webhdfs/v1/"):
path = "/vsiwebhdfs/" + webhdfs_url
statres = gdal.VSIStatL(path)
assert statres is not None and stat.S_ISDIR(statres.mode), \
('%s is not a valid bucket' % path)
assert statres is not None and stat.S_ISDIR(statres.mode), (
"%s is not a valid bucket" % path
)
readdir = gdal.ReadDir(path)
assert readdir is not None, 'ReadDir() should not return empty list'
assert readdir is not None, "ReadDir() should not return empty list"
for filename in readdir:
if filename != '.':
subpath = path + '/' + filename
assert gdal.VSIStatL(subpath) is not None, \
('Stat(%s) should not return an error' % subpath)
if filename != ".":
subpath = path + "/" + filename
assert gdal.VSIStatL(subpath) is not None, (
"Stat(%s) should not return an error" % subpath
)
unique_id = 'vsiwebhdfs_test'
subpath = path + '/' + unique_id
unique_id = "vsiwebhdfs_test"
subpath = path + "/" + unique_id
ret = gdal.Mkdir(subpath, 0)
assert ret >= 0, ('Mkdir(%s) should not return an error' % subpath)
assert ret >= 0, "Mkdir(%s) should not return an error" % subpath
readdir = gdal.ReadDir(path)
assert unique_id in readdir, \
('ReadDir(%s) should contain %s' % (path, unique_id))
assert unique_id in readdir, "ReadDir(%s) should contain %s" % (path, unique_id)
#ret = gdal.Mkdir(subpath, 0)
# ret = gdal.Mkdir(subpath, 0)
# if ret == 0:
# gdaltest.post_reason('fail')
# print('Mkdir(%s) repeated should return an error' % subpath)
# return 'fail'
ret = gdal.Rmdir(subpath)
assert ret >= 0, ('Rmdir(%s) should not return an error' % subpath)
assert ret >= 0, "Rmdir(%s) should not return an error" % subpath
readdir = gdal.ReadDir(path)
assert unique_id not in readdir, \
('ReadDir(%s) should not contain %s' % (path, unique_id))
assert unique_id not in readdir, "ReadDir(%s) should not contain %s" % (
path,
unique_id,
)
ret = gdal.Rmdir(subpath)
assert ret != 0, ('Rmdir(%s) repeated should return an error' % subpath)
assert ret != 0, "Rmdir(%s) repeated should return an error" % subpath
ret = gdal.Mkdir(subpath, 0)
assert ret >= 0, ('Mkdir(%s) should not return an error' % subpath)
assert ret >= 0, "Mkdir(%s) should not return an error" % subpath
f = gdal.VSIFOpenL(subpath + '/test.txt', 'wb')
f = gdal.VSIFOpenL(subpath + "/test.txt", "wb")
assert f is not None
gdal.VSIFWriteL('hello', 1, 5, f)
gdal.VSIFWriteL("hello", 1, 5, f)
gdal.VSIFCloseL(f)
ret = gdal.Rmdir(subpath)
assert ret != 0, \
('Rmdir(%s) on non empty directory should return an error' % subpath)
assert ret != 0, (
"Rmdir(%s) on non empty directory should return an error" % subpath
)
f = gdal.VSIFOpenL(subpath + '/test.txt', 'rb')
f = gdal.VSIFOpenL(subpath + "/test.txt", "rb")
assert f is not None
data = gdal.VSIFReadL(1, 5, f).decode('utf-8')
assert data == 'hello'
data = gdal.VSIFReadL(1, 5, f).decode("utf-8")
assert data == "hello"
gdal.VSIFCloseL(f)
ret = gdal.Unlink(subpath + '/test.txt')
assert ret >= 0, ('Unlink(%s) should not return an error' %
(subpath + '/test.txt'))
ret = gdal.Unlink(subpath + "/test.txt")
assert ret >= 0, "Unlink(%s) should not return an error" % (
subpath + "/test.txt"
)
ret = gdal.Rmdir(subpath)
assert ret >= 0, ('Rmdir(%s) should not return an error' % subpath)
assert ret >= 0, "Rmdir(%s) should not return an error" % subpath
return
f = open_for_read('/vsiwebhdfs/' + webhdfs_url)
f = open_for_read("/vsiwebhdfs/" + webhdfs_url)
assert f is not None
ret = gdal.VSIFReadL(1, 1, f)
gdal.VSIFCloseL(f)
assert len(ret) == 1
###############################################################################

View File

@ -31,11 +31,11 @@
import random
import gdaltest
from osgeo import gdal
import pytest
from osgeo import gdal
###############################################################################
# Test writing a ZIP with multiple files and directories
@ -44,11 +44,11 @@ def test_vsizip_1():
# We can keep the handle open during all the ZIP writing
hZIP = gdal.VSIFOpenL("/vsizip/vsimem/test.zip", "wb")
assert hZIP is not None, 'fail 1'
assert hZIP is not None, "fail 1"
# One way to create a directory
f = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/subdir2/", "wb")
assert f is not None, 'fail 2'
assert f is not None, "fail 2"
gdal.VSIFCloseL(f)
# A more natural one
@ -56,32 +56,35 @@ def test_vsizip_1():
# Create 1st file
f2 = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/subdir3/abcd", "wb")
assert f2 is not None, 'fail 3'
assert f2 is not None, "fail 3"
gdal.VSIFWriteL("abcd", 1, 4, f2)
gdal.VSIFCloseL(f2)
# Test that we cannot read a zip file being written
gdal.ErrorReset()
gdal.PushErrorHandler('CPLQuietErrorHandler')
gdal.PushErrorHandler("CPLQuietErrorHandler")
f = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/subdir3/abcd", "rb")
gdal.PopErrorHandler()
assert gdal.GetLastErrorMsg() == 'Cannot read a zip file being written', \
'expected error'
assert f is None, 'should not have been successful 1'
assert (
gdal.GetLastErrorMsg() == "Cannot read a zip file being written"
), "expected error"
assert f is None, "should not have been successful 1"
# Create 2nd file
f3 = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/subdir3/efghi", "wb")
assert f3 is not None, 'fail 4'
assert f3 is not None, "fail 4"
gdal.VSIFWriteL("efghi", 1, 5, f3)
# Try creating a 3d file
gdal.ErrorReset()
gdal.PushErrorHandler('CPLQuietErrorHandler')
gdal.PushErrorHandler("CPLQuietErrorHandler")
f4 = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/that_wont_work", "wb")
gdal.PopErrorHandler()
assert gdal.GetLastErrorMsg() == 'Cannot create that_wont_work while another file is being written in the .zip', \
'expected error'
assert f4 is None, 'should not have been successful 2'
assert (
gdal.GetLastErrorMsg()
== "Cannot create that_wont_work while another file is being written in the .zip"
), "expected error"
assert f4 is None, "should not have been successful 2"
gdal.VSIFCloseL(f3)
@ -91,17 +94,17 @@ def test_vsizip_1():
# ERROR 6: Support only 1 file in archive file /vsimem/test.zip when no explicit in-archive filename is specified
gdal.ErrorReset()
with gdaltest.error_handler():
f = gdal.VSIFOpenL('/vsizip/vsimem/test.zip', 'rb')
f = gdal.VSIFOpenL("/vsizip/vsimem/test.zip", "rb")
if f is not None:
gdal.VSIFCloseL(f)
assert gdal.GetLastErrorMsg() != '', 'expected error'
assert gdal.GetLastErrorMsg() != "", "expected error"
f = gdal.VSIFOpenL("/vsizip/vsimem/test.zip/subdir3/abcd", "rb")
assert f is not None, 'fail 5'
assert f is not None, "fail 5"
data = gdal.VSIFReadL(1, 4, f)
gdal.VSIFCloseL(f)
assert data.decode('ASCII') == 'abcd'
assert data.decode("ASCII") == "abcd"
# Test alternate uri syntax
gdal.Rename("/vsimem/test.zip", "/vsimem/test.xxx")
@ -110,7 +113,7 @@ def test_vsizip_1():
data = gdal.VSIFReadL(1, 4, f)
gdal.VSIFCloseL(f)
assert data.decode('ASCII') == 'abcd'
assert data.decode("ASCII") == "abcd"
# With a trailing slash
f = gdal.VSIFOpenL("/vsizip/{/vsimem/test.xxx}/subdir3/abcd/", "rb")
@ -138,7 +141,7 @@ def test_vsizip_1():
# Test nested { { } }
hZIP = gdal.VSIFOpenL("/vsizip/{/vsimem/zipinzip.yyy}", "wb")
assert hZIP is not None, 'fail 1'
assert hZIP is not None, "fail 1"
f = gdal.VSIFOpenL("/vsizip/{/vsimem/zipinzip.yyy}/test.xxx", "wb")
f_src = gdal.VSIFOpenL("/vsimem/test.xxx", "rb")
data = gdal.VSIFReadL(1, 10000, f_src)
@ -147,21 +150,24 @@ def test_vsizip_1():
gdal.VSIFCloseL(f)
gdal.VSIFCloseL(hZIP)
f = gdal.VSIFOpenL("/vsizip/{/vsizip/{/vsimem/zipinzip.yyy}/test.xxx}/subdir3/abcd/", "rb")
f = gdal.VSIFOpenL(
"/vsizip/{/vsizip/{/vsimem/zipinzip.yyy}/test.xxx}/subdir3/abcd/", "rb"
)
assert f is not None
data = gdal.VSIFReadL(1, 4, f)
gdal.VSIFCloseL(f)
assert data.decode('ASCII') == 'abcd'
assert data.decode("ASCII") == "abcd"
gdal.Unlink("/vsimem/test.xxx")
gdal.Unlink("/vsimem/zipinzip.yyy")
# Test VSIStatL on a non existing file
assert gdal.VSIStatL('/vsizip//vsimem/foo.zip') is None
assert gdal.VSIStatL("/vsizip//vsimem/foo.zip") is None
# Test ReadDir on a non existing file
assert gdal.ReadDir('/vsizip//vsimem/foo.zip') is None
assert gdal.ReadDir("/vsizip//vsimem/foo.zip") is None
###############################################################################
# Test writing 2 files in the ZIP by closing it completely between the 2
@ -169,47 +175,48 @@ def test_vsizip_1():
def test_vsizip_2():
zip_name = '/vsimem/test2.zip'
zip_name = "/vsimem/test2.zip"
fmain = gdal.VSIFOpenL("/vsizip/" + zip_name + "/foo.bar", "wb")
assert fmain is not None, 'fail 1'
assert fmain is not None, "fail 1"
gdal.VSIFWriteL("12345", 1, 5, fmain)
gdal.VSIFCloseL(fmain)
content = gdal.ReadDir("/vsizip/" + zip_name)
assert content == ['foo.bar'], 'bad content 1'
assert content == ["foo.bar"], "bad content 1"
# Now append a second file
fmain = gdal.VSIFOpenL("/vsizip/" + zip_name + "/bar.baz", "wb")
assert fmain is not None, 'fail 2'
assert fmain is not None, "fail 2"
gdal.VSIFWriteL("67890", 1, 5, fmain)
gdal.ErrorReset()
gdal.PushErrorHandler('CPLQuietErrorHandler')
gdal.PushErrorHandler("CPLQuietErrorHandler")
content = gdal.ReadDir("/vsizip/" + zip_name)
gdal.PopErrorHandler()
assert gdal.GetLastErrorMsg() == 'Cannot read a zip file being written', \
'expected error'
assert content is None, 'bad content 2'
assert (
gdal.GetLastErrorMsg() == "Cannot read a zip file being written"
), "expected error"
assert content is None, "bad content 2"
gdal.VSIFCloseL(fmain)
content = gdal.ReadDir("/vsizip/" + zip_name)
assert content == ['foo.bar', 'bar.baz'], 'bad content 3'
assert content == ["foo.bar", "bar.baz"], "bad content 3"
fmain = gdal.VSIFOpenL("/vsizip/" + zip_name + "/foo.bar", "rb")
assert fmain is not None, 'fail 3'
assert fmain is not None, "fail 3"
data = gdal.VSIFReadL(1, 5, fmain)
gdal.VSIFCloseL(fmain)
assert data.decode('ASCII') == '12345'
assert data.decode("ASCII") == "12345"
fmain = gdal.VSIFOpenL("/vsizip/" + zip_name + "/bar.baz", "rb")
assert fmain is not None, 'fail 4'
assert fmain is not None, "fail 4"
data = gdal.VSIFReadL(1, 5, fmain)
gdal.VSIFCloseL(fmain)
assert data.decode('ASCII') == '67890'
assert data.decode("ASCII") == "67890"
gdal.Unlink(zip_name)
@ -217,6 +224,7 @@ def test_vsizip_2():
###############################################################################
# Test opening in write mode a file inside a zip archive whose content has been listed before (testcase for fix of r22625)
def test_vsizip_3():
fmain = gdal.VSIFOpenL("/vsizip/vsimem/test3.zip", "wb")
@ -240,7 +248,8 @@ def test_vsizip_3():
gdal.Unlink("/vsimem/test3.zip")
assert res == ['foo', 'bar', 'baz']
assert res == ["foo", "bar", "baz"]
###############################################################################
# Test ReadRecursive on valid zip
@ -250,10 +259,17 @@ def test_vsizip_4():
# read recursive and validate content
res = gdal.ReadDirRecursive("/vsizip/data/testzip.zip")
assert res is not None, 'fail read'
assert (res == ['subdir/', 'subdir/subdir/', 'subdir/subdir/uint16.tif',
'subdir/subdir/test_rpc.txt', 'subdir/test_rpc.txt',
'test_rpc.txt', 'uint16.tif']), 'bad content'
assert res is not None, "fail read"
assert res == [
"subdir/",
"subdir/subdir/",
"subdir/subdir/uint16.tif",
"subdir/subdir/test_rpc.txt",
"subdir/test_rpc.txt",
"test_rpc.txt",
"uint16.tif",
], "bad content"
###############################################################################
# Test ReadRecursive on deep zip
@ -262,23 +278,24 @@ def test_vsizip_4():
def test_vsizip_5():
# make file in memory
fmain = gdal.VSIFOpenL('/vsizip/vsimem/bigdepthzip.zip', 'wb')
fmain = gdal.VSIFOpenL("/vsizip/vsimem/bigdepthzip.zip", "wb")
assert fmain is not None
filename = "a" + "/a" * 1000
finside = gdal.VSIFOpenL('/vsizip/vsimem/bigdepthzip.zip/' + filename, 'wb')
finside = gdal.VSIFOpenL("/vsizip/vsimem/bigdepthzip.zip/" + filename, "wb")
assert finside is not None
gdal.VSIFCloseL(finside)
gdal.VSIFCloseL(fmain)
# read recursive and validate content
res = gdal.ReadDirRecursive("/vsizip/vsimem/bigdepthzip.zip")
assert res is not None, 'fail read'
assert len(res) == 1001, ('wrong size: ' + str(len(res)))
assert res[10] == 'a/a/a/a/a/a/a/a/a/a/a/', ('bad content: ' + res[10])
assert res is not None, "fail read"
assert len(res) == 1001, "wrong size: " + str(len(res))
assert res[10] == "a/a/a/a/a/a/a/a/a/a/a/", "bad content: " + res[10]
gdal.Unlink("/vsimem/bigdepthzip.zip")
###############################################################################
# Test writing 2 files with same name in a ZIP (#4785)
@ -293,7 +310,7 @@ def test_vsizip_6():
gdal.VSIFCloseL(f)
f = None
gdal.PushErrorHandler('CPLQuietErrorHandler')
gdal.PushErrorHandler("CPLQuietErrorHandler")
f = gdal.VSIFOpenL("/vsizip/vsimem/test6.zip/foo.bar", "wb")
gdal.PopErrorHandler()
if f is not None:
@ -311,7 +328,7 @@ def test_vsizip_6():
gdal.VSIFCloseL(f)
f = None
gdal.PushErrorHandler('CPLQuietErrorHandler')
gdal.PushErrorHandler("CPLQuietErrorHandler")
f = gdal.VSIFOpenL("/vsizip/vsimem/test6.zip/foo.bar", "wb")
gdal.PopErrorHandler()
if f is not None:
@ -320,6 +337,7 @@ def test_vsizip_6():
gdal.Unlink("/vsimem/test6.zip")
###############################################################################
# Test that we use the extended field for UTF-8 filenames (#5361).
@ -329,25 +347,36 @@ def test_vsizip_7():
content = gdal.ReadDir("/vsizip/data/cp866_plus_utf8.zip")
ok = 0
try:
local_vars = {'content': content, 'ok': ok}
exec("if content == [u'\u0430\u0431\u0432\u0433\u0434\u0435', u'\u0436\u0437\u0438\u0439\u043a\u043b']: ok = 1", None, local_vars)
ok = local_vars['ok']
local_vars = {"content": content, "ok": ok}
exec(
"if content == [u'\u0430\u0431\u0432\u0433\u0434\u0435', u'\u0436\u0437\u0438\u0439\u043a\u043b']: ok = 1",
None,
local_vars,
)
ok = local_vars["ok"]
except:
if content == ['\u0430\u0431\u0432\u0433\u0434\u0435', '\u0436\u0437\u0438\u0439\u043a\u043b']:
if content == [
"\u0430\u0431\u0432\u0433\u0434\u0435",
"\u0436\u0437\u0438\u0439\u043a\u043b",
]:
ok = 1
if ok == 0:
print(content)
pytest.fail('bad content')
pytest.fail("bad content")
###############################################################################
# Basic test for ZIP64 support (5 GB file that compresses in less than 4 GB)
def test_vsizip_8():
assert gdal.VSIStatL('/vsizip/vsizip/data/zero.bin.zip.zip/zero.bin.zip').size == 5000 * 1000 * 1000 + 1
assert (
gdal.VSIStatL("/vsizip/vsizip/data/zero.bin.zip.zip/zero.bin.zip").size
== 5000 * 1000 * 1000 + 1
)
###############################################################################
# Basic test for ZIP64 support (5 GB file that is stored)
@ -355,20 +384,29 @@ def test_vsizip_8():
def test_vsizip_9():
assert gdal.VSIStatL('/vsizip//vsisparse/data/zero_stored.bin.xml.zip/zero.bin').size == 5000 * 1000 * 1000 + 1
assert (
gdal.VSIStatL("/vsizip//vsisparse/data/zero_stored.bin.xml.zip/zero.bin").size
== 5000 * 1000 * 1000 + 1
)
assert gdal.VSIStatL('/vsizip//vsisparse/data/zero_stored.bin.xml.zip/hello.txt').size == 6
assert (
gdal.VSIStatL("/vsizip//vsisparse/data/zero_stored.bin.xml.zip/hello.txt").size
== 6
)
f = gdal.VSIFOpenL('/vsizip//vsisparse/data/zero_stored.bin.xml.zip/zero.bin', 'rb')
f = gdal.VSIFOpenL("/vsizip//vsisparse/data/zero_stored.bin.xml.zip/zero.bin", "rb")
gdal.VSIFSeekL(f, 5000 * 1000 * 1000, 0)
data = gdal.VSIFReadL(1, 1, f)
gdal.VSIFCloseL(f)
assert data.decode('ascii') == '\x03'
assert data.decode("ascii") == "\x03"
f = gdal.VSIFOpenL('/vsizip//vsisparse/data/zero_stored.bin.xml.zip/hello.txt', 'rb')
f = gdal.VSIFOpenL(
"/vsizip//vsisparse/data/zero_stored.bin.xml.zip/hello.txt", "rb"
)
data = gdal.VSIFReadL(1, 6, f)
gdal.VSIFCloseL(f)
assert data.decode('ascii') == 'HELLO\n'
assert data.decode("ascii") == "HELLO\n"
###############################################################################
# Test that we recode filenames in ZIP (#5361)
@ -376,26 +414,33 @@ def test_vsizip_9():
def test_vsizip_10():
gdal.SetConfigOption('CPL_ZIP_ENCODING', 'CP866')
gdal.SetConfigOption("CPL_ZIP_ENCODING", "CP866")
content = gdal.ReadDir("/vsizip/data/cp866.zip")
gdal.SetConfigOption('CPL_ZIP_ENCODING', None)
gdal.SetConfigOption("CPL_ZIP_ENCODING", None)
ok = 0
try:
local_vars = {'content': content, 'ok': ok}
exec("if content == [u'\u0430\u0431\u0432\u0433\u0434\u0435', u'\u0436\u0437\u0438\u0439\u043a\u043b']: ok = 1", None, local_vars)
ok = local_vars['ok']
local_vars = {"content": content, "ok": ok}
exec(
"if content == [u'\u0430\u0431\u0432\u0433\u0434\u0435', u'\u0436\u0437\u0438\u0439\u043a\u043b']: ok = 1",
None,
local_vars,
)
ok = local_vars["ok"]
except:
if content == ['\u0430\u0431\u0432\u0433\u0434\u0435', '\u0436\u0437\u0438\u0439\u043a\u043b']:
if content == [
"\u0430\u0431\u0432\u0433\u0434\u0435",
"\u0436\u0437\u0438\u0439\u043a\u043b",
]:
ok = 1
if ok == 0:
if gdal.GetLastErrorMsg().find('Recode from CP866 to UTF-8 not supported') >= 0:
if gdal.GetLastErrorMsg().find("Recode from CP866 to UTF-8 not supported") >= 0:
pytest.skip()
print(content)
pytest.fail('bad content')
pytest.fail("bad content")
###############################################################################
# Test that we don't do anything with ZIP with filenames in UTF-8 already (#5361)
@ -405,18 +450,25 @@ def test_vsizip_11():
content = gdal.ReadDir("/vsizip/data/utf8.zip")
ok = 0
try:
local_vars = {'content': content, 'ok': ok}
exec("if content == [u'\u0430\u0431\u0432\u0433\u0434\u0435', u'\u0436\u0437\u0438\u0439\u043a\u043b']: ok = 1", None, local_vars)
ok = local_vars['ok']
local_vars = {"content": content, "ok": ok}
exec(
"if content == [u'\u0430\u0431\u0432\u0433\u0434\u0435', u'\u0436\u0437\u0438\u0439\u043a\u043b']: ok = 1",
None,
local_vars,
)
ok = local_vars["ok"]
except:
if content == ['\u0430\u0431\u0432\u0433\u0434\u0435', '\u0436\u0437\u0438\u0439\u043a\u043b']:
if content == [
"\u0430\u0431\u0432\u0433\u0434\u0435",
"\u0436\u0437\u0438\u0439\u043a\u043b",
]:
ok = 1
if ok == 0:
print(content)
pytest.fail('bad content')
pytest.fail("bad content")
###############################################################################
# Test changing the content of a zip file (#6005)
@ -425,45 +477,46 @@ def test_vsizip_12():
fmain = gdal.VSIFOpenL("/vsizip/vsimem/vsizip_12_src1.zip", "wb")
f = gdal.VSIFOpenL("/vsizip/vsimem/vsizip_12_src1.zip/foo.bar", "wb")
data = '0123456'
data = "0123456"
gdal.VSIFWriteL(data, 1, len(data), f)
gdal.VSIFCloseL(f)
gdal.VSIFCloseL(fmain)
fmain = gdal.VSIFOpenL("/vsizip/vsimem/vsizip_12_src2.zip", "wb")
f = gdal.VSIFOpenL("/vsizip/vsimem/vsizip_12_src2.zip/bar.baz", "wb")
data = '01234567'
data = "01234567"
gdal.VSIFWriteL(data, 1, len(data), f)
gdal.VSIFCloseL(f)
gdal.VSIFCloseL(fmain)
# Copy vsizip_12_src1 into vsizip_12
f = gdal.VSIFOpenL('/vsimem/vsizip_12_src1.zip', 'rb')
f = gdal.VSIFOpenL("/vsimem/vsizip_12_src1.zip", "rb")
data = gdal.VSIFReadL(1, 10000, f)
gdal.VSIFCloseL(f)
f = gdal.VSIFOpenL('/vsimem/vsizip_12.zip', 'wb')
f = gdal.VSIFOpenL("/vsimem/vsizip_12.zip", "wb")
gdal.VSIFWriteL(data, 1, len(data), f)
gdal.VSIFCloseL(f)
gdal.ReadDir('/vsizip/vsimem/vsizip_12.zip')
gdal.ReadDir("/vsizip/vsimem/vsizip_12.zip")
# Copy vsizip_12_src2 into vsizip_12
f = gdal.VSIFOpenL('/vsimem/vsizip_12_src2.zip', 'rb')
f = gdal.VSIFOpenL("/vsimem/vsizip_12_src2.zip", "rb")
data = gdal.VSIFReadL(1, 10000, f)
gdal.VSIFCloseL(f)
f = gdal.VSIFOpenL('/vsimem/vsizip_12.zip', 'wb')
f = gdal.VSIFOpenL("/vsimem/vsizip_12.zip", "wb")
gdal.VSIFWriteL(data, 1, len(data), f)
gdal.VSIFCloseL(f)
content = gdal.ReadDir('/vsizip/vsimem/vsizip_12.zip')
content = gdal.ReadDir("/vsizip/vsimem/vsizip_12.zip")
gdal.Unlink('/vsimem/vsizip_12_src1.zip')
gdal.Unlink('/vsimem/vsizip_12_src2.zip')
gdal.Unlink('/vsimem/vsizip_12.zip')
gdal.Unlink("/vsimem/vsizip_12_src1.zip")
gdal.Unlink("/vsimem/vsizip_12_src2.zip")
gdal.Unlink("/vsimem/vsizip_12.zip")
assert content == ["bar.baz"]
assert content == ['bar.baz']
###############################################################################
# Test ReadDir() truncation
@ -477,13 +530,14 @@ def test_vsizip_13():
gdal.VSIFCloseL(f)
gdal.VSIFCloseL(fmain)
lst = gdal.ReadDir('/vsizip/vsimem/vsizip_13.zip')
lst = gdal.ReadDir("/vsizip/vsimem/vsizip_13.zip")
assert len(lst) >= 4
# Test truncation
lst_truncated = gdal.ReadDir('/vsizip/vsimem/vsizip_13.zip', int(len(lst) / 2))
lst_truncated = gdal.ReadDir("/vsizip/vsimem/vsizip_13.zip", int(len(lst) / 2))
assert len(lst_truncated) > int(len(lst) / 2)
gdal.Unlink('/vsimem/vsizip_13.zip')
gdal.Unlink("/vsimem/vsizip_13.zip")
###############################################################################
# Test that we can recode filenames in ZIP when writing (#6631)
@ -491,29 +545,30 @@ def test_vsizip_13():
def test_vsizip_14():
fmain = gdal.VSIFOpenL('/vsizip//vsimem/vsizip_14.zip', 'wb')
fmain = gdal.VSIFOpenL("/vsizip//vsimem/vsizip_14.zip", "wb")
try:
x = ['']
x = [""]
exec("x[0] = u'\u0430\u0431\u0432\u0433\u0434\u0435'")
cp866_filename = x[0]
except:
cp866_filename = '\u0430\u0431\u0432\u0433\u0434\u0435'
cp866_filename = "\u0430\u0431\u0432\u0433\u0434\u0435"
with gdaltest.error_handler():
f = gdal.VSIFOpenL('/vsizip//vsimem/vsizip_14.zip/' + cp866_filename, 'wb')
f = gdal.VSIFOpenL("/vsizip//vsimem/vsizip_14.zip/" + cp866_filename, "wb")
if f is None:
gdal.VSIFCloseL(fmain)
gdal.Unlink('/vsimem/vsizip_14.zip')
gdal.Unlink("/vsimem/vsizip_14.zip")
pytest.skip()
gdal.VSIFWriteL('hello', 1, 5, f)
gdal.VSIFWriteL("hello", 1, 5, f)
gdal.VSIFCloseL(f)
gdal.VSIFCloseL(fmain)
content = gdal.ReadDir("/vsizip//vsimem/vsizip_14.zip")
assert content == [cp866_filename], 'bad content'
assert content == [cp866_filename], "bad content"
gdal.Unlink("/vsimem/vsizip_14.zip")
gdal.Unlink('/vsimem/vsizip_14.zip')
###############################################################################
# Test multithreaded compression
@ -521,29 +576,31 @@ def test_vsizip_14():
def test_vsizip_multi_thread():
with gdaltest.config_options({'GDAL_NUM_THREADS': 'ALL_CPUS',
'CPL_VSIL_DEFLATE_CHUNK_SIZE': '32K'}):
fmain = gdal.VSIFOpenL('/vsizip//vsimem/vsizip_multi_thread.zip', 'wb')
f = gdal.VSIFOpenL('/vsizip//vsimem/vsizip_multi_thread.zip/test', 'wb')
with gdaltest.config_options(
{"GDAL_NUM_THREADS": "ALL_CPUS", "CPL_VSIL_DEFLATE_CHUNK_SIZE": "32K"}
):
fmain = gdal.VSIFOpenL("/vsizip//vsimem/vsizip_multi_thread.zip", "wb")
f = gdal.VSIFOpenL("/vsizip//vsimem/vsizip_multi_thread.zip/test", "wb")
for i in range(100000):
gdal.VSIFWriteL('hello', 1, 5, f)
gdal.VSIFWriteL("hello", 1, 5, f)
gdal.VSIFCloseL(f)
gdal.VSIFCloseL(fmain)
f = gdal.VSIFOpenL('/vsizip//vsimem/vsizip_multi_thread.zip/test', 'rb')
data = gdal.VSIFReadL(100000, 5, f).decode('ascii')
f = gdal.VSIFOpenL("/vsizip//vsimem/vsizip_multi_thread.zip/test", "rb")
data = gdal.VSIFReadL(100000, 5, f).decode("ascii")
gdal.VSIFCloseL(f)
gdal.Unlink('/vsimem/vsizip_multi_thread.zip')
gdal.Unlink("/vsimem/vsizip_multi_thread.zip")
if data != 'hello' * 100000:
if data != "hello" * 100000:
for i in range(10000):
if data[i*5:i*5+5] != 'hello':
print(i*5, data[i*5:i*5+5], data[i*5-5:i*5+5-5])
if data[i * 5 : i * 5 + 5] != "hello":
print(i * 5, data[i * 5 : i * 5 + 5], data[i * 5 - 5 : i * 5 + 5 - 5])
break
pytest.fail()
###############################################################################
# Test multithreaded compression, with I/O error
@ -551,16 +608,22 @@ def test_vsizip_multi_thread():
def test_vsizip_multi_thread_error():
with gdaltest.error_handler():
with gdaltest.config_options({'GDAL_NUM_THREADS': 'ALL_CPUS',
'CPL_VSIL_DEFLATE_CHUNK_SIZE': '16K'}):
fmain = gdal.VSIFOpenL('/vsizip/{/vsimem/vsizip_multi_thread.zip||maxlength=1000}', 'wb')
f = gdal.VSIFOpenL('/vsizip/{/vsimem/vsizip_multi_thread.zip||maxlength=1000}/test', 'wb')
with gdaltest.config_options(
{"GDAL_NUM_THREADS": "ALL_CPUS", "CPL_VSIL_DEFLATE_CHUNK_SIZE": "16K"}
):
fmain = gdal.VSIFOpenL(
"/vsizip/{/vsimem/vsizip_multi_thread.zip||maxlength=1000}", "wb"
)
f = gdal.VSIFOpenL(
"/vsizip/{/vsimem/vsizip_multi_thread.zip||maxlength=1000}/test", "wb"
)
for i in range(100000):
gdal.VSIFWriteL('hello', 1, 5, f)
gdal.VSIFWriteL("hello", 1, 5, f)
gdal.VSIFCloseL(f)
gdal.VSIFCloseL(fmain)
gdal.Unlink('/vsimem/vsizip_multi_thread.zip')
gdal.Unlink("/vsimem/vsizip_multi_thread.zip")
###############################################################################
# Test multithreaded compression, below the threshold where it triggers
@ -568,20 +631,21 @@ def test_vsizip_multi_thread_error():
def test_vsizip_multi_thread_below_threshold():
with gdaltest.config_options({'GDAL_NUM_THREADS': 'ALL_CPUS'}):
fmain = gdal.VSIFOpenL('/vsizip//vsimem/vsizip_multi_thread.zip', 'wb')
f = gdal.VSIFOpenL('/vsizip//vsimem/vsizip_multi_thread.zip/test', 'wb')
gdal.VSIFWriteL('hello', 1, 5, f)
with gdaltest.config_options({"GDAL_NUM_THREADS": "ALL_CPUS"}):
fmain = gdal.VSIFOpenL("/vsizip//vsimem/vsizip_multi_thread.zip", "wb")
f = gdal.VSIFOpenL("/vsizip//vsimem/vsizip_multi_thread.zip/test", "wb")
gdal.VSIFWriteL("hello", 1, 5, f)
gdal.VSIFCloseL(f)
gdal.VSIFCloseL(fmain)
f = gdal.VSIFOpenL('/vsizip//vsimem/vsizip_multi_thread.zip/test', 'rb')
data = gdal.VSIFReadL(1, 5, f).decode('ascii')
f = gdal.VSIFOpenL("/vsizip//vsimem/vsizip_multi_thread.zip/test", "rb")
data = gdal.VSIFReadL(1, 5, f).decode("ascii")
gdal.VSIFCloseL(f)
gdal.Unlink('/vsimem/vsizip_multi_thread.zip')
gdal.Unlink("/vsimem/vsizip_multi_thread.zip")
assert data == "hello"
assert data == 'hello'
###############################################################################
# Test creating ZIP64 file: uncompressed larger than 4GB, but compressed
@ -594,11 +658,11 @@ def test_vsizip_create_zip64():
pytest.skip()
niters = 1000
s = 'hello' * 1000 * 1000
zip_name = '/vsimem/vsizip_create_zip64.zip'
with gdaltest.config_options({'GDAL_NUM_THREADS': 'ALL_CPUS'}):
fmain = gdal.VSIFOpenL('/vsizip/' + zip_name, 'wb')
f = gdal.VSIFOpenL('/vsizip/' + zip_name + '/test', 'wb')
s = "hello" * 1000 * 1000
zip_name = "/vsimem/vsizip_create_zip64.zip"
with gdaltest.config_options({"GDAL_NUM_THREADS": "ALL_CPUS"}):
fmain = gdal.VSIFOpenL("/vsizip/" + zip_name, "wb")
f = gdal.VSIFOpenL("/vsizip/" + zip_name + "/test", "wb")
for i in range(niters):
gdal.VSIFWriteL(s, 1, len(s), f)
gdal.VSIFCloseL(f)
@ -607,16 +671,17 @@ def test_vsizip_create_zip64():
size = gdal.VSIStatL(zip_name).size
assert size <= 0xFFFFFFFF
size = gdal.VSIStatL('/vsizip/' + zip_name + '/test').size
size = gdal.VSIStatL("/vsizip/" + zip_name + "/test").size
assert size == len(s) * niters
f = gdal.VSIFOpenL('/vsizip/' + zip_name + '/test', 'rb')
data = gdal.VSIFReadL(1, len(s), f).decode('ascii')
f = gdal.VSIFOpenL("/vsizip/" + zip_name + "/test", "rb")
data = gdal.VSIFReadL(1, len(s), f).decode("ascii")
gdal.VSIFCloseL(f)
assert data == s
gdal.Unlink(zip_name)
###############################################################################
# Test creating ZIP64 file: compressed data stream > 4 GB
@ -626,14 +691,14 @@ def test_vsizip_create_zip64_stream_larger_than_4G():
if not gdaltest.run_slow_tests():
pytest.skip()
zip_name = 'tmp/vsizip_create_zip64_stream_larger_than_4G.zip'
zip_name = "tmp/vsizip_create_zip64_stream_larger_than_4G.zip"
gdal.Unlink(zip_name)
niters = 999
s = ''.join([chr(random.randint(0, 127)) for i in range(5 * 1000 * 1000)])
with gdaltest.config_options({'GDAL_NUM_THREADS': 'ALL_CPUS'}):
f = gdal.VSIFOpenL('/vsizip/' + zip_name + '/test2', 'wb')
s = "".join([chr(random.randint(0, 127)) for i in range(5 * 1000 * 1000)])
with gdaltest.config_options({"GDAL_NUM_THREADS": "ALL_CPUS"}):
f = gdal.VSIFOpenL("/vsizip/" + zip_name + "/test2", "wb")
for i in range(niters):
gdal.VSIFWriteL(s, 1, len(s), f)
gdal.VSIFCloseL(f)
@ -641,11 +706,11 @@ def test_vsizip_create_zip64_stream_larger_than_4G():
size = gdal.VSIStatL(zip_name).size
assert size > 0xFFFFFFFF
size = gdal.VSIStatL('/vsizip/' + zip_name + '/test2').size
size = gdal.VSIStatL("/vsizip/" + zip_name + "/test2").size
assert size == len(s) * niters
f = gdal.VSIFOpenL('/vsizip/' + zip_name + '/test2', 'rb')
data = gdal.VSIFReadL(1, len(s), f).decode('ascii')
f = gdal.VSIFOpenL("/vsizip/" + zip_name + "/test2", "rb")
data = gdal.VSIFReadL(1, len(s), f).decode("ascii")
gdal.VSIFCloseL(f)
assert data == s
@ -655,8 +720,7 @@ def test_vsizip_create_zip64_stream_larger_than_4G():
###############################################################################
def test_vsizip_byte_zip64_local_header_zeroed():
size = gdal.VSIStatL('/vsizip/data/byte_zip64_local_header_zeroed.zip/byte.tif').size
size = gdal.VSIStatL(
"/vsizip/data/byte_zip64_local_header_zeroed.zip/byte.tif"
).size
assert size == 736

View File

@ -32,62 +32,79 @@
###############################################################################
import os
from osgeo import gdal
import gdaltest
import pytest
from osgeo import gdal
###############################################################################
# Perform simple read test.
def test_aaigrid_1():
tst = gdaltest.GDALTest('aaigrid', 'aaigrid/pixel_per_line.asc', 1, 1123)
tst = gdaltest.GDALTest("aaigrid", "aaigrid/pixel_per_line.asc", 1, 1123)
return tst.testOpen()
###############################################################################
# Verify some auxiliary data.
def test_aaigrid_2():
ds = gdal.Open('data/aaigrid/pixel_per_line.asc')
ds = gdal.Open("data/aaigrid/pixel_per_line.asc")
gt = ds.GetGeoTransform()
assert gt[0] == 100000.0 and gt[1] == 50 and gt[2] == 0 and gt[3] == 650600.0 and gt[4] == 0 and gt[5] == -50, \
'Aaigrid geotransform wrong.'
assert (
gt[0] == 100000.0
and gt[1] == 50
and gt[2] == 0
and gt[3] == 650600.0
and gt[4] == 0
and gt[5] == -50
), "Aaigrid geotransform wrong."
prj = ds.GetProjection()
assert prj == 'PROJCS["unnamed",GEOGCS["NAD83",DATUM["North_American_Datum_1983",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],AUTHORITY["EPSG","6269"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4269"]],PROJECTION["Albers_Conic_Equal_Area"],PARAMETER["latitude_of_center",59],PARAMETER["longitude_of_center",-132.5],PARAMETER["standard_parallel_1",61.6666666666667],PARAMETER["standard_parallel_2",68],PARAMETER["false_easting",500000],PARAMETER["false_northing",500000],UNIT["METERS",1],AXIS["Easting",EAST],AXIS["Northing",NORTH]]', \
('Projection does not match expected:\n%s' % prj)
assert (
prj
== 'PROJCS["unnamed",GEOGCS["NAD83",DATUM["North_American_Datum_1983",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],AUTHORITY["EPSG","6269"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4269"]],PROJECTION["Albers_Conic_Equal_Area"],PARAMETER["latitude_of_center",59],PARAMETER["longitude_of_center",-132.5],PARAMETER["standard_parallel_1",61.6666666666667],PARAMETER["standard_parallel_2",68],PARAMETER["false_easting",500000],PARAMETER["false_northing",500000],UNIT["METERS",1],AXIS["Easting",EAST],AXIS["Northing",NORTH]]'
), ("Projection does not match expected:\n%s" % prj)
band1 = ds.GetRasterBand(1)
assert band1.GetNoDataValue() == -99999, 'Grid NODATA value wrong or missing.'
assert band1.GetNoDataValue() == -99999, "Grid NODATA value wrong or missing."
assert band1.DataType == gdal.GDT_Float32, 'Data type is not Float32!'
assert band1.DataType == gdal.GDT_Float32, "Data type is not Float32!"
###############################################################################
# Test reading a file where decimal separator is comma (#3668)
def test_aaigrid_comma():
ds = gdal.Open('data/aaigrid/pixel_per_line_comma.asc')
ds = gdal.Open("data/aaigrid/pixel_per_line_comma.asc")
gt = ds.GetGeoTransform()
assert gt[0] == 100000.0 and gt[1] == 50 and gt[2] == 0 and gt[3] == 650600.0 and gt[4] == 0 and gt[5] == -50, \
'Aaigrid geotransform wrong.'
assert (
gt[0] == 100000.0
and gt[1] == 50
and gt[2] == 0
and gt[3] == 650600.0
and gt[4] == 0
and gt[5] == -50
), "Aaigrid geotransform wrong."
band1 = ds.GetRasterBand(1)
assert band1.Checksum() == 1123, 'Did not get expected nodata value.'
assert band1.Checksum() == 1123, "Did not get expected nodata value."
assert band1.GetNoDataValue() == -99999, 'Grid NODATA value wrong or missing.'
assert band1.GetNoDataValue() == -99999, "Grid NODATA value wrong or missing."
assert band1.DataType == gdal.GDT_Float32, "Data type is not Float32!"
assert band1.DataType == gdal.GDT_Float32, 'Data type is not Float32!'
###############################################################################
# Create simple copy and check.
@ -95,22 +112,23 @@ def test_aaigrid_comma():
def test_aaigrid_3():
tst = gdaltest.GDALTest('AAIGRID', 'byte.tif', 1, 4672)
tst = gdaltest.GDALTest("AAIGRID", "byte.tif", 1, 4672)
prj = 'PROJCS["NAD27 / UTM zone 11N",GEOGCS["NAD27",DATUM["North_American_Datum_1927",SPHEROID["Clarke_1866",6378206.4,294.9786982138982]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",-117],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",0],UNIT["Meter",1]]'
return tst.testCreateCopy(check_gt=1, check_srs=prj)
###############################################################################
# Read subwindow. Tests the tail recursion problem.
def test_aaigrid_4():
tst = gdaltest.GDALTest('aaigrid', 'aaigrid/pixel_per_line.asc', 1, 187,
5, 5, 5, 5)
tst = gdaltest.GDALTest("aaigrid", "aaigrid/pixel_per_line.asc", 1, 187, 5, 5, 5, 5)
return tst.testOpen()
###############################################################################
# Perform simple read test on mixed-case .PRJ filename
@ -121,7 +139,7 @@ def test_aaigrid_5():
# - case_sensitive.ASC
# - case_sensitive.PRJ
tst = gdaltest.GDALTest('aaigrid', 'aaigrid/case_sensitive.ASC', 1, 1123)
tst = gdaltest.GDALTest("aaigrid", "aaigrid/case_sensitive.ASC", 1, 1123)
prj = """PROJCS["unnamed",
GEOGCS["NAD83",
@ -147,18 +165,20 @@ def test_aaigrid_5():
return tst.testOpen(check_prj=prj)
###############################################################################
# Verify data type determination from type of nodata
def test_aaigrid_6():
ds = gdal.Open('data/aaigrid/nodata_float.asc')
ds = gdal.Open("data/aaigrid/nodata_float.asc")
b = ds.GetRasterBand(1)
assert b.GetNoDataValue() == -99999, 'Grid NODATA value wrong or missing.'
assert b.GetNoDataValue() == -99999, "Grid NODATA value wrong or missing."
assert b.DataType == gdal.GDT_Float32, "Data type is not Float32!"
assert b.DataType == gdal.GDT_Float32, 'Data type is not Float32!'
###############################################################################
# Verify data type determination from type of nodata
@ -166,12 +186,13 @@ def test_aaigrid_6():
def test_aaigrid_6bis():
ds = gdal.Open('data/aaigrid/nodata_int.asc')
ds = gdal.Open("data/aaigrid/nodata_int.asc")
b = ds.GetRasterBand(1)
assert b.GetNoDataValue() == -99999, 'Grid NODATA value wrong or missing.'
assert b.GetNoDataValue() == -99999, "Grid NODATA value wrong or missing."
assert b.DataType == gdal.GDT_Int32, "Data type is not Int32!"
assert b.DataType == gdal.GDT_Int32, 'Data type is not Int32!'
###############################################################################
# Verify writing files with non-square pixels.
@ -179,7 +200,7 @@ def test_aaigrid_6bis():
def test_aaigrid_7():
tst = gdaltest.GDALTest('AAIGRID', 'aaigrid/nonsquare.vrt', 1, 12481)
tst = gdaltest.GDALTest("AAIGRID", "aaigrid/nonsquare.vrt", 1, 12481)
return tst.testCreateCopy(check_gt=1)
@ -187,9 +208,10 @@ def test_aaigrid_7():
###############################################################################
# Test creating an in memory copy.
def test_aaigrid_8():
tst = gdaltest.GDALTest('AAIGRID', 'byte.tif', 1, 4672)
tst = gdaltest.GDALTest("AAIGRID", "byte.tif", 1, 4672)
return tst.testCreateCopy(vsimem=1)
@ -197,19 +219,23 @@ def test_aaigrid_8():
###############################################################################
# Test DECIMAL_PRECISION creation option
def test_aaigrid_9():
ds = gdal.Open('data/ehdr/float32.bil')
ds2 = gdal.GetDriverByName('AAIGRID').CreateCopy('tmp/aaigrid.tmp', ds, options=['DECIMAL_PRECISION=2'])
ds = gdal.Open("data/ehdr/float32.bil")
ds2 = gdal.GetDriverByName("AAIGRID").CreateCopy(
"tmp/aaigrid.tmp", ds, options=["DECIMAL_PRECISION=2"]
)
got_minmax = ds2.GetRasterBand(1).ComputeRasterMinMax()
ds2 = None
gdal.GetDriverByName('AAIGRID').Delete('tmp/aaigrid.tmp')
gdal.GetDriverByName("AAIGRID").Delete("tmp/aaigrid.tmp")
if got_minmax[0] == pytest.approx(-0.84, abs=1e-7):
return
pytest.fail()
###############################################################################
# Test AAIGRID_DATATYPE configuration option and DATATYPE open options
@ -217,36 +243,44 @@ def test_aaigrid_9():
def test_aaigrid_10():
# By default detected as 32bit float
ds = gdal.Open('data/aaigrid/float64.asc')
assert ds.GetRasterBand(1).DataType == gdal.GDT_Float32, 'Data type is not Float32!'
ds = gdal.Open("data/aaigrid/float64.asc")
assert ds.GetRasterBand(1).DataType == gdal.GDT_Float32, "Data type is not Float32!"
for i in range(2):
try:
os.remove('data/aaigrid/float64.asc.aux.xml')
os.remove("data/aaigrid/float64.asc.aux.xml")
except OSError:
pass
if i == 0:
gdal.SetConfigOption('AAIGRID_DATATYPE', 'Float64')
ds = gdal.Open('data/aaigrid/float64.asc')
gdal.SetConfigOption('AAIGRID_DATATYPE', None)
gdal.SetConfigOption("AAIGRID_DATATYPE", "Float64")
ds = gdal.Open("data/aaigrid/float64.asc")
gdal.SetConfigOption("AAIGRID_DATATYPE", None)
else:
ds = gdal.OpenEx('data/aaigrid/float64.asc', open_options=['DATATYPE=Float64'])
ds = gdal.OpenEx(
"data/aaigrid/float64.asc", open_options=["DATATYPE=Float64"]
)
assert ds.GetRasterBand(1).DataType == gdal.GDT_Float64, 'Data type is not Float64!'
assert (
ds.GetRasterBand(1).DataType == gdal.GDT_Float64
), "Data type is not Float64!"
nv = ds.GetRasterBand(1).GetNoDataValue()
assert nv == pytest.approx(-1.234567890123, abs=1e-16), 'did not get expected nodata value'
assert nv == pytest.approx(
-1.234567890123, abs=1e-16
), "did not get expected nodata value"
got_minmax = ds.GetRasterBand(1).ComputeRasterMinMax()
assert got_minmax[0] == pytest.approx(1.234567890123, abs=1e-16), \
'did not get expected min value'
assert got_minmax[1] == pytest.approx(1.234567890123, abs=1e-16), \
'did not get expected max value'
assert got_minmax[0] == pytest.approx(
1.234567890123, abs=1e-16
), "did not get expected min value"
assert got_minmax[1] == pytest.approx(
1.234567890123, abs=1e-16
), "did not get expected max value"
try:
os.remove('data/aaigrid/float64.asc.aux.xml')
os.remove("data/aaigrid/float64.asc.aux.xml")
except OSError:
pass
@ -257,37 +291,42 @@ def test_aaigrid_10():
def test_aaigrid_11():
ds = gdal.Open('data/ehdr/float32.bil')
ds2 = gdal.GetDriverByName('AAIGRID').CreateCopy('tmp/aaigrid.tmp', ds, options=['SIGNIFICANT_DIGITS=2'])
ds = gdal.Open("data/ehdr/float32.bil")
ds2 = gdal.GetDriverByName("AAIGRID").CreateCopy(
"tmp/aaigrid.tmp", ds, options=["SIGNIFICANT_DIGITS=2"]
)
got_minmax = ds2.GetRasterBand(1).ComputeRasterMinMax()
ds2 = None
gdal.GetDriverByName('AAIGRID').Delete('tmp/aaigrid.tmp')
gdal.GetDriverByName("AAIGRID").Delete("tmp/aaigrid.tmp")
if got_minmax[0] == pytest.approx(-0.84, abs=1e-7):
return
pytest.fail()
###############################################################################
# Test no data is written to correct precision with DECIMAL_PRECISION.
def test_aaigrid_12():
ds = gdal.Open('data/aaigrid/nodata_float.asc')
ds2 = gdal.GetDriverByName('AAIGRID').CreateCopy('tmp/aaigrid.tmp', ds,
options=['DECIMAL_PRECISION=3'])
ds = gdal.Open("data/aaigrid/nodata_float.asc")
ds2 = gdal.GetDriverByName("AAIGRID").CreateCopy(
"tmp/aaigrid.tmp", ds, options=["DECIMAL_PRECISION=3"]
)
del ds2
aai = open('tmp/aaigrid.tmp')
aai = open("tmp/aaigrid.tmp")
assert aai
for _ in range(5):
aai.readline()
ndv = aai.readline().strip().lower()
aai.close()
gdal.GetDriverByName('AAIGRID').Delete('tmp/aaigrid.tmp')
assert ndv.startswith('nodata_value')
assert ndv.endswith('-99999.000')
gdal.GetDriverByName("AAIGRID").Delete("tmp/aaigrid.tmp")
assert ndv.startswith("nodata_value")
assert ndv.endswith("-99999.000")
###############################################################################
# Test no data is written to correct precision WITH SIGNIFICANT_DIGITS.
@ -295,20 +334,22 @@ def test_aaigrid_12():
def test_aaigrid_13():
ds = gdal.Open('data/aaigrid/nodata_float.asc')
ds2 = gdal.GetDriverByName('AAIGRID').CreateCopy('tmp/aaigrid.tmp', ds,
options=['SIGNIFICANT_DIGITS=3'])
ds = gdal.Open("data/aaigrid/nodata_float.asc")
ds2 = gdal.GetDriverByName("AAIGRID").CreateCopy(
"tmp/aaigrid.tmp", ds, options=["SIGNIFICANT_DIGITS=3"]
)
del ds2
aai = open('tmp/aaigrid.tmp')
aai = open("tmp/aaigrid.tmp")
assert aai
for _ in range(5):
aai.readline()
ndv = aai.readline().strip().lower()
aai.close()
gdal.GetDriverByName('AAIGRID').Delete('tmp/aaigrid.tmp')
assert ndv.startswith('nodata_value')
assert ndv.endswith('-1e+05') or ndv.endswith('-1e+005')
gdal.GetDriverByName("AAIGRID").Delete("tmp/aaigrid.tmp")
assert ndv.startswith("nodata_value")
assert ndv.endswith("-1e+05") or ndv.endswith("-1e+005")
###############################################################################
# Test fix for #6060
@ -316,19 +357,22 @@ def test_aaigrid_13():
def test_aaigrid_14():
ds = gdal.Open('data/byte.tif')
mem_ds = gdal.GetDriverByName('MEM').Create('', 20, 20, 1, gdal.GDT_Float32)
mem_ds.GetRasterBand(1).WriteRaster(0, 0, 20, 20, ds.ReadRaster(0, 0, 20, 20, buf_type=gdal.GDT_Float32))
ds = gdal.Open("data/byte.tif")
mem_ds = gdal.GetDriverByName("MEM").Create("", 20, 20, 1, gdal.GDT_Float32)
mem_ds.GetRasterBand(1).WriteRaster(
0, 0, 20, 20, ds.ReadRaster(0, 0, 20, 20, buf_type=gdal.GDT_Float32)
)
ds = None
gdal.GetDriverByName('AAIGRID').CreateCopy('/vsimem/aaigrid_14.asc', mem_ds)
gdal.GetDriverByName("AAIGRID").CreateCopy("/vsimem/aaigrid_14.asc", mem_ds)
f = gdal.VSIFOpenL('/vsimem/aaigrid_14.asc', 'rb')
data = gdal.VSIFReadL(1, 10000, f).decode('ascii')
f = gdal.VSIFOpenL("/vsimem/aaigrid_14.asc", "rb")
data = gdal.VSIFReadL(1, 10000, f).decode("ascii")
gdal.VSIFCloseL(f)
gdal.GetDriverByName('AAIGRID').Delete('/vsimem/aaigrid_14.asc')
gdal.GetDriverByName("AAIGRID").Delete("/vsimem/aaigrid_14.asc")
assert "107.0 123" in data
assert '107.0 123' in data
###############################################################################
# Test Float64 detection when nodata = DBL_MIN
@ -336,20 +380,23 @@ def test_aaigrid_14():
def test_aaigrid_15():
gdal.FileFromMemBuffer('/vsimem/aaigrid_15.asc', """ncols 4
gdal.FileFromMemBuffer(
"/vsimem/aaigrid_15.asc",
"""ncols 4
nrows 1
xllcorner 0
yllcorner -1
cellsize 1
NODATA_value 2.2250738585072014e-308
2.2250738585072014e-308 0 1 2.3e-308
""")
""",
)
ds = gdal.Open('/vsimem/aaigrid_15.asc')
ds = gdal.Open("/vsimem/aaigrid_15.asc")
assert ds.GetRasterBand(1).DataType == gdal.GDT_Float64
ds = None
gdal.Unlink('/vsimem/aaigrid_15.asc')
gdal.Unlink("/vsimem/aaigrid_15.asc")
###############################################################################
@ -358,22 +405,25 @@ NODATA_value 2.2250738585072014e-308
def test_aaigrid_null():
gdal.FileFromMemBuffer('/vsimem/test_aaigrid_null.asc', """ncols 4
gdal.FileFromMemBuffer(
"/vsimem/test_aaigrid_null.asc",
"""ncols 4
nrows 1
xllcorner 0
yllcorner -1
cellsize 1
NODATA_value null
null 1.5 null 3.5
""")
""",
)
ds = gdal.Open('/vsimem/test_aaigrid_null.asc')
ds = gdal.Open("/vsimem/test_aaigrid_null.asc")
assert ds.GetRasterBand(1).DataType == gdal.GDT_Float32
assert ds.GetRasterBand(1).GetNoDataValue() < -1e38
assert ds.GetRasterBand(1).ComputeRasterMinMax() == (1.5,3.5)
assert ds.GetRasterBand(1).ComputeRasterMinMax() == (1.5, 3.5)
ds = None
gdal.Unlink('/vsimem/test_aaigrid_null.asc')
gdal.Unlink("/vsimem/test_aaigrid_null.asc")
###############################################################################
@ -382,21 +432,23 @@ null 1.5 null 3.5
def test_aaigrid_null_float64():
gdal.FileFromMemBuffer('/vsimem/test_aaigrid_null.asc', """ncols 4
gdal.FileFromMemBuffer(
"/vsimem/test_aaigrid_null.asc",
"""ncols 4
nrows 1
xllcorner 0
yllcorner -1
cellsize 1
NODATA_value null
null 1.5 null 3.5
""")
""",
)
with gdaltest.config_option('AAIGRID_DATATYPE', 'Float64'):
ds = gdal.Open('/vsimem/test_aaigrid_null.asc')
with gdaltest.config_option("AAIGRID_DATATYPE", "Float64"):
ds = gdal.Open("/vsimem/test_aaigrid_null.asc")
assert ds.GetRasterBand(1).DataType == gdal.GDT_Float64
assert ds.GetRasterBand(1).GetNoDataValue() < -1e308
assert ds.GetRasterBand(1).ComputeRasterMinMax() == (1.5,3.5)
assert ds.GetRasterBand(1).ComputeRasterMinMax() == (1.5, 3.5)
ds = None
gdal.Unlink('/vsimem/test_aaigrid_null.asc')
gdal.Unlink("/vsimem/test_aaigrid_null.asc")

View File

@ -28,23 +28,24 @@
# DEALINGS IN THE SOFTWARE.
###############################################################################
from osgeo import gdal
import gdaltest
from osgeo import gdal
###############################################################################
# Test a fake ACE2 dataset
def test_ace2_1():
f = gdal.VSIFOpenL('/vsimem/45N015E_5M.ACE2', 'wb')
f = gdal.VSIFOpenL("/vsimem/45N015E_5M.ACE2", "wb")
gdal.VSIFSeekL(f, 180 * 180 * 4 - 1, 0)
gdal.VSIFWriteL('\0', 1, 1, f)
gdal.VSIFWriteL("\0", 1, 1, f)
gdal.VSIFCloseL(f)
tst = gdaltest.GDALTest('ACE2', '/vsimem/45N015E_5M.ACE2', 1, 0, filename_absolute=1)
tst = gdaltest.GDALTest(
"ACE2", "/vsimem/45N015E_5M.ACE2", 1, 0, filename_absolute=1
)
expected_gt = [15.0, 0.08333333333333333, 0.0, 60.0, 0.0, -0.08333333333333333]
expected_srs = """GEOGCS["WGS 84",
DATUM["WGS_1984",
@ -59,9 +60,6 @@ def test_ace2_1():
AUTHORITY["EPSG","4326"]]"""
ret = tst.testOpen(check_gt=expected_gt, check_prj=expected_srs)
gdal.Unlink('/vsimem/45N015E_5M.ACE2')
gdal.Unlink("/vsimem/45N015E_5M.ACE2")
return ret

View File

@ -31,56 +31,66 @@
import os
import shutil
from osgeo import gdal
import gdaltest
from osgeo import gdal
###############################################################################
# Read test of simple byte reference data.
def test_adrg_read_gen():
tst = gdaltest.GDALTest('ADRG', 'adrg/SMALL_ADRG/ABCDEF01.GEN', 1, 62833)
tst = gdaltest.GDALTest("ADRG", "adrg/SMALL_ADRG/ABCDEF01.GEN", 1, 62833)
return tst.testOpen()
###############################################################################
# Read test of simple byte reference data by the TRANSH01.THF file .
def test_adrg_read_transh():
tst = gdaltest.GDALTest('ADRG', 'adrg/SMALL_ADRG/TRANSH01.THF', 1, 62833)
tst = gdaltest.GDALTest("ADRG", "adrg/SMALL_ADRG/TRANSH01.THF", 1, 62833)
return tst.testOpen()
###############################################################################
# Read test of simple byte reference data by a subdataset file
def test_adrg_read_subdataset_img():
tst = gdaltest.GDALTest('ADRG', 'ADRG:data/adrg/SMALL_ADRG/ABCDEF01.GEN,data/adrg/SMALL_ADRG/ABCDEF01.IMG', 1, 62833, filename_absolute=1)
tst = gdaltest.GDALTest(
"ADRG",
"ADRG:data/adrg/SMALL_ADRG/ABCDEF01.GEN,data/adrg/SMALL_ADRG/ABCDEF01.IMG",
1,
62833,
filename_absolute=1,
)
return tst.testOpen()
###############################################################################
# Test copying.
def test_adrg_copy():
drv = gdal.GetDriverByName('ADRG')
srcds = gdal.Open('data/adrg/SMALL_ADRG/ABCDEF01.GEN')
drv = gdal.GetDriverByName("ADRG")
srcds = gdal.Open("data/adrg/SMALL_ADRG/ABCDEF01.GEN")
dstds = drv.CreateCopy('tmp/ABCDEF01.GEN', srcds)
dstds = drv.CreateCopy("tmp/ABCDEF01.GEN", srcds)
chksum = dstds.GetRasterBand(1).Checksum()
assert chksum == 62833, 'Wrong checksum'
assert chksum == 62833, "Wrong checksum"
dstds = None
drv.Delete('tmp/ABCDEF01.GEN')
drv.Delete("tmp/ABCDEF01.GEN")
###############################################################################
# Test creating a fake 2 subdataset image and reading it.
@ -88,35 +98,36 @@ def test_adrg_copy():
def test_adrg_2subdatasets():
drv = gdal.GetDriverByName('ADRG')
srcds = gdal.Open('data/adrg/SMALL_ADRG/ABCDEF01.GEN')
drv = gdal.GetDriverByName("ADRG")
srcds = gdal.Open("data/adrg/SMALL_ADRG/ABCDEF01.GEN")
gdal.SetConfigOption('ADRG_SIMULATE_MULTI_IMG', 'ON')
dstds = drv.CreateCopy('tmp/XXXXXX01.GEN', srcds)
gdal.SetConfigOption("ADRG_SIMULATE_MULTI_IMG", "ON")
dstds = drv.CreateCopy("tmp/XXXXXX01.GEN", srcds)
del dstds
gdal.SetConfigOption('ADRG_SIMULATE_MULTI_IMG', 'OFF')
gdal.SetConfigOption("ADRG_SIMULATE_MULTI_IMG", "OFF")
shutil.copy('tmp/XXXXXX01.IMG', 'tmp/XXXXXX02.IMG')
shutil.copy("tmp/XXXXXX01.IMG", "tmp/XXXXXX02.IMG")
ds = gdal.Open('tmp/TRANSH01.THF')
assert ds.RasterCount == 0, 'did not expected non 0 RasterCount'
ds = gdal.Open("tmp/TRANSH01.THF")
assert ds.RasterCount == 0, "did not expected non 0 RasterCount"
ds = None
ds = gdal.Open('ADRG:tmp/XXXXXX01.GEN,tmp/XXXXXX02.IMG')
ds = gdal.Open("ADRG:tmp/XXXXXX01.GEN,tmp/XXXXXX02.IMG")
chksum = ds.GetRasterBand(1).Checksum()
assert chksum == 62833, 'Wrong checksum'
assert chksum == 62833, "Wrong checksum"
md = ds.GetMetadata('')
assert md['ADRG_NAM'] == 'XXXXXX02', 'metadata wrong.'
md = ds.GetMetadata("")
assert md["ADRG_NAM"] == "XXXXXX02", "metadata wrong."
ds = None
os.remove('tmp/XXXXXX01.GEN')
os.remove('tmp/XXXXXX01.GEN.aux.xml')
os.remove('tmp/XXXXXX01.IMG')
os.remove('tmp/XXXXXX02.IMG')
os.remove('tmp/TRANSH01.THF')
os.remove("tmp/XXXXXX01.GEN")
os.remove("tmp/XXXXXX01.GEN.aux.xml")
os.remove("tmp/XXXXXX01.IMG")
os.remove("tmp/XXXXXX02.IMG")
os.remove("tmp/TRANSH01.THF")
###############################################################################
# Test creating an in memory copy.
@ -124,27 +135,28 @@ def test_adrg_2subdatasets():
def test_adrg_copy_vsimem():
drv = gdal.GetDriverByName('ADRG')
srcds = gdal.Open('data/adrg/SMALL_ADRG/ABCDEF01.GEN')
drv = gdal.GetDriverByName("ADRG")
srcds = gdal.Open("data/adrg/SMALL_ADRG/ABCDEF01.GEN")
dstds = drv.CreateCopy('/vsimem/ABCDEF01.GEN', srcds)
dstds = drv.CreateCopy("/vsimem/ABCDEF01.GEN", srcds)
chksum = dstds.GetRasterBand(1).Checksum()
assert chksum == 62833, 'Wrong checksum'
assert chksum == 62833, "Wrong checksum"
dstds = None
# Reopen file
ds = gdal.Open('/vsimem/ABCDEF01.GEN')
ds = gdal.Open("/vsimem/ABCDEF01.GEN")
chksum = ds.GetRasterBand(1).Checksum()
assert chksum == 62833, 'Wrong checksum'
assert chksum == 62833, "Wrong checksum"
ds = None
drv.Delete('/vsimem/ABCDEF01.GEN')
gdal.Unlink('/vsimem/TRANSH01.THF')
drv.Delete("/vsimem/ABCDEF01.GEN")
gdal.Unlink("/vsimem/TRANSH01.THF")
###############################################################################
# Test reading a fake North Polar dataset (#6560)
@ -152,14 +164,25 @@ def test_adrg_copy_vsimem():
def test_adrg_zna_9():
ds = gdal.Open('data/adrg/SMALL_ADRG_ZNA9/ABCDEF01.GEN')
expected_gt = (-307675.73602473765, 100.09145391818853, 0.0, -179477.5051066006, 0.0, -100.09145391818853)
ds = gdal.Open("data/adrg/SMALL_ADRG_ZNA9/ABCDEF01.GEN")
expected_gt = (
-307675.73602473765,
100.09145391818853,
0.0,
-179477.5051066006,
0.0,
-100.09145391818853,
)
gt = ds.GetGeoTransform()
assert max(abs(gt[i] - expected_gt[i]) for i in range(6)) <= 1e-5, \
'Wrong geotransfsorm'
assert (
max(abs(gt[i] - expected_gt[i]) for i in range(6)) <= 1e-5
), "Wrong geotransfsorm"
wkt = ds.GetProjectionRef()
assert wkt == """PROJCS["ARC_System_Zone_09",GEOGCS["Unknown datum based upon the Authalic Sphere",DATUM["Not_specified_based_on_Authalic_Sphere",SPHEROID["Sphere",6378137,0],AUTHORITY["EPSG","6035"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]],PROJECTION["Azimuthal_Equidistant"],PARAMETER["latitude_of_center",90],PARAMETER["longitude_of_center",0],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["metre",1],AXIS["Easting",EAST],AXIS["Northing",NORTH]]""", \
'Wrong WKT'
assert (
wkt
== """PROJCS["ARC_System_Zone_09",GEOGCS["Unknown datum based upon the Authalic Sphere",DATUM["Not_specified_based_on_Authalic_Sphere",SPHEROID["Sphere",6378137,0],AUTHORITY["EPSG","6035"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]],PROJECTION["Azimuthal_Equidistant"],PARAMETER["latitude_of_center",90],PARAMETER["longitude_of_center",0],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["metre",1],AXIS["Easting",EAST],AXIS["Northing",NORTH]]"""
), "Wrong WKT"
###############################################################################
# Test reading a fake South Polar dataset (#6560)
@ -167,15 +190,24 @@ def test_adrg_zna_9():
def test_adrg_zna_18():
ds = gdal.Open('data/adrg/SMALL_ADRG_ZNA18/ABCDEF01.GEN')
expected_gt = (-307675.73602473765, 100.09145391818853, 0.0, 179477.5051066006, 0.0, -100.09145391818853)
ds = gdal.Open("data/adrg/SMALL_ADRG_ZNA18/ABCDEF01.GEN")
expected_gt = (
-307675.73602473765,
100.09145391818853,
0.0,
179477.5051066006,
0.0,
-100.09145391818853,
)
gt = ds.GetGeoTransform()
assert max(abs(gt[i] - expected_gt[i]) for i in range(6)) <= 1e-5, \
'Wrong geotransfsorm'
assert (
max(abs(gt[i] - expected_gt[i]) for i in range(6)) <= 1e-5
), "Wrong geotransfsorm"
wkt = ds.GetProjectionRef()
assert wkt == """PROJCS["ARC_System_Zone_18",GEOGCS["Unknown datum based upon the Authalic Sphere",DATUM["Not_specified_based_on_Authalic_Sphere",SPHEROID["Sphere",6378137,0],AUTHORITY["EPSG","6035"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]],PROJECTION["Azimuthal_Equidistant"],PARAMETER["latitude_of_center",-90],PARAMETER["longitude_of_center",0],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["metre",1],AXIS["Easting",EAST],AXIS["Northing",NORTH]]""", \
'Wrong WKT'
assert (
wkt
== """PROJCS["ARC_System_Zone_18",GEOGCS["Unknown datum based upon the Authalic Sphere",DATUM["Not_specified_based_on_Authalic_Sphere",SPHEROID["Sphere",6378137,0],AUTHORITY["EPSG","6035"]],PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433]],PROJECTION["Azimuthal_Equidistant"],PARAMETER["latitude_of_center",-90],PARAMETER["longitude_of_center",0],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["metre",1],AXIS["Easting",EAST],AXIS["Northing",NORTH]]"""
), "Wrong WKT"
###############################################################################

View File

@ -31,42 +31,54 @@
import os
import shutil
from osgeo import gdal
import gdaltest
import pytest
from osgeo import gdal
###############################################################################
# Read test of simple byte reference data.
def test_aigrid_1():
tst = gdaltest.GDALTest('AIG', 'aigrid/abc3x1', 1, 3)
tst = gdaltest.GDALTest("AIG", "aigrid/abc3x1", 1, 3)
return tst.testOpen()
###############################################################################
# Verify some auxiliary data.
def test_aigrid_2():
ds = gdal.Open('data/aigrid/abc3x1/prj.adf')
ds = gdal.Open("data/aigrid/abc3x1/prj.adf")
gt = ds.GetGeoTransform()
assert gt[0] == -0.5 and gt[1] == 1.0 and gt[2] == 0.0 and gt[3] == 0.5 and gt[4] == 0.0 and gt[5] == -1.0, \
'Aigrid geotransform wrong.'
assert (
gt[0] == -0.5
and gt[1] == 1.0
and gt[2] == 0.0
and gt[3] == 0.5
and gt[4] == 0.0
and gt[5] == -1.0
), "Aigrid geotransform wrong."
prj = ds.GetProjection()
assert prj.find('PROJCS["unnamed",GEOGCS["GDA94",DATUM["Geocentric_Datum_of_Australia_1994"') != -1, \
('Projection does not match expected:\n%s' % prj)
assert (
prj.find(
'PROJCS["unnamed",GEOGCS["GDA94",DATUM["Geocentric_Datum_of_Australia_1994"'
)
!= -1
), ("Projection does not match expected:\n%s" % prj)
band1 = ds.GetRasterBand(1)
assert band1.GetNoDataValue() == 255, 'Grid NODATA value wrong or missing.'
assert band1.GetNoDataValue() == 255, "Grid NODATA value wrong or missing."
assert band1.DataType == gdal.GDT_Byte, "Data type is not Byte!"
assert band1.DataType == gdal.GDT_Byte, 'Data type is not Byte!'
###############################################################################
# Verify the colormap, and nodata setting for test file.
@ -74,37 +86,47 @@ def test_aigrid_2():
def test_aigrid_3():
ds = gdal.Open('data/aigrid/abc3x1')
ds = gdal.Open("data/aigrid/abc3x1")
cm = ds.GetRasterBand(1).GetRasterColorTable()
assert cm.GetCount() == 256 and cm.GetColorEntry(0) == (95, 113, 150, 255) and cm.GetColorEntry(1) == (95, 57, 29, 255), \
'Wrong colormap entries'
assert (
cm.GetCount() == 256
and cm.GetColorEntry(0) == (95, 113, 150, 255)
and cm.GetColorEntry(1) == (95, 57, 29, 255)
), "Wrong colormap entries"
cm = None
assert ds.GetRasterBand(1).GetNoDataValue() == 255.0, 'Wrong nodata value.'
assert ds.GetRasterBand(1).GetNoDataValue() == 255.0, "Wrong nodata value."
###############################################################################
# Read test of simple byte reference data with data directory name in all uppercase
def test_aigrid_4():
tst = gdaltest.GDALTest('AIG', 'aigrid/ABC3X1UC', 1, 3)
tst = gdaltest.GDALTest("AIG", "aigrid/ABC3X1UC", 1, 3)
return tst.testOpen()
###############################################################################
# Verify the colormap, and nodata setting for test file with names of coverage directory and all files in it in all uppercase. Additionally also test for case where clr file resides in parent directory of coverage.
def test_aigrid_5():
ds = gdal.Open('data/aigrid/ABC3X1UC')
ds = gdal.Open("data/aigrid/ABC3X1UC")
cm = ds.GetRasterBand(1).GetRasterColorTable()
assert cm.GetCount() == 256 and cm.GetColorEntry(0) == (95, 113, 150, 255) and cm.GetColorEntry(1) == (95, 57, 29, 255), \
'Wrong colormap entries'
assert (
cm.GetCount() == 256
and cm.GetColorEntry(0) == (95, 113, 150, 255)
and cm.GetColorEntry(1) == (95, 57, 29, 255)
), "Wrong colormap entries"
cm = None
assert ds.GetRasterBand(1).GetNoDataValue() == 255.0, 'Wrong nodata value.'
assert ds.GetRasterBand(1).GetNoDataValue() == 255.0, "Wrong nodata value."
###############################################################################
# Verify dataset whose sta.adf is 24 bytes
@ -112,11 +134,12 @@ def test_aigrid_5():
def test_aigrid_6():
ds = gdal.Open('data/aigrid/aigrid_sta_24bytes/teststa')
ds = gdal.Open("data/aigrid/aigrid_sta_24bytes/teststa")
assert ds.GetRasterBand(1).GetMinimum() == 0.0, 'Wrong minimum'
assert ds.GetRasterBand(1).GetMinimum() == 0.0, "Wrong minimum"
assert ds.GetRasterBand(1).GetMaximum() == 2.0, "Wrong maximum"
assert ds.GetRasterBand(1).GetMaximum() == 2.0, 'Wrong maximum'
###############################################################################
# Read twice a broken tile (https://github.com/OSGeo/gdal/issues/4316)
@ -124,25 +147,26 @@ def test_aigrid_6():
def test_aigrid_broken():
if os.path.exists('tmp/broken_aigrid'):
shutil.rmtree('tmp/broken_aigrid')
if os.path.exists("tmp/broken_aigrid"):
shutil.rmtree("tmp/broken_aigrid")
shutil.copytree('data/aigrid/abc3x1', 'tmp/broken_aigrid')
shutil.copytree("data/aigrid/abc3x1", "tmp/broken_aigrid")
# Write a bad offset for a block
f = gdal.VSIFOpenL('tmp/broken_aigrid/w001001x.adf', 'rb+')
f = gdal.VSIFOpenL("tmp/broken_aigrid/w001001x.adf", "rb+")
gdal.VSIFSeekL(f, 100, 0)
gdal.VSIFWriteL(b'\xff' * 4, 1, 4, f)
gdal.VSIFWriteL(b"\xff" * 4, 1, 4, f)
gdal.VSIFCloseL(f)
ds = gdal.Open('tmp/broken_aigrid')
ds = gdal.Open("tmp/broken_aigrid")
with gdaltest.error_handler():
assert ds.GetRasterBand(1).Checksum() == 0
with gdaltest.error_handler():
assert ds.GetRasterBand(1).Checksum() == 0
ds = None
shutil.rmtree('tmp/broken_aigrid')
shutil.rmtree("tmp/broken_aigrid")
###############################################################################
# Test on real dataset downloaded from http://download.osgeo.org/gdal/data/aig/nzdem
@ -150,67 +174,75 @@ def test_aigrid_broken():
def test_aigrid_online_1():
list_files = ['info/arc.dir',
'info/arc0000.dat',
'info/arc0000.nit',
'info/arc0001.dat',
'info/arc0001.nit',
'info/arc0002.dat',
'info/arc0002.nit',
'info/arc0002r.001',
'nzdem500/dblbnd.adf',
'nzdem500/hdr.adf',
'nzdem500/log',
'nzdem500/sta.adf',
'nzdem500/vat.adf',
'nzdem500/w001001.adf',
'nzdem500/w001001x.adf']
list_files = [
"info/arc.dir",
"info/arc0000.dat",
"info/arc0000.nit",
"info/arc0001.dat",
"info/arc0001.nit",
"info/arc0002.dat",
"info/arc0002.nit",
"info/arc0002r.001",
"nzdem500/dblbnd.adf",
"nzdem500/hdr.adf",
"nzdem500/log",
"nzdem500/sta.adf",
"nzdem500/vat.adf",
"nzdem500/w001001.adf",
"nzdem500/w001001x.adf",
]
try:
os.mkdir('tmp/cache/nzdem')
os.mkdir('tmp/cache/nzdem/info')
os.mkdir('tmp/cache/nzdem/nzdem500')
os.mkdir("tmp/cache/nzdem")
os.mkdir("tmp/cache/nzdem/info")
os.mkdir("tmp/cache/nzdem/nzdem500")
except OSError:
pass
for filename in list_files:
if not gdaltest.download_file('http://download.osgeo.org/gdal/data/aig/nzdem/' + filename, 'nzdem/' + filename):
if not gdaltest.download_file(
"http://download.osgeo.org/gdal/data/aig/nzdem/" + filename,
"nzdem/" + filename,
):
pytest.skip()
tst = gdaltest.GDALTest('AIG', 'tmp/cache/nzdem/nzdem500/hdr.adf', 1, 45334, filename_absolute=1)
tst = gdaltest.GDALTest(
"AIG", "tmp/cache/nzdem/nzdem500/hdr.adf", 1, 45334, filename_absolute=1
)
tst.testOpen()
ds = gdal.Open('tmp/cache/nzdem/nzdem500/hdr.adf')
ds = gdal.Open("tmp/cache/nzdem/nzdem500/hdr.adf")
try:
rat = ds.GetRasterBand(1).GetDefaultRAT()
except:
print('Skipping RAT checking... OG Python bindings have no RAT API')
print("Skipping RAT checking... OG Python bindings have no RAT API")
return
assert rat is not None, 'No RAT found'
assert rat is not None, "No RAT found"
assert rat.GetRowCount() == 2642, 'Wrong row count in RAT'
assert rat.GetRowCount() == 2642, "Wrong row count in RAT"
assert rat.GetColumnCount() == 2, 'Wrong column count in RAT'
assert rat.GetColumnCount() == 2, "Wrong column count in RAT"
assert rat.GetNameOfCol(0) == 'VALUE', 'Wrong name of col 0'
assert rat.GetNameOfCol(0) == "VALUE", "Wrong name of col 0"
assert rat.GetTypeOfCol(0) == gdal.GFT_Integer, 'Wrong type of col 0'
assert rat.GetTypeOfCol(0) == gdal.GFT_Integer, "Wrong type of col 0"
assert rat.GetUsageOfCol(0) == gdal.GFU_MinMax, 'Wrong usage of col 0'
assert rat.GetUsageOfCol(0) == gdal.GFU_MinMax, "Wrong usage of col 0"
assert rat.GetNameOfCol(1) == 'COUNT', 'Wrong name of col 1'
assert rat.GetNameOfCol(1) == "COUNT", "Wrong name of col 1"
assert rat.GetTypeOfCol(1) == gdal.GFT_Integer, 'Wrong type of col 1'
assert rat.GetTypeOfCol(1) == gdal.GFT_Integer, "Wrong type of col 1"
assert rat.GetUsageOfCol(1) == gdal.GFU_PixelCount, 'Wrong usage of col 1'
assert rat.GetUsageOfCol(1) == gdal.GFU_PixelCount, "Wrong usage of col 1"
assert rat.GetValueAsInt(2641, 0) == 3627, 'Wrong value in RAT'
assert rat.GetValueAsInt(2641, 0) == 3627, "Wrong value in RAT"
assert ds.GetRasterBand(1).GetMinimum() == 0.0, 'Wrong minimum'
assert ds.GetRasterBand(1).GetMinimum() == 0.0, "Wrong minimum"
assert ds.GetRasterBand(1).GetMaximum() == 3627.0, "Wrong maximum"
assert ds.GetRasterBand(1).GetMaximum() == 3627.0, 'Wrong maximum'
###############################################################################
# Test on real dataset downloaded from http://download.osgeo.org/gdal/data/aig/nzdem
@ -218,25 +250,27 @@ def test_aigrid_online_1():
def test_aigrid_online_2():
if not gdaltest.download_file('http://download.osgeo.org/gdal/data/aig/ai_bug_6886.zip', 'ai_bug_6886.zip'):
if not gdaltest.download_file(
"http://download.osgeo.org/gdal/data/aig/ai_bug_6886.zip", "ai_bug_6886.zip"
):
pytest.skip()
try:
os.stat('tmp/cache/ai_bug')
os.stat("tmp/cache/ai_bug")
except OSError:
try:
gdaltest.unzip('tmp/cache', 'tmp/cache/ai_bug_6886')
gdaltest.unzip("tmp/cache", "tmp/cache/ai_bug_6886")
try:
os.stat('tmp/cache/ai_bug')
os.stat("tmp/cache/ai_bug")
except OSError:
pytest.skip()
except:
pytest.skip()
tst = gdaltest.GDALTest('AIG', 'tmp/cache/ai_bug/ai_bug/hdr.adf', 1, 16018, filename_absolute=1)
tst = gdaltest.GDALTest(
"AIG", "tmp/cache/ai_bug/ai_bug/hdr.adf", 1, 16018, filename_absolute=1
)
return tst.testOpen()
###############################################################################

View File

@ -31,12 +31,12 @@
import os
import struct
from copy import copy
from osgeo import gdal
import gdaltest
import pytest
from osgeo import gdal
# given fmt and nodata, encodes a value as bytes
@ -45,19 +45,21 @@ def pack(fmt, nodata, value):
value = nodata
return struct.pack(fmt, value)
# packs the given values together as bytes
def encode(fmt, nodata, values):
chunks = [pack(fmt, nodata, v) for v in values]
return ''.encode('ascii').join(chunks)
return "".encode("ascii").join(chunks)
###############################################################################
#
def test_arg_init():
gdaltest.argDriver = gdal.GetDriverByName('ARG')
gdaltest.argDriver = gdal.GetDriverByName("ARG")
if gdaltest.argDriver is None:
pytest.skip()
@ -75,53 +77,64 @@ def test_arg_init():
"cols": %(cols)d
}"""
gdaltest.argDefaults = {
'xmin': 0.0,
'ymin': 0.0,
'xmax': 2.0,
'ymax': 2.0,
'width': 1.0,
'height': 1.0,
'rows': 2,
'cols': 2
"xmin": 0.0,
"ymin": 0.0,
"xmax": 2.0,
"ymax": 2.0,
"width": 1.0,
"height": 1.0,
"rows": 2,
"cols": 2,
}
# None means "no data"
gdaltest.argTests = [
{'formats': [('int8', '>b', -(1 << 7)),
('int16', '>h', -(1 << 15)),
('int32', '>i', -(1 << 31)),
('int64', '>q', -(1 << 63))],
'data': [None, 2, -3, -4]},
{'formats': [('uint8', '>B', (1 << 8) - 1),
('uint16', '>H', (1 << 16) - 1),
('uint32', '>I', (1 << 32) - 1),
('uint64', '>Q', (1 << 64) - 1)],
'data': [None, 2, 3, 4]},
{'formats': [('float32', '>f', gdaltest.NaN()),
('float64', '>d', gdaltest.NaN())],
'data': [None, 1.1, -20.02, 300.003]},
{
"formats": [
("int8", ">b", -(1 << 7)),
("int16", ">h", -(1 << 15)),
("int32", ">i", -(1 << 31)),
("int64", ">q", -(1 << 63)),
],
"data": [None, 2, -3, -4],
},
{
"formats": [
("uint8", ">B", (1 << 8) - 1),
("uint16", ">H", (1 << 16) - 1),
("uint32", ">I", (1 << 32) - 1),
("uint64", ">Q", (1 << 64) - 1),
],
"data": [None, 2, 3, 4],
},
{
"formats": [
("float32", ">f", gdaltest.NaN()),
("float64", ">d", gdaltest.NaN()),
],
"data": [None, 1.1, -20.02, 300.003],
},
]
for d in gdaltest.argTests:
for (name, fmt, nodata) in d['formats']:
arg = open('data/arg-' + name + '.arg', 'wb')
arg.write(encode(fmt, nodata, d['data']))
for (name, fmt, nodata) in d["formats"]:
arg = open("data/arg-" + name + ".arg", "wb")
arg.write(encode(fmt, nodata, d["data"]))
arg.close()
meta = copy(gdaltest.argDefaults)
meta.update(fmt='arg-' + name, dt=name)
json = open('data/arg-' + name + '.json', 'w')
meta.update(fmt="arg-" + name, dt=name)
json = open("data/arg-" + name + ".json", "w")
json.write(gdaltest.argJsontpl % meta)
json.close()
ds = gdal.Open('data/arg-' + gdaltest.argTests[0]['formats'][1][0] + '.arg')
ds = gdal.Open("data/arg-" + gdaltest.argTests[0]["formats"][1][0] + ".arg")
if ds is None:
gdaltest.argDriver = None
if gdaltest.argDriver is None:
pytest.skip()
def test_arg_unsupported():
if gdaltest.argDriver is None:
@ -129,79 +142,83 @@ def test_arg_unsupported():
# int8 is unsupported
for d in gdaltest.argTests:
for (name, _, _) in d['formats']:
if name == 'int64' or name == 'uint64':
with gdaltest.error_handler('CPLQuietErrorHandler'):
ds = gdal.Open('data/arg-' + name + '.arg')
for (name, _, _) in d["formats"]:
if name == "int64" or name == "uint64":
with gdaltest.error_handler("CPLQuietErrorHandler"):
ds = gdal.Open("data/arg-" + name + ".arg")
assert ds is None
else:
ds = gdal.Open('data/arg-' + name + '.arg')
ds = gdal.Open("data/arg-" + name + ".arg")
assert ds is not None
def test_arg_getrastercount():
if gdaltest.argDriver is None:
pytest.skip()
for d in gdaltest.argTests:
for (name, _, _) in d['formats']:
with gdaltest.error_handler('CPLQuietErrorHandler'):
ds = gdal.Open('data/arg-' + name + '.arg')
for (name, _, _) in d["formats"]:
with gdaltest.error_handler("CPLQuietErrorHandler"):
ds = gdal.Open("data/arg-" + name + ".arg")
if ds is None:
continue
assert ds.RasterCount == 1
def test_arg_getgeotransform():
if gdaltest.argDriver is None:
pytest.skip()
for d in gdaltest.argTests:
for (name, _, _) in d['formats']:
with gdaltest.error_handler('CPLQuietErrorHandler'):
ds = gdal.Open('data/arg-' + name + '.arg')
for (name, _, _) in d["formats"]:
with gdaltest.error_handler("CPLQuietErrorHandler"):
ds = gdal.Open("data/arg-" + name + ".arg")
if ds is None:
continue
gt = ds.GetGeoTransform()
assert (gt[0] == 0 and \
gt[1] == 1 and \
gt[2] == 0 and \
gt[3] == 2 and \
gt[4] == 0 and \
gt[5] == -1)
assert (
gt[0] == 0
and gt[1] == 1
and gt[2] == 0
and gt[3] == 2
and gt[4] == 0
and gt[5] == -1
)
def test_arg_blocksize():
if gdaltest.argDriver is None:
pytest.skip()
tifDriver = gdal.GetDriverByName('GTiff')
tifDriver = gdal.GetDriverByName("GTiff")
assert tifDriver is not None
ds = gdal.Open('data/utm.tif')
ds = gdal.Open("data/utm.tif")
xsize = ds.RasterXSize
ysize = ds.RasterYSize
# create a blocked tiff, where blocks don't line up evenly
# with the image boundary
ds2 = tifDriver.CreateCopy('data/utm-uneven-blocks.tif', ds, False, ['BLOCKXSIZE=25', 'BLOCKYSIZE=25', 'TILED=NO'])
ds2 = tifDriver.CreateCopy(
"data/utm-uneven-blocks.tif",
ds,
False,
["BLOCKXSIZE=25", "BLOCKYSIZE=25", "TILED=NO"],
)
# convert the new blocked tiff to arg
ds = gdaltest.argDriver.CreateCopy('data/utm.arg', ds2, False)
ds = gdaltest.argDriver.CreateCopy("data/utm.arg", ds2, False)
ds2 = None
ds = None
stat = os.stat('data/utm.arg')
stat = os.stat("data/utm.arg")
os.remove('data/utm-uneven-blocks.tif')
gdal.GetDriverByName('ARG').Delete('data/utm.arg')
os.remove("data/utm-uneven-blocks.tif")
gdal.GetDriverByName("ARG").Delete("data/utm.arg")
assert stat.st_size == (xsize * ysize)
@ -215,34 +232,34 @@ def test_arg_layername():
if gdaltest.argDriver is None:
pytest.skip()
ds = gdal.Open('data/arg-int16.arg')
ds = gdal.Open("data/arg-int16.arg")
lyr = 'ARG FTW'
lyr = "ARG FTW"
# set the layer name
ds.SetMetadataItem('LAYER', lyr)
ds.SetMetadataItem("LAYER", lyr)
# did the layer name stick?
assert ds.GetMetadata()['LAYER'] == lyr
assert ds.GetMetadata()["LAYER"] == lyr
# copy the dataset to a new ARG
ds2 = gdaltest.argDriver.CreateCopy('data/arg-int16-2.arg', ds, False)
ds2 = gdaltest.argDriver.CreateCopy("data/arg-int16-2.arg", ds, False)
ds = None
del ds2
# open the new dataset
ds = gdal.Open('data/arg-int16-2.arg')
ds = gdal.Open("data/arg-int16-2.arg")
lyr2 = ds.GetMetadata()['LAYER']
lyr2 = ds.GetMetadata()["LAYER"]
ds = None
gdal.GetDriverByName('ARG').Delete('data/arg-int16-2.arg')
gdal.GetDriverByName("ARG").Delete("data/arg-int16-2.arg")
# does the new dataset's layer match the layer set before copying
assert lyr2 == lyr
os.unlink('data/arg-int16.arg.aux.xml')
os.unlink("data/arg-int16.arg.aux.xml")
def test_arg_nodata():
@ -253,7 +270,7 @@ def test_arg_nodata():
if gdaltest.argDriver is None:
pytest.skip()
ds = gdal.Open('data/arg-int8.arg')
ds = gdal.Open("data/arg-int8.arg")
assert ds.GetRasterBand(1).GetNoDataValue() == 128
@ -269,30 +286,30 @@ def test_arg_byteorder():
if gdaltest.argDriver is None:
pytest.skip()
tifDriver = gdal.GetDriverByName('GTiff')
tifDriver = gdal.GetDriverByName("GTiff")
assert tifDriver is not None
for d in gdaltest.argTests:
for (name, _, _) in d['formats']:
for (name, _, _) in d["formats"]:
basename = 'data/arg-' + name
with gdaltest.error_handler('CPLQuietErrorHandler'):
orig = gdal.Open(basename + '.arg')
basename = "data/arg-" + name
with gdaltest.error_handler("CPLQuietErrorHandler"):
orig = gdal.Open(basename + ".arg")
if orig is None:
continue
dest = tifDriver.CreateCopy(basename + '.tif', orig, False)
dest = tifDriver.CreateCopy(basename + ".tif", orig, False)
assert dest is not None
mirror = gdaltest.argDriver.CreateCopy(basename + '2.arg', dest, False)
mirror = gdaltest.argDriver.CreateCopy(basename + "2.arg", dest, False)
assert mirror is not None
orig = None
dest = None
mirror = None
tmp1 = open(basename + '.arg', 'rb')
tmp2 = open(basename + '2.arg', 'rb')
tmp1 = open(basename + ".arg", "rb")
tmp2 = open(basename + "2.arg", "rb")
data1 = tmp1.read()
data2 = tmp2.read()
@ -300,22 +317,17 @@ def test_arg_byteorder():
tmp1.close()
tmp2.close()
gdal.GetDriverByName('GTiff').Delete(basename + '.tif')
gdal.GetDriverByName('ARG').Delete(basename + '2.arg')
gdal.GetDriverByName("GTiff").Delete(basename + ".tif")
gdal.GetDriverByName("ARG").Delete(basename + "2.arg")
assert data1 == data2
def test_arg_destroy():
if gdaltest.argDriver is None:
pytest.skip()
for d in gdaltest.argTests:
for (name, _, _) in d['formats']:
os.remove('data/arg-' + name + '.arg')
os.remove('data/arg-' + name + '.json')
for (name, _, _) in d["formats"]:
os.remove("data/arg-" + name + ".arg")
os.remove("data/arg-" + name + ".json")

File diff suppressed because it is too large Load Diff

View File

@ -28,32 +28,36 @@
# DEALINGS IN THE SOFTWARE.
###############################################################################
from osgeo import gdal
import gdaltest
import pytest
pytestmark = pytest.mark.require_driver('BASISU')
from osgeo import gdal
pytestmark = pytest.mark.require_driver("BASISU")
def test_basisu_read_etc1s():
ds = gdal.Open('data/basisu/byte_etc1s.basis')
assert ds.GetMetadataItem('COMPRESSION', 'IMAGE_STRUCTURE') == 'ETC1S'
ds = gdal.Open("data/basisu/byte_etc1s.basis")
assert ds.GetMetadataItem("COMPRESSION", "IMAGE_STRUCTURE") == "ETC1S"
assert ds.RasterXSize == 20
assert ds.RasterYSize == 20
assert ds.RasterCount == 3
assert [ds.GetRasterBand(i+1).Checksum() for i in range(ds.RasterCount)] == [4916] * 3
assert [ds.GetRasterBand(i + 1).Checksum() for i in range(ds.RasterCount)] == [
4916
] * 3
assert ds.GetRasterBand(1).GetOverviewCount() == 0
assert ds.GetRasterBand(1).GetOverview(0) is None
def test_basisu_read_uastc():
ds = gdal.Open('data/basisu/byte_uastc.basis')
assert ds.GetMetadataItem('COMPRESSION', 'IMAGE_STRUCTURE') == 'UASTC'
ds = gdal.Open("data/basisu/byte_uastc.basis")
assert ds.GetMetadataItem("COMPRESSION", "IMAGE_STRUCTURE") == "UASTC"
assert ds.RasterXSize == 20
assert ds.RasterYSize == 20
assert ds.RasterCount == 3
assert [ds.GetRasterBand(i+1).Checksum() for i in range(ds.RasterCount)] == [4775] * 3
assert [ds.GetRasterBand(i + 1).Checksum() for i in range(ds.RasterCount)] == [
4775
] * 3
assert ds.GetRasterBand(1).GetOverviewCount() == 0
assert ds.GetRasterBand(1).GetOverview(0) is None
@ -62,13 +66,13 @@ def test_basisu_read_two_images():
# File created with "./basisu -tex_type video -multifile_printf "file%d.png" -multifile_first 1 -multifile_num 2"
# where file1.png is ../gcore/data/stefan_full_rgba.tif
# and file2.png the output of ´gdal_translate file1.png file2.png -scale_1 0 255 255 0 -scale_2 0 255 255 0 -scale_3 0 255 255 0
ds = gdal.Open('data/basisu/two_images.basis')
ds = gdal.Open("data/basisu/two_images.basis")
assert ds.RasterXSize == 0
assert ds.RasterYSize == 0
assert ds.RasterCount == 0
subds_list = ds.GetSubDatasets()
assert len(subds_list) == 2
src_ds = gdal.Open('../gcore/data/stefan_full_rgba.tif')
src_ds = gdal.Open("../gcore/data/stefan_full_rgba.tif")
subds = gdal.Open(subds_list[0][0])
assert subds
assert subds.RasterXSize == src_ds.RasterXSize
@ -79,107 +83,143 @@ def test_basisu_read_two_images():
assert subds2.RasterXSize == src_ds.RasterXSize
assert subds2.RasterYSize == src_ds.RasterYSize
assert subds2.RasterCount == src_ds.RasterCount
assert [subds2.GetRasterBand(i+1).Checksum() for i in range(subds.RasterCount)] != [subds.GetRasterBand(i+1).Checksum() for i in range(subds.RasterCount)]
assert [
subds2.GetRasterBand(i + 1).Checksum() for i in range(subds.RasterCount)
] != [subds.GetRasterBand(i + 1).Checksum() for i in range(subds.RasterCount)]
@pytest.mark.parametrize("filename", ['BASISU:',
'BASISU:data/basisu/two_images.basis',
'BASISU:data/basisu/i_do_not_exist.basis:0',
'BASISU:data/basisu/two_images.basis:2',
])
@pytest.mark.parametrize(
"filename",
[
"BASISU:",
"BASISU:data/basisu/two_images.basis",
"BASISU:data/basisu/i_do_not_exist.basis:0",
"BASISU:data/basisu/two_images.basis:2",
],
)
def test_basisu_read_wrong_subds(filename):
with gdaltest.error_handler():
assert gdal.Open(filename) is None
def test_basisu_write_rgba_output_on_filesystem():
src_ds = gdal.Open('../gcore/data/stefan_full_rgba.tif')
out_filename = 'tmp/out.basis'
assert gdal.GetDriverByName('BASISU').CreateCopy(out_filename, src_ds) is not None
src_ds = gdal.Open("../gcore/data/stefan_full_rgba.tif")
out_filename = "tmp/out.basis"
assert gdal.GetDriverByName("BASISU").CreateCopy(out_filename, src_ds) is not None
out_ds = gdal.Open(out_filename)
assert out_ds.RasterXSize == src_ds.RasterXSize
assert out_ds.RasterYSize == src_ds.RasterYSize
assert out_ds.RasterCount == src_ds.RasterCount
got_cs = [out_ds.GetRasterBand(i+1).Checksum() for i in range(out_ds.RasterCount)]
assert got_cs in ([7694, 58409, 37321, 8494], # Linux
[7913, 58488, 37737, 8324]) # Windows
assert out_ds.GetMetadataItem('COMPRESSION', 'IMAGE_STRUCTURE') == 'ETC1S'
got_cs = [out_ds.GetRasterBand(i + 1).Checksum() for i in range(out_ds.RasterCount)]
assert got_cs in (
[7694, 58409, 37321, 8494], # Linux
[7913, 58488, 37737, 8324],
) # Windows
assert out_ds.GetMetadataItem("COMPRESSION", "IMAGE_STRUCTURE") == "ETC1S"
gdal.Unlink(out_filename)
@pytest.mark.parametrize("compression", ['ETC1S', 'UASTC'])
@pytest.mark.parametrize("compression", ["ETC1S", "UASTC"])
def test_basisu_write_compression(compression):
gdal.ErrorReset()
src_ds = gdal.Open('data/byte.tif')
out_filename = '/vsimem/out.basis'
gdal.GetDriverByName('BASISU').CreateCopy(
out_filename, src_ds, options = ['COMPRESSION=' + compression])
gdal.Unlink(out_filename + '.aux.xml')
src_ds = gdal.Open("data/byte.tif")
out_filename = "/vsimem/out.basis"
gdal.GetDriverByName("BASISU").CreateCopy(
out_filename, src_ds, options=["COMPRESSION=" + compression]
)
gdal.Unlink(out_filename + ".aux.xml")
ds = gdal.Open(out_filename)
assert ds.GetMetadataItem('COMPRESSION', 'IMAGE_STRUCTURE') == compression
assert ds.GetMetadataItem("COMPRESSION", "IMAGE_STRUCTURE") == compression
gdal.Unlink(out_filename)
def test_basisu_write_mipmap():
gdal.ErrorReset()
src_ds = gdal.Open('../gcore/data/stefan_full_rgba.tif')
out_filename = '/vsimem/out.basis'
out_ds = gdal.GetDriverByName('BASISU').CreateCopy(
out_filename, src_ds, options = ['MIPMAP=YES'])
assert gdal.GetLastErrorMsg() == ''
src_ds = gdal.Open("../gcore/data/stefan_full_rgba.tif")
out_filename = "/vsimem/out.basis"
out_ds = gdal.GetDriverByName("BASISU").CreateCopy(
out_filename, src_ds, options=["MIPMAP=YES"]
)
assert gdal.GetLastErrorMsg() == ""
assert out_ds.GetRasterBand(1).GetOverviewCount() == 7
ovr_ds = out_ds.GetRasterBand(1).GetOverview(0).GetDataset()
assert ovr_ds.RasterXSize == 81
assert ovr_ds.RasterYSize == 75
got_cs = [ovr_ds.GetRasterBand(i+1).Checksum() for i in range(ovr_ds.RasterCount)]
assert got_cs in ([19694, 16863, 11239, 35973], # Linux
[19968, 16919, 11262, 36022]) # Windows
got_cs = [ovr_ds.GetRasterBand(i + 1).Checksum() for i in range(ovr_ds.RasterCount)]
assert got_cs in (
[19694, 16863, 11239, 35973], # Linux
[19968, 16919, 11262, 36022],
) # Windows
gdal.Unlink(out_filename)
def test_basisu_write_uastc_level():
gdal.ErrorReset()
src_ds = gdal.Open('../gcore/data/stefan_full_rgba.tif')
out_filename = '/vsimem/out.basis'
assert gdal.GetDriverByName('BASISU').CreateCopy(
out_filename, src_ds, options = ['COMPRESSION=UASTC', 'UASTC_LEVEL=0']) is not None
assert gdal.GetLastErrorMsg() == ''
#size = gdal.VSIStatL(out_filename).size
assert gdal.GetDriverByName('BASISU').CreateCopy(
out_filename, src_ds, options = ['COMPRESSION=UASTC', 'UASTC_LEVEL=2']) is not None
assert gdal.GetLastErrorMsg() == ''
#new_size = gdal.VSIStatL(out_filename).size
#assert new_size != size # would only be true after LZ compression
src_ds = gdal.Open("../gcore/data/stefan_full_rgba.tif")
out_filename = "/vsimem/out.basis"
assert (
gdal.GetDriverByName("BASISU").CreateCopy(
out_filename, src_ds, options=["COMPRESSION=UASTC", "UASTC_LEVEL=0"]
)
is not None
)
assert gdal.GetLastErrorMsg() == ""
# size = gdal.VSIStatL(out_filename).size
assert (
gdal.GetDriverByName("BASISU").CreateCopy(
out_filename, src_ds, options=["COMPRESSION=UASTC", "UASTC_LEVEL=2"]
)
is not None
)
assert gdal.GetLastErrorMsg() == ""
# new_size = gdal.VSIStatL(out_filename).size
# assert new_size != size # would only be true after LZ compression
gdal.Unlink(out_filename)
def test_basisu_write_uastc_rdo_level():
gdal.ErrorReset()
src_ds = gdal.Open('../gcore/data/stefan_full_rgba.tif')
out_filename = '/vsimem/out.basis'
assert gdal.GetDriverByName('BASISU').CreateCopy(
out_filename, src_ds, options = ['COMPRESSION=UASTC', 'UASTC_RDO_LEVEL=0.3']) is not None
assert gdal.GetLastErrorMsg() == ''
#size = gdal.VSIStatL(out_filename).size
assert gdal.GetDriverByName('BASISU').CreateCopy(
out_filename, src_ds, options = ['COMPRESSION=UASTC', 'UASTC_RDO_LEVEL=3']) is not None
assert gdal.GetLastErrorMsg() == ''
#new_size = gdal.VSIStatL(out_filename).size
#assert new_size < size # would only be true after LZ compression
src_ds = gdal.Open("../gcore/data/stefan_full_rgba.tif")
out_filename = "/vsimem/out.basis"
assert (
gdal.GetDriverByName("BASISU").CreateCopy(
out_filename, src_ds, options=["COMPRESSION=UASTC", "UASTC_RDO_LEVEL=0.3"]
)
is not None
)
assert gdal.GetLastErrorMsg() == ""
# size = gdal.VSIStatL(out_filename).size
assert (
gdal.GetDriverByName("BASISU").CreateCopy(
out_filename, src_ds, options=["COMPRESSION=UASTC", "UASTC_RDO_LEVEL=3"]
)
is not None
)
assert gdal.GetLastErrorMsg() == ""
# new_size = gdal.VSIStatL(out_filename).size
# assert new_size < size # would only be true after LZ compression
gdal.Unlink(out_filename)
def test_basisu_write_etc1s_level():
gdal.ErrorReset()
src_ds = gdal.Open('../gcore/data/stefan_full_rgba.tif')
out_filename = '/vsimem/out.basis'
assert gdal.GetDriverByName('BASISU').CreateCopy(
out_filename, src_ds, options = ['ETC1S_LEVEL=0']) is not None
assert gdal.GetLastErrorMsg() == ''
src_ds = gdal.Open("../gcore/data/stefan_full_rgba.tif")
out_filename = "/vsimem/out.basis"
assert (
gdal.GetDriverByName("BASISU").CreateCopy(
out_filename, src_ds, options=["ETC1S_LEVEL=0"]
)
is not None
)
assert gdal.GetLastErrorMsg() == ""
size = gdal.VSIStatL(out_filename).size
assert gdal.GetDriverByName('BASISU').CreateCopy(
out_filename, src_ds, options = ['ETC1S_LEVEL=3']) is not None
assert gdal.GetLastErrorMsg() == ''
assert (
gdal.GetDriverByName("BASISU").CreateCopy(
out_filename, src_ds, options=["ETC1S_LEVEL=3"]
)
is not None
)
assert gdal.GetLastErrorMsg() == ""
new_size = gdal.VSIStatL(out_filename).size
assert new_size != size
gdal.Unlink(out_filename)
@ -187,15 +227,23 @@ def test_basisu_write_etc1s_level():
def test_basisu_write_etc1s_quality_level():
gdal.ErrorReset()
src_ds = gdal.Open('../gcore/data/stefan_full_rgba.tif')
out_filename = '/vsimem/out.basis'
assert gdal.GetDriverByName('BASISU').CreateCopy(
out_filename, src_ds, options = ['ETC1S_QUALITY_LEVEL=1']) is not None
assert gdal.GetLastErrorMsg() == ''
src_ds = gdal.Open("../gcore/data/stefan_full_rgba.tif")
out_filename = "/vsimem/out.basis"
assert (
gdal.GetDriverByName("BASISU").CreateCopy(
out_filename, src_ds, options=["ETC1S_QUALITY_LEVEL=1"]
)
is not None
)
assert gdal.GetLastErrorMsg() == ""
size = gdal.VSIStatL(out_filename).size
assert gdal.GetDriverByName('BASISU').CreateCopy(
out_filename, src_ds, options = ['ETC1S_QUALITY_LEVEL=255']) is not None
assert gdal.GetLastErrorMsg() == ''
assert (
gdal.GetDriverByName("BASISU").CreateCopy(
out_filename, src_ds, options=["ETC1S_QUALITY_LEVEL=255"]
)
is not None
)
assert gdal.GetLastErrorMsg() == ""
new_size = gdal.VSIStatL(out_filename).size
assert new_size > size
gdal.Unlink(out_filename)
@ -203,37 +251,64 @@ def test_basisu_write_etc1s_quality_level():
def test_basisu_write_etc1s_clusters_options():
gdal.ErrorReset()
src_ds = gdal.Open('../gcore/data/stefan_full_rgba.tif')
out_filename = '/vsimem/out.basis'
assert gdal.GetDriverByName('BASISU').CreateCopy(
out_filename, src_ds, options = []) is not None
assert gdal.GetLastErrorMsg() == ''
src_ds = gdal.Open("../gcore/data/stefan_full_rgba.tif")
out_filename = "/vsimem/out.basis"
assert (
gdal.GetDriverByName("BASISU").CreateCopy(out_filename, src_ds, options=[])
is not None
)
assert gdal.GetLastErrorMsg() == ""
size = gdal.VSIStatL(out_filename).size
assert gdal.GetDriverByName('BASISU').CreateCopy(
out_filename, src_ds, options = ['ETC1S_MAX_ENDPOINTS_CLUSTERS=16128',
'ETC1S_MAX_SELECTOR_CLUSTERS=16128']) is not None
assert gdal.GetLastErrorMsg() == ''
assert (
gdal.GetDriverByName("BASISU").CreateCopy(
out_filename,
src_ds,
options=[
"ETC1S_MAX_ENDPOINTS_CLUSTERS=16128",
"ETC1S_MAX_SELECTOR_CLUSTERS=16128",
],
)
is not None
)
assert gdal.GetLastErrorMsg() == ""
new_size = gdal.VSIStatL(out_filename).size
assert new_size > size
gdal.Unlink(out_filename)
gdal.ErrorReset()
with gdaltest.error_handler():
assert gdal.GetDriverByName('BASISU').CreateCopy(
out_filename, src_ds, options = ['ETC1S_MAX_ENDPOINTS_CLUSTERS=16129', # too big
'ETC1S_MAX_SELECTOR_CLUSTERS=16128']) is None
assert (
gdal.GetDriverByName("BASISU").CreateCopy(
out_filename,
src_ds,
options=[
"ETC1S_MAX_ENDPOINTS_CLUSTERS=16129", # too big
"ETC1S_MAX_SELECTOR_CLUSTERS=16128",
],
)
is None
)
def test_basisu_write_colorspace():
gdal.ErrorReset()
src_ds = gdal.Open('../gcore/data/stefan_full_rgba.tif')
out_filename = '/vsimem/out.basis'
assert gdal.GetDriverByName('BASISU').CreateCopy(
out_filename, src_ds, options = ['COLORSPACE=PERCEPTUAL_SRGB']) is not None
assert gdal.GetLastErrorMsg() == ''
src_ds = gdal.Open("../gcore/data/stefan_full_rgba.tif")
out_filename = "/vsimem/out.basis"
assert (
gdal.GetDriverByName("BASISU").CreateCopy(
out_filename, src_ds, options=["COLORSPACE=PERCEPTUAL_SRGB"]
)
is not None
)
assert gdal.GetLastErrorMsg() == ""
size = gdal.VSIStatL(out_filename).size
assert gdal.GetDriverByName('BASISU').CreateCopy(
out_filename, src_ds, options = ['COLORSPACE=LINEAR']) is not None
assert gdal.GetLastErrorMsg() == ''
assert (
gdal.GetDriverByName("BASISU").CreateCopy(
out_filename, src_ds, options=["COLORSPACE=LINEAR"]
)
is not None
)
assert gdal.GetLastErrorMsg() == ""
new_size = gdal.VSIStatL(out_filename).size
assert new_size != size
gdal.Unlink(out_filename)
@ -241,60 +316,82 @@ def test_basisu_write_colorspace():
def test_basisu_write_num_threads():
gdal.ErrorReset()
src_ds = gdal.Open('../gcore/data/stefan_full_rgba.tif')
out_filename = '/vsimem/out.basis'
src_ds = gdal.Open("../gcore/data/stefan_full_rgba.tif")
out_filename = "/vsimem/out.basis"
# Just check that it works
assert gdal.GetDriverByName('BASISU').CreateCopy(
out_filename, src_ds, options = ['NUM_THREADS=1']) is not None
assert gdal.GetLastErrorMsg() == ''
assert (
gdal.GetDriverByName("BASISU").CreateCopy(
out_filename, src_ds, options=["NUM_THREADS=1"]
)
is not None
)
assert gdal.GetLastErrorMsg() == ""
gdal.Unlink(out_filename)
def test_basisu_write_etc1s_incompatible_or_missing_options():
src_ds = gdal.Open('../gcore/data/stefan_full_rgba.tif')
out_filename = '/vsimem/out.basis'
src_ds = gdal.Open("../gcore/data/stefan_full_rgba.tif")
out_filename = "/vsimem/out.basis"
gdal.ErrorReset()
with gdaltest.error_handler():
assert gdal.GetDriverByName('BASISU').CreateCopy(
out_filename, src_ds, options = ['ETC1S_MAX_ENDPOINTS_CLUSTERS=16128']) is None
assert gdal.GetLastErrorMsg() != ''
assert (
gdal.GetDriverByName("BASISU").CreateCopy(
out_filename, src_ds, options=["ETC1S_MAX_ENDPOINTS_CLUSTERS=16128"]
)
is None
)
assert gdal.GetLastErrorMsg() != ""
gdal.ErrorReset()
with gdaltest.error_handler():
assert gdal.GetDriverByName('BASISU').CreateCopy(
out_filename, src_ds, options = ['ETC1S_MAX_SELECTOR_CLUSTERS=16128']) is None
assert gdal.GetLastErrorMsg() != ''
assert (
gdal.GetDriverByName("BASISU").CreateCopy(
out_filename, src_ds, options=["ETC1S_MAX_SELECTOR_CLUSTERS=16128"]
)
is None
)
assert gdal.GetLastErrorMsg() != ""
gdal.ErrorReset()
with gdaltest.error_handler():
assert gdal.GetDriverByName('BASISU').CreateCopy(
out_filename, src_ds, options = ['ETC1S_QUALITY_LEVEL=1',
'ETC1S_MAX_ENDPOINTS_CLUSTERS=16128']) is not None
assert gdal.GetLastErrorType() == 2 # warning
assert (
gdal.GetDriverByName("BASISU").CreateCopy(
out_filename,
src_ds,
options=["ETC1S_QUALITY_LEVEL=1", "ETC1S_MAX_ENDPOINTS_CLUSTERS=16128"],
)
is not None
)
assert gdal.GetLastErrorType() == 2 # warning
gdal.Unlink(out_filename)
gdal.ErrorReset()
with gdaltest.error_handler():
assert gdal.GetDriverByName('BASISU').CreateCopy(
out_filename, src_ds, options = ['ETC1S_QUALITY_LEVEL=1',
'ETC1S_MAX_SELECTOR_CLUSTERS=16128']) is not None
assert gdal.GetLastErrorType() == 2 # warning
assert (
gdal.GetDriverByName("BASISU").CreateCopy(
out_filename,
src_ds,
options=["ETC1S_QUALITY_LEVEL=1", "ETC1S_MAX_SELECTOR_CLUSTERS=16128"],
)
is not None
)
assert gdal.GetLastErrorType() == 2 # warning
gdal.Unlink(out_filename)
def test_basisu_write_incompatible_source():
out_filename = '/vsimem/out.basis'
out_filename = "/vsimem/out.basis"
src_ds = gdal.GetDriverByName('MEM').Create('', 1, 1, 0)
src_ds = gdal.GetDriverByName("MEM").Create("", 1, 1, 0)
with gdaltest.error_handler():
assert gdal.GetDriverByName('BASISU').CreateCopy(out_filename, src_ds) is None
assert gdal.GetDriverByName("BASISU").CreateCopy(out_filename, src_ds) is None
src_ds = gdal.GetDriverByName('MEM').Create('', 1, 1, 5)
src_ds = gdal.GetDriverByName("MEM").Create("", 1, 1, 5)
with gdaltest.error_handler():
assert gdal.GetDriverByName('BASISU').CreateCopy(out_filename, src_ds) is None
assert gdal.GetDriverByName("BASISU").CreateCopy(out_filename, src_ds) is None
src_ds = gdal.GetDriverByName('MEM').Create('', 1, 1, 1, gdal.GDT_UInt16)
src_ds = gdal.GetDriverByName("MEM").Create("", 1, 1, 1, gdal.GDT_UInt16)
with gdaltest.error_handler():
assert gdal.GetDriverByName('BASISU').CreateCopy(out_filename, src_ds) is None
assert gdal.GetDriverByName("BASISU").CreateCopy(out_filename, src_ds) is None

View File

@ -28,74 +28,73 @@
# DEALINGS IN THE SOFTWARE.
###############################################################################
from osgeo import gdal
import gdaltest
from osgeo import gdal
###############################################################################
# Test reading a little-endian BLX
def test_blx_1():
prj = 'WGS84'
prj = "WGS84"
gt = [20.0004166, 0.0008333, 0.0, 50.0004166, 0.0, -0.0008333]
tst = gdaltest.GDALTest('BLX', 'blx/s4103.blx', 1, 47024)
tst = gdaltest.GDALTest("BLX", "blx/s4103.blx", 1, 47024)
return tst.testOpen(check_prj=prj, check_gt=gt)
###############################################################################
# Test reading a big-endian BLX
def test_blx_2():
prj = 'WGS84'
prj = "WGS84"
gt = [20.0004166, 0.0008333, 0.0, 50.0004166, 0.0, -0.0008333]
tst = gdaltest.GDALTest('BLX', 'blx/s4103.xlb', 1, 47024)
tst = gdaltest.GDALTest("BLX", "blx/s4103.xlb", 1, 47024)
return tst.testOpen(check_prj=prj, check_gt=gt)
###############################################################################
# Test writing a little-endian BLX
def test_blx_3():
tst = gdaltest.GDALTest('BLX', 'blx/s4103.xlb', 1, 47024)
tst = gdaltest.GDALTest("BLX", "blx/s4103.xlb", 1, 47024)
return tst.testCreateCopy(check_gt=1, check_srs=1)
###############################################################################
# Test writing a big-endian BLX
def test_blx_4():
tst = gdaltest.GDALTest('BLX', 'blx/s4103.blx', 1, 47024, options=['BIGENDIAN=YES'])
tst = gdaltest.GDALTest("BLX", "blx/s4103.blx", 1, 47024, options=["BIGENDIAN=YES"])
return tst.testCreateCopy(check_gt=1, check_srs=1)
###############################################################################
# Test overviews
def test_blx_5():
ds = gdal.Open('data/blx/s4103.blx')
ds = gdal.Open("data/blx/s4103.blx")
band = ds.GetRasterBand(1)
assert band.GetOverviewCount() == 4, 'did not get expected overview count'
assert band.GetOverviewCount() == 4, "did not get expected overview count"
cs = band.GetOverview(0).Checksum()
assert cs == 42981, ('wrong overview checksum (%d)' % cs)
assert cs == 42981, "wrong overview checksum (%d)" % cs
cs = band.GetOverview(1).Checksum()
assert cs == 61363, ('wrong overview checksum (%d)' % cs)
assert cs == 61363, "wrong overview checksum (%d)" % cs
cs = band.GetOverview(2).Checksum()
assert cs == 48060, ('wrong overview checksum (%d)' % cs)
assert cs == 48060, "wrong overview checksum (%d)" % cs
cs = band.GetOverview(3).Checksum()
assert cs == 12058, ('wrong overview checksum (%d)' % cs)
assert cs == 12058, "wrong overview checksum (%d)" % cs

View File

@ -28,23 +28,21 @@
# DEALINGS IN THE SOFTWARE.
###############################################################################
from osgeo import gdal
from osgeo import osr
import gdaltest
import pytest
from osgeo import gdal, osr
###############################################################################
# Test driver availability
def test_bsb_0():
gdaltest.bsb_dr = gdal.GetDriverByName('BSB')
gdaltest.bsb_dr = gdal.GetDriverByName("BSB")
if gdaltest.bsb_dr is None:
pytest.skip()
###############################################################################
# Test Read
@ -53,10 +51,11 @@ def test_bsb_1():
if gdaltest.bsb_dr is None:
pytest.skip()
tst = gdaltest.GDALTest('BSB', 'bsb/rgbsmall.kap', 1, 30321)
tst = gdaltest.GDALTest("BSB", "bsb/rgbsmall.kap", 1, 30321)
return tst.testOpen()
###############################################################################
# Test CreateCopy
@ -66,13 +65,14 @@ def test_bsb_2():
pytest.skip()
md = gdaltest.bsb_dr.GetMetadata()
if 'DMD_CREATIONDATATYPES' not in md:
if "DMD_CREATIONDATATYPES" not in md:
pytest.skip()
tst = gdaltest.GDALTest('BSB', 'bsb/rgbsmall.kap', 1, 30321)
tst = gdaltest.GDALTest("BSB", "bsb/rgbsmall.kap", 1, 30321)
return tst.testCreateCopy()
###############################################################################
# Read a BSB with an index table at the end (#2782)
# The rgbsmall_index.kap has been generated from rgbsmall.kap by moving the
@ -84,10 +84,11 @@ def test_bsb_3():
if gdaltest.bsb_dr is None:
pytest.skip()
tst = gdaltest.GDALTest('BSB', 'bsb/rgbsmall_index.kap', 1, 30321)
tst = gdaltest.GDALTest("BSB", "bsb/rgbsmall_index.kap", 1, 30321)
return tst.testOpen()
###############################################################################
# Read a BSB without an index table but with 0 in the middle of line data
# The rgbsmall_with_line_break.kap has been generated from rgbsmall.kap by
@ -98,10 +99,11 @@ def test_bsb_4():
if gdaltest.bsb_dr is None:
pytest.skip()
tst = gdaltest.GDALTest('BSB', 'bsb/rgbsmall_with_line_break.kap', 1, 30321)
tst = gdaltest.GDALTest("BSB", "bsb/rgbsmall_with_line_break.kap", 1, 30321)
return tst.testOpen()
###############################################################################
# Read a truncated BSB (at the level of the written scanline number starting a new row)
@ -110,14 +112,15 @@ def test_bsb_5():
if gdaltest.bsb_dr is None:
pytest.skip()
tst = gdaltest.GDALTest('BSB', 'bsb/rgbsmall_truncated.kap', 1, 29696)
tst = gdaltest.GDALTest("BSB", "bsb/rgbsmall_truncated.kap", 1, 29696)
gdal.PushErrorHandler('CPLQuietErrorHandler')
gdal.PushErrorHandler("CPLQuietErrorHandler")
ret = tst.testOpen()
gdal.PopErrorHandler()
return ret
###############################################################################
# Read another truncated BSB (in the middle of row data)
@ -126,25 +129,41 @@ def test_bsb_6():
if gdaltest.bsb_dr is None:
pytest.skip()
tst = gdaltest.GDALTest('BSB', 'bsb/rgbsmall_truncated2.kap', 1, 29696)
tst = gdaltest.GDALTest("BSB", "bsb/rgbsmall_truncated2.kap", 1, 29696)
gdal.PushErrorHandler('CPLQuietErrorHandler')
gdal.PushErrorHandler("CPLQuietErrorHandler")
ret = tst.testOpen()
gdal.PopErrorHandler()
return ret
###############################################################################
def test_bsb_tmerc():
if gdaltest.bsb_dr is None:
pytest.skip()
ds = gdal.Open('data/bsb/transverse_mercator.kap')
ds = gdal.Open("data/bsb/transverse_mercator.kap")
gt = ds.GetGeoTransform()
expected_gt = [28487.6637325402, 1.2711141208521637, 0.009061669923111566,
6539651.728646593, 0.015209115944776083, -1.267821834560455]
assert min([gt[i] == pytest.approx(expected_gt[i], abs=1e-8 * abs(expected_gt[i])) for i in range(6)]) == True, gt
expected_gt = [
28487.6637325402,
1.2711141208521637,
0.009061669923111566,
6539651.728646593,
0.015209115944776083,
-1.267821834560455,
]
assert (
min(
[
gt[i] == pytest.approx(expected_gt[i], abs=1e-8 * abs(expected_gt[i]))
for i in range(6)
]
)
== True
), gt
expected_wkt = """PROJCS["unnamed",
GEOGCS["WGS 84",
DATUM["WGS_1984",
@ -167,7 +186,7 @@ def test_bsb_tmerc():
AXIS["Northing",NORTH]]"""
expected_sr = osr.SpatialReference()
expected_sr.SetFromUserInput(expected_wkt)
expected_sr.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER);
expected_sr.SetAxisMappingStrategy(osr.OAMS_TRADITIONAL_GIS_ORDER)
got_sr = ds.GetSpatialRef()
assert expected_sr.IsSame(got_sr), got_sr.ExportToWkt()
got_sr = ds.GetGCPSpatialRef()
@ -176,11 +195,14 @@ def test_bsb_tmerc():
gcps = ds.GetGCPs()
assert len(gcps) == 3
assert gcps[0].GCPPixel == 25 and \
gcps[0].GCPLine == 577 and \
gcps[0].GCPX == pytest.approx(28524.670169107143, abs=1e-5) and \
gcps[0].GCPY == pytest.approx(6538920.57567595, abs=1e-5) and \
gcps[0].GCPZ == 0
assert (
gcps[0].GCPPixel == 25
and gcps[0].GCPLine == 577
and gcps[0].GCPX == pytest.approx(28524.670169107143, abs=1e-5)
and gcps[0].GCPY == pytest.approx(6538920.57567595, abs=1e-5)
and gcps[0].GCPZ == 0
)
###############################################################################
@ -189,5 +211,8 @@ def test_bsb_cutline():
if gdaltest.bsb_dr is None:
pytest.skip()
ds = gdal.Open('data/bsb/australia4c.kap')
assert ds.GetMetadataItem('BSB_CUTLINE') == 'POLYGON ((112.72859333333334 -8.25404666666667,156.57827333333333 -7.66159166666667,164.28394166666666 -40.89653000000000,106.53042166666667 -41.14970000000000))'
ds = gdal.Open("data/bsb/australia4c.kap")
assert (
ds.GetMetadataItem("BSB_CUTLINE")
== "POLYGON ((112.72859333333334 -8.25404666666667,156.57827333333333 -7.66159166666667,164.28394166666666 -40.89653000000000,106.53042166666667 -41.14970000000000))"
)

View File

@ -28,23 +28,25 @@
# DEALINGS IN THE SOFTWARE.
###############################################################################
from osgeo import osr
from osgeo import gdal
import gdaltest
from osgeo import gdal, osr
###############################################################################
# Test CreateCopy() of int16.tif
def test_bt_1():
tst = gdaltest.GDALTest('BT', 'int16.tif', 1, 4672)
tst = gdaltest.GDALTest("BT", "int16.tif", 1, 4672)
srs = osr.SpatialReference()
srs.SetWellKnownGeogCS('NAD27')
return tst.testCreateCopy(vsimem=1, check_srs=srs.ExportToWkt(),
check_gt=(-67.00041667, 0.00083333, 0.0, 50.000416667, 0.0, -0.00083333))
srs.SetWellKnownGeogCS("NAD27")
return tst.testCreateCopy(
vsimem=1,
check_srs=srs.ExportToWkt(),
check_gt=(-67.00041667, 0.00083333, 0.0, 50.000416667, 0.0, -0.00083333),
)
###############################################################################
# Test CreateCopy() of int32.tif
@ -52,11 +54,14 @@ def test_bt_1():
def test_bt_2():
tst = gdaltest.GDALTest('BT', 'int32.tif', 1, 4672)
tst = gdaltest.GDALTest("BT", "int32.tif", 1, 4672)
srs = osr.SpatialReference()
srs.SetWellKnownGeogCS('NAD27')
return tst.testCreateCopy(check_srs=srs.ExportToWkt(),
check_gt=(-67.00041667, 0.00083333, 0.0, 50.000416667, 0.0, -0.00083333))
srs.SetWellKnownGeogCS("NAD27")
return tst.testCreateCopy(
check_srs=srs.ExportToWkt(),
check_gt=(-67.00041667, 0.00083333, 0.0, 50.000416667, 0.0, -0.00083333),
)
###############################################################################
# Test CreateCopy() of float32.tif
@ -64,11 +69,14 @@ def test_bt_2():
def test_bt_3():
tst = gdaltest.GDALTest('BT', 'float32.tif', 1, 4672)
tst = gdaltest.GDALTest("BT", "float32.tif", 1, 4672)
srs = osr.SpatialReference()
srs.SetWellKnownGeogCS('NAD27')
return tst.testCreateCopy(check_srs=srs.ExportToWkt(),
check_gt=(-67.00041667, 0.00083333, 0.0, 50.000416667, 0.0, -0.00083333))
srs.SetWellKnownGeogCS("NAD27")
return tst.testCreateCopy(
check_srs=srs.ExportToWkt(),
check_gt=(-67.00041667, 0.00083333, 0.0, 50.000416667, 0.0, -0.00083333),
)
###############################################################################
# Test Create() of float32.tif
@ -76,36 +84,36 @@ def test_bt_3():
def test_bt_4():
tst = gdaltest.GDALTest('BT', 'float32.tif', 1, 4672)
tst = gdaltest.GDALTest("BT", "float32.tif", 1, 4672)
return tst.testCreate(out_bands=1)
###############################################################################
# Test testSetProjection() of float32.tif
def test_bt_5():
tst = gdaltest.GDALTest('BT', 'float32.tif', 1, 4672)
tst = gdaltest.GDALTest("BT", "float32.tif", 1, 4672)
return tst.testSetProjection()
###############################################################################
# Test testSetGeoTransform() of float32.tif
def test_bt_6():
tst = gdaltest.GDALTest('BT', 'float32.tif', 1, 4672)
tst = gdaltest.GDALTest("BT", "float32.tif", 1, 4672)
return tst.testSetGeoTransform()
###############################################################################
# Cleanup
def test_bt_cleanup():
gdal.Unlink('/vsimem/int16.tif.prj')
gdal.Unlink('tmp/int32.tif.prj')
gdal.Unlink('tmp/float32.tif.prj')
gdal.Unlink("/vsimem/int16.tif.prj")
gdal.Unlink("tmp/int32.tif.prj")
gdal.Unlink("tmp/float32.tif.prj")

View File

@ -31,7 +31,6 @@
###############################################################################
import gdaltest
###############################################################################
@ -40,22 +39,25 @@ import gdaltest
def test_byn_1():
tst = gdaltest.GDALTest('BYN', 'byn/cgg2013ai08_reduced.byn', 1, 64764)
tst = gdaltest.GDALTest("BYN", "byn/cgg2013ai08_reduced.byn", 1, 64764)
return tst.testOpen()
###############################################################################
#
def test_byn_2():
tst = gdaltest.GDALTest('BYN', 'byn/cgg2013ai08_reduced.byn', 1, 64764)
return tst.testCreateCopy(new_filename='tmp/byn_test_2.byn')
tst = gdaltest.GDALTest("BYN", "byn/cgg2013ai08_reduced.byn", 1, 64764)
return tst.testCreateCopy(new_filename="tmp/byn_test_2.byn")
###############################################################################
#
def test_byn_invalid_header_bytes():
tst = gdaltest.GDALTest('BYN', 'byn/test_invalid_header_bytes.byn', 1, 64764)
tst = gdaltest.GDALTest("BYN", "byn/test_invalid_header_bytes.byn", 1, 64764)
return tst.testOpen()

View File

@ -29,9 +29,9 @@
###############################################################################
import gdaltest
from osgeo import gdal
import gdaltest
###############################################################################
# Source has no color table
@ -39,10 +39,11 @@ import gdaltest
def test_cals_1():
tst = gdaltest.GDALTest('CALS', 'hfa/small1bit.img', 1, 9907)
tst = gdaltest.GDALTest("CALS", "hfa/small1bit.img", 1, 9907)
return tst.testCreateCopy()
###############################################################################
# Source has a color table (0,0,0),(255,255,255)
@ -50,31 +51,33 @@ def test_cals_1():
def test_cals_2():
# Has no color table
tst = gdaltest.GDALTest('CALS', '../../gcore/data/oddsize1bit.tif', 1, 3883)
tst = gdaltest.GDALTest("CALS", "../../gcore/data/oddsize1bit.tif", 1, 3883)
return tst.testCreateCopy()
###############################################################################
# Source has a color table (255,255,255),(0,0,0)
def test_cals_3():
src_ds = gdal.Open('../gcore/data/oddsize1bit.tif')
tmp_ds = gdal.GetDriverByName('CALS').CreateCopy('/vsimem/cals_2_tmp.cal', src_ds)
tmp_ds.SetMetadataItem('TIFFTAG_XRESOLUTION', '600')
tmp_ds.SetMetadataItem('TIFFTAG_YRESOLUTION', '600')
out_ds = gdal.GetDriverByName('CALS').CreateCopy('/vsimem/cals_2.cal', tmp_ds)
assert gdal.VSIStatL('/vsimem/cals_2.cal.aux.xml') is None
src_ds = gdal.Open("../gcore/data/oddsize1bit.tif")
tmp_ds = gdal.GetDriverByName("CALS").CreateCopy("/vsimem/cals_2_tmp.cal", src_ds)
tmp_ds.SetMetadataItem("TIFFTAG_XRESOLUTION", "600")
tmp_ds.SetMetadataItem("TIFFTAG_YRESOLUTION", "600")
out_ds = gdal.GetDriverByName("CALS").CreateCopy("/vsimem/cals_2.cal", tmp_ds)
assert gdal.VSIStatL("/vsimem/cals_2.cal.aux.xml") is None
assert out_ds.GetRasterBand(1).Checksum() == 3883
assert out_ds.GetMetadataItem('PIXEL_PATH') is None
assert out_ds.GetMetadataItem('TIFFTAG_XRESOLUTION') == '600'
assert out_ds.GetMetadataItem("PIXEL_PATH") is None
assert out_ds.GetMetadataItem("TIFFTAG_XRESOLUTION") == "600"
assert out_ds.GetRasterBand(1).GetColorInterpretation() == gdal.GCI_PaletteIndex
tmp_ds = None
out_ds = None
gdal.Unlink('/vsimem/cals_2_tmp.cal')
gdal.Unlink('/vsimem/cals_2_tmp.cal.aux.xml')
gdal.Unlink('/vsimem/cals_2.cal')
gdal.Unlink("/vsimem/cals_2_tmp.cal")
gdal.Unlink("/vsimem/cals_2_tmp.cal.aux.xml")
gdal.Unlink("/vsimem/cals_2.cal")
###############################################################################
# Test CreateCopy() error conditions
@ -83,61 +86,67 @@ def test_cals_3():
def test_cals_4():
# 0 band
src_ds = gdal.GetDriverByName('MEM').Create('', 1, 1, 0)
src_ds = gdal.GetDriverByName("MEM").Create("", 1, 1, 0)
gdal.PushErrorHandler()
out_ds = gdal.GetDriverByName('CALS').CreateCopy('/vsimem/cals_4.cal', src_ds)
out_ds = gdal.GetDriverByName("CALS").CreateCopy("/vsimem/cals_4.cal", src_ds)
gdal.PopErrorHandler()
assert out_ds is None
# 2 bands
src_ds = gdal.GetDriverByName('MEM').Create('', 1, 1, 2)
src_ds = gdal.GetDriverByName("MEM").Create("", 1, 1, 2)
gdal.PushErrorHandler()
out_ds = gdal.GetDriverByName('CALS').CreateCopy('/vsimem/cals_4.cal', src_ds, strict=True)
out_ds = gdal.GetDriverByName("CALS").CreateCopy(
"/vsimem/cals_4.cal", src_ds, strict=True
)
gdal.PopErrorHandler()
assert out_ds is None
# 1 band but not 1-bit
src_ds = gdal.GetDriverByName('MEM').Create('', 1, 1, 1)
src_ds = gdal.GetDriverByName("MEM").Create("", 1, 1, 1)
gdal.PushErrorHandler()
out_ds = gdal.GetDriverByName('CALS').CreateCopy('/vsimem/cals_4.cal', src_ds, strict=True)
out_ds = gdal.GetDriverByName("CALS").CreateCopy(
"/vsimem/cals_4.cal", src_ds, strict=True
)
gdal.PopErrorHandler()
assert out_ds is None
# Dimension > 999999
src_ds = gdal.GetDriverByName('MEM').Create('', 1000000, 1, 1)
src_ds.GetRasterBand(1).SetMetadataItem('NBITS', '1', 'IMAGE_STRUCTURE')
src_ds = gdal.GetDriverByName("MEM").Create("", 1000000, 1, 1)
src_ds.GetRasterBand(1).SetMetadataItem("NBITS", "1", "IMAGE_STRUCTURE")
gdal.PushErrorHandler()
out_ds = gdal.GetDriverByName('CALS').CreateCopy('/vsimem/cals_4.cal', src_ds, strict=True)
out_ds = gdal.GetDriverByName("CALS").CreateCopy(
"/vsimem/cals_4.cal", src_ds, strict=True
)
gdal.PopErrorHandler()
assert out_ds is None
# Invalid output filename
src_ds = gdal.GetDriverByName('MEM').Create('', 1, 1, 1)
src_ds.GetRasterBand(1).SetMetadataItem('NBITS', '1', 'IMAGE_STRUCTURE')
src_ds = gdal.GetDriverByName("MEM").Create("", 1, 1, 1)
src_ds.GetRasterBand(1).SetMetadataItem("NBITS", "1", "IMAGE_STRUCTURE")
gdal.PushErrorHandler()
out_ds = gdal.GetDriverByName('CALS').CreateCopy('/not_existing_dir/cals_4.cal', src_ds)
out_ds = gdal.GetDriverByName("CALS").CreateCopy(
"/not_existing_dir/cals_4.cal", src_ds
)
gdal.PopErrorHandler()
assert out_ds is None
###############################################################################
# Test PIXEL_PATH & LINE_PROGRESSION metadata item
def test_cals_5():
src_ds = gdal.GetDriverByName('MEM').Create('', 1, 1, 1)
src_ds.GetRasterBand(1).SetMetadataItem('NBITS', '1', 'IMAGE_STRUCTURE')
src_ds.SetMetadataItem('PIXEL_PATH', '90')
src_ds.SetMetadataItem('LINE_PROGRESSION', '270')
out_ds = gdal.GetDriverByName('CALS').CreateCopy('/vsimem/cals_5.cal', src_ds)
assert gdal.VSIStatL('/vsimem/cals_5.cal.aux.xml') is None
assert out_ds.GetMetadataItem('PIXEL_PATH') == '90'
assert out_ds.GetMetadataItem('LINE_PROGRESSION') == '270'
src_ds = gdal.GetDriverByName("MEM").Create("", 1, 1, 1)
src_ds.GetRasterBand(1).SetMetadataItem("NBITS", "1", "IMAGE_STRUCTURE")
src_ds.SetMetadataItem("PIXEL_PATH", "90")
src_ds.SetMetadataItem("LINE_PROGRESSION", "270")
out_ds = gdal.GetDriverByName("CALS").CreateCopy("/vsimem/cals_5.cal", src_ds)
assert gdal.VSIStatL("/vsimem/cals_5.cal.aux.xml") is None
assert out_ds.GetMetadataItem("PIXEL_PATH") == "90"
assert out_ds.GetMetadataItem("LINE_PROGRESSION") == "270"
out_ds = None
gdal.Unlink('/vsimem/cals_5.cal')
gdal.Unlink("/vsimem/cals_5.cal")
###############################################################################

View File

@ -29,7 +29,6 @@
###############################################################################
import gdaltest
###############################################################################
@ -39,9 +38,7 @@ import gdaltest
def test_ceos_1():
tst = gdaltest.GDALTest('CEOS', 'ceos/IMAGERY-75K.L-3', 4, 9956,
xoff=0, yoff=0, xsize=5932, ysize=3)
tst = gdaltest.GDALTest(
"CEOS", "ceos/IMAGERY-75K.L-3", 4, 9956, xoff=0, yoff=0, xsize=5932, ysize=3
)
return tst.testOpen()

View File

@ -29,7 +29,6 @@
###############################################################################
import gdaltest
###############################################################################
@ -38,8 +37,5 @@ import gdaltest
def test_cpg_1():
tst = gdaltest.GDALTest('CPG', 'cpg/fakecpgSIRC.hdr', 1, 0)
tst = gdaltest.GDALTest("CPG", "cpg/fakecpgSIRC.hdr", 1, 0)
return tst.testOpen()

View File

@ -28,18 +28,17 @@
# DEALINGS IN THE SOFTWARE.
###############################################################################
from osgeo import gdal
import gdaltest
from osgeo import gdal
###############################################################################
# Test a fake CTG dataset
def test_ctg_1():
tst = gdaltest.GDALTest('CTG', 'ctg/fake_grid_cell', 1, 21)
tst = gdaltest.GDALTest("CTG", "ctg/fake_grid_cell", 1, 21)
expected_gt = [421000.0, 200.0, 0.0, 5094400.0, 0.0, -200.0]
expected_srs = """PROJCS["WGS 84 / UTM zone 14N",
GEOGCS["WGS 84",
@ -65,16 +64,14 @@ def test_ctg_1():
AXIS["Northing",NORTH]]"""
ret = tst.testOpen(check_gt=expected_gt, check_prj=expected_srs)
if ret == 'success':
ds = gdal.Open('data/ctg/fake_grid_cell')
if ret == "success":
ds = gdal.Open("data/ctg/fake_grid_cell")
lst = ds.GetRasterBand(1).GetCategoryNames()
assert lst is not None and lst, 'expected non empty category names for band 1'
assert lst is not None and lst, "expected non empty category names for band 1"
lst = ds.GetRasterBand(2).GetCategoryNames()
assert lst is None, 'expected empty category names for band 2'
assert ds.GetRasterBand(1).GetNoDataValue() == 0, \
'did not get expected nodata value'
assert lst is None, "expected empty category names for band 2"
assert (
ds.GetRasterBand(1).GetNoDataValue() == 0
), "did not get expected nodata value"
return ret

File diff suppressed because it is too large Load Diff

Some files were not shown because too many files have changed in this diff Show More