2008-08-06 02:13:06 +08:00
|
|
|
"""
|
|
|
|
Utilities for manipulating Geometry WKT.
|
|
|
|
"""
|
2016-04-17 02:40:05 +08:00
|
|
|
import warnings
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2012-07-20 20:22:00 +08:00
|
|
|
from django.utils import six
|
2016-04-17 02:40:05 +08:00
|
|
|
from django.utils.deprecation import RemovedInDjango20Warning
|
2012-07-20 20:22:00 +08:00
|
|
|
|
2013-11-03 04:12:09 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
def precision_wkt(geom, prec):
|
|
|
|
"""
|
2012-07-20 20:22:00 +08:00
|
|
|
Returns WKT text of the geometry according to the given precision (an
|
2008-08-06 02:13:06 +08:00
|
|
|
integer or a string). If the precision is an integer, then the decimal
|
|
|
|
places of coordinates WKT will be truncated to that number:
|
|
|
|
|
2013-10-11 19:25:14 +08:00
|
|
|
>>> from django.contrib.gis.geos import Point
|
2008-08-06 02:13:06 +08:00
|
|
|
>>> pnt = Point(5, 23)
|
|
|
|
>>> pnt.wkt
|
|
|
|
'POINT (5.0000000000000000 23.0000000000000000)'
|
2013-10-11 19:25:14 +08:00
|
|
|
>>> precision_wkt(pnt, 1)
|
2008-08-06 02:13:06 +08:00
|
|
|
'POINT (5.0 23.0)'
|
|
|
|
|
2012-07-20 20:22:00 +08:00
|
|
|
If the precision is a string, it must be valid Python format string
|
2008-08-06 02:13:06 +08:00
|
|
|
(e.g., '%20.7f') -- thus, you should know what you're doing.
|
|
|
|
"""
|
2016-04-17 02:40:05 +08:00
|
|
|
warnings.warn(
|
|
|
|
"precision_wkt() is deprecated in favor of the WKTWriter class.",
|
|
|
|
RemovedInDjango20Warning, stacklevel=2
|
|
|
|
)
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
if isinstance(prec, int):
|
|
|
|
num_fmt = '%%.%df' % prec
|
2012-07-20 20:22:00 +08:00
|
|
|
elif isinstance(prec, six.string_types):
|
2008-08-06 02:13:06 +08:00
|
|
|
num_fmt = prec
|
|
|
|
else:
|
|
|
|
raise TypeError
|
|
|
|
|
|
|
|
# TODO: Support 3D geometries.
|
|
|
|
coord_fmt = ' '.join([num_fmt, num_fmt])
|
|
|
|
|
|
|
|
def formatted_coords(coords):
|
2013-08-30 07:20:00 +08:00
|
|
|
return ','.join(coord_fmt % c[:2] for c in coords)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def formatted_poly(poly):
|
2013-08-30 07:20:00 +08:00
|
|
|
return ','.join('(%s)' % formatted_coords(r) for r in poly)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def formatted_geom(g):
|
|
|
|
gtype = str(g.geom_type).upper()
|
|
|
|
yield '%s(' % gtype
|
|
|
|
if gtype == 'POINT':
|
|
|
|
yield formatted_coords((g.coords,))
|
|
|
|
elif gtype in ('LINESTRING', 'LINEARRING'):
|
|
|
|
yield formatted_coords(g.coords)
|
|
|
|
elif gtype in ('POLYGON', 'MULTILINESTRING'):
|
|
|
|
yield formatted_poly(g)
|
|
|
|
elif gtype == 'MULTIPOINT':
|
|
|
|
yield formatted_coords(g.coords)
|
|
|
|
elif gtype == 'MULTIPOLYGON':
|
2013-08-30 07:20:00 +08:00
|
|
|
yield ','.join('(%s)' % formatted_poly(p) for p in g)
|
2008-08-06 02:13:06 +08:00
|
|
|
elif gtype == 'GEOMETRYCOLLECTION':
|
2013-08-30 07:20:00 +08:00
|
|
|
yield ','.join(''.join(wkt for wkt in formatted_geom(child)) for child in g)
|
2008-08-06 02:13:06 +08:00
|
|
|
else:
|
|
|
|
raise TypeError
|
|
|
|
yield ')'
|
|
|
|
|
2013-08-30 07:20:00 +08:00
|
|
|
return ''.join(wkt for wkt in formatted_geom(geom))
|