Skip to content

Commit

Permalink
[1.2.X] Fixed regression introduced in r13755 that prevented the runn…
Browse files Browse the repository at this point in the history
…ing of the GEOS/GDAL test suites without configuring Django settings; moved reference geometry data from Python module to compressed JSON fixture; put in workaround in tests for GDAL bug #3783.

Backport of r14414 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@14415 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
jbronn committed Nov 1, 2010
1 parent d1175f8 commit 2854336
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 326 deletions.
49 changes: 21 additions & 28 deletions django/contrib/gis/gdal/tests/test_ds.py
@@ -1,20 +1,7 @@
import os, os.path, unittest
from django.contrib.gis.gdal import DataSource, Envelope, OGRGeometry, OGRException, OGRIndexError
from django.contrib.gis.gdal import DataSource, Envelope, OGRGeometry, OGRException, OGRIndexError, GDAL_VERSION
from django.contrib.gis.gdal.field import OFTReal, OFTInteger, OFTString
from django.contrib import gis

# Path for SHP files
data_path = os.path.join(os.path.dirname(gis.__file__), 'tests' + os.sep + 'data')
def get_ds_file(name, ext):
return os.sep.join([data_path, name, name + '.%s' % ext])

# Test SHP data source object
class TestDS:
def __init__(self, name, **kwargs):
ext = kwargs.pop('ext', 'shp')
self.ds = get_ds_file(name, ext)
for key, value in kwargs.items():
setattr(self, key, value)
from django.contrib.gis.geometry.test_data import get_ds_file, TestDS

