def getLength(self):
"""Calculate the length of the wire.
Returns:
The float value of the length.
"""
linestring = geo.LineString(self.pointlist)
return linestring.length
python类LineString()的实例源码
def nearest_point(self, point):
'''Find nearest point in geometry, measured from given point.'''
if isinstance(self.location, Polygon):
segs = self.pairs(self.location.exterior.coords)
elif isinstance(self.location, LineString):
segs = self.pairs(self.location.coords)
else:
raise NotImplementedError('project_point_to_object not implemented'
"for geometry type '{}'.".format(
self.location.type))
nearest_point = None
min_dist = float("inf")
for seg_start, seg_end in segs:
line_start = Point(seg_start)
line_end = Point(seg_end)
intersection_point = self.project_point_to_line(
point, line_start, line_end)
cur_dist = point.distance(intersection_point)
if cur_dist < min_dist:
min_dist = cur_dist
nearest_point = intersection_point
return nearest_point
def tick(self):
logging.debug('TICK')
features = []
for f in self.features:
p1 = (f.attribute('From X'),
f.attribute('From Y'),
f.attribute('From Z'))
p2 = (f.attribute('To X'),
f.attribute('To Y'),
f.attribute('To Z'))
geom = LineString([p1, p2])
new_feature = QgsFeature()
new_feature.setGeometry(qgeom_from_wkt(geom.wkt.replace(' Z', 'Z')))
attrs = f.attributes()
attrs += [self.my_id]
new_feature.setAttributes(attrs)
self.my_id = self.my_id + 1
features += [new_feature]
self.dialog.setValue(self.my_id)
if len(features) == 1000:
break
insert_features_in_layer(features, self.new_layer)
if self.dialog.wasCanceled():
pass
elif self.features.isClosed():
pass
else:
self.timer = QTimer.singleShot(0, self.tick)
def test_arrays(self):
x, y = geometry.LineString(((0, 0), (1, 1))).xy
self.failUnless(len(x) == 2)
self.failUnless(list(x) == [0.0, 1.0])
self.failUnless(len(y) == 2)
self.failUnless(list(y) == [0.0, 1.0])
test_emptiness.py 文件源码
项目:Vector-Tiles-Reader-QGIS-Plugin
作者: geometalab
项目源码
文件源码
阅读 17
收藏 0
点赞 0
评论 0
def test_empty_linestring(self):
self.assertTrue(sgeom.LineString().is_empty)
test_products_z.py 文件源码
项目:Vector-Tiles-Reader-QGIS-Plugin
作者: geometalab
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
def test_line_intersection(self):
line1 = LineString([(0,0,0), (1,1,1)])
line2 = LineString([(0,1,1), (1,0,0)])
interxn = line1.intersection(line2)
self.failUnless(interxn.has_z)
self.failUnless(interxn._ndim == 3)
self.failUnless(0.0 <= interxn.z <= 1.0)
test_linear_referencing.py 文件源码
项目:Vector-Tiles-Reader-QGIS-Plugin
作者: geometalab
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def setUp(self):
self.point = Point(1, 1)
self.line1 = LineString(([0, 0], [2, 0]))
self.line2 = LineString(([3, 0], [3, 6]))
self.multiline = MultiLineString([
list(self.line1.coords), list(self.line2.coords)
])
test_linear_referencing.py 文件源码
项目:Vector-Tiles-Reader-QGIS-Plugin
作者: geometalab
项目源码
文件源码
阅读 19
收藏 0
点赞 0
评论 0
def test_suite():
try:
LineString(([0, 0], [2, 0])).project(Point(0, 0))
except AttributeError:
return lambda x: None
return unittest.TestLoader().loadTestsFromTestCase(
LinearReferencingTestCase
)
test_speedups.py 文件源码
项目:Vector-Tiles-Reader-QGIS-Plugin
作者: geometalab
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
def test_create_polygon_from_linestring(self):
ls = LineString([(0, 0), (2, 0), (2, 2), (0, 2)])
p = Polygon(ls)
self.assertEqual(p.length, 8)
test_getitem.py 文件源码
项目:Vector-Tiles-Reader-QGIS-Plugin
作者: geometalab
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
def test_index_2d_coords(self):
c = [(float(x), float(-x)) for x in range(4)]
g = geometry.LineString(c)
for i in range(-4,4):
self.assertTrue(g.coords[i] == c[i])
self.assertRaises(IndexError, lambda: g.coords[4])
self.assertRaises(IndexError, lambda: g.coords[-5])
test_getitem.py 文件源码
项目:Vector-Tiles-Reader-QGIS-Plugin
作者: geometalab
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def test_index_3d_coords(self):
c = [(float(x), float(-x), float(x*2)) for x in range(4)]
g = geometry.LineString(c)
for i in range(-4,4):
self.assertTrue(g.coords[i] == c[i])
self.assertRaises(IndexError, lambda: g.coords[4])
self.assertRaises(IndexError, lambda: g.coords[-5])
test_getitem.py 文件源码
项目:Vector-Tiles-Reader-QGIS-Plugin
作者: geometalab
项目源码
文件源码
阅读 19
收藏 0
点赞 0
评论 0
def test_index_coords_misc(self):
g = geometry.LineString() # empty
self.assertRaises(IndexError, lambda: g.coords[0])
self.assertRaises(TypeError, lambda: g.coords[0.0])
test_getitem.py 文件源码
项目:Vector-Tiles-Reader-QGIS-Plugin
作者: geometalab
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
def test_slice_2d_coords(self):
c = [(float(x), float(-x)) for x in range(4)]
g = geometry.LineString(c)
self.assertTrue(g.coords[1:] == c[1:])
self.assertTrue(g.coords[:-1] == c[:-1])
self.assertTrue(g.coords[::-1] == c[::-1])
self.assertTrue(g.coords[::2] == c[::2])
self.assertTrue(g.coords[:4] == c[:4])
self.assertTrue(g.coords[4:] == c[4:] == [])
test_transform.py 文件源码
项目:Vector-Tiles-Reader-QGIS-Plugin
作者: geometalab
项目源码
文件源码
阅读 33
收藏 0
点赞 0
评论 0
def test_line(self):
g = geometry.LineString(((0, 1), (2, 3)))
h = transform(self.func, g)
self.failUnlessEqual(h.geom_type, 'LineString')
self.failUnlessEqual(list(h.coords), [(0, 1), (2, 3)])
test_transform.py 文件源码
项目:Vector-Tiles-Reader-QGIS-Plugin
作者: geometalab
项目源码
文件源码
阅读 31
收藏 0
点赞 0
评论 0
def test_line(self):
g = geometry.LineString(((0, 1), (2, 3)))
h = transform(lambda x, y, z=None: (x+1.0, y+1.0), g)
self.failUnlessEqual(h.geom_type, 'LineString')
self.failUnlessEqual(list(h.coords), [(1.0, 2.0), (3.0, 4.0)])
test_pickle.py 文件源码
项目:Vector-Tiles-Reader-QGIS-Plugin
作者: geometalab
项目源码
文件源码
阅读 33
收藏 0
点赞 0
评论 0
def test_linestring(self):
l = geometry.LineString(((0.0, 0.0), (0.0, 1.0), (1.0, 1.0)))
self.failUnlessEqual(l._ndim, 2)
s = dumps(l, HIGHEST_PROTOCOL)
t = loads(s)
self.failUnlessEqual(t._ndim, 2)
test_collection.py 文件源码
项目:Vector-Tiles-Reader-QGIS-Plugin
作者: geometalab
项目源码
文件源码
阅读 28
收藏 0
点赞 0
评论 0
def test_child_with_deleted_parent(self):
# test that we can remove a collection while having
# childs around
a = LineString([(0, 0), (1, 1), (1,2), (2,2)])
b = LineString([(0, 0), (1, 1), (2,1), (2,2)])
collection = a.intersection(b)
child = collection.geoms[0]
# delete parent of child
del collection
# access geometry, this should not seg fault as 1.2.15 did
child.to_wkt()
def intersection(s0, s1):
i = LineString(s0).intersection(LineString(s1))
if i:
return (i.x, i.y)
return None
def is_same_geometry(data, data_other):
"""
Check if LineString geometries in two edge data dicts are the same, in
normal or reversed order of points.
Parameters
----------
data : dict
the first edge's data
data_other : dict
the second edge's data
Returns
-------
bool
"""
# extract geometries from each edge data dict
geom1 = [list(coords) for coords in data['geometry'].xy]
geom2 = [list(coords) for coords in data_other['geometry'].xy]
# reverse the first edge's list of x's and y's to look for a match in
# either order
geom1_r = [list(reversed(list(coords))) for coords in data['geometry'].xy]
# if the edge's geometry matches its reverse's geometry in either order,
# return True
return (geom1 == geom2 or geom1_r == geom2)
def dilation(mapfile):
line = LineString([(0, 0), (1, 1), (0, 2), (2, 2), (3, 1), (1, 0)])
ll = mappyfile.find(mapfile["layers"], "name", "line")
ll["features"][0]["wkt"] = "'%s'" % line.wkt
dilated = line.buffer(0.5, cap_style=3)
pl = mappyfile.find(mapfile["layers"], "name", "polygon")
pl["features"][0]["wkt"] = "'%s'" % dilated.wkt
mapfile["extent"] = " ".join(map(str, dilated.buffer(0.8).bounds))
return dilated