本文整理汇总了Python中Polygon类的典型用法代码示例。如果您正苦于以下问题:Python Polygon类的具体用法?Python Polygon怎么用?Python Polygon使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Polygon类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: decomposeToConvex
def decomposeToConvex(self):
print 'decomposing'
polygon = Polygon(self.points)
self.convexPolygons.extend(polygon.splitToConvex())
示例2: getPolyCSpace
def getPolyCSpace(self, polygon):
segments = []
obs = self.polygon.copy()
rob = polygon.copy()
for i in obs.segments:
segments.append((i.copy(), i.normalAngle("left"),"o"))
for j in rob.segments:
segments.append((j.copy(), j.normalAngle("right"),"r"))
sorted_segments = sorted(segments, key= itemgetter(1))
if sorted_segments[0][2] == "o":
sorted_segments[0][0].reverse()
for i in xrange(1, len(sorted_segments)):
if sorted_segments[i][2] == "o":
print sorted_segments[i][0]
sorted_segments[i][0].reverse()
sorted_segments[i][0].move(sorted_segments[i-1][0].p2)
vertices =[]
for s in sorted_segments:
vertices.append(s[0].p1.toTuple())
tempPoly = Polygon(self.polygon.window, vertices)
delta = self.findDelta(tempPoly)
tempPoly.moveDelta(delta[0],delta[1])
return tempPoly
示例3: remove_short_lines
def remove_short_lines(self, minlen = 1):
minlen*=minlen
newp = Polygon()
c=-1
for cont in self.poly:
c+=1
#cont=self.poly[0]
keeplist = []
lastp = None
pindex = -1
for p in cont:
pindex+=1
if lastp:
#check here
xd = p[0]-lastp[0]
yd = p[0]-lastp[0]
td = xd*xd+yd*yd
if td < minlen:
pass
#lastp = None
else:
keeplist.append(p)
lastp = p
else:
keeplist.append(p)
lastp = p
ishole = self.poly.isHole(c)
newp.addContour(keeplist,ishole)
self.poly = newp
示例4: Intersect
def Intersect(rect1, rect2): #{{{
Width = rect1[2].x * 2
hw = Width/2 #half width
Height = rect1[2].y * 2
hh = Height/2 #half height
#A = math.radians(rect1[1])
A = rect1[1]
sina = math.sin(A)
cosa = math.cos(A)
ul1 = Vertex2(rect1[0].x - hw*cosa - hh*sina, rect1[0].y + hh*cosa - hw*sina)
ur1 = Vertex2(rect1[0].x + hw*cosa - hh*sina, rect1[0].y + hh*cosa + hw*sina)
dl1 = Vertex2(rect1[0].x - hw*cosa + hh*sina, rect1[0].y - hh*cosa - hw*sina)
dr1 = Vertex2(rect1[0].x + hw*cosa + hh*sina, rect1[0].y - hh*cosa + hw*sina)
Width = rect2[2].x * 2
hw = Width/2 #half width
Height = rect2[2].y * 2
hh = Height/2 #half height
#A = math.radians(rect2[1])
A = rect2[1]
sina = math.sin(A)
cosa = math.cos(A)
ul2 = Vertex2(rect2[0].x - hw * cosa - hh * sina, rect2[0].y + hh * cosa - hw*sina)
ur2 = Vertex2(rect2[0].x + hw * cosa - hh * sina, rect2[0].y + hh * cosa + hw*sina)
dl2 = Vertex2(rect2[0].x - hw * cosa + hh * sina, rect2[0].y - hh * cosa - hw*sina)
dr2 = Vertex2(rect2[0].x + hw * cosa + hh * sina, rect2[0].y - hh * cosa + hw*sina)
points1 = [ul1, ur1, dr1, dl1]
points2 = [ul2, ur2, dr2, dl2]
p1 = Polygon(points1)
p2 = Polygon(points2)
return p1.intersects(p2)
示例5: sizeTestExample
def sizeTestExample():
import pickle
p = Polygon('testpoly.gpf')
p.write('sizetest.gpf')
tlen = len(open('sizetest.gpf', 'r').read())
blen = len(dumpBinary(p))
xlen = len(dumpXML(p))
plen = len(pickle.dumps(p))
print "Text(gpc): %d, Binary: %d, XML: %d, Pickle: %d" % (tlen, blen, xlen, plen)
示例6: getOverLapArea
def getOverLapArea(A,B,startA,endA,startB,endB,TB):
Bt=getTransformedFragment(B,TB)
Polygon.setDataStyle(Polygon.STYLE_NUMPY)
b_poly = Polygon.Polygon(Bt.points[0])
a_poly = Polygon.Polygon(A.points[0])
intersection_polygon=b_poly&a_poly
area = intersection_polygon.area()
return area
示例7: testInit
def testInit(self):
Polygon.setDataStyle(Polygon.STYLE_TUPLE)
# tuple
p = Polygon.Polygon(self.cont)
self.assertEqual(p[0], self.cont)
# list
p = Polygon.Polygon(list(self.cont))
self.assertEqual(p[0], self.cont)
if Polygon.withNumeric:
# array
import Numeric
p = Polygon.Polygon(Numeric.array(self.cont))
self.assertEqual(p[0], self.cont)
示例8: testInit
def testInit(self):
Polygon.setDataStyle(Polygon.STYLE_TUPLE)
# tuple
p = Polygon.Polygon(self.cont)
self.assertEqual(p[0], tuple(self.cont))
# list
p = Polygon.Polygon(self.cont)
self.assertEqual(p[0], tuple(self.cont))
if Polygon.withNumPy:
import numpy
a = numpy.array(self.cont)
p = Polygon.Polygon(a)
self.assertEqual(self.cont, list(p[0]))
示例9: readShpPolygon
def readShpPolygon(layer,fileName):# parameter fileName is the pathfile name without extension
indexName = fileName + '.shx'
shpFile = open(indexName,"rb")
fileLength = os.path.getsize(indexName)
polygonNum = (fileLength-100)/8
recordsOffset = []
print fileName
starttime = time.clock()
shpFile.seek(0)
s = shpFile.read(fileLength)
shpFile.close()
layer.minx, layer.miny, layer.maxx, layer.maxy = struct.unpack("<dddd",s[36:68])
pointer = 100
for i in range(0,polygonNum):
offset = struct.unpack('>i',s[pointer:pointer+4])
recordsOffset.append(offset[0]*2)
pointer += 8
shpFile.close()
shpFile = open(fileName+'.shp',"rb")
shpFile.seek(24)
s = shpFile.read(4)
header = struct.unpack(">i",s)
fileLength = header[0]*2
shpFile.seek(0)
s = shpFile.read(fileLength)
shpFile.close()
for offset in recordsOffset:
x, y = [], []
polygon = Polygon()
pointer = offset + 8 + 4
polygon.minx,polygon.miny,polygon.maxx,polygon.maxy = struct.unpack('dddd',s[pointer:pointer+32])
pointer = offset + 8 + 36
polygon.numParts, polygon.numPoints = struct.unpack('ii',s[pointer:pointer+8])
pointer += 8
str = ''
for i in range(polygon.numParts):
str = str+'i'
polygon.partsIndex = struct.unpack(str,s[pointer:pointer+polygon.numParts*4])
pointer += polygon.numParts*4
for i in range(polygon.numPoints):
pointx, pointy = struct.unpack('dd',s[pointer:pointer+16])
x.append(pointx)
y.append(pointy)
pointer+=16
polygon.x, polygon.y = x, y
layer.features.append(polygon)
ts = time.clock()-starttime
print ' %f seconds' % (ts)
示例10: remove_some_pts
def remove_some_pts(self, prop=.9):
newp = Polygon()
c=-1
for cont in self.poly:
c+=1
cr = reducePoints(cont,int(len(cont)*prop))
ishole = self.poly.isHole(c)
if len(cr)>2:
newp.addContour(cr, ishole)
parea = self.poly.area()
nparea = newp.area()
#areadiff = math.fabs(nparea-parea)
arearatio = parea/nparea
arearatio = math.fabs(arearatio-1)
#print areadiff, arearatio
if arearatio < 0.1:
self.poly = newp
示例11: Polygon
class Domen:
id = 0
polygon = Polygon()
def __init__(self, points, id=0):
self.polygon = Polygon(points)
def contains(self, point):
return self.polygon.isInside(point.x, point.y) == 1
示例12: testCoverOverlap
def testCoverOverlap(self):
p1 = Polygon.polyStar(radius=1.0, nodes=6)
p2 = Polygon.Polygon(p1)
p2.scale(0.9, 0.9)
self.assertEqual(p1.covers(p2), 1)
self.assertEqual(p1.overlaps(p2), 1)
p2.shift(0.2, 0.2)
self.assertEqual(p1.covers(p2), 0)
self.assertEqual(p1.overlaps(p2), 1)
p2.shift(5.0, 0.0)
self.assertEqual(p1.covers(p2), 0)
self.assertEqual(p1.overlaps(p2), 0)
示例13: reduceExample
def reduceExample():
# read Polygon from file
p = Polygon('testpoly.gpf')
# use ireland only, I know it's contour 0
pnew = Polygon(p[0])
# number of points
l = len(pnew[0])
# get shift value to show many polygons in drawing
bb = pnew.boundingBox()
xs = 1.1 * (bb[1]-bb[0])
# list with polygons to plot
plist = [pnew]
while l > 30:
# reduce points to the half
l /= 2
print "Reducing contour to %d points" % l
pnew = Polygon(reducePoints(pnew[0], l))
pnew.shift(xs, 0)
plist.append(pnew)
# draw the results
print "Plotting ReduceExample.svg"
drawSVG(plist, height=400, ofile="ReduceTest.svg")
示例14: reduceExample
def reduceExample():
# read Polygon from file
p = Polygon('testpoly.gpf')
# use ireland only, I know it's contour 0
pnew = Polygon(p[0])
# number of points
l = len(pnew[0])
# get shift value to show many polygons in drawing
bb = pnew.boundingBox()
xs = 1.1 * (bb[1]-bb[0])
# list with polygons to plot
plist = [pnew]
while l > 30:
# reduce points to the half
l /= 2
print 'Reducing contour to %d points' % l
pnew = Polygon(reducePoints(pnew[0], l))
pnew.shift(xs, 0)
plist.append(pnew)
# draw the results
writeSVG('ReducePoints.svg', plist, height=400)
if hasPDFExport:
writePDF('ReducePoints.pdf', plist)
示例15: testDataStyle
def testDataStyle(self):
p = Polygon.Polygon(self.cont)
# tuple
Polygon.setDataStyle(Polygon.STYLE_TUPLE)
self.assertEqual(p[0], tuple(self.cont))
# list
Polygon.setDataStyle(Polygon.STYLE_LIST)
self.assertEqual(p[0], self.cont)
if Polygon.withNumPy:
import numpy
Polygon.setDataStyle(Polygon.STYLE_NUMPY)
self.assertEqual(type(p[0]), numpy.ndarray)