本文整理汇总了Python中django.contrib.gis.db.models.functions.Transform方法的典型用法代码示例。如果您正苦于以下问题:Python functions.Transform方法的具体用法?Python functions.Transform怎么用?Python functions.Transform使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.contrib.gis.db.models.functions
的用法示例。
在下文中一共展示了functions.Transform方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: select
# 需要导入模块: from django.contrib.gis.db.models import functions [as 别名]
# 或者: from django.contrib.gis.db.models.functions import Transform [as 别名]
def select(self):
kwargs = {}
data = self.cleaned_data
tolerance, srs, format = map(data.get, ('simplify', 'srs', 'format'))
expr = field = query.geo_field(self.queryset).name
srid = getattr(srs, 'srid', None)
if srid:
expr = functions.Transform(expr, srid)
if data['op']:
expr = data['op'](expr)
if data['precision'] is not None:
kwargs.update(precision=data['precision'])
if tolerance:
expr = query.Simplify(expr, tolerance)
if format:
expr = format(expr, **kwargs)
if expr != field:
attrname = self.data.get('format')
self.queryset = self.queryset.annotate(**{attrname: expr})
示例2: calculate_area_field
# 需要导入模块: from django.contrib.gis.db.models import functions [as 别名]
# 或者: from django.contrib.gis.db.models.functions import Transform [as 别名]
def calculate_area_field(apps, schema_editor):
SpatialUnit = apps.get_model('spatial', 'SpatialUnit')
SpatialUnit.objects.exclude(geometry=None).extra(
where=["geometrytype(geometry) LIKE 'POLYGON'"]).update(
area=Area(Transform('geometry', 3857)))
示例3: extent
# 需要导入模块: from django.contrib.gis.db.models import functions [as 别名]
# 或者: from django.contrib.gis.db.models.functions import Transform [as 别名]
def extent(self, srid=None):
"""Returns the GeoQuerySet extent as a 4-tuple.
Keyword args:
srid -- EPSG id for for transforming the output geometry.
"""
expr = self.geo_field.name
if srid:
expr = geofn.Transform(expr, srid)
expr = models.Extent(expr)
clone = self.all()
name, val = clone.aggregate(expr).popitem()
return val
示例4: tile
# 需要导入模块: from django.contrib.gis.db.models import functions [as 别名]
# 或者: from django.contrib.gis.db.models.functions import Transform [as 别名]
def tile(self, bbox, z=0, format=None, clip=True):
"""Returns a GeoQuerySet intersecting a tile boundary.
Arguments:
bbox -- tile extent as geometry
Keyword args:
z -- tile zoom level used as basis for geometry simplification
format -- vector tile format as str (pbf, geojson)
clip -- clip geometries to tile boundary as boolean
"""
# Tile grid uses 3857, but GeoJSON coordinates should be in 4326.
tile_srid = 3857
bbox = getattr(bbox, 'geos', bbox)
clone = filter_geometry(self, intersects=bbox)
field = clone.geo_field
srid = field.srid
sql = field.name
try:
tilew = self.tilewidths[z]
except IndexError:
tilew = self.tilewidths[-1]
if bbox.srid != srid:
bbox = bbox.transform(srid, clone=True)
# Estimate tile width in degrees instead of meters.
if bbox.srs.geographic:
p = geos.Point(tilew, tilew, srid=tile_srid)
p.transform(srid)
tilew = p.x
if clip:
bufbox = bbox.buffer(tilew)
sql = geofn.Intersection(sql, bufbox.envelope)
sql = SimplifyPreserveTopology(sql, tilew)
if format == 'pbf':
return clone.pbf(bbox, geo_col=sql)
sql = geofn.Transform(sql, 4326)
return clone.annotate(**{format: sql})
示例5: test_serialize_queryset_simplify
# 需要导入模块: from django.contrib.gis.db.models import functions [as 别名]
# 或者: from django.contrib.gis.db.models.functions import Transform [as 别名]
def test_serialize_queryset_simplify(self):
fn = query.Simplify(functions.Transform('geom', 4269), 1.01)
qs = Location.objects.all()
for obj in qs:
obj.geom = obj.geom.buffer(1.5)
obj.save()
qs = qs.annotate(simplify=fn)
obj = qs[0]
serializer = SimplifyLocationSerializer(obj)
g = geos.GEOSGeometry(json.dumps(serializer.data['geometry']),
srid=obj.simplify.srid)
self.assertEqual(g, obj.simplify)
self.assertEqual(obj.simplify.srid, 4269)
self.assertEqual(serializer.data['crs']['properties']['name'][-4:], '4269')
示例6: test_simplify_geojson
# 需要导入模块: from django.contrib.gis.db.models import functions [as 别名]
# 或者: from django.contrib.gis.db.models.functions import Transform [as 别名]
def test_simplify_geojson(self):
fn = functions.AsGeoJSON(
query.Simplify(functions.Transform('geom', self.srid), self.tol),
precision=2)
sqs = self.qs.all().annotate(geojson=fn)
geom = geos.GEOSGeometry(sqs[0].geojson, self.srid)
source = self.qs[0].geom
self.assertNotEqual(geom, source)
self.assertNotEqual(geom.srid, source.srid)
self.assertLess(geom.num_coords, source.num_coords)
示例7: test_inherited_geofields
# 需要导入模块: from django.contrib.gis.db.models import functions [as 别名]
# 或者: from django.contrib.gis.db.models.functions import Transform [as 别名]
def test_inherited_geofields(self):
"Database functions on inherited Geometry fields."
# Creating a Pennsylvanian city.
PennsylvaniaCity.objects.create(name='Mansfield', county='Tioga', point='POINT(-77.071445 41.823881)')
# All transformation SQL will need to be performed on the
# _parent_ table.
qs = PennsylvaniaCity.objects.annotate(new_point=functions.Transform('point', srid=32128))
self.assertEqual(1, qs.count())
for pc in qs:
self.assertEqual(32128, pc.new_point.srid)
示例8: test_transform
# 需要导入模块: from django.contrib.gis.db.models import functions [as 别名]
# 或者: from django.contrib.gis.db.models.functions import Transform [as 别名]
def test_transform(self):
# Pre-transformed points for Houston and Pueblo.
ptown = fromstr('POINT(992363.390841912 481455.395105533)', srid=2774)
prec = 3 # Precision is low due to version variations in PROJ and GDAL.
# Asserting the result of the transform operation with the values in
# the pre-transformed points.
h = City.objects.annotate(pt=functions.Transform('point', ptown.srid)).get(name='Pueblo')
self.assertEqual(2774, h.pt.srid)
self.assertAlmostEqual(ptown.x, h.pt.x, prec)
self.assertAlmostEqual(ptown.y, h.pt.y, prec)
示例9: kml
# 需要导入模块: from django.contrib.gis.db.models import functions [as 别名]
# 或者: from django.contrib.gis.db.models.functions import Transform [as 别名]
def kml(request, label, model, field_name=None, compress=False, using=DEFAULT_DB_ALIAS):
"""
This view generates KML for the given app label, model, and field name.
The field name must be that of a geographic field.
"""
placemarks = []
try:
klass = apps.get_model(label, model)
except LookupError:
raise Http404('You must supply a valid app label and module name. Got "%s.%s"' % (label, model))
if field_name:
try:
field = klass._meta.get_field(field_name)
if not isinstance(field, GeometryField):
raise FieldDoesNotExist
except FieldDoesNotExist:
raise Http404('Invalid geometry field.')
connection = connections[using]
if connection.features.has_AsKML_function:
# Database will take care of transformation.
placemarks = klass._default_manager.using(using).annotate(kml=AsKML(field_name))
else:
# If the database offers no KML method, we use the `kml`
# attribute of the lazy geometry instead.
placemarks = []
if connection.features.has_Transform_function:
qs = klass._default_manager.using(using).annotate(
**{'%s_4326' % field_name: Transform(field_name, 4326)})
field_name += '_4326'
else:
qs = klass._default_manager.using(using).all()
for mod in qs:
mod.kml = getattr(mod, field_name).kml
placemarks.append(mod)
# Getting the render function and rendering to the correct.
if compress:
render = render_to_kmz
else:
render = render_to_kml
return render('gis/kml/placemarks.kml', {'places': placemarks})
示例10: kml
# 需要导入模块: from django.contrib.gis.db.models import functions [as 别名]
# 或者: from django.contrib.gis.db.models.functions import Transform [as 别名]
def kml(request, label, model, field_name=None, compress=False, using=DEFAULT_DB_ALIAS):
"""
This view generates KML for the given app label, model, and field name.
The model's default manager must be GeoManager, and the field name
must be that of a geographic field.
"""
placemarks = []
try:
klass = apps.get_model(label, model)
except LookupError:
raise Http404('You must supply a valid app label and module name. Got "%s.%s"' % (label, model))
if field_name:
try:
field = klass._meta.get_field(field_name)
if not isinstance(field, GeometryField):
raise FieldDoesNotExist
except FieldDoesNotExist:
raise Http404('Invalid geometry field.')
connection = connections[using]
if connection.features.has_AsKML_function:
# Database will take care of transformation.
placemarks = klass._default_manager.using(using).annotate(kml=AsKML(field_name))
else:
# If the database offers no KML method, we use the `kml`
# attribute of the lazy geometry instead.
placemarks = []
if connection.features.has_Transform_function:
qs = klass._default_manager.using(using).annotate(
**{'%s_4326' % field_name: Transform(field_name, 4326)})
field_name += '_4326'
else:
qs = klass._default_manager.using(using).all()
for mod in qs:
mod.kml = getattr(mod, field_name).kml
placemarks.append(mod)
# Getting the render function and rendering to the correct.
if compress:
render = render_to_kmz
else:
render = render_to_kml
return render('gis/kml/placemarks.kml', {'places': placemarks})