|
@@ -1,49 +1,29 @@
|
|
|
-import os, unittest
|
|
|
+import re, os, unittest
|
|
|
+from django.db import connection
|
|
|
from django.contrib.gis import gdal
|
|
|
-from django.contrib.gis.db.backend import SpatialBackend
|
|
|
from django.contrib.gis.geos import *
|
|
|
from django.contrib.gis.measure import Distance
|
|
|
-from django.contrib.gis.tests.utils import no_oracle, no_postgis, no_spatialite
|
|
|
+from django.contrib.gis.tests.utils import \
|
|
|
+ no_mysql, no_oracle, no_postgis, no_spatialite, \
|
|
|
+ mysql, oracle, postgis, spatialite
|
|
|
+from django.test import TestCase
|
|
|
+
|
|
|
from models import Country, City, PennsylvaniaCity, State
|
|
|
|
|
|
-if not SpatialBackend.spatialite:
|
|
|
+if not spatialite:
|
|
|
from models import Feature, MinusOneSRID
|
|
|
|
|
|
-# TODO: Some tests depend on the success/failure of previous tests, these should
|
|
|
-# be decoupled. This flag is an artifact of this problem, and makes debugging easier;
|
|
|
-# specifically, the DISABLE flag will disables all tests, allowing problem tests to
|
|
|
-# be examined individually.
|
|
|
-DISABLE = False
|
|
|
-
|
|
|
-class GeoModelTest(unittest.TestCase):
|
|
|
-
|
|
|
- def test01_initial_sql(self):
|
|
|
- "Testing geographic initial SQL."
|
|
|
- if DISABLE: return
|
|
|
- if SpatialBackend.oracle:
|
|
|
- # Oracle doesn't allow strings longer than 4000 characters
|
|
|
- # in SQL files, and I'm stumped on how to use Oracle BFILE's
|
|
|
- # in PLSQL, so we set up the larger geometries manually, rather
|
|
|
- # than relying on the initial SQL.
|
|
|
-
|
|
|
- # Routine for returning the path to the data files.
|
|
|
- data_dir = os.path.join(os.path.dirname(__file__), 'sql')
|
|
|
- def get_file(wkt_file):
|
|
|
- return os.path.join(data_dir, wkt_file)
|
|
|
- State(name='Puerto Rico', poly=None).save()
|
|
|
- State(name='Colorado', poly=fromfile(get_file('co.wkt'))).save()
|
|
|
- State(name='Kansas', poly=fromfile(get_file('ks.wkt'))).save()
|
|
|
- Country(name='Texas', mpoly=fromfile(get_file('tx.wkt'))).save()
|
|
|
- Country(name='New Zealand', mpoly=fromfile(get_file('nz.wkt'))).save()
|
|
|
-
|
|
|
- # Ensuring that data was loaded from initial SQL.
|
|
|
+class GeoModelTest(TestCase):
|
|
|
+
|
|
|
+ def test01_fixtures(self):
|
|
|
+ "Testing geographic model initialization from fixtures."
|
|
|
+ # Ensuring that data was loaded from initial data fixtures.
|
|
|
self.assertEqual(2, Country.objects.count())
|
|
|
self.assertEqual(8, City.objects.count())
|
|
|
- self.assertEqual(3, State.objects.count())
|
|
|
+ self.assertEqual(2, State.objects.count())
|
|
|
|
|
|
def test02_proxy(self):
|
|
|
"Testing Lazy-Geometry support (using the GeometryProxy)."
|
|
|
- if DISABLE: return
|
|
|
## Testing on a Point
|
|
|
pnt = Point(0, 0)
|
|
|
nullcity = City(name='NullCity', point=pnt)
|
|
@@ -110,11 +90,13 @@ class GeoModelTest(unittest.TestCase):
|
|
|
self.assertEqual(ply, State.objects.get(name='NullState').poly)
|
|
|
ns.delete()
|
|
|
|
|
|
- @no_oracle # Oracle does not support KML.
|
|
|
- @no_spatialite # SpatiaLite does not support KML.
|
|
|
def test03a_kml(self):
|
|
|
"Testing KML output from the database using GeoQuerySet.kml()."
|
|
|
- if DISABLE: return
|
|
|
+ # Only PostGIS supports KML serialization
|
|
|
+ if not postgis:
|
|
|
+ self.assertRaises(NotImplementedError, State.objects.all().kml, field_name='poly')
|
|
|
+ return
|
|
|
+
|
|
|
# Should throw a TypeError when trying to obtain KML from a
|
|
|
# non-geometry field.
|
|
|
qs = City.objects.all()
|
|
@@ -122,14 +104,10 @@ class GeoModelTest(unittest.TestCase):
|
|
|
|
|
|
# The reference KML depends on the version of PostGIS used
|
|
|
# (the output stopped including altitude in 1.3.3).
|
|
|
- major, minor1, minor2 = SpatialBackend.version
|
|
|
- ref_kml1 = '<Point><coordinates>-104.609252,38.255001,0</coordinates></Point>'
|
|
|
- ref_kml2 = '<Point><coordinates>-104.609252,38.255001</coordinates></Point>'
|
|
|
- if major == 1:
|
|
|
- if minor1 > 3 or (minor1 == 3 and minor2 >= 3): ref_kml = ref_kml2
|
|
|
- else: ref_kml = ref_kml1
|
|
|
+ if connection.ops.spatial_version >= (1, 3, 3):
|
|
|
+ ref_kml = '<Point><coordinates>-104.609252,38.255001</coordinates></Point>'
|
|
|
else:
|
|
|
- ref_kml = ref_kml2
|
|
|
+ ref_kml = '<Point><coordinates>-104.609252,38.255001,0</coordinates></Point>'
|
|
|
|
|
|
# Ensuring the KML is as expected.
|
|
|
ptown1 = City.objects.kml(field_name='point', precision=9).get(name='Pueblo')
|
|
@@ -137,19 +115,20 @@ class GeoModelTest(unittest.TestCase):
|
|
|
for ptown in [ptown1, ptown2]:
|
|
|
self.assertEqual(ref_kml, ptown.kml)
|
|
|
|
|
|
- @no_spatialite # SpatiaLite does not support GML.
|
|
|
def test03b_gml(self):
|
|
|
"Testing GML output from the database using GeoQuerySet.gml()."
|
|
|
- if DISABLE: return
|
|
|
+ if mysql or spatialite:
|
|
|
+ self.assertRaises(NotImplementedError, Country.objects.all().gml, field_name='mpoly')
|
|
|
+ return
|
|
|
+
|
|
|
# Should throw a TypeError when tyring to obtain GML from a
|
|
|
- # non-geometry field.
|
|
|
+ # non-geometry field.
|
|
|
qs = City.objects.all()
|
|
|
self.assertRaises(TypeError, qs.gml, field_name='name')
|
|
|
ptown1 = City.objects.gml(field_name='point', precision=9).get(name='Pueblo')
|
|
|
ptown2 = City.objects.gml(precision=9).get(name='Pueblo')
|
|
|
|
|
|
- import re
|
|
|
- if SpatialBackend.oracle:
|
|
|
+ if oracle:
|
|
|
# No precision parameter for Oracle :-/
|
|
|
gml_regex = re.compile(r'^<gml:Point srsName="SDO:4326" xmlns:gml="http://www.opengis.net/gml"><gml:coordinates decimal="\." cs="," ts=" ">-104.60925\d+,38.25500\d+ </gml:coordinates></gml:Point>')
|
|
|
for ptown in [ptown1, ptown2]:
|
|
@@ -159,17 +138,14 @@ class GeoModelTest(unittest.TestCase):
|
|
|
for ptown in [ptown1, ptown2]:
|
|
|
self.failUnless(gml_regex.match(ptown.gml))
|
|
|
|
|
|
- @no_spatialite
|
|
|
- @no_oracle
|
|
|
def test03c_geojson(self):
|
|
|
"Testing GeoJSON output from the database using GeoQuerySet.geojson()."
|
|
|
- if DISABLE: return
|
|
|
- # PostGIS only supports GeoJSON on 1.3.4+
|
|
|
- if not SpatialBackend.geojson:
|
|
|
+ # Only PostGIS 1.3.4+ supports GeoJSON.
|
|
|
+ if not connection.ops.geojson:
|
|
|
+ self.assertRaises(NotImplementedError, Country.objects.all().geojson, field_name='mpoly')
|
|
|
return
|
|
|
|
|
|
- major, minor1, minor2 = SpatialBackend.version
|
|
|
- if major >=1 and minor1 >= 4:
|
|
|
+ if connection.ops.spatial_version >= (1, 4, 0):
|
|
|
pueblo_json = '{"type":"Point","coordinates":[-104.609252,38.255001]}'
|
|
|
houston_json = '{"type":"Point","crs":{"type":"name","properties":{"name":"EPSG:4326"}},"coordinates":[-95.363151,29.763374]}'
|
|
|
victoria_json = '{"type":"Point","bbox":[-123.30519600,48.46261100,-123.30519600,48.46261100],"coordinates":[-123.305196,48.462611]}'
|
|
@@ -179,10 +155,10 @@ class GeoModelTest(unittest.TestCase):
|
|
|
houston_json = '{"type":"Point","crs":{"type":"EPSG","properties":{"EPSG":4326}},"coordinates":[-95.36315100,29.76337400]}'
|
|
|
victoria_json = '{"type":"Point","bbox":[-123.30519600,48.46261100,-123.30519600,48.46261100],"coordinates":[-123.30519600,48.46261100]}'
|
|
|
chicago_json = '{"type":"Point","crs":{"type":"EPSG","properties":{"EPSG":4326}},"bbox":[-87.65018,41.85039,-87.65018,41.85039],"coordinates":[-87.65018,41.85039]}'
|
|
|
-
|
|
|
+
|
|
|
# Precision argument should only be an integer
|
|
|
self.assertRaises(TypeError, City.objects.geojson, precision='foo')
|
|
|
-
|
|
|
+
|
|
|
# Reference queries and values.
|
|
|
# SELECT ST_AsGeoJson("geoapp_city"."point", 8, 0) FROM "geoapp_city" WHERE "geoapp_city"."name" = 'Pueblo';
|
|
|
self.assertEqual(pueblo_json, City.objects.geojson().get(name='Pueblo').geojson)
|
|
@@ -200,11 +176,13 @@ class GeoModelTest(unittest.TestCase):
|
|
|
# 1.(3|4).x: SELECT ST_AsGeoJson("geoapp_city"."point", 5, 3) FROM "geoapp_city" WHERE "geoapp_city"."name" = 'Chicago';
|
|
|
# Finally, we set every available keyword.
|
|
|
self.assertEqual(chicago_json, City.objects.geojson(bbox=True, crs=True, precision=5).get(name='Chicago').geojson)
|
|
|
-
|
|
|
- @no_oracle
|
|
|
+
|
|
|
def test03d_svg(self):
|
|
|
"Testing SVG output using GeoQuerySet.svg()."
|
|
|
- if DISABLE: return
|
|
|
+ if mysql or oracle:
|
|
|
+ self.assertRaises(NotImplementedError, City.objects.svg)
|
|
|
+ return
|
|
|
+
|
|
|
self.assertRaises(TypeError, City.objects.svg, precision='foo')
|
|
|
# SELECT AsSVG(geoapp_city.point, 0, 8) FROM geoapp_city WHERE name = 'Pueblo';
|
|
|
svg1 = 'cx="-104.609252" cy="-38.255001"'
|
|
@@ -214,9 +192,9 @@ class GeoModelTest(unittest.TestCase):
|
|
|
self.assertEqual(svg1, City.objects.svg().get(name='Pueblo').svg)
|
|
|
self.assertEqual(svg2, City.objects.svg(relative=5).get(name='Pueblo').svg)
|
|
|
|
|
|
+ @no_mysql
|
|
|
def test04_transform(self):
|
|
|
"Testing the transform() GeoManager method."
|
|
|
- if DISABLE: return
|
|
|
# Pre-transformed points for Houston and Pueblo.
|
|
|
htown = fromstr('POINT(1947516.83115183 6322297.06040572)', srid=3084)
|
|
|
ptown = fromstr('POINT(992363.390841912 481455.395105533)', srid=2774)
|
|
@@ -224,7 +202,7 @@ class GeoModelTest(unittest.TestCase):
|
|
|
|
|
|
# Asserting the result of the transform operation with the values in
|
|
|
# the pre-transformed points. Oracle does not have the 3084 SRID.
|
|
|
- if not SpatialBackend.oracle:
|
|
|
+ if not oracle:
|
|
|
h = City.objects.transform(htown.srid).get(name='Houston')
|
|
|
self.assertEqual(3084, h.point.srid)
|
|
|
self.assertAlmostEqual(htown.x, h.point.x, prec)
|
|
@@ -237,10 +215,10 @@ class GeoModelTest(unittest.TestCase):
|
|
|
self.assertAlmostEqual(ptown.x, p.point.x, prec)
|
|
|
self.assertAlmostEqual(ptown.y, p.point.y, prec)
|
|
|
|
|
|
+ @no_mysql
|
|
|
@no_spatialite # SpatiaLite does not have an Extent function
|
|
|
def test05_extent(self):
|
|
|
"Testing the `extent` GeoQuerySet method."
|
|
|
- if DISABLE: return
|
|
|
# Reference query:
|
|
|
# `SELECT ST_extent(point) FROM geoapp_city WHERE (name='Houston' or name='Dallas');`
|
|
|
# => BOX(-96.8016128540039 29.7633724212646,-95.3631439208984 32.7820587158203)
|
|
@@ -252,11 +230,12 @@ class GeoModelTest(unittest.TestCase):
|
|
|
for val, exp in zip(extent, expected):
|
|
|
self.assertAlmostEqual(exp, val, 4)
|
|
|
|
|
|
+ # Only PostGIS has support for the MakeLine aggregate.
|
|
|
+ @no_mysql
|
|
|
@no_oracle
|
|
|
- @no_spatialite # SpatiaLite does not have a MakeLine function
|
|
|
+ @no_spatialite
|
|
|
def test06_make_line(self):
|
|
|
"Testing the `make_line` GeoQuerySet method."
|
|
|
- if DISABLE: return
|
|
|
# Ensuring that a `TypeError` is raised on models without PointFields.
|
|
|
self.assertRaises(TypeError, State.objects.make_line)
|
|
|
self.assertRaises(TypeError, Country.objects.make_line)
|
|
@@ -265,34 +244,26 @@ class GeoModelTest(unittest.TestCase):
|
|
|
ref_line = GEOSGeometry('LINESTRING(-95.363151 29.763374,-96.801611 32.782057,-97.521157 34.464642,174.783117 -41.315268,-104.609252 38.255001,-95.23506 38.971823,-87.650175 41.850385,-123.305196 48.462611)', srid=4326)
|
|
|
self.assertEqual(ref_line, City.objects.make_line())
|
|
|
|
|
|
+ @no_mysql
|
|
|
def test09_disjoint(self):
|
|
|
"Testing the `disjoint` lookup type."
|
|
|
- if DISABLE: return
|
|
|
ptown = City.objects.get(name='Pueblo')
|
|
|
qs1 = City.objects.filter(point__disjoint=ptown.point)
|
|
|
self.assertEqual(7, qs1.count())
|
|
|
|
|
|
- if not (SpatialBackend.postgis or SpatialBackend.spatialite):
|
|
|
- # TODO: Do NULL columns bork queries on PostGIS? The following
|
|
|
- # error is encountered:
|
|
|
- # psycopg2.ProgrammingError: invalid memory alloc request size 4294957297
|
|
|
- #
|
|
|
- # Similarly, on SpatiaLite Puerto Rico is also returned (could be a
|
|
|
- # manifestation of
|
|
|
- qs2 = State.objects.filter(poly__disjoint=ptown.point)
|
|
|
- self.assertEqual(1, qs2.count())
|
|
|
- self.assertEqual('Kansas', qs2[0].name)
|
|
|
+ qs2 = State.objects.filter(poly__disjoint=ptown.point)
|
|
|
+ self.assertEqual(1, qs2.count())
|
|
|
+ self.assertEqual('Kansas', qs2[0].name)
|
|
|
|
|
|
def test10_contains_contained(self):
|
|
|
"Testing the 'contained', 'contains', and 'bbcontains' lookup types."
|
|
|
- if DISABLE: return
|
|
|
# Getting Texas, yes we were a country -- once ;)
|
|
|
texas = Country.objects.get(name='Texas')
|
|
|
|
|
|
# Seeing what cities are in Texas, should get Houston and Dallas,
|
|
|
# and Oklahoma City because 'contained' only checks on the
|
|
|
# _bounding box_ of the Geometries.
|
|
|
- if not SpatialBackend.oracle:
|
|
|
+ if not oracle:
|
|
|
qs = City.objects.filter(point__contained=texas.mpoly)
|
|
|
self.assertEqual(3, qs.count())
|
|
|
cities = ['Houston', 'Dallas', 'Oklahoma City']
|
|
@@ -313,30 +284,31 @@ class GeoModelTest(unittest.TestCase):
|
|
|
self.assertEqual('New Zealand', nz.name)
|
|
|
|
|
|
# Spatialite 2.3 thinks that Lawrence is in Puerto Rico (a NULL geometry).
|
|
|
- if not SpatialBackend.spatialite:
|
|
|
+ if not spatialite:
|
|
|
ks = State.objects.get(poly__contains=lawrence.point)
|
|
|
self.assertEqual('Kansas', ks.name)
|
|
|
|
|
|
# Pueblo and Oklahoma City (even though OK City is within the bounding box of Texas)
|
|
|
- # are not contained in Texas or New Zealand.
|
|
|
+ # are not contained in Texas or New Zealand.
|
|
|
self.assertEqual(0, len(Country.objects.filter(mpoly__contains=pueblo.point))) # Query w/GEOSGeometry object
|
|
|
- self.assertEqual(0, len(Country.objects.filter(mpoly__contains=okcity.point.wkt))) # Qeury w/WKT
|
|
|
+ self.assertEqual((mysql and 1) or 0,
|
|
|
+ len(Country.objects.filter(mpoly__contains=okcity.point.wkt))) # Qeury w/WKT
|
|
|
|
|
|
# OK City is contained w/in bounding box of Texas.
|
|
|
- if not SpatialBackend.oracle:
|
|
|
+ if not oracle:
|
|
|
qs = Country.objects.filter(mpoly__bbcontains=okcity.point)
|
|
|
self.assertEqual(1, len(qs))
|
|
|
self.assertEqual('Texas', qs[0].name)
|
|
|
|
|
|
+ @no_mysql
|
|
|
def test11_lookup_insert_transform(self):
|
|
|
"Testing automatic transform for lookups and inserts."
|
|
|
- if DISABLE: return
|
|
|
# San Antonio in 'WGS84' (SRID 4326)
|
|
|
sa_4326 = 'POINT (-98.493183 29.424170)'
|
|
|
wgs_pnt = fromstr(sa_4326, srid=4326) # Our reference point in WGS84
|
|
|
|
|
|
# Oracle doesn't have SRID 3084, using 41157.
|
|
|
- if SpatialBackend.oracle:
|
|
|
+ if oracle:
|
|
|
# San Antonio in 'Texas 4205, Southern Zone (1983, meters)' (SRID 41157)
|
|
|
# Used the following Oracle SQL to get this value:
|
|
|
# SELECT SDO_UTIL.TO_WKTGEOMETRY(SDO_CS.TRANSFORM(SDO_GEOMETRY('POINT (-98.493183 29.424170)', 4326), 41157)) FROM DUAL;
|
|
@@ -351,15 +323,14 @@ class GeoModelTest(unittest.TestCase):
|
|
|
# `SDO_OVERLAPBDYINTERSECT` operates differently from
|
|
|
# `ST_Intersects`, so contains is used instead.
|
|
|
nad_pnt = fromstr(nad_wkt, srid=nad_srid)
|
|
|
- if SpatialBackend.oracle:
|
|
|
+ if oracle:
|
|
|
tx = Country.objects.get(mpoly__contains=nad_pnt)
|
|
|
else:
|
|
|
tx = Country.objects.get(mpoly__intersects=nad_pnt)
|
|
|
self.assertEqual('Texas', tx.name)
|
|
|
|
|
|
# Creating San Antonio. Remember the Alamo.
|
|
|
- sa = City(name='San Antonio', point=nad_pnt)
|
|
|
- sa.save()
|
|
|
+ sa = City.objects.create(name='San Antonio', point=nad_pnt)
|
|
|
|
|
|
# Now verifying that San Antonio was transformed correctly
|
|
|
sa = City.objects.get(name='San Antonio')
|
|
@@ -369,14 +340,17 @@ class GeoModelTest(unittest.TestCase):
|
|
|
# If the GeometryField SRID is -1, then we shouldn't perform any
|
|
|
# transformation if the SRID of the input geometry is different.
|
|
|
# SpatiaLite does not support missing SRID values.
|
|
|
- if not SpatialBackend.spatialite:
|
|
|
+ if not spatialite:
|
|
|
m1 = MinusOneSRID(geom=Point(17, 23, srid=4326))
|
|
|
m1.save()
|
|
|
self.assertEqual(-1, m1.geom.srid)
|
|
|
|
|
|
+ @no_mysql
|
|
|
def test12_null_geometries(self):
|
|
|
"Testing NULL geometry support, and the `isnull` lookup type."
|
|
|
- if DISABLE: return
|
|
|
+ # Creating a state with a NULL boundary.
|
|
|
+ State.objects.create(name='Puerto Rico')
|
|
|
+
|
|
|
# Querying for both NULL and Non-NULL values.
|
|
|
nullqs = State.objects.filter(poly__isnull=True)
|
|
|
validqs = State.objects.filter(poly__isnull=False)
|
|
@@ -401,27 +375,28 @@ class GeoModelTest(unittest.TestCase):
|
|
|
State.objects.filter(name='Northern Mariana Islands').update(poly=None)
|
|
|
self.assertEqual(None, State.objects.get(name='Northern Mariana Islands').poly)
|
|
|
|
|
|
- @no_oracle # No specific `left` or `right` operators in Oracle.
|
|
|
- @no_spatialite # No `left` or `right` operators in SpatiaLite.
|
|
|
+ # Only PostGIS has `left` and `right` lookup types.
|
|
|
+ @no_mysql
|
|
|
+ @no_oracle
|
|
|
+ @no_spatialite
|
|
|
def test13_left_right(self):
|
|
|
"Testing the 'left' and 'right' lookup types."
|
|
|
- if DISABLE: return
|
|
|
# Left: A << B => true if xmax(A) < xmin(B)
|
|
|
# Right: A >> B => true if xmin(A) > xmax(B)
|
|
|
- # See: BOX2D_left() and BOX2D_right() in lwgeom_box2dfloat4.c in PostGIS source.
|
|
|
+ # See: BOX2D_left() and BOX2D_right() in lwgeom_box2dfloat4.c in PostGIS source.
|
|
|
|
|
|
# Getting the borders for Colorado & Kansas
|
|
|
co_border = State.objects.get(name='Colorado').poly
|
|
|
ks_border = State.objects.get(name='Kansas').poly
|
|
|
|
|
|
# Note: Wellington has an 'X' value of 174, so it will not be considered
|
|
|
- # to the left of CO.
|
|
|
+ # to the left of CO.
|
|
|
|
|
|
# These cities should be strictly to the right of the CO border.
|
|
|
- cities = ['Houston', 'Dallas', 'San Antonio', 'Oklahoma City',
|
|
|
+ cities = ['Houston', 'Dallas', 'Oklahoma City',
|
|
|
'Lawrence', 'Chicago', 'Wellington']
|
|
|
qs = City.objects.filter(point__right=co_border)
|
|
|
- self.assertEqual(7, len(qs))
|
|
|
+ self.assertEqual(6, len(qs))
|
|
|
for c in qs: self.assertEqual(True, c.name in cities)
|
|
|
|
|
|
# These cities should be strictly to the right of the KS border.
|
|
@@ -442,16 +417,16 @@ class GeoModelTest(unittest.TestCase):
|
|
|
|
|
|
def test14_equals(self):
|
|
|
"Testing the 'same_as' and 'equals' lookup types."
|
|
|
- if DISABLE: return
|
|
|
pnt = fromstr('POINT (-95.363151 29.763374)', srid=4326)
|
|
|
c1 = City.objects.get(point=pnt)
|
|
|
c2 = City.objects.get(point__same_as=pnt)
|
|
|
c3 = City.objects.get(point__equals=pnt)
|
|
|
for c in [c1, c2, c3]: self.assertEqual('Houston', c.name)
|
|
|
|
|
|
+ @no_mysql
|
|
|
def test15_relate(self):
|
|
|
"Testing the 'relate' lookup type."
|
|
|
- if DISABLE: return
|
|
|
+ return
|
|
|
# To make things more interesting, we will have our Texas reference point in
|
|
|
# different SRIDs.
|
|
|
pnt1 = fromstr('POINT (649287.0363174 4177429.4494686)', srid=2847)
|
|
@@ -459,19 +434,20 @@ class GeoModelTest(unittest.TestCase):
|
|
|
|
|
|
# Not passing in a geometry as first param shoud
|
|
|
# raise a type error when initializing the GeoQuerySet
|
|
|
- self.assertRaises(TypeError, Country.objects.filter, mpoly__relate=(23, 'foo'))
|
|
|
+ self.assertRaises(ValueError, Country.objects.filter(mpoly__relate=(23, 'foo')).count)
|
|
|
+
|
|
|
# Making sure the right exception is raised for the given
|
|
|
# bad arguments.
|
|
|
- for bad_args, e in [((pnt1, 0), TypeError), ((pnt2, 'T*T***FF*', 0), ValueError)]:
|
|
|
+ for bad_args, e in [((pnt1, 0), ValueError), ((pnt2, 'T*T***FF*', 0), ValueError)]:
|
|
|
qs = Country.objects.filter(mpoly__relate=bad_args)
|
|
|
self.assertRaises(e, qs.count)
|
|
|
|
|
|
# Relate works differently for the different backends.
|
|
|
- if SpatialBackend.postgis or SpatialBackend.spatialite:
|
|
|
+ if postgis or spatialite:
|
|
|
contains_mask = 'T*T***FF*'
|
|
|
within_mask = 'T*F**F***'
|
|
|
intersects_mask = 'T********'
|
|
|
- elif SpatialBackend.oracle:
|
|
|
+ elif oracle:
|
|
|
contains_mask = 'contains'
|
|
|
within_mask = 'inside'
|
|
|
# TODO: This is not quite the same as the PostGIS mask above
|
|
@@ -486,24 +462,23 @@ class GeoModelTest(unittest.TestCase):
|
|
|
self.assertEqual('Lawrence', City.objects.get(point__relate=(ks.poly, within_mask)).name)
|
|
|
|
|
|
# Testing intersection relation mask.
|
|
|
- if not SpatialBackend.oracle:
|
|
|
+ if not oracle:
|
|
|
self.assertEqual('Texas', Country.objects.get(mpoly__relate=(pnt1, intersects_mask)).name)
|
|
|
self.assertEqual('Texas', Country.objects.get(mpoly__relate=(pnt2, intersects_mask)).name)
|
|
|
self.assertEqual('Lawrence', City.objects.get(point__relate=(ks.poly, intersects_mask)).name)
|
|
|
|
|
|
def test16_createnull(self):
|
|
|
"Testing creating a model instance and the geometry being None"
|
|
|
- if DISABLE: return
|
|
|
c = City()
|
|
|
self.assertEqual(c.point, None)
|
|
|
|
|
|
+ @no_mysql
|
|
|
def test17_unionagg(self):
|
|
|
"Testing the `unionagg` (aggregate union) GeoManager method."
|
|
|
- if DISABLE: return
|
|
|
tx = Country.objects.get(name='Texas').mpoly
|
|
|
- # Houston, Dallas, San Antonio -- Oracle has different order.
|
|
|
- union1 = fromstr('MULTIPOINT(-98.493183 29.424170,-96.801611 32.782057,-95.363151 29.763374)')
|
|
|
- union2 = fromstr('MULTIPOINT(-96.801611 32.782057,-95.363151 29.763374,-98.493183 29.424170)')
|
|
|
+ # Houston, Dallas -- Oracle has different order.
|
|
|
+ union1 = fromstr('MULTIPOINT(-96.801611 32.782057,-95.363151 29.763374)')
|
|
|
+ union2 = fromstr('MULTIPOINT(-96.801611 32.782057,-95.363151 29.763374)')
|
|
|
qs = City.objects.filter(point__within=tx)
|
|
|
self.assertRaises(TypeError, qs.unionagg, 'name')
|
|
|
# Using `field_name` keyword argument in one query and specifying an
|
|
@@ -512,7 +487,7 @@ class GeoModelTest(unittest.TestCase):
|
|
|
u1 = qs.unionagg(field_name='point')
|
|
|
u2 = qs.order_by('name').unionagg()
|
|
|
tol = 0.00001
|
|
|
- if SpatialBackend.oracle:
|
|
|
+ if oracle:
|
|
|
union = union2
|
|
|
else:
|
|
|
union = union1
|
|
@@ -523,8 +498,7 @@ class GeoModelTest(unittest.TestCase):
|
|
|
|
|
|
@no_spatialite # SpatiaLite does not support abstract geometry columns
|
|
|
def test18_geometryfield(self):
|
|
|
- "Testing GeometryField."
|
|
|
- if DISABLE: return
|
|
|
+ "Testing the general GeometryField."
|
|
|
Feature(name='Point', geom=Point(1, 1)).save()
|
|
|
Feature(name='LineString', geom=LineString((0, 0), (1, 1), (5, 5))).save()
|
|
|
Feature(name='Polygon', geom=Polygon(LinearRing((0, 0), (0, 5), (5, 5), (5, 0), (0, 0)))).save()
|
|
@@ -545,60 +519,62 @@ class GeoModelTest(unittest.TestCase):
|
|
|
self.assertEqual(True, isinstance(f_4.geom, GeometryCollection))
|
|
|
self.assertEqual(f_3.geom, f_4.geom[2])
|
|
|
|
|
|
+ @no_mysql
|
|
|
def test19_centroid(self):
|
|
|
"Testing the `centroid` GeoQuerySet method."
|
|
|
- if DISABLE: return
|
|
|
qs = State.objects.exclude(poly__isnull=True).centroid()
|
|
|
- if SpatialBackend.oracle:
|
|
|
+ if oracle:
|
|
|
tol = 0.1
|
|
|
- elif SpatialBackend.spatialite:
|
|
|
+ elif spatialite:
|
|
|
tol = 0.000001
|
|
|
else:
|
|
|
tol = 0.000000001
|
|
|
for s in qs:
|
|
|
self.assertEqual(True, s.poly.centroid.equals_exact(s.centroid, tol))
|
|
|
|
|
|
+ @no_mysql
|
|
|
def test20_pointonsurface(self):
|
|
|
"Testing the `point_on_surface` GeoQuerySet method."
|
|
|
- if DISABLE: return
|
|
|
# Reference values.
|
|
|
- if SpatialBackend.oracle:
|
|
|
+ if oracle:
|
|
|
# SELECT SDO_UTIL.TO_WKTGEOMETRY(SDO_GEOM.SDO_POINTONSURFACE(GEOAPP_COUNTRY.MPOLY, 0.05)) FROM GEOAPP_COUNTRY;
|
|
|
ref = {'New Zealand' : fromstr('POINT (174.616364 -36.100861)', srid=4326),
|
|
|
'Texas' : fromstr('POINT (-103.002434 36.500397)', srid=4326),
|
|
|
}
|
|
|
|
|
|
- elif SpatialBackend.postgis or SpatialBackend.spatialite:
|
|
|
+ elif postgis or spatialite:
|
|
|
# Using GEOSGeometry to compute the reference point on surface values
|
|
|
# -- since PostGIS also uses GEOS these should be the same.
|
|
|
ref = {'New Zealand' : Country.objects.get(name='New Zealand').mpoly.point_on_surface,
|
|
|
'Texas' : Country.objects.get(name='Texas').mpoly.point_on_surface
|
|
|
}
|
|
|
- for cntry in Country.objects.point_on_surface():
|
|
|
- if SpatialBackend.spatialite:
|
|
|
+
|
|
|
+ for c in Country.objects.point_on_surface():
|
|
|
+ if spatialite:
|
|
|
# XXX This seems to be a WKT-translation-related precision issue?
|
|
|
tol = 0.00001
|
|
|
- else: tol = 0.000000001
|
|
|
- self.assertEqual(True, ref[cntry.name].equals_exact(cntry.point_on_surface, tol))
|
|
|
+ else:
|
|
|
+ tol = 0.000000001
|
|
|
+ self.assertEqual(True, ref[c.name].equals_exact(c.point_on_surface, tol))
|
|
|
|
|
|
+ @no_mysql
|
|
|
@no_oracle
|
|
|
def test21_scale(self):
|
|
|
"Testing the `scale` GeoQuerySet method."
|
|
|
- if DISABLE: return
|
|
|
xfac, yfac = 2, 3
|
|
|
+ tol = 5 # XXX The low precision tolerance is for SpatiaLite
|
|
|
qs = Country.objects.scale(xfac, yfac, model_att='scaled')
|
|
|
for c in qs:
|
|
|
for p1, p2 in zip(c.mpoly, c.scaled):
|
|
|
for r1, r2 in zip(p1, p2):
|
|
|
for c1, c2 in zip(r1.coords, r2.coords):
|
|
|
- # XXX The low precision is for SpatiaLite
|
|
|
- self.assertAlmostEqual(c1[0] * xfac, c2[0], 5)
|
|
|
- self.assertAlmostEqual(c1[1] * yfac, c2[1], 5)
|
|
|
+ self.assertAlmostEqual(c1[0] * xfac, c2[0], tol)
|
|
|
+ self.assertAlmostEqual(c1[1] * yfac, c2[1], tol)
|
|
|
|
|
|
+ @no_mysql
|
|
|
@no_oracle
|
|
|
def test22_translate(self):
|
|
|
"Testing the `translate` GeoQuerySet method."
|
|
|
- if DISABLE: return
|
|
|
xfac, yfac = 5, -23
|
|
|
qs = Country.objects.translate(xfac, yfac, model_att='translated')
|
|
|
for c in qs:
|
|
@@ -609,57 +585,60 @@ class GeoModelTest(unittest.TestCase):
|
|
|
self.assertAlmostEqual(c1[0] + xfac, c2[0], 5)
|
|
|
self.assertAlmostEqual(c1[1] + yfac, c2[1], 5)
|
|
|
|
|
|
+ @no_mysql
|
|
|
def test23_numgeom(self):
|
|
|
"Testing the `num_geom` GeoQuerySet method."
|
|
|
- if DISABLE: return
|
|
|
# Both 'countries' only have two geometries.
|
|
|
for c in Country.objects.num_geom(): self.assertEqual(2, c.num_geom)
|
|
|
for c in City.objects.filter(point__isnull=False).num_geom():
|
|
|
# Oracle will return 1 for the number of geometries on non-collections,
|
|
|
# whereas PostGIS will return None.
|
|
|
- if SpatialBackend.postgis: self.assertEqual(None, c.num_geom)
|
|
|
- else: self.assertEqual(1, c.num_geom)
|
|
|
+ if postgis:
|
|
|
+ self.assertEqual(None, c.num_geom)
|
|
|
+ else:
|
|
|
+ self.assertEqual(1, c.num_geom)
|
|
|
|
|
|
+ @no_mysql
|
|
|
@no_spatialite # SpatiaLite can only count vertices in LineStrings
|
|
|
def test24_numpoints(self):
|
|
|
"Testing the `num_points` GeoQuerySet method."
|
|
|
- if DISABLE: return
|
|
|
for c in Country.objects.num_points():
|
|
|
self.assertEqual(c.mpoly.num_points, c.num_points)
|
|
|
- if not SpatialBackend.oracle:
|
|
|
+
|
|
|
+ if not oracle:
|
|
|
# Oracle cannot count vertices in Point geometries.
|
|
|
for c in City.objects.num_points(): self.assertEqual(1, c.num_points)
|
|
|
|
|
|
+ @no_mysql
|
|
|
def test25_geoset(self):
|
|
|
"Testing the `difference`, `intersection`, `sym_difference`, and `union` GeoQuerySet methods."
|
|
|
- if DISABLE: return
|
|
|
geom = Point(5, 23)
|
|
|
tol = 1
|
|
|
qs = Country.objects.all().difference(geom).sym_difference(geom).union(geom)
|
|
|
|
|
|
# XXX For some reason SpatiaLite does something screwey with the Texas geometry here. Also,
|
|
|
# XXX it doesn't like the null intersection.
|
|
|
- if SpatialBackend.spatialite:
|
|
|
+ if spatialite:
|
|
|
qs = qs.exclude(name='Texas')
|
|
|
else:
|
|
|
qs = qs.intersection(geom)
|
|
|
-
|
|
|
+
|
|
|
for c in qs:
|
|
|
- if SpatialBackend.oracle:
|
|
|
+ if oracle:
|
|
|
# Should be able to execute the queries; however, they won't be the same
|
|
|
# as GEOS (because Oracle doesn't use GEOS internally like PostGIS or
|
|
|
# SpatiaLite).
|
|
|
pass
|
|
|
else:
|
|
|
self.assertEqual(c.mpoly.difference(geom), c.difference)
|
|
|
- if not SpatialBackend.spatialite:
|
|
|
+ if not spatialite:
|
|
|
self.assertEqual(c.mpoly.intersection(geom), c.intersection)
|
|
|
self.assertEqual(c.mpoly.sym_difference(geom), c.sym_difference)
|
|
|
self.assertEqual(c.mpoly.union(geom), c.union)
|
|
|
|
|
|
+ @no_mysql
|
|
|
def test26_inherited_geofields(self):
|
|
|
"Test GeoQuerySet methods on inherited Geometry fields."
|
|
|
- if DISABLE: return
|
|
|
# Creating a Pennsylvanian city.
|
|
|
mansfield = PennsylvaniaCity.objects.create(name='Mansfield', county='Tioga', point='POINT(-77.071445 41.823881)')
|
|
|
|
|
@@ -669,13 +648,12 @@ class GeoModelTest(unittest.TestCase):
|
|
|
|
|
|
self.assertEqual(1, qs.count())
|
|
|
for pc in qs: self.assertEqual(32128, pc.point.srid)
|
|
|
-
|
|
|
- @no_spatialite
|
|
|
+
|
|
|
+ @no_mysql
|
|
|
@no_oracle
|
|
|
+ @no_spatialite
|
|
|
def test27_snap_to_grid(self):
|
|
|
"Testing GeoQuerySet.snap_to_grid()."
|
|
|
- if DISABLE: return
|
|
|
-
|
|
|
# Let's try and break snap_to_grid() with bad combinations of arguments.
|
|
|
for bad_args in ((), range(3), range(5)):
|
|
|
self.assertRaises(ValueError, Country.objects.snap_to_grid, *bad_args)
|