本文整理汇总了Python中shapely.geometry.LineString.crosses方法的典型用法代码示例。如果您正苦于以下问题:Python LineString.crosses方法的具体用法?Python LineString.crosses怎么用?Python LineString.crosses使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类shapely.geometry.LineString
的用法示例。
在下文中一共展示了LineString.crosses方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: find_valid_edges
# 需要导入模块: from shapely.geometry import LineString [as 别名]
# 或者: from shapely.geometry.LineString import crosses [as 别名]
def find_valid_edges(vertexes, edges, env, polygons):
valid_edges = []
for i, p1 in enumerate(vertexes):
print i
for p2 in [x for x in vertexes if not x == p1]:
add = True
line2 = LineString([p1, p2])
if env.crosses(line2):
continue
xx, yy = line2.xy
# Midpoint will lie within a shape if the line is composed of two vertices of the polygon
m = Point(sum(xx)/2., sum(yy)/2.)
if [x for x in polygons if m.within(x)]:
continue # skip this edge if it is within a polygon
for edge in edges:
line1 = LineString(edge)
if add and not line1 == line2 and line1.crosses(line2):
add = False
break
if add:
valid_edges.append([p1, p2])
return valid_edges
示例2: SpatialToplogy
# 需要导入模块: from shapely.geometry import LineString [as 别名]
# 或者: from shapely.geometry.LineString import crosses [as 别名]
def SpatialToplogy (Spatial_A,Spatial_B):
if Spatial_A[4] == 'Point' and Spatial_B[4]== 'Point':
Point_0=Point(Spatial_A[0],Spatial_A[1])
Point_1=Point(Spatial_B[0],Spatial_B[1])
#Point to point relationships
if Point_0.equals(Point_1): return 'Point1 equals Point2'
if Point_0.within(Point_1.buffer(2)): return 'Point1 lies within a buffer of 2 m from Point2'
if Point_0.overlaps(Point_1): return 'Point1 overlaps Point2'
#if Point_0.disjoint(Point_1): return 'Point1 disjoint Point2'
#Point to line relationships
if Spatial_A[4] == 'Point' and Spatial_B[4]== 'Line':
Point_0=Point(Spatial_A[0],Spatial_A[1])
Line_0=LineString([(Spatial_B[0],Spatial_B[1]),(Spatial_B[2],Spatial_B[3])])
if Point_0.touches(Line_0):return 'Point1 touches Line1'
if Point_0.within(Line_0.buffer(2)):return 'Point1 lies within a buffer of 2 m from L1'
#Point to polygon relationships
if Spatial_A[4] == 'Point' and Spatial_B[4]== 'Polygon':
Point_0=Point(Spatial_A[0],Spatial_A[1])
Polygon_0=Polygon([(Spatial_B[0],Spatial_B[1]),(Spatial_B[2],Spatial_B[1]),(Spatial_B[2],Spatial_B[3]),(Spatial_B[0],Spatial_B[3])])
if Point_0.touches(Polygon_0):return 'Point1 touches Polygon1'
if Point_0.within(Polygon_0):return'Point1 lies within Polygon1'
if Point_0.overlaps(Polygon_0):return 'Point1 lies overlaps Polygon1'
#Line to line relationships
if Spatial_A[4]=='Line' and Spatial_B[4]=='Line':
Line_0=LineString([(Spatial_A[0],Spatial_A[1]),(Spatial_A[2],Spatial_A[3])])
Line_1=LineString([(Spatial_B[0],Spatial_B[1]),(Spatial_B[2],Spatial_B[3])])
if Line_0.equals(Line_1):return 'Line0 equals Line1'
if Line_0.touches(Line_1):return 'Line0 touches Line1'
if Line_0.crosses(Line_1):return 'Line0 crosses Line1'
if Line_0.within(Line_1.buffer(2)):return 'Line0 lies within a buffer of 2 m Line1'
if Line_0.overlaps(Line_1):return 'Line0 overlaps Line1'
#Line to polygon relationships
if Spatial_A[4]=='Line' and Spatial_B[4]=='Polygon':
Line_0=LineString([(Spatial_A[0],Spatial_A[1]),(Spatial_A[2],Spatial_A[3])])
Polygon_0=Polygon([(Spatial_B[0],Spatial_B[1]),(Spatial_B[2],Spatial_B[1]),(Spatial_B[2],Spatial_B[3]),(Spatial_B[0],Spatial_B[3])])
if Line_0.touches(Polygon_0):return 'Line0 touches Polygon1'
if Line_0.crosses(Polygon_0):return 'Line0 crosses Polygon1'
if Line_0.within(Polygon_0):return 'Line0 lies within Polygon1'
#Polygon to Polygon relationships
if Spatial_A[4]=='Polygon' and Spatial_B[4]=='Polygon':
Polygon_0=Polygon([(Spatial_A[0],Spatial_A[1]),(Spatial_A[2],Spatial_A[1]),(Spatial_A[2],Spatial_A[3]),(Spatial_A[0],Spatial_A[3])])
Polygon_1=Polygon([(Spatial_B[0],Spatial_B[1]),(Spatial_B[2],Spatial_B[1]),(Spatial_B[2],Spatial_B[3]),(Spatial_B[0],Spatial_B[3])])
if Polygon_0.touches(Polygon_1):return 'Polygon touches Polygon1'
if Polygon_0.equals(Polygon_1):return 'Polygon0 equals Polygon1'
if Polygon_0.within(Polygon_1):return 'Polygon lies within Polygon1'
if Polygon_0.within(Polygon_1.buffer(2)):return 'Polygon lies within a buffer of 2m Polygon1'
示例3: _crosses_antimeridian
# 需要导入模块: from shapely.geometry import LineString [as 别名]
# 或者: from shapely.geometry.LineString import crosses [as 别名]
def _crosses_antimeridian(region: Polygon) -> bool:
"""
Determine if the given region crosses the Antimeridian line, by converting
the given Polygon from -180;180 to 0;360 and checking if the antimeridian
line crosses it.
This only works with Polygons without holes
:param region: Polygon to test
"""
# Convert region to only have positive longitudes.
# This way we can perform a simple antimeridian check
old_exterior = region.exterior.coords
new_exterior = []
for point in old_exterior:
lon, lat = point[0], point[1]
if -180. <= lon < 0.:
lon += 360.
new_exterior.append((lon, lat))
converted_region = Polygon(new_exterior)
# There's a problem at this point. Any polygon crossed by the zero-th
# meridian can in principle convert to an inverted polygon that is crossed
# by the antimeridian.
if not converted_region.is_valid:
# The polygon 'became' invalid upon conversion => probably the original
# polygon is what we want
# noinspection PyBroadException
try:
# First try cleaning up geometry that is invalid
converted_region = converted_region.buffer(0)
except BaseException:
pass
if not converted_region.is_valid:
return False
test_line = LineString([(180, -90), (180, 90)])
if test_line.crosses(converted_region):
# The converted polygon seems to be valid and crossed by the
# antimeridian. At this point there's no 'perfect' way how to tell if
# we wanted the converted polygon or the original one.
# A simple heuristic is to check for size. The smaller one is quite
# likely the desired one
if converted_region.area < region.area:
return True
else:
return False
else:
return False
示例4: check_course_geofence
# 需要导入模块: from shapely.geometry import LineString [as 别名]
# 或者: from shapely.geometry.LineString import crosses [as 别名]
def check_course_geofence (self, x_utm_start, y_utm_start, az = None, dist = None, x_utm_end = None, y_utm_end = None):
'''
method to check a prospective course to see how it stacks up against
the geofence. If the start is outside the geofence, this returns true
if the course intersects into the geofence. If the start is inside the
geofence, this returns true if we don't intersect the geofence, and false
if we do cross the geofence.
x_utm_start = the starting point of the line
y_utm_start = the starting point of the line
az = the compass azimuth of where the line is going
dist = the distance of the line
x_utm_end = optionally supplied x_utm location for course line
y_utm_end = optionally supplied x_utm location for course line
returns true (yes, this course is fine), or false (not so good)
'''
# calc the end points with az and dist if end points are unsupplied
if not az is None and not dist is None:
x_utm_end = x_utm_start + (dist * sin (az * pi / 180.0))
y_utm_end = y_utm_start + (dist * cos (az * pi / 180.0))
target_line = LineString ([(x_utm_start, y_utm_start), (x_utm_end, y_utm_end)])
# check to see if the course crosses the edge of the geofence
crossing_geofence = target_line.crosses (self.geofence)
# now, see if we are going into the geofence, or out of the geofence
start_in_geofence = self.check_geofence (x_utm_start, y_utm_start)
# check the combinations
if start_in_geofence:
if crossing_geofence:
good_course = False # looks bad, crossing geofence
else:
good_course = True # looks good, not crossing geofence
else:
if crossing_geofence:
good_course = True # looks good, we are going back into the geofence
else:
good_course = False # looks bad, not going back into the geofence
return (good_course)
示例5: LineString
# 需要导入模块: from shapely.geometry import LineString [as 别名]
# 或者: from shapely.geometry.LineString import crosses [as 别名]
geo_axes.set_extent([-120, -115, 30, 35], ccrs.PlateCarree())
geo_axes.plot(-117.1625, 32.715, "bo", markersize=7, transform=ccrs.Geodetic())
geo_axes.plot(-118.1625, 33.715, "bo", markersize=7, transform=ccrs.Geodetic())
geo_axes.text(-117, 33, "San Diego", transform=ccrs.Geodetic())
geo_axes.set_xmargin(0.05)
geo_axes.set_ymargin(0.10)
# ridge_vertices is the "voronoi lines".
for vert1, vert2 in vor.ridge_vertices:
# each one is a pair [vert1, vert2]
if vert1 < 0 or vert2 < 0:
continue
point1 = np.array(vor.vertices[vert1])
point2 = np.array(vor.vertices[vert2])
line1 = LineString([point1, point2])
if line1.crosses(bounds):
line_within = line1.intersection(bounds)
map.add_children(folium.PolyLine(locations=line_within.coords))
# |bounds| is a polygon so the intersection is the part that is
# within the polygon.
geometries.append(line_within)
elif not line1.within(bounds):
continue # don't draw this, it's outside SF
else:
map.add_children(folium.PolyLine(locations=[point1, point2]))
geometries.append(line1)
map.save(args.map_output_file)
geo_axes.add_geometries(geometries, ccrs.PlateCarree())
plt.show()
示例6: ADPolyline
# 需要导入模块: from shapely.geometry import LineString [as 别名]
# 或者: from shapely.geometry.LineString import crosses [as 别名]
class ADPolyline(object):
def __init__(self, shapely_geo=None, vertices=None, use_arcpy=False, use_shapely=False):
"""
Can be initiated with either a shapely Linestring or a list of ADPoints (vertices)
:param shapely_geo: Linestring object
:param vertices: list of ADPoint objects
:param use_arcpy: not implemented yet
:param use_shapely: default
:return: None
"""
if vertices is not None and shapely_geo is None:
# vertices supplied, create geo
# assume vertices are all ADPoint
self.vertices = vertices
self.__geo_from_vertices(vertices)
elif vertices is None and shapely_geo is not None:
# Extract vertices from shapely geo
self.vertices = []
self.shapely_geo = shapely_geo
# Make coordinates python friendly
coord_list = list(shapely_geo.coords)
if len(coord_list[0]) == 2:
# 2d linestring
for x, y in coord_list:
vertex = ADPoint(x, y)
self.vertices.append(vertex)
elif len(coord_list[0]) == 3:
# 3d linestring - ignore z value
for x, y, _ in coord_list:
vertex = ADPoint(x, y)
self.vertices.append(vertex)
self.first_point = self.vertices[0]
self.last_point = self.vertices[-1]
else:
# got nothing, bail
raise
# Only allowed to use arcpy or shapely
if use_arcpy and use_arcpy:
raise
if use_arcpy:
raise NotImplementedError
# self.geometry = arcpy.yadayada
# self.point_at_distance() = arcpy.yada
if use_shapely:
pass
self.length = self.shapely_geo.length
def __geo_from_vertices(self, vertices):
temp_vertices = []
for vertex in vertices:
temp_vertex = (vertex.X, vertex.Y)
temp_vertices.append(temp_vertex)
self.shapely_geo = LineString(temp_vertices)
self.first_point = self.vertices[0]
self.last_point = self.vertices[-1]
def __str__(self):
s = ''
for vertex in self.vertices:
s += str(vertex) + ', '
return s[:-2]
def crosses(self, line):
return self.shapely_geo.crosses(line.shapely_geo)
def is_same_as(self, polyline):
if not isinstance(polyline, ADPolyline):
raise # something
if DEBUG_same_as:
print 'comparing 2 polylines'
for vertex1, vertex2 in zip(self.vertices, polyline.vertices):
if not vertex1.is_same_as(vertex2):
return False
return True
def intersection(self, polyline):
"""
Intersects self with polyline. Returns either ADPoint, list of ADPoints, or returns None
:param polyline: ADPolyline
:return: ADPoint, list of ADPoints or None
"""
new_geo = self.shapely_geo.intersection(polyline.shapely_geo)
if type(new_geo) is Point:
return ADPoint(shapely_geo=new_geo)
elif type(new_geo) is MultiPoint:
shapely_points = list(new_geo)
ad_points = []
for point in shapely_points:
ad_points.append(ADPoint(shapely_geo=point))
return ad_points
else:
return None
def mid_point(self):
"""
#.........这里部分代码省略.........
示例7: sqrt
# 需要导入模块: from shapely.geometry import LineString [as 别名]
# 或者: from shapely.geometry.LineString import crosses [as 别名]
print 'line2', line2
print 'line2 type', line2.geom_type
print 'line2 coordinates:', line2.coords[:]
print 'line2 length:', line2.length, '== sqrt(2)/2', line2.length==sqrt(2)/2
print ''
line3 = LineString([(0,0),(-1,1)])
print 'line3', line3
print 'line3 type', line3.geom_type
print 'line3 coordinates:', line3.coords[:]
print 'line3 length:', line3.length, '== sqrt(2)', line3.length==sqrt(2)
print ''
print 'line1-line2'
ipt = line1.intersection(line2)
print 'ipt', ipt
print 'touches:', line1.touches(line2)
print 'crosses:', line1.crosses(line2)
print ''
print 'line1-line3'
ipt = line1.intersection(line3)
print 'ipt', ipt
print 'touches:', line1.touches(line3)
print 'crosses:', line1.crosses(line3)
示例8: createConvexPath
# 需要导入模块: from shapely.geometry import LineString [as 别名]
# 或者: from shapely.geometry.LineString import crosses [as 别名]
def createConvexPath(self, pair):
#pr = cProfile.Profile()
#pr2 = cProfile.Profile()
print pair[1]
odPointsList = ((pair[0][0].x, pair[0][0].y), (pair[0][1].x, pair[0][1].y))
#st_line = LineString(odPointsList)
labeledObstaclePoly = []
totalConvexPathList = {}
dealtArcList = {}
totalConvexPathList[odPointsList] = LineString(odPointsList)
terminate = 0
idx_loop1 = 0
#sp_l_set = []
time_loop1 = 0
time_contain2 = 0
time_crossingDict = 0
time_convexLoop = 0
time_impedingArcs = 0
time_spatialFiltering = 0
time_loop1_crossingDict = 0
time_buildConvexHulls = 0
while terminate == 0:
t1s = time.time()
idx_loop1 += 1
t6s = time.time()
#w = shapefile.Writer(shapefile.POLYLINE)
#w.field('nem')
#for line in totalConvexPathList:
#w.line(parts=[[ list(x) for x in line ]])
#w.record('ff')
#w.save(self.path + "graph_" + str(idx_loop1) + self.version_name)
totalGrpah = self.createGraph(totalConvexPathList.keys())
spatial_filter_n = networkx.dijkstra_path(totalGrpah, odPointsList[0], odPointsList[1])
spatial_filter = []
for i in xrange(len(spatial_filter_n)-1):
spatial_filter.append([spatial_filter_n[i], spatial_filter_n[i+1]])
#w = shapefile.Writer(shapefile.POLYLINE)
#w.field('nem')
#for line in spatial_filter:
#w.line(parts=[[ list(x) for x in line ]])
#w.record('ff')
#w.save(self.path + "spatial Filter_" + str(idx_loop1) + self.version_name)
#sp_length = 0
#for j in spatial_filter:
#sp_length += LineString(j).length
#sp_l_set.append(sp_length)
crossingDict = defaultdict(list)
for line in spatial_filter:
Line = LineString(line)
for obs in self.obstaclesPolygons:
if Line.crosses(obs):
if obs not in labeledObstaclePoly:
labeledObstaclePoly.append(obs)
crossingDict[tuple(line)].append(obs)
t6e = time.time()
time_spatialFiltering += t6e - t6s
if len(crossingDict.keys()) == 0:
terminate = 1
continue
else:
t7s = time.time()
for tLine in crossingDict.keys():
#cLine = list(tLine)
if dealtArcList.has_key(tLine):
try:
del totalConvexPathList[tLine]
except:
del totalConvexPathList[(tLine[1], tLine[0])]
continue
else:
dealtArcList[tLine] = LineString(list(tLine))
try:
del totalConvexPathList[tLine]
except:
del totalConvexPathList[(tLine[1], tLine[0])]
containingObs = []
for obs in crossingDict[tLine]:
convexHull = self.createConvexhull(obs, tLine)
self.splitBoundary(totalConvexPathList, convexHull)
convexHull = self.createConvexhull(obs, odPointsList)
self.splitBoundary(totalConvexPathList, convexHull)
convexHull2 = self.createConvexhull(obs)
if convexHull2.contains(Point(tLine[0])):
containingObs.append(obs)
elif convexHull2.contains(Point(tLine[1])):
#.........这里部分代码省略.........
示例9: createConvexPath_FD
# 需要导入模块: from shapely.geometry import LineString [as 别名]
# 或者: from shapely.geometry.LineString import crosses [as 别名]
def createConvexPath_FD(pair):
#For F_D pair only
#return demand_id if ESP distance to target demand is less than fd_delivery
print pair[1]
odPointsList = ((pair[0][0].x, pair[0][0].y), (pair[0][1].x, pair[0][1].y))
st_line = LineString(odPointsList)
labeledObstaclePoly = []
totalConvexPathList = {}
if st_line.length > fd_delivery * 5280:
return 0
dealtArcList = {}
totalConvexPathList[odPointsList] = LineString(odPointsList)
LineString
terminate = 0
idx_loop1 = 0
time_loop1 = 0
time_contain2 = 0
time_crossingDict = 0
time_convexLoop = 0
time_impedingArcs = 0
time_spatialFiltering = 0
time_loop1_crossingDict = 0
time_buildConvexHulls = 0
no_obs = False
while terminate == 0:
t1s = time.time()
idx_loop1 += 1
t6s = time.time()
#w = shapefile.Writer(shapefile.POLYLINE)
#w.field('nem')
#for line in totalConvexPathList:
#w.line(parts=[[ list(x) for x in line ]])
#w.record('ff')
#w.save(path + "graph_" + str(idx_loop1) + version_name)
totalGrpah = createGraph(totalConvexPathList.keys())
spatial_filter_n = networkx.dijkstra_path(totalGrpah, odPointsList[0], odPointsList[1])
spatial_filter = []
for i in xrange(len(spatial_filter_n)-1):
spatial_filter.append([spatial_filter_n[i], spatial_filter_n[i+1]])
#w = shapefile.Writer(shapefile.POLYLINE)
#w.field('nem')
#for line in spatial_filter:
#w.line(parts=[[ list(x) for x in line ]])
#w.record('ff')
#w.save(self.path + "spatial Filter_" + str(idx_loop1) + self.version_name)
#sp_length = 0
#for j in spatial_filter:
#sp_length += LineString(j).length
#sp_l_set.append(sp_length)
crossingDict = defaultdict(list)
for line in spatial_filter:
Line = LineString(line)
for obs in obstaclesPolygons:
if Line.crosses(obs):
if obs not in labeledObstaclePoly:
labeledObstaclePoly.append(obs)
crossingDict[tuple(line)].append(obs)
t6e = time.time()
time_spatialFiltering += t6e - t6s
if len(crossingDict.keys()) == 0:
terminate = 1
no_obs = True
continue
else:
t7s = time.time()
for tLine in crossingDict.keys():
#cLine = list(tLine)
if dealtArcList.has_key(tLine):
try:
del totalConvexPathList[tLine]
except:
del totalConvexPathList[(tLine[1], tLine[0])]
continue
else:
dealtArcList[tLine] = LineString(list(tLine))
try:
del totalConvexPathList[tLine]
except:
del totalConvexPathList[(tLine[1], tLine[0])]
containingObs = []
for obs in crossingDict[tLine]:
convexHull = createConvexhull(obs, tLine)
splitBoundary(totalConvexPathList, convexHull)
convexHull = createConvexhull(obs, odPointsList)
splitBoundary(totalConvexPathList, convexHull)
convexHull2 = createConvexhull(obs)
if convexHull2.contains(Point(tLine[0])):
containingObs.append(obs)
elif convexHull2.contains(Point(tLine[1])):
#.........这里部分代码省略.........