本文整理汇总了Python中shapely.geometry.Point.distance方法的典型用法代码示例。如果您正苦于以下问题:Python Point.distance方法的具体用法?Python Point.distance怎么用?Python Point.distance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类shapely.geometry.Point
的用法示例。
在下文中一共展示了Point.distance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getNosePoints
# 需要导入模块: from shapely.geometry import Point [as 别名]
# 或者: from shapely.geometry.Point import distance [as 别名]
def getNosePoints(line,prevPoint):
start=Point(line.coords[0])
end=Point(line.coords[-1])
if start.distance(prevPoint)<end.distance(prevPoint):
return start
else:
return end
示例2: GetIntersectionDistance
# 需要导入模块: from shapely.geometry import Point [as 别名]
# 或者: from shapely.geometry.Point import distance [as 别名]
def GetIntersectionDistance(self, l1, l2):
# l1 should be just two coordinate positions
# get its starting coorinate
pt1 = Point(l1.coords[0])
x = l1.intersection(l2)
# intersections can return a lot of things
d = -1
if x.wkt == 'GEOMETRYCOLLECTION EMPTY':
d = -1
# print "nothing"
elif re.match('^POINT', x.wkt):
# print "point"
pt2 = Point(x.coords[0])
d = pt1.distance(pt2)
elif re.match('^MULTI', x.wkt):
# print "mpoint"
# this will return the minimum distance
pt2 = Point(x[0].coords[0])
d = pt1.distance(pt2)
for pt2 in x:
pt2 = Point(pt2)
if d < pt1.distance(pt2):
d = pt1.distance(pt2)
else:
print 'dunno what intersection passed me'
return d
示例3: pymol_select_memb_old
# 需要导入模块: from shapely.geometry import Point [as 别名]
# 或者: from shapely.geometry.Point import distance [as 别名]
def pymol_select_memb_old(pdb: MyPDB) -> set():
"""
print a pymol selection line for all residues that are in the membrane
!!! this assumes that cntr is at 0 0 0 and norm at 15 0 0 !!!
"""
from shapely.geometry import LineString, Point
# create Points from center & thickness
cntr_pnt = Point(pdb.memb_res.cntr.x, pdb.memb_res.cntr.y, pdb.memb_res.cntr.z)
thkn_m_pnt = Point(-pdb.memb_res.thkn.x, pdb.memb_res.thkn.y, pdb.memb_res.thkn.z)
thkn_pnt = Point(pdb.memb_res.thkn.x, pdb.memb_res.thkn.y, pdb.memb_res.thkn.z)
# define the line between center and thickness
line = LineString([thkn_m_pnt, thkn_pnt])
thickness = cntr_pnt.distance(thkn_pnt)
result = set()
# iterate over all CAs in the pdb
for cid in sorted(pdb.chains.keys()):
for rid in sorted(pdb[cid].residues.keys()):
atom = pdb[cid][rid]['CA']
# the atom as a Point
p = Point(atom.xyz.x, atom.xyz.y, atom.xyz.z)
# projection of the CA atom on the center-thickness line
np = line.interpolate(line.project(p))
# if the distance on the center-thickness line is smaller than 15, than this is in the membrane
if cntr_pnt.distance(np) < thickness-0.1:
result.add(atom.res_seq_num)
return result
示例4: project
# 需要导入模块: from shapely.geometry import Point [as 别名]
# 或者: from shapely.geometry.Point import distance [as 别名]
def project(p1, p2, p3):
"""Project a Point, p3 onto a line between Points p1 and p2.
Uses Shapely and GEOS functions, which set distance to zero for all negative distances.
Parameters:
p1 (Point) : point at zero distance on line between p1 and p2.
p2 (Point) : endpoint of line.
p3 (Point) : the point to project.
Returns:
result (dict) : the projected Point, disctance along line, offset from line, and fractional distance along line.
"""
line = LineString([(p1.x, p1.y),(p2.x, p2.y)])
u = line.project(p3, normalized=True)
d = line.project(p3, normalized=False)
pt_xy = line.interpolate(d)
pt = Point([pt_xy.x, pt_xy.y, p3.z])
# calculate the offset distance of p3 from the line
if (p1.y - p2.y) * (p3.x - p2.x) - (p1.x - p2.x) * (p3.y - p2.y) < 0:
offset = -pt.distance(p3) # the point is offset left of the line
else:
offset = pt.distance(p3) # the point is offset right of the line
result = {'pt':pt, 'd':d, 'o':offset, 'u':u}
return result
示例5: fuse_point_to_polygon
# 需要导入模块: from shapely.geometry import Point [as 别名]
# 或者: from shapely.geometry.Point import distance [as 别名]
def fuse_point_to_polygon(point, polygon):
"""
:param point:
:param polygon:
"""
p = Point(point)
min_distance = float("inf")
min_index = -1
polygon = polygon[::-1] ##FIXME the received polygon is wrong. This is POG
# Identify the nearest line segment.
for i in range(len(polygon)):
seg = polygon[i:i + 2]
if len(seg) < 2:
# close the polygon
seg = [polygon[-1]] + [polygon[0]]
line_segment = LineString(seg)
dist = p.distance(line_segment)
# print seg, dist
if dist < min_distance:
min_distance = dist
min_index = i
# print min_distance, min_index
fused_polygon = polygon[min_index + 1:] + polygon[:min_index + 1] + [point]
# fused_polygon = polygon[min_index + 1:] + [point]
# aa = identify_first_point_in_polygon(fused_polygon)
# print "FUSING", aa
# print point, polygon, fused_polygon
return fused_polygon
示例6: compute_repulsive_ws
# 需要导入模块: from shapely.geometry import Point [as 别名]
# 或者: from shapely.geometry.Point import distance [as 别名]
def compute_repulsive_ws(self, control_point, obstacle):
point, _, eta = control_point
# need to unpack obstacle tuple into polygon and threshold
obstacle_poly = obstacle[0]
obstacle_thresh = obstacle[1]
d2obstacle = point.distance(obstacle_poly)
# this is straight from book
if d2obstacle > obstacle_thresh:
return Point(0, 0)
else:
# scalar is length of vector that points away from closest obstacle
# point
scalar = eta * ((obstacle_thresh ** -1) - (d2obstacle ** -1)) * (d2obstacle ** -2)
# construct gradient vector
# find closest point, you can ignore the details Yinan
pol_ext = obstacle_poly
if obstacle_poly.geom_type != "LineString":
pol_ext = LinearRing(obstacle_poly.exterior.coords)
d = pol_ext.project(point)
p = pol_ext.interpolate(d)
# closest point
c = Point(list(p.coords)[0])
dqc = c.distance(point)
# from book, formula for delta vector
delta_d_i = Point(((point.x - c.x) / dqc, (point.y - c.y) / dqc))
return Point((-1 * delta_d_i.x * scalar, -1 * delta_d_i.y * scalar))
示例7: find_edge_nodes
# 需要导入模块: from shapely.geometry import Point [as 别名]
# 或者: from shapely.geometry.Point import distance [as 别名]
def find_edge_nodes(fargs):
""" Find nodes are near the edge of the hull"""
cluster, cluster_hull, nodes = fargs
# There is no hull for this community, it's been deleted.
if cluster_hull is None:
log.error("Missing hull, keeping all nodes in cluster %i",
cluster)
return len(nodes), nodes
characteristic_size = math.sqrt(cluster_hull.area)
allowed_distance = characteristic_size * args.within
boundary = cluster_hull.boundary
output = []
for node in nodes:
# check if it is an interior node
point = Point((node.lon, node.lat))
keep = False
if random.random() < args.keep:
keep = True
elif point.distance(boundary) < allowed_distance:
keep = True
if keep:
output.append(node)
return len(nodes), output
示例8: project_tracks_to_road
# 需要导入模块: from shapely.geometry import Point [as 别名]
# 或者: from shapely.geometry.Point import distance [as 别名]
def project_tracks_to_road(tracks,
road_segments):
"""
Compute tracks that fall into each road segment.
Args:
- tracks
- road_segments
Return:
- track_on_road: a dictionary, each key is the index of a road segment. Each
value is a set of indices of the tracks fall onto this road.
"""
track_on_road = {}
for seg_idx in np.arange(len(road_segments)):
track_on_road[seg_idx] = set([])
simplified_tracks = []
for track in tracks:
line = LineString([(pt[0], pt[1]) for pt in track.utm])
simplified_track = line.simplify(10.0)
simplified_tracks.append(simplified_track)
# Compute road segment linestrings
road_segment_linestrings = []
for r_seg in road_segments:
r_start = r_seg.center - r_seg.half_length*r_seg.direction
r_end = r_seg.center + r_seg.half_length*r_seg.direction
r_linestring = LineString([r_start, r_end])
road_segment_linestrings.append(r_linestring)
for seg_idx in np.arange(len(road_segments)):
print seg_idx
for track_idx in np.arange(len(tracks)):
if road_segment_linestrings[seg_idx].distance(simplified_tracks[track_idx]) > 1.2*road_segments[seg_idx].half_width:
continue
track = tracks[track_idx]
if len(track.utm) <= 1:
continue
for utm_idx in np.arange(len(track.utm)):
utm = track.utm[utm_idx]
if utm_idx == 0:
direction = np.array([track.utm[utm_idx+1][0], track.utm[utm_idx+1][1]]) - \
np.array([track.utm[utm_idx][0], track.utm[utm_idx][1]])
elif utm_idx == len(track.utm) - 1:
direction = np.array([track.utm[utm_idx][0], track.utm[utm_idx][1]]) - \
np.array([track.utm[utm_idx-1][0], track.utm[utm_idx-1][1]])
else:
direction = np.array([track.utm[utm_idx+1][0], track.utm[utm_idx+1][1]]) - \
np.array([track.utm[utm_idx-1][0], track.utm[utm_idx-1][1]])
direction /= np.linalg.norm(direction)
if np.dot(direction, road_segments[seg_idx].direction) < np.cos(np.pi/4.0):
continue
pt = Point(utm[0], utm[1])
if pt.distance(road_segment_linestrings[seg_idx]) < 1.2*road_segments[seg_idx].half_width:
track_on_road[seg_idx].add(track_idx)
break
return track_on_road
示例9: test_operations
# 需要导入模块: from shapely.geometry import Point [as 别名]
# 或者: from shapely.geometry.Point import distance [as 别名]
def test_operations(self):
point = Point(0.0, 0.0)
# General geometry
self.assertEqual(point.area, 0.0)
self.assertEqual(point.length, 0.0)
self.assertAlmostEqual(point.distance(Point(-1.0, -1.0)),
1.4142135623730951)
# Topology operations
# Envelope
self.assertIsInstance(point.envelope, Point)
# Intersection
self.assertIsInstance(point.intersection(Point(-1, -1)),
GeometryCollection)
# Buffer
self.assertIsInstance(point.buffer(10.0), Polygon)
self.assertIsInstance(point.buffer(10.0, 32), Polygon)
# Simplify
p = loads('POLYGON ((120 120, 121 121, 122 122, 220 120, 180 199, '
'160 200, 140 199, 120 120))')
expected = loads('POLYGON ((120 120, 140 199, 160 200, 180 199, '
'220 120, 120 120))')
s = p.simplify(10.0, preserve_topology=False)
self.assertTrue(s.equals_exact(expected, 0.001))
p = loads('POLYGON ((80 200, 240 200, 240 60, 80 60, 80 200),'
'(120 120, 220 120, 180 199, 160 200, 140 199, 120 120))')
expected = loads(
'POLYGON ((80 200, 240 200, 240 60, 80 60, 80 200),'
'(120 120, 220 120, 180 199, 160 200, 140 199, 120 120))')
s = p.simplify(10.0, preserve_topology=True)
self.assertTrue(s.equals_exact(expected, 0.001))
# Convex Hull
self.assertIsInstance(point.convex_hull, Point)
# Differences
self.assertIsInstance(point.difference(Point(-1, 1)), Point)
self.assertIsInstance(point.symmetric_difference(Point(-1, 1)),
MultiPoint)
# Boundary
self.assertIsInstance(point.boundary, GeometryCollection)
# Union
self.assertIsInstance(point.union(Point(-1, 1)), MultiPoint)
self.assertIsInstance(point.representative_point(), Point)
self.assertIsInstance(point.centroid, Point)
# Relate
self.assertEqual(point.relate(Point(-1, -1)), 'FF0FFF0F2')
示例10: getclosestcity
# 需要导入模块: from shapely.geometry import Point [as 别名]
# 或者: from shapely.geometry.Point import distance [as 别名]
def getclosestcity(lon,lat):
qp = Point(lon,lat)
cities = meta.Session.query(City).filter(cities_table.c.lon!=None).filter(cities_table.c.lat!=None).all()
if not len(cities): return None
distances = sorted([(qp.distance(Point(ct.lon,ct.lat)),ct.name) for ct in cities])
#distances_num = sorted([(qp.distance(Point(ct.lon,ct.lat)),ct.id) for ct in cities]) ; raise Exception('distance from (%s,%s) - %s'%(lon,lat,distances_num))
rt = distances[0][1]
return rt
示例11: cartesiandistance
# 需要导入模块: from shapely.geometry import Point [as 别名]
# 或者: from shapely.geometry.Point import distance [as 别名]
def cartesiandistance(origin, destination):
lon1, lat1 = origin
lon2, lat2 = destination
proj = Proj(init="epsg:3785") # spherical mercator, should work anywhere
point1 = proj(lon1, lat1)
point2 = proj(lon2, lat2)
point1_cart = Point(point1)
point2_cart = Point(point2)
return point1_cart.distance(point2_cart) #meters
示例12: test_point_point
# 需要导入模块: from shapely.geometry import Point [as 别名]
# 或者: from shapely.geometry.Point import distance [as 别名]
def test_point_point():
p1 = Point(0,0)
p2 = Point(1,1)
ok_(p1.geom_type=='Point', p1.geom_type)
assert_ae(p1.coords[:][0], (0,0))
d = p1.distance(p2)
assert_eq(d, sqrt(2))
示例13: distance_point_to_polygon
# 需要导入模块: from shapely.geometry import Point [as 别名]
# 或者: from shapely.geometry.Point import distance [as 别名]
def distance_point_to_polygon(point, poly):
"""
Compute the minimum distance from a point to a polygon
:param point:
:param poly:
:return: the distance
"""
p = Point(point)
pol = Polygon(poly)
if not pol.is_valid:
pol = MultiPoint(poly).convex_hull
return p.distance(pol)
示例14: project2
# 需要导入模块: from shapely.geometry import Point [as 别名]
# 或者: from shapely.geometry.Point import distance [as 别名]
def project2(p1, p2, p3):
"""Project a Point, p3 onto a line intersecting Points p1 and p2.
Adapted from tutorial by Paul Bourke: http://paulbourke.net/geometry/pointline/
This projection function allows for points at negative distances.
Parameters:
p1 (Point) : point at zero distance on line between p1 and p2.
p2 (Point) : endpoint on line.
p3 (Point) : the point to project.
Returns:
result (dict) : the projected Point, distance along line, offset from line, and fractional distance along line.
"""
x_delta = p2.x - p1.x
y_delta = p2.y - p1.y
if x_delta == 0 and y_delta == 0:
logger.warning("p1 and p2 cannot be the same point")
return
u = ((p3.x - p1.x) * x_delta + (p3.y - p1.y) * y_delta) / (x_delta * x_delta + y_delta * y_delta)
pt = Point(p1.x + u * x_delta, p1.y + u * y_delta, p3.z)
# calculate distance along the line from p1
if u < 0:
d = -pt.distance(p1)
else:
d = pt.distance(p1)
# calculate the offset distance of p3 from the line
if (p1.y - p2.y) * (p3.x - p2.x) - (p1.x - p2.x) * (p3.y - p2.y) < 0:
offset = -pt.distance(p3) # the point is offset left of the line
else:
offset = pt.distance(p3) # the point is offset right of the line
result = {'pt':pt, 'd':d, 'o':offset, 'u':u}
return result
示例15: score
# 需要导入模块: from shapely.geometry import Point [as 别名]
# 或者: from shapely.geometry.Point import distance [as 别名]
def score(ob):
g = geom(ob)
r = region
if r is None:
if len(coords) == 2:
r = Point(coords)
else:
r = asShape(box2poly(coords))
# local scaling might be better, degree units are implicit
# in this value
k = 0.2
d = r.distance(g)
return int(math.exp(-d/k) * 1000)