# List of acceptable data sources.
ds_list = (TestDS('test_point', nfeat=5, nfld=3, geom='POINT', gtype=1, driver='ESRI Shapefile',
Expand All @@ -28,7 +15,7 @@ def __init__(self, name, **kwargs):
extent=(1.0, 2.0, 100.0, 523.5), # Min/Max from CSV
field_values={'POINT_X' : ['1.0', '5.0', '100.0'], 'POINT_Y' : ['2.0', '23.0', '523.5'], 'NUM' : ['5', '17', '23']},
fids=range(1,4)),
TestDS('test_poly', nfeat=3, nfld=3, geom='POLYGON', gtype=3,
TestDS('test_poly', nfeat=3, nfld=3, geom='POLYGON', gtype=3,
driver='ESRI Shapefile',
fields={'float' : OFTReal, 'int' : OFTInteger, 'str' : OFTString,},
extent=(-1.01513,-0.558245,0.161876,0.839637), # Got extent from QGIS
Expand Down Expand Up @@ -63,7 +50,7 @@ def test01_valid_shp(self):
pass
else:
self.fail('Expected an IndexError!')

def test02_invalid_shp(self):
"Testing invalid SHP files for the Data Source."
for source in bad_ds:
Expand All @@ -76,7 +63,7 @@ def test03a_layers(self):
ds = DataSource(source.ds)

# Incrementing through each layer, this tests DataSource.__iter__
for layer in ds:
for layer in ds:
# Making sure we get the number of features we expect
self.assertEqual(len(layer), source.nfeat)

Expand All @@ -85,16 +72,22 @@ def test03a_layers(self):
self.assertEqual(source.nfld, len(layer.fields))

# Testing the layer's extent (an Envelope), and it's properties
self.assertEqual(True, isinstance(layer.extent, Envelope))
self.assertAlmostEqual(source.extent[0], layer.extent.min_x, 5)
self.assertAlmostEqual(source.extent[1], layer.extent.min_y, 5)
self.assertAlmostEqual(source.extent[2], layer.extent.max_x, 5)
self.assertAlmostEqual(source.extent[3], layer.extent.max_y, 5)
if source.driver == 'VRT' and (GDAL_VERSION > (1, 7, 0) and GDAL_VERSION < (1, 7, 3)):
# There's a known GDAL regression with retrieving the extent
# of a VRT layer in versions 1.7.0-1.7.2:
# http://trac.osgeo.org/gdal/ticket/3783
pass
else:
self.assertEqual(True, isinstance(layer.extent, Envelope))
self.assertAlmostEqual(source.extent[0], layer.extent.min_x, 5)
self.assertAlmostEqual(source.extent[1], layer.extent.min_y, 5)
self.assertAlmostEqual(source.extent[2], layer.extent.max_x, 5)
self.assertAlmostEqual(source.extent[3], layer.extent.max_y, 5)

# Now checking the field names.
flds = layer.fields
for f in flds: self.assertEqual(True, f in source.fields)

# Negative FIDs are not allowed.
self.assertRaises(OGRIndexError, layer.__getitem__, -1)
self.assertRaises(OGRIndexError, layer.__getitem__, 50000)
Expand All @@ -115,7 +108,7 @@ def test03a_layers(self):
for fld_name in fld_names:
self.assertEqual(source.field_values[fld_name][i], feat.get(fld_name))
print "\nEND - expecting out of range feature id error; safe to ignore."

def test03b_layer_slice(self):
"Test indexing and slicing on Layers."
# Using the first data-source because the same slice
Expand Down Expand Up @@ -146,7 +139,7 @@ def get_layer():
# Making sure we can call OGR routines on the Layer returned.
lyr = get_layer()
self.assertEqual(source.nfeat, len(lyr))
self.assertEqual(source.gtype, lyr.geom_type.num)
self.assertEqual(source.gtype, lyr.geom_type.num)

def test04_features(self):
"Testing Data Source Features."
Expand All @@ -170,7 +163,7 @@ def test04_features(self):

# Testing Feature.__iter__
for fld in feat: self.assertEqual(True, fld.name in source.fields.keys())

def test05_geometries(self):
"Testing Geometries from Data Source Features."
for source in ds_list:
Expand Down Expand Up @@ -223,7 +216,7 @@ def test06_spatial_filter(self):
# should indicate that there are 3 features in the Layer.
lyr.spatial_filter = None
self.assertEqual(3, len(lyr))

def suite():
s = unittest.TestSuite()
s.addTest(unittest.makeSuite(DataSourceTest))
Expand Down
85 changes: 40 additions & 45 deletions django/contrib/gis/gdal/tests/test_geom.py
Expand Up @@ -2,9 +2,9 @@
from django.contrib.gis.gdal import OGRGeometry, OGRGeomType, \
OGRException, OGRIndexError, SpatialReference, CoordTransform, \
gdal_version
from django.contrib.gis.tests.geometries import *
from django.contrib.gis.geometry.test_data import TestDataMixin

class OGRGeomTest(unittest.TestCase):
class OGRGeomTest(unittest.TestCase, TestDataMixin):
"This tests the OGR Geometry."

def test00a_geomtype(self):
Expand Down Expand Up @@ -55,7 +55,7 @@ def test00b_geomtype_25d(self):

def test01a_wkt(self):
"Testing WKT output."
for g in wkt_out:
for g in self.geometries.wkt_out:
geom = OGRGeometry(g.wkt)
self.assertEqual(g.wkt, geom.wkt)

Expand All @@ -72,13 +72,13 @@ def test01a_ewkt(self):

def test01b_gml(self):
"Testing GML output."
for g in wkt_out:
for g in self.geometries.wkt_out:
geom = OGRGeometry(g.wkt)
self.assertEqual(g.gml, geom.gml)

def test01c_hex(self):
"Testing HEX input/output."
for g in hex_wkt:
for g in self.geometries.hex_wkt:
geom1 = OGRGeometry(g.wkt)
self.assertEqual(g.hex, geom1.hex)
# Constructing w/HEX
Expand All @@ -88,7 +88,7 @@ def test01c_hex(self):
def test01d_wkb(self):
"Testing WKB input/output."
from binascii import b2a_hex
for g in hex_wkt:
for g in self.geometries.hex_wkt:
geom1 = OGRGeometry(g.wkt)
wkb = geom1.wkb
self.assertEqual(b2a_hex(wkb).upper(), g.hex)
Expand All @@ -100,7 +100,7 @@ def test01e_json(self):
"Testing GeoJSON input/output."
from django.contrib.gis.gdal.prototypes.geom import GEOJSON
if not GEOJSON: return
for g in json_geoms:
for g in self.geometries.json_geoms:
geom = OGRGeometry(g.wkt)
if not hasattr(g, 'not_equal'):
self.assertEqual(g.json, geom.json)
Expand All @@ -111,7 +111,7 @@ def test02_points(self):
"Testing Point objects."

prev = OGRGeometry('POINT(0 0)')
for p in points:
for p in self.geometries.points:
if not hasattr(p, 'z'): # No 3D
pnt = OGRGeometry(p.wkt)
self.assertEqual(1, pnt.geom_type)
Expand All @@ -122,8 +122,7 @@ def test02_points(self):

def test03_multipoints(self):
"Testing MultiPoint objects."

for mp in multipoints:
for mp in self.geometries.multipoints:
mgeom1 = OGRGeometry(mp.wkt) # First one from WKT
self.assertEqual(4, mgeom1.geom_type)
self.assertEqual('MULTIPOINT', mgeom1.geom_name)
Expand All @@ -134,38 +133,38 @@ def test03_multipoints(self):
mgeom3.add(g.wkt) # should take WKT as well
self.assertEqual(mgeom1, mgeom2) # they should equal
self.assertEqual(mgeom1, mgeom3)
self.assertEqual(mp.points, mgeom2.tuple)
self.assertEqual(mp.coords, mgeom2.coords)
self.assertEqual(mp.n_p, mgeom2.point_count)

def test04_linestring(self):
"Testing LineString objects."
prev = OGRGeometry('POINT(0 0)')
for ls in linestrings:
for ls in self.geometries.linestrings:
linestr = OGRGeometry(ls.wkt)
self.assertEqual(2, linestr.geom_type)
self.assertEqual('LINESTRING', linestr.geom_name)
self.assertEqual(ls.n_p, linestr.point_count)
self.assertEqual(ls.tup, linestr.tuple)
self.assertEqual(ls.coords, linestr.tuple)
self.assertEqual(True, linestr == OGRGeometry(ls.wkt))
self.assertEqual(True, linestr != prev)
self.assertRaises(OGRIndexError, linestr.__getitem__, len(linestr))
prev = linestr

# Testing the x, y properties.
x = [tmpx for tmpx, tmpy in ls.tup]
y = [tmpy for tmpx, tmpy in ls.tup]
x = [tmpx for tmpx, tmpy in ls.coords]
y = [tmpy for tmpx, tmpy in ls.coords]
self.assertEqual(x, linestr.x)
self.assertEqual(y, linestr.y)

def test05_multilinestring(self):
"Testing MultiLineString objects."
prev = OGRGeometry('POINT(0 0)')
for mls in multilinestrings:
for mls in self.geometries.multilinestrings:
mlinestr = OGRGeometry(mls.wkt)
self.assertEqual(5, mlinestr.geom_type)
self.assertEqual('MULTILINESTRING', mlinestr.geom_name)
self.assertEqual(mls.n_p, mlinestr.point_count)
self.assertEqual(mls.tup, mlinestr.tuple)
self.assertEqual(mls.coords, mlinestr.tuple)
self.assertEqual(True, mlinestr == OGRGeometry(mls.wkt))
self.assertEqual(True, mlinestr != prev)
prev = mlinestr
Expand All @@ -177,7 +176,7 @@ def test05_multilinestring(self):
def test06_linearring(self):
"Testing LinearRing objects."
prev = OGRGeometry('POINT(0 0)')
for rr in linearrings:
for rr in self.geometries.linearrings:
lr = OGRGeometry(rr.wkt)
#self.assertEqual(101, lr.geom_type.num)
self.assertEqual('LINEARRING', lr.geom_name)
Expand All @@ -195,7 +194,7 @@ def test07a_polygons(self):
self.assertEqual(bbox, p.extent)

prev = OGRGeometry('POINT(0 0)')
for p in polygons:
for p in self.geometries.polygons:
poly = OGRGeometry(p.wkt)
self.assertEqual(3, poly.geom_type)
self.assertEqual('POLYGON', poly.geom_name)
Expand Down Expand Up @@ -249,7 +248,7 @@ def test07b_closepolygons(self):
def test08_multipolygons(self):
"Testing MultiPolygon objects."
prev = OGRGeometry('POINT(0 0)')
for mp in multipolygons:
for mp in self.geometries.multipolygons:
mpoly = OGRGeometry(mp.wkt)
self.assertEqual(6, mpoly.geom_type)
self.assertEqual('MULTIPOLYGON', mpoly.geom_name)
Expand All @@ -264,7 +263,7 @@ def test08_multipolygons(self):

def test09a_srs(self):
"Testing OGR Geometries with Spatial Reference objects."
for mp in multipolygons:
for mp in self.geometries.multipolygons:
# Creating a geometry w/spatial reference
sr = SpatialReference('WGS84')
mpoly = OGRGeometry(mp.wkt, sr)
Expand All @@ -282,8 +281,8 @@ def test09a_srs(self):
self.assertEqual(sr.wkt, ring.srs.wkt)

# Ensuring SRS propagate in topological ops.
a, b = topology_geoms[0]
a, b = OGRGeometry(a.wkt, sr), OGRGeometry(b.wkt, sr)
a = OGRGeometry(self.geometries.topology_geoms[0].wkt_a, sr)
b = OGRGeometry(self.geometries.topology_geoms[0].wkt_b, sr)
diff = a.difference(b)
union = a.union(b)
self.assertEqual(sr.wkt, diff.srs.wkt)
Expand Down Expand Up @@ -351,11 +350,10 @@ def test09c_transform_dim(self):

def test10_difference(self):
"Testing difference()."
for i in xrange(len(topology_geoms)):
g_tup = topology_geoms[i]
a = OGRGeometry(g_tup[0].wkt)
b = OGRGeometry(g_tup[1].wkt)
d1 = OGRGeometry(diff_geoms[i].wkt)
for i in xrange(len(self.geometries.topology_geoms)):
a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a)
b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b)
d1 = OGRGeometry(self.geometries.diff_geoms[i].wkt)
d2 = a.difference(b)
self.assertEqual(d1, d2)
self.assertEqual(d1, a - b) # __sub__ is difference operator
Expand All @@ -364,11 +362,10 @@ def test10_difference(self):

def test11_intersection(self):
"Testing intersects() and intersection()."
for i in xrange(len(topology_geoms)):
g_tup = topology_geoms[i]
a = OGRGeometry(g_tup[0].wkt)
b = OGRGeometry(g_tup[1].wkt)
i1 = OGRGeometry(intersect_geoms[i].wkt)
for i in xrange(len(self.geometries.topology_geoms)):
a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a)
b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b)
i1 = OGRGeometry(self.geometries.intersect_geoms[i].wkt)
self.assertEqual(True, a.intersects(b))
i2 = a.intersection(b)
self.assertEqual(i1, i2)
Expand All @@ -378,11 +375,10 @@ def test11_intersection(self):

def test12_symdifference(self):
"Testing sym_difference()."
for i in xrange(len(topology_geoms)):
g_tup = topology_geoms[i]
a = OGRGeometry(g_tup[0].wkt)
b = OGRGeometry(g_tup[1].wkt)
d1 = OGRGeometry(sdiff_geoms[i].wkt)
for i in xrange(len(self.geometries.topology_geoms)):
a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a)
b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b)
d1 = OGRGeometry(self.geometries.sdiff_geoms[i].wkt)
d2 = a.sym_difference(b)
self.assertEqual(d1, d2)
self.assertEqual(d1, a ^ b) # __xor__ is symmetric difference operator
Expand All @@ -391,11 +387,10 @@ def test12_symdifference(self):

def test13_union(self):
"Testing union()."
for i in xrange(len(topology_geoms)):
g_tup = topology_geoms[i]
a = OGRGeometry(g_tup[0].wkt)
b = OGRGeometry(g_tup[1].wkt)
u1 = OGRGeometry(union_geoms[i].wkt)
for i in xrange(len(self.geometries.topology_geoms)):
a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a)
b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b)
u1 = OGRGeometry(self.geometries.union_geoms[i].wkt)
u2 = a.union(b)
self.assertEqual(u1, u2)
self.assertEqual(u1, a | b) # __or__ is union operator
Expand All @@ -411,7 +406,7 @@ def test14_add(self):

# GeometryCollection.add may take an OGRGeometry (if another collection
# of the same type all child geoms will be added individually) or WKT.
for mp in multipolygons:
for mp in self.geometries.multipolygons:
mpoly = OGRGeometry(mp.wkt)
mp1 = OGRGeometry('MultiPolygon')
mp2 = OGRGeometry('MultiPolygon')
Expand All @@ -429,7 +424,7 @@ def test15_extent(self):
mp = OGRGeometry('MULTIPOINT(5 23, 0 0, 10 50)')
self.assertEqual((0.0, 0.0, 10.0, 50.0), mp.extent)
# Testing on the 'real world' Polygon.
poly = OGRGeometry(polygons[3].wkt)
poly = OGRGeometry(self.geometries.polygons[3].wkt)
ring = poly.shell
x, y = ring.x, ring.y
xmin, ymin = min(x), min(y)
Expand Down

0 comments on commit 2854336

Please sign in to comment.