本文整理匯總了Python中django.contrib.gis.geos.Polygon方法的典型用法代碼示例。如果您正苦於以下問題:Python geos.Polygon方法的具體用法?Python geos.Polygon怎麽用?Python geos.Polygon使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.contrib.gis.geos
的用法示例。
在下文中一共展示了geos.Polygon方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: find
# 需要導入模塊: from django.contrib.gis import geos [as 別名]
# 或者: from django.contrib.gis.geos import Polygon [as 別名]
def find(self, bbox=None, text=None, adm1=None, adm2=None, is_primary=True, threshold=0.5, srid=4326):
qset = self.get_query_set().filter(is_primary=is_primary)
if bbox:
(minx, miny, maxx, maxy) = bbox
bbox = Polygon(((minx,miny),(minx,maxy),(maxx,maxy),(maxx,miny),(minx,miny)),srid=srid)
if srid != 4326: bbox.transform(4326) # convert to lon/lat
qset = qset.filter(geometry__bboverlaps=bbox)
if text:
self.set_threshold(threshold)
# use the pg_trgm index
qset = qset.extra(select={"similarity":"similarity(preferred_name, %s)"},
select_params=[text],
where=["preferred_name %% %s"],
params=[text],
order_by=["-similarity"])
if adm1: qset = qset.filter(admin1__exact=adm1)
if adm2: qset = qset.filter(admin2__exact=adm2)
return qset
示例2: tile
# 需要導入模塊: from django.contrib.gis import geos [as 別名]
# 或者: from django.contrib.gis.geos import Polygon [as 別名]
def tile(self, lonlat, zoom):
"""
Returns a Polygon corresponding to the region represented by a fictional
Google Tile for the given longitude/latitude pair and zoom level. This
tile is used to determine the size of a tile at the given point.
"""
# The given lonlat is the center of the tile.
delta = self._tilesize / 2
# Getting the pixel coordinates corresponding to the
# the longitude/latitude.
px = self.lonlat_to_pixel(lonlat, zoom)
# Getting the lower-left and upper-right lat/lon coordinates
# for the bounding box of the tile.
ll = self.pixel_to_lonlat((px[0] - delta, px[1] - delta), zoom)
ur = self.pixel_to_lonlat((px[0] + delta, px[1] + delta), zoom)
# Constructing the Polygon, representing the tile and returning.
return Polygon(LinearRing(ll, (ll[0], ur[1]), ur, (ur[0], ll[1]), ll), srid=4326)
示例3: create_country
# 需要導入模塊: from django.contrib.gis import geos [as 別名]
# 或者: from django.contrib.gis.geos import Polygon [as 別名]
def create_country(name=None, fips='TT', gmi='TT', iso2='TT', iso3='TST', iso_num=0, border=None, effective=None):
"""Creates a country data model for unit testing
:returns: The file model
:rtype: :class:`storage.models.CountryData`
"""
if not name:
global COUNTRY_NAME_COUNTER
name = 'test-country-%i' % COUNTRY_NAME_COUNTER
COUNTRY_NAME_COUNTER += 1
if not border:
border = geos.Polygon(((0, 0), (0, 10), (10, 10), (10, 0), (0, 0)))
if not effective:
effective = timezone.now()
return CountryData.objects.create(name=name, fips=fips, gmi=gmi, iso2=iso2, iso3=iso3, iso_num=iso_num,
border=border, effective=effective)
示例4: test_country_data
# 需要導入模塊: from django.contrib.gis import geos [as 別名]
# 或者: from django.contrib.gis.geos import Polygon [as 別名]
def test_country_data(self):
"""Tests adding a border and country intersection calculation."""
testborder = geos.Polygon(((0, 0), (0, 10), (10, 10), (10, 0), (0, 0)))
testborder2 = geos.Polygon(((11, 0), (11, 8), (19, 8), (19, 0), (11, 0)))
testborder3 = geos.Polygon(((11, 11), (11, 15), (15, 15), (15, 11), (11, 11)))
testeffective = datetime.datetime(2000, 1, 1, 0, 0, 0, tzinfo=utc)
CountryData.objects.create(name='Test Country', fips='TC', gmi='TCY', iso2='TC', iso3='TCY', iso_num=42,
border=testborder, effective=testeffective)
CountryData.objects.create(name='Test Country 2', fips='TT', gmi='TCT', iso2='TT', iso3='TCT', iso_num=43,
border=testborder2, effective=testeffective)
CountryData.objects.create(name='Test Country 3', fips='TH', gmi='TCH', iso2='TH', iso3='TCH', iso_num=44,
border=testborder3, effective=testeffective)
ws = storage_test_utils.create_workspace(name='test', base_url='http://localhost')
scale_file = storage_test_utils.create_file(file_name='test.txt', workspace=ws)
with transaction.atomic():
scale_file.geometry = geos.Polygon(((5, 5), (5, 10), (12, 10), (12, 5), (5, 5)))
scale_file.set_countries()
scale_file.save()
tmp = [c.iso2 for c in scale_file.countries.all()]
self.assertEqual(len(tmp), 2)
self.assertIn('TC', tmp)
self.assertIn('TT', tmp)
示例5: extent_polygon
# 需要導入模塊: from django.contrib.gis import geos [as 別名]
# 或者: from django.contrib.gis.geos import Polygon [as 別名]
def extent_polygon(self):
"""
Convert extent into something more useful--a simple geos polygon
"""
try:
# This seems to raise if no rows exist
extent = self.extent()
except:
return None
bounds = Polygon((
(extent[0], extent[1]),
(extent[0], extent[3]),
(extent[2], extent[3]),
(extent[2], extent[1]),
(extent[0], extent[1]),
))
return bounds
示例6: chop_geom
# 需要導入模塊: from django.contrib.gis import geos [as 別名]
# 或者: from django.contrib.gis.geos import Polygon [as 別名]
def chop_geom(multipolygon, fraction):
"""
Transforms each point fraction the distance to the geometry's centroid to form a smaller geometry
:param geom:
:return: a multipolygon reduced by the fraction from the original
"""
def transform_polygon(polygon):
def transform_linear_ring(linear_ring):
centroid = polygon.centroid
return LinearRing(
map(lambda point: to_tuple(LineString((point, centroid)).interpolate(fraction, normalized=True)),
linear_ring))
linear_rings = map(lambda linear_ring: transform_linear_ring(linear_ring), polygon)
if len(linear_rings) > 1:
return Polygon(linear_rings[0], [linear_rings[1:]])
else:
return Polygon(linear_rings[0], [])
return MultiPolygon(map(lambda polygon: transform_polygon(polygon), multipolygon))
示例7: test_alter_geom_field_dim
# 需要導入模塊: from django.contrib.gis import geos [as 別名]
# 或者: from django.contrib.gis.geos import Polygon [as 別名]
def test_alter_geom_field_dim(self):
Neighborhood = self.current_state.apps.get_model('gis', 'Neighborhood')
p1 = Polygon(((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)))
Neighborhood.objects.create(name='TestDim', geom=MultiPolygon(p1, p1))
# Add 3rd dimension.
self.alter_gis_model(
migrations.AlterField, 'Neighborhood', 'geom', False,
fields.MultiPolygonField, field_class_kwargs={'srid': 4326, 'dim': 3}
)
self.assertTrue(Neighborhood.objects.first().geom.hasz)
# Rewind to 2 dimensions.
self.alter_gis_model(
migrations.AlterField, 'Neighborhood', 'geom', False,
fields.MultiPolygonField, field_class_kwargs={'srid': 4326, 'dim': 2}
)
self.assertFalse(Neighborhood.objects.first().geom.hasz)
示例8: administrative_division
# 需要導入模塊: from django.contrib.gis import geos [as 別名]
# 或者: from django.contrib.gis.geos import Polygon [as 別名]
def administrative_division(administrative_division_type, municipality):
division = AdministrativeDivision.objects.create(
name_en='test division',
type=administrative_division_type,
ocd_id='ocd-division/test:1',
municipality=municipality,
)
coords = ((0, 0), (0, 200), (200, 200), (200, 0), (0, 0))
AdministrativeDivisionGeometry.objects.create(division=division, boundary=MultiPolygon([Polygon(coords)]))
return division
示例9: administrative_division2
# 需要導入模塊: from django.contrib.gis import geos [as 別名]
# 或者: from django.contrib.gis.geos import Polygon [as 別名]
def administrative_division2(administrative_division_type):
division = AdministrativeDivision.objects.create(
name_en='test division 2',
type=administrative_division_type,
ocd_id='ocd-division/test:2'
)
coords = ((100, 100), (100, 300), (300, 300), (300, 100), (100, 100))
AdministrativeDivisionGeometry.objects.create(division=division, boundary=MultiPolygon([Polygon(coords)]))
return division
示例10: write
# 需要導入模塊: from django.contrib.gis import geos [as 別名]
# 或者: from django.contrib.gis.geos import Polygon [as 別名]
def write(self, geom):
"Return the WKB representation of the given geometry."
from django.contrib.gis.geos import Polygon
geom = self._handle_empty_point(geom)
wkb = wkb_writer_write(self.ptr, geom.ptr, byref(c_size_t()))
if isinstance(geom, Polygon) and geom.empty:
# Fix GEOS output for empty polygon.
# See https://trac.osgeo.org/geos/ticket/680.
wkb = wkb[:-8] + b'\0' * 4
return memoryview(wkb)
示例11: write_hex
# 需要導入模塊: from django.contrib.gis import geos [as 別名]
# 或者: from django.contrib.gis.geos import Polygon [as 別名]
def write_hex(self, geom):
"Return the HEXEWKB representation of the given geometry."
from django.contrib.gis.geos.polygon import Polygon
geom = self._handle_empty_point(geom)
wkb = wkb_writer_write_hex(self.ptr, geom.ptr, byref(c_size_t()))
if isinstance(geom, Polygon) and geom.empty:
wkb = wkb[:-16] + b'0' * 8
return wkb
# ### WKBWriter Properties ###
# Property for getting/setting the byteorder.
示例12: setUp
# 需要導入模塊: from django.contrib.gis import geos [as 別名]
# 或者: from django.contrib.gis.geos import Polygon [as 別名]
def setUp(self):
django.setup()
self.testborder = geos.Polygon(((0, 0), (0, 10), (10, 10), (0, 10), (0, 0)))
self.testeffective = datetime.datetime(2000, 1, 1, 0, 0, 0, tzinfo=utc)
CountryData.objects.create(name='Test Country', fips='TC', gmi='TCY', iso2='TC', iso3='TCY', iso_num=42,
border=self.testborder, effective=self.testeffective)
示例13: test_border_update
# 需要導入模塊: from django.contrib.gis import geos [as 別名]
# 或者: from django.contrib.gis.geos import Polygon [as 別名]
def test_border_update(self):
newborder = geos.Polygon(((0, 0), (42, 0), (42, 42), (0, 42), (0, 0)))
neweffective = datetime.datetime(2010, 4, 5, 18, 26, 0, tzinfo=utc)
CountryData.objects.update_border('Test Country', newborder, neweffective)
tmp = CountryData.objects.filter(name='Test Country', effective=neweffective)
self.assertEqual(tmp[0].border, newborder)
self.assertEqual(tmp[0].effective, neweffective)
self.assertEqual(tmp[0].fips, 'TC')
示例14: test_border_update_not_found
# 需要導入模塊: from django.contrib.gis import geos [as 別名]
# 或者: from django.contrib.gis.geos import Polygon [as 別名]
def test_border_update_not_found(self):
newborder = geos.Polygon(((0, 0), (42, 0), (42, 42), (0, 42), (0, 0)))
self.assertRaises(CountryData.DoesNotExist, CountryData.objects.update_border, 'Kerblekistan', newborder)
示例15: reassign_spatial_geometry
# 需要導入模塊: from django.contrib.gis import geos [as 別名]
# 或者: from django.contrib.gis.geos import Polygon [as 別名]
def reassign_spatial_geometry(instance):
coords = list(instance.geometry.coords)
if type(coords[0]) == float:
coords = [coords]
else:
while (type(coords[0][0]) != float):
coords = coords[0]
coords = [list(x) for x in coords]
for point in coords:
if point[0] >= -180 and point[0] <= 180:
return
while coords[0][0] < -180:
for point in coords:
point[0] += 360
while coords[0][0] > 180:
for point in coords:
point[0] -= 360
geometry = []
for point in coords:
latlng = [point[0], point[1]]
geometry.append(tuple(latlng))
if len(geometry) > 1:
if geometry[0] == geometry[-1]:
instance.geometry = dumps(Polygon(geometry))
else:
instance.geometry = dumps(LineString(geometry))
else:
instance.geometry = dumps(Point(geometry))