def test_wrong_type(self): g1 = Point(0, 0) g2 = LineString([(5, 0), (15, 0)]) with self.assertRaises(TypeError): result = shared_paths(g1, g2) with self.assertRaises(TypeError): result = shared_paths(g2, g1)
def test_mplp(self): mpl = self.make1() p1 = mpl.geoms[0] p2 = mpl.geoms[1] t1, t2 = ops.shared_paths(p1.exterior, p2.exterior) print(t1) print(t2)
def findCommonEdges(a, b): aLines = getLineListFromBoundary(a.boundary) edgesInCommon = [] for aLine in aLines: bLines = getLineListFromBoundary(b.boundary) for bLine in bLines: edgesInCommon.append(shared_paths(aLine, bLine)) edgesInCommon = [edge for edge in edgesInCommon if not edge.is_empty] return edgesInCommon
def test_shared_paths_forward(self): g1 = LineString([(0, 0), (10, 0), (10, 5), (20, 5)]) g2 = LineString([(5, 0), (15, 0)]) result = shared_paths(g1, g2) self.assertTrue(isinstance(result, GeometryCollection)) self.assertTrue(len(result) == 2) a, b = result self.assertTrue(isinstance(a, MultiLineString)) self.assertTrue(len(a) == 1) self.assertEqual(a[0].coords[:], [(5, 0), (10, 0)]) self.assertTrue(b.is_empty)
def _adj_pair(self, r1, r2): try: na = ops.shared_paths(r1.exterior, r2.exterior) if na.is_empty or not na.is_valid: return t1, t2 = na if not t1.is_empty or not t2.is_empty: self._adj[r1.name].add(r2.name) self._adj[r2.name].add(r1.name) return True except: print('error:', r1, r2)
def _forward(self, layout): ent = self._from_layout(layout) sp = ops.shared_paths(ent.exterior, self._geom) if sp.is_empty: return False seg1, seg2 = sp shared_len = 0. if not seg1.is_empty: shared_len += seg1.length if not seg2.is_empty: shared_len += seg2.length return shared_len
def shared_segs(self, g1, g2): """ This function returns the segments that are shared with two input geometries. The shapely function `shapely.ops.shared_paths()` is adopted and can catch both the shared paths with the same direction for both inputs as well as the shared paths with the opposite direction for one the two inputs. The returned object extents the `segments` property with detected segments. Where each seperate segment is a linestring between two points. Parameters ---------- g1 : shapely.geometry.LineString first geometry g2 : shapely.geometry.LineString second geometry """ # detect potential shared paths between two linestrings try: fw_bw = shared_paths(g1, g2) except ValueError: self.valerr = True fw_bw = False # fw_bw = shared_paths(snap(g1, g2, tolerance=6), g2) # continue if any shared path was detected if fw_bw and not fw_bw.is_empty: forward = fw_bw[0] backward = fw_bw[1] if backward.is_empty: # only contains forward objects shared_segments = forward elif forward.is_empty: # only contains backward objects shared_segments = backward else: # both backward and forward contains objects, so combine forward = self.validate_linemerge(linemerge(forward)) backward = self.validate_linemerge(linemerge(backward)) shared_segments = geometry.MultiLineString(forward + backward) # add shared paths to segments self.segments.extend([list(shared_segments)]) # also add the first coordinates of both geoms as a vertice to segments p1_g1 = geometry.Point([g1.xy[0][0], g1.xy[1][0]]) p1_g2 = geometry.Point([g2.xy[0][0], g2.xy[1][0]]) ls_p1_g1g2 = geometry.LineString([p1_g1, p1_g2]) self.segments.extend([[ls_p1_g1g2]])
def is_satisfied(self, layout=None): if self._ent2 in layout._adj[self._ent]: if self._dim is None: return True r1 = layout[self._ent].exterior r2 = layout[self._ent2].exterior adj = ops.shared_paths(r1, r2) if adj.is_empty: return False t1, t2 = adj if not t1.is_empty and t1.length >= self._dim: return True if not t2.is_empty and t2.length >= self._dim: return True return False