Now support SVG output in SpatiaLite; added tests for SVG output.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@10428 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Justin Bronn 2009-04-07 20:12:08 +00:00
parent f2bdc14a95
commit 2ec48a1dd9
4 changed files with 31 additions and 7 deletions

View File

@ -47,6 +47,7 @@ SpatialBackend = BaseSpatialBackend(name='spatialite', spatialite=True,
point_on_surface=POINT_ON_SURFACE, point_on_surface=POINT_ON_SURFACE,
scale=SCALE, scale=SCALE,
select=GEOM_SELECT, select=GEOM_SELECT,
svg=ASSVG,
sym_difference=SYM_DIFFERENCE, sym_difference=SYM_DIFFERENCE,
transform=TRANSFORM, transform=TRANSFORM,
translate=TRANSLATE, translate=TRANSLATE,

View File

@ -17,6 +17,7 @@ def get_func(str):
# Functions used by the GeoManager & GeoQuerySet # Functions used by the GeoManager & GeoQuerySet
AREA = get_func('Area') AREA = get_func('Area')
ASSVG = get_func('AsSVG')
CENTROID = get_func('Centroid') CENTROID = get_func('Centroid')
CONTAINED = get_func('MbrWithin') CONTAINED = get_func('MbrWithin')
DIFFERENCE = get_func('Difference') DIFFERENCE = get_func('Difference')

View File

@ -275,15 +275,26 @@ class GeoQuerySet(QuerySet):
return self._spatial_attribute('snap_to_grid', s, **kwargs) return self._spatial_attribute('snap_to_grid', s, **kwargs)
def svg(self, **kwargs): def svg(self, relative=False, precision=8, **kwargs):
""" """
Returns SVG representation of the geographic field in a `svg` Returns SVG representation of the geographic field in a `svg`
attribute on each element of this GeoQuerySet. attribute on each element of this GeoQuerySet.
Keyword Arguments:
`relative` => If set to True, this will evaluate the path in
terms of relative moves (rather than absolute).
`precision` => May be used to set the maximum number of decimal
digits used in output (defaults to 8).
""" """
relative = int(bool(relative))
if not isinstance(precision, (int, long)):
raise TypeError('SVG precision keyword argument must be an integer.')
s = {'desc' : 'SVG', s = {'desc' : 'SVG',
'procedure_fmt' : '%(geo_col)s,%(rel)s,%(precision)s', 'procedure_fmt' : '%(geo_col)s,%(rel)s,%(precision)s',
'procedure_args' : {'rel' : int(kwargs.pop('relative', 0)), 'procedure_args' : {'rel' : relative,
'precision' : kwargs.pop('precision', 8)}, 'precision' : precision,
}
} }
return self._spatial_attribute('svg', s, **kwargs) return self._spatial_attribute('svg', s, **kwargs)

View File

@ -196,6 +196,19 @@ class GeoModelTest(unittest.TestCase):
# Finally, we set every available keyword. # Finally, we set every available keyword.
self.assertEqual(City.objects.geojson(bbox=True, crs=True, precision=5).get(name='Chicago').geojson, json) self.assertEqual(City.objects.geojson(bbox=True, crs=True, precision=5).get(name='Chicago').geojson, json)
@no_oracle
def test03d_svg(self):
"Testing SVG output using GeoQuerySet.svg()."
if DISABLE: 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"'
# Even though relative, only one point so it's practically the same except for
# the 'c' letter prefix on the x,y values.
svg2 = svg1.replace('c', '')
self.assertEqual(svg1, City.objects.svg().get(name='Pueblo').svg)
self.assertEqual(svg2, City.objects.svg(relative=5).get(name='Pueblo').svg)
def test04_transform(self): def test04_transform(self):
"Testing the transform() GeoManager method." "Testing the transform() GeoManager method."
if DISABLE: return if DISABLE: return
@ -499,8 +512,6 @@ class GeoModelTest(unittest.TestCase):
union = union1 union = union1
self.assertEqual(True, union.equals_exact(u1, tol)) self.assertEqual(True, union.equals_exact(u1, tol))
self.assertEqual(True, union.equals_exact(u2, tol)) self.assertEqual(True, union.equals_exact(u2, tol))
# SpatiaLite will segfault trying to union a NULL geometry.
if not SpatialBackend.spatialite:
qs = City.objects.filter(name='NotACity') qs = City.objects.filter(name='NotACity')
self.assertEqual(None, qs.unionagg(field_name='point')) self.assertEqual(None, qs.unionagg(field_name='point'))