本文整理汇总了Python中shapely.geometry.polygon.Polygon方法的典型用法代码示例。如果您正苦于以下问题:Python polygon.Polygon方法的具体用法?Python polygon.Polygon怎么用?Python polygon.Polygon使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类shapely.geometry.polygon
的用法示例。
在下文中一共展示了polygon.Polygon方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: polygon_2_component
# 需要导入模块: from shapely.geometry import polygon [as 别名]
# 或者: from shapely.geometry.polygon import Polygon [as 别名]
def polygon_2_component(polygon):
"""
To convert a polygon into a component
Parameters
----------
polygon: shapely.geometry.Polygon
The polygon to convert to a componen
Returns
-------
tuple(list, list)
the first list contains the coordinates of the exterior ring
the second list contains the interior rings, each defined by a list of coordinates
"""
exterior = list(polygon.exterior.coords)
interiors = []
for interior in polygon.interiors:
interiors.append(list(interior.coords))
return exterior, interiors
示例2: _drop_degenerate_inners
# 需要导入模块: from shapely.geometry import polygon [as 别名]
# 或者: from shapely.geometry.polygon import Polygon [as 别名]
def _drop_degenerate_inners(shape):
"""
Drop degenerate (zero-size) inners from the polygon.
This is implemented as dropping anything with a size less than 0.5, as the
polygon is in integer coordinates and the smallest valid inner would be a
triangle with height and width 1.
"""
assert shape.geom_type == 'Polygon'
new_inners = []
for inner in shape.interiors:
# need to make a polygon of the linearring to get the _filled_ area of
# the closed ring.
if abs(Polygon(inner).area) >= 0.5:
new_inners.append(inner)
return Polygon(shape.exterior, new_inners)
示例3: _union_in_blocks
# 需要导入模块: from shapely.geometry import polygon [as 别名]
# 或者: from shapely.geometry.polygon import Polygon [as 别名]
def _union_in_blocks(contours, block_size):
"""
Generator which yields a valid shape for each block_size multiple of
input contours. This merges together the contours for each block before
yielding them.
"""
n_contours = len(contours)
for i in range(0, n_contours, block_size):
j = min(i + block_size, n_contours)
inners = []
for c in contours[i:j]:
p = _contour_to_poly(c)
if p.type == 'Polygon':
inners.append(p)
elif p.type == 'MultiPolygon':
inners.extend(p.geoms)
holes = unary_union(inners)
assert holes.is_valid
yield holes
示例4: make_valid_polygon
# 需要导入模块: from shapely.geometry import polygon [as 别名]
# 或者: from shapely.geometry.polygon import Polygon [as 别名]
def make_valid_polygon(shape):
"""
Make a polygon valid. Polygons can be invalid in many ways, such as
self-intersection, self-touching and degeneracy. This process attempts to
make a polygon valid while retaining as much of its extent or area as
possible.
First, we call pyclipper to robustly union the polygon. Using this on its
own appears to be good for "cleaning" the polygon.
This might result in polygons which still have degeneracies according to
the OCG standard of validity - as pyclipper does not consider these to be
invalid. Therefore we follow by using the `buffer(0)` technique to attempt
to remove any remaining degeneracies.
"""
assert shape.geom_type == 'Polygon'
shape = make_valid_pyclipper(shape)
assert shape.is_valid
return shape
示例5: enforce_polygon_winding_order
# 需要导入模块: from shapely.geometry import polygon [as 别名]
# 或者: from shapely.geometry.polygon import Polygon [as 别名]
def enforce_polygon_winding_order(self, shape, y_coord_down, n_try):
assert shape.type == 'Polygon'
def fn(point):
x, y = point
return self._round(x), self._round(y)
exterior = apply_map(fn, shape.exterior.coords)
rings = None
if len(shape.interiors) > 0:
rings = [apply_map(fn, ring.coords) for ring in shape.interiors]
sign = 1.0 if y_coord_down else -1.0
oriented_shape = orient(Polygon(exterior, rings), sign=sign)
oriented_shape = self.handle_shape_validity(
oriented_shape, y_coord_down, n_try)
return oriented_shape
示例6: get_as_shapely
# 需要导入模块: from shapely.geometry import polygon [as 别名]
# 或者: from shapely.geometry.polygon import Polygon [as 别名]
def get_as_shapely(self, cell, layer=None, datatype=None):
geometry = []
gdspy_cell = self.gdslib.cells[cell] if isinstance(cell, str) else cell
for polygon in gdspy_cell.polygons:
if self.layer is not None and layer != polygon.layers[0]:
continue
if self.datatype is not None and datatype != polygon.datatypes[0]:
continue
geometry.append(Polygon(polygon.polygons[0]).buffer(0)) # .buffer(0) for healing geometries
for reference in gdspy_cell.references:
sub_geometry = self.get_as_shapely(reference.ref_cell, layer, datatype)
if sub_geometry.is_empty:
continue
sub_geometry = scale(sub_geometry,
*[reference.magnification] * 2) if reference.magnification else sub_geometry
sub_geometry = scale(sub_geometry, -1) if reference.x_reflection else sub_geometry
sub_geometry = rotate(sub_geometry, reference.rotation,
origin=(0, 0)) if reference.rotation else sub_geometry
sub_geometry = translate(sub_geometry, *reference.origin)
geometry.extend(sub_geometry)
return MultiPolygon(geometry)
示例7: hull_accuracy
# 需要导入模块: from shapely.geometry import polygon [as 别名]
# 或者: from shapely.geometry.polygon import Polygon [as 别名]
def hull_accuracy(problem, result, target):
nzr = numpy.nonzero(result)[0]
nzt = numpy.nonzero(target)[0]
result = result[nzr]
target = target[nzt]
if len(result) < 3 or len(set(result)) != len(result):
return -1.0, 0.0
pp = Polygon(problem[result])
if pp.is_valid:
# intersected area
tt = Polygon(problem[target])
intersection = tt.intersection(pp)
intersec_per = intersection.area / tt.area
if set(result) == set(target):
return 1.0, intersec_per
else:
return 0.0, intersec_per
else:
return -1.0, 0.0
示例8: _get_merged_polygon
# 需要导入模块: from shapely.geometry import polygon [as 别名]
# 或者: from shapely.geometry.polygon import Polygon [as 别名]
def _get_merged_polygon(self, cluster):
"""Merge polygons using shapely (Internal).
Given a single cluster from _get_merge_clusters_from_df(), This creates
and merges polygons into a single cascaded union. It first dilates the
polygons by buffer_size pixels to make them overlap, merges them,
then erodes back by buffer_size to get the merged polygon.
"""
buffer_size = self.merge_thresh + 3
nest_polygons = []
for nestinfo in cluster:
nest = dict(self.edge_contours[nestinfo['roiname']].loc[
nestinfo['nid'], :])
roitop = self.roiinfos[nestinfo['roiname']]['top']
roileft = self.roiinfos[nestinfo['roiname']]['left']
coords = _parse_annot_coords(
nest, x_offset=roileft, y_offset=roitop)
nest_polygons.append(Polygon(coords).buffer(buffer_size))
merged_polygon = cascaded_union(nest_polygons).buffer(-buffer_size)
return merged_polygon
# %% =====================================================================
示例9: offset
# 需要导入模块: from shapely.geometry import polygon [as 别名]
# 或者: from shapely.geometry.polygon import Polygon [as 别名]
def offset(self, dist, side):
if dist == 0:
return _SidedPolygon(self.poly, self.level)
if dist < 0:
side = not side
dist = abs(dist)
if (side == c.OUTSIDE and self.isFeature) or (side == c.INSIDE and not self.isFeature):
return _SidedPolygon(self.poly.buffer(dist), self.level)
try:
buffPoly = self.poly.exterior.buffer(dist)
if len(buffPoly.interiors) > 1:
inPoly = cascaded_union([Polygon(i) for i in buffPoly.interiors])
else:
inPoly = Polygon(buffPoly.interiors[0])
return _SidedPolygon(inPoly, self.level)
except Exception:
return None
示例10: push_to_front
# 需要导入模块: from shapely.geometry import polygon [as 别名]
# 或者: from shapely.geometry.polygon import Polygon [as 别名]
def push_to_front(self, pg_ori):
pg_new = copy.deepcopy(pg_ori)
for index in range(self.num_object):
if index == 0:
print pg_new.objects[index].terminal.obj_center_proposals
for step in range(10):
center = pg_new.objects[index].terminal.obj_center
corners = copy.deepcopy(pg_new.layouts.corners)
p1, p2, p3, p4 = rectangle_shrink(corners[0], corners[1], corners[2], corners[3], 0.8)
point = Point(center[0], center[1])
polygon = Polygon([(p1[0], p1[1]), (p2[0], p2[1]), (p3[0], p3[1]), (p4[0], p4[1])])
if not polygon.contains(point) or pg_new.objects[index].terminal.obj_center[2] - pg_new.objects[index].terminal.obj_size[2] / 4 < pg_new.layouts.floor[0][2]:
pg_new.objects[index].terminal.set_center(copy.deepcopy(pg_new.objects[index].terminal.obj_center_proposals[step]))
else:
break
e_total_new = self.compute_total_likelihood(pg_new, show_energy=True) + self.compute_prior(pg_new)
return pg_new, e_total_new
# infer the best parse graph with lowest energy
示例11: buffer
# 需要导入模块: from shapely.geometry import polygon [as 别名]
# 或者: from shapely.geometry.polygon import Polygon [as 别名]
def buffer(self, distance=None, join_style=2):
# type: (Optional[float], Optional[int]) -> Polygon2D
"""Returns a representation of all points within a given distance of the polygon.
:param join_style: The styles of joins between offset segments: 1 (round), 2 (mitre), and 3 (bevel).
"""
s_poly = SPoly(self.vertices)
core = orient(s_poly.buffer(distance=distance, join_style=join_style), sign=1.0)
return Polygon2D(core.boundary.coords)
示例12: _coords
# 需要导入模块: from shapely.geometry import polygon [as 别名]
# 或者: from shapely.geometry.polygon import Polygon [as 别名]
def _coords(shape):
"""
Return a list of lists of coordinates of the polygon. The list consists
firstly of the list of exterior coordinates followed by zero or more lists
of any interior coordinates.
"""
assert shape.geom_type == 'Polygon'
coords = [list(shape.exterior.coords)]
for interior in shape.interiors:
coords.append(list(interior.coords))
return coords
示例13: _contour_to_poly
# 需要导入模块: from shapely.geometry import polygon [as 别名]
# 或者: from shapely.geometry.polygon import Polygon [as 别名]
def _contour_to_poly(contour):
poly = Polygon(contour)
if not poly.is_valid:
poly = poly.buffer(0)
assert poly.is_valid, \
"Contour %r did not make valid polygon %s because %s" \
% (contour, poly.wkt, explain_validity(poly))
return poly
示例14: enforce_winding_order
# 需要导入模块: from shapely.geometry import polygon [as 别名]
# 或者: from shapely.geometry.polygon import Polygon [as 别名]
def enforce_winding_order(self, shape, y_coord_down, n_try=1):
if shape.type == 'MultiPolygon':
# If we are a multipolygon, we need to ensure that the
# winding orders of the consituent polygons are
# correct. In particular, the winding order of the
# interior rings need to be the opposite of the
# exterior ones, and all interior rings need to follow
# the exterior one. This is how the end of one polygon
# and the beginning of another are signaled.
shape = self.enforce_multipolygon_winding_order(
shape, y_coord_down, n_try)
elif shape.type == 'Polygon':
# Ensure that polygons are also oriented with the
# appropriate winding order. Their exterior rings must
# have a clockwise order, which is translated into a
# clockwise order in MVT's tile-local coordinates with
# the Y axis in "screen" (i.e: +ve down) configuration.
# Note that while the Y axis flips, we also invert the
# Y coordinate to get the tile-local value, which means
# the clockwise orientation is unchanged.
shape = self.enforce_polygon_winding_order(
shape, y_coord_down, n_try)
# other shapes just get passed through
return shape
示例15: _get_feature_type
# 需要导入模块: from shapely.geometry import polygon [as 别名]
# 或者: from shapely.geometry.polygon import Polygon [as 别名]
def _get_feature_type(self, shape):
if shape.type == 'Point' or shape.type == 'MultiPoint':
return self.tile.Point
elif shape.type == 'LineString' or shape.type == 'MultiLineString':
return self.tile.LineString
elif shape.type == 'Polygon' or shape.type == 'MultiPolygon':
return self.tile.Polygon
elif shape.type == 'GeometryCollection':
raise ValueError('Encoding geometry collections not supported')
else:
raise ValueError('Cannot encode unknown geometry type: %s' %
shape.type)