2010-11-02 05:56:48 +08:00
|
|
|
"""
|
|
|
|
This module has the mock object definitions used to hold reference geometry
|
|
|
|
for the GEOS and GDAL tests.
|
|
|
|
"""
|
2012-04-30 01:58:00 +08:00
|
|
|
import json
|
2010-11-02 05:56:48 +08:00
|
|
|
import os
|
|
|
|
|
2014-11-20 04:53:24 +08:00
|
|
|
from django.utils.functional import cached_property
|
2010-11-02 05:56:48 +08:00
|
|
|
|
|
|
|
# Path where reference test data is located.
|
2017-01-20 21:01:02 +08:00
|
|
|
TEST_DATA = os.path.join(os.path.dirname(__file__), 'data')
|
2010-11-02 05:56:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
def tuplize(seq):
|
|
|
|
"Turn all nested sequences to tuples in given sequence."
|
|
|
|
if isinstance(seq, (list, tuple)):
|
2013-08-30 00:09:35 +08:00
|
|
|
return tuple(tuplize(i) for i in seq)
|
2010-11-02 05:56:48 +08:00
|
|
|
return seq
|
|
|
|
|
|
|
|
|
2010-11-07 00:29:19 +08:00
|
|
|
def strconvert(d):
|
|
|
|
"Converts all keys in dictionary to str type."
|
2017-01-07 19:11:46 +08:00
|
|
|
return {str(k): v for k, v in d.items()}
|
2010-11-07 00:29:19 +08:00
|
|
|
|
|
|
|
|
2010-11-02 05:56:48 +08:00
|
|
|
def get_ds_file(name, ext):
|
|
|
|
return os.path.join(TEST_DATA,
|
|
|
|
name,
|
|
|
|
name + '.%s' % ext
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class TestObj:
|
2010-11-02 05:56:48 +08:00
|
|
|
"""
|
|
|
|
Base testing object, turns keyword args into attributes.
|
|
|
|
"""
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
for key, value in kwargs.items():
|
|
|
|
setattr(self, key, value)
|
|
|
|
|
|
|
|
|
|
|
|
class TestDS(TestObj):
|
|
|
|
"""
|
|
|
|
Object for testing GDAL data sources.
|
|
|
|
"""
|
|
|
|
def __init__(self, name, **kwargs):
|
|
|
|
# Shapefile is default extension, unless specified otherwise.
|
|
|
|
ext = kwargs.pop('ext', 'shp')
|
|
|
|
self.ds = get_ds_file(name, ext)
|
|
|
|
super(TestDS, self).__init__(**kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
class TestGeom(TestObj):
|
|
|
|
"""
|
|
|
|
Testing object used for wrapping reference geometry data
|
|
|
|
in GEOS/GDAL tests.
|
|
|
|
"""
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
# Converting lists to tuples of certain keyword args
|
|
|
|
# so coordinate test cases will match (JSON has no
|
|
|
|
# concept of tuple).
|
|
|
|
coords = kwargs.pop('coords', None)
|
|
|
|
if coords:
|
|
|
|
self.coords = tuplize(coords)
|
|
|
|
|
|
|
|
centroid = kwargs.pop('centroid', None)
|
|
|
|
if centroid:
|
|
|
|
self.centroid = tuple(centroid)
|
|
|
|
|
|
|
|
ext_ring_cs = kwargs.pop('ext_ring_cs', None)
|
|
|
|
if ext_ring_cs:
|
|
|
|
ext_ring_cs = tuplize(ext_ring_cs)
|
|
|
|
self.ext_ring_cs = ext_ring_cs
|
|
|
|
|
|
|
|
super(TestGeom, self).__init__(**kwargs)
|
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class TestGeomSet:
|
2010-11-02 05:56:48 +08:00
|
|
|
"""
|
|
|
|
Each attribute of this object is a list of `TestGeom` instances.
|
|
|
|
"""
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
for key, value in kwargs.items():
|
2010-11-07 00:29:19 +08:00
|
|
|
setattr(self, key, [TestGeom(**strconvert(kw)) for kw in value])
|
2010-11-02 05:56:48 +08:00
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class TestDataMixin:
|
2010-11-02 05:56:48 +08:00
|
|
|
"""
|
|
|
|
Mixin used for GEOS/GDAL test cases that defines a `geometries`
|
|
|
|
property, which returns and/or loads the reference geometry data.
|
|
|
|
"""
|
2014-11-20 04:53:24 +08:00
|
|
|
@cached_property
|
2010-11-02 05:56:48 +08:00
|
|
|
def geometries(self):
|
2014-11-20 04:53:24 +08:00
|
|
|
# Load up the test geometry data from fixture into global.
|
|
|
|
with open(os.path.join(TEST_DATA, 'geometries.json')) as f:
|
|
|
|
geometries = json.load(f)
|
|
|
|
return TestGeomSet(**strconvert(geometries))
|