Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_unpack_srid_from_bad_ewkt(self):
with pytest.raises(ArgumentError):
WKTElement('SRID=BAD SRID;POINT (1 2 3)', extended=True)
def test_check_ctor_args_bad_srid(self):
with pytest.raises(ArgumentError):
Geometry(srid='foo')
def test_check_ctor_args_management_zm(self):
with pytest.raises(ArgumentError):
Geometry(geometry_type='POINTZM', management=True)
def __init__(self, data, srid=-1, extended=False):
if extended and srid == -1:
# read srid from EWKT
if not data.startswith('SRID='):
raise ArgumentError('invalid EWKT string {}'.format(data))
data_s = data.split(';', 1)
if len(data_s) != 2:
raise ArgumentError('invalid EWKT string {}'.format(data))
header = data_s[0]
try:
srid = int(header[5:])
except ValueError:
raise ArgumentError('invalid EWKT string {}'.format(data))
_SpatialElement.__init__(self, data, srid, extended)
def process(bindvalue):
if isinstance(bindvalue, WKTElement):
if bindvalue.extended:
return '%s' % (bindvalue.data)
else:
return 'SRID=%d;%s' % (bindvalue.srid, bindvalue.data)
elif isinstance(bindvalue, WKBElement):
if dialect.name == 'sqlite' or not bindvalue.extended:
# With SpatiaLite or when the WKBElement includes a WKB value rather
# than a EWKB value we use Shapely to convert the WKBElement to an
# EWKT string
if not SHAPELY:
raise ArgumentError('Shapely is required for handling WKBElement bind '
'values when using SpatiaLite or when the bind value '
'is a WKB rather than an EWKB')
shape = to_shape(bindvalue)
return 'SRID=%d;%s' % (bindvalue.srid, shape.wkt)
else:
# PostGIS ST_GeomFromEWKT works with EWKT strings as well
# as EWKB hex strings
return bindvalue.desc
elif isinstance(bindvalue, RasterElement):
return '%s' % (bindvalue.data)
else:
return bindvalue
return process
def check_ctor_args(geometry_type, srid, dimension, management, use_typmod):
try:
srid = int(srid)
except ValueError:
raise ArgumentError('srid must be convertible to an integer')
if geometry_type:
geometry_type = geometry_type.upper()
if management:
if geometry_type.endswith('ZM'):
# PostGIS' AddGeometryColumn does not work with ZM geometry types. Instead
# the simple geometry type (e.g. POINT rather POINTZM) should be used with
# dimension set to 4
raise ArgumentError(
'with management=True use geometry_type={!r} and '
'dimension=4 for {!r} geometries'.format(geometry_type[:-2], geometry_type))
elif geometry_type[-1] in ('Z', 'M') and dimension != 3:
# If a Z or M geometry type is used then dimension must be set to 3
raise ArgumentError(
'with management=True dimension must be 3 for '
'{!r} geometries'.format(geometry_type))
else:
if management:
if geometry_type.endswith('ZM'):
# PostGIS' AddGeometryColumn does not work with ZM geometry types. Instead
# the simple geometry type (e.g. POINT rather POINTZM) should be used with
# dimension set to 4
raise ArgumentError(
'with management=True use geometry_type={!r} and '
'dimension=4 for {!r} geometries'.format(geometry_type[:-2], geometry_type))
elif geometry_type[-1] in ('Z', 'M') and dimension != 3:
# If a Z or M geometry type is used then dimension must be set to 3
raise ArgumentError(
'with management=True dimension must be 3 for '
'{!r} geometries'.format(geometry_type))
else:
if management:
raise ArgumentError('geometry_type set to None not compatible '
'with management')
if srid > 0:
warnings.warn('srid not enforced when geometry_type is None')
if use_typmod and not management:
warnings.warn('use_typmod ignored when management is False')
return geometry_type, srid
srid = int(srid)
except ValueError:
raise ArgumentError('srid must be convertible to an integer')
if geometry_type:
geometry_type = geometry_type.upper()
if management:
if geometry_type.endswith('ZM'):
# PostGIS' AddGeometryColumn does not work with ZM geometry types. Instead
# the simple geometry type (e.g. POINT rather POINTZM) should be used with
# dimension set to 4
raise ArgumentError(
'with management=True use geometry_type={!r} and '
'dimension=4 for {!r} geometries'.format(geometry_type[:-2], geometry_type))
elif geometry_type[-1] in ('Z', 'M') and dimension != 3:
# If a Z or M geometry type is used then dimension must be set to 3
raise ArgumentError(
'with management=True dimension must be 3 for '
'{!r} geometries'.format(geometry_type))
else:
if management:
raise ArgumentError('geometry_type set to None not compatible '
'with management')
if srid > 0:
warnings.warn('srid not enforced when geometry_type is None')
if use_typmod and not management:
warnings.warn('use_typmod ignored when management is False')
return geometry_type, srid