def cutoff(self, recta2): """Return the cutoff point between this straight an a given one. :: >>> line = Straight(Point(0,0,0), Point(10,10,10), 0, 0) >>> line2 = Straight(Point(30,0,0), Point(20,10,10), 0, 0) >>> line.cutoff(line2) RoadPoint(15.0, 15.0, 0.0, 21.2132034356, 0, , , 0, none, 0, none, 0) """ if self.pstart.x == self.pend.x: m_2 = (recta2.pend.y - recta2.pstart.y) / (recta2.pend.x - recta2.pstart.x) coord_x = self.pstart.x coord_y = recta2.pstart.y + m_2 * (self.pstart.x - recta2.pstart.x) elif recta2.pstart.x == recta2.pend.x: m_1 = (self.pend.y - self.pstart.y) / (self.pend.x - self.pstart.x) coord_x = recta2.pstart.x coord_y = self.pstart.y + m_1 * (recta2.pstart.x - self.pstart.x) else: m_1 = (self.pend.y - self.pstart.y) / (self.pend.x - self.pstart.x) m_2 = (recta2.pend.y - recta2.pstart.y) / (recta2.pend.x - recta2.pstart.x) coord_x = (m_1 * self.pstart.x - m_2 * recta2.pstart.x - self.pstart.y + recta2.pstart.y) / (m_1 - m_2) coord_y = m_1 * (coord_x - self.pstart.x) + self.pstart.y return RoadPoint( Point(coord_x, coord_y), self.pstart.distance(Point(coord_x, coord_y)), self.azi, "", )
def _axes_y_marks(self): """Return """ mark_y = [] labels = [] for i in range(self.cols): for j in range(self.opts2['rows']): lim_sup = self._get_trans_lim_sup(self.mat_trans[i][j]) for k in range(0, int(lim_sup) + 1, int(self.opts1['mark_y_dist'] * self.scale)): labels.append( [self.mat_trans[i][j].min_height() + k / self.scale]) mark_y.append( Line([ Point( self.centers[i][j].x - self.opts1['mark_lon'], k + self.centers[i][j].y), Point( self.centers[i][j].x + self.opts1['mark_lon'], k + self.centers[i][j].y) ])) return mark_y, labels
def test_distance(self): """Test distance method""" point0 = Point(0, 0, 0) point1 = Point(1, 0) self.assertEqual(point0.distance(point1), 1.0) point1.z = 1 self.assertAlmostEqual(point0.distance(point1), np.sqrt(2.))
def test_eq(self): """Test __eq__""" point0 = Point(0, 0) point1 = Point(1, 0) self.assertFalse(point0 == point1) self.assertFalse(point0 == (1, 0)) self.assertTrue(point0 == point0) self.assertTrue(point0 == (0, 0))
def _axis_y(self): """Return""" return [ Line([ Point(self.zero_x, self.zero_y), Point(self.zero_x, self.alt_y) ]) ], [["Axis Y", "", ""]]
def test_switch_2D_3D_2D(self): """Test switch between: 2D => 3D => 2D""" point = Point() self.assertIsNone(point.z) self.assertTrue(point.is2D) point.z = 1 self.assertFalse(point.is2D) point.z = None self.assertTrue(point.is2D, True)
def _axis_y(self): """Return """ return [ Line([ Point(self.zero_x, self.zero_y), Point(self.zero_x, self.alt_y) ]) ], [['Axis Y', '', '']]
def test_to_wkt(self): """Test coords method""" self.assertEqual( Point(1, 2).to_wkt(), "POINT (1.0000000000000000 2.0000000000000000)") self.assertEqual( Point(1, 2, 3).to_wkt(), "POINT Z (1.0000000000000000 2.0000000000000000 3.0000000000000000)", )
def _axis_x(self, r_line): """Return """ lines = [] for j in range(0, 4): lines.append( Line([ Point(self.zero_x, self.zero_y - j * self.dist_ejes_x), Point(self.zero_x + r_line[-1].npk, self.zero_y - j * self.dist_ejes_x) ])) return lines, [['Axis X', '', ''], ['Elevation', '', ''], ['Terrain', '', ''], ['Red elevation', '', '']]
def uppoints(self): """Return""" name_map = self.name_map + "__Topo" topo = VectorTopo(name_map) topo.open("r", layer=1) pts_org = [] pts_chg = [] attrs = [] for i in range(1, len(topo) + 1): act = topo.read(i).attrs["action"] if act != "": cat = topo.read(i).attrs["cat"] pto_org = topo.cat(cat, "points", 1)[0] pto_chg = Point(pto_org.x, pto_org.y, pto_org.z) if topo.read(i).attrs["x"] is not None: pto_chg.x = float(topo.read(i).attrs["x"]) if topo.read(i).attrs["y"] is not None: pto_chg.y = float(topo.read(i).attrs["y"]) if topo.read(i).attrs["z"] is not None: pto_chg.z = float(topo.read(i).attrs["z"]) pts_org.append(pto_org) pts_chg.append(pto_chg) attrs.append( [ cat, topo.read(i).attrs["pk"], topo.read(i).attrs["name"], topo.read(i).attrs["azi"], topo.read(i).attrs["p_type"], topo.read(i).attrs["align"], topo.read(i).attrs["vparam"], topo.read(i).attrs["v_type"], topo.read(i).attrs["terr"], topo.read(i).attrs["t_type"], topo.read(i).attrs["dist_d"], pto_chg.x, pto_chg.y, pto_chg.z, "", ] ) topo.close() if pts_org != []: topo.open("rw", 1, with_z=True) for i, pto in enumerate(pts_org): topo.rewrite(pto, pts_chg[i], attrs[i][1:]) topo.table.conn.commit() topo.close()
def SetDatasets(self, datasets, coors, output, dpi): """Set the data :param list datasets: a list of temporal dataset's name :param list coors: a list with x/y coordinates :param str output: the name of output png file :param int dpi: the dpi value for png file """ if not datasets or not coors: return try: datasets = self._checkDatasets(datasets) if not datasets: return except GException: GError(parent=self, message=_("Invalid input temporal dataset")) return try: self.poi = Point(float(coors[0]), float(coors[1])) except GException: GError(parent=self, message=_("Invalid input coordinates")) return self.datasets = datasets self.output = output self.dpi = dpi self.datasetSelect.SetValue(','.join( map(lambda x: x[0] + '@' + x[1], datasets))) self.coorval.SetValue(','.join(coors)) self._redraw()
def OnRedraw(self, event): """Required redrawing.""" datasets = self.datasetSelect.GetValue().strip() if not datasets: return datasets = datasets.split(',') try: datasets = self._checkDatasets(datasets) if not datasets: return except GException: GError(parent=self, message=_("Invalid input data")) return self.datasets = datasets try: coordx, coordy = self.coorval.GetValue().split(',') coordx, coordy = float(coordx), float(coordy) except ValueError: GMessage(_("Incorrect format of coordinates, should be: x,y")) coors = [coordx, coordy] if coors: try: self.poi = Point(float(coors[0]), float(coors[1])) except GException: GError(parent=self, message=_("Invalid input coordinates")) return self._redraw()
def parallel(self, dist, g90): """ Return """ return RoadPoint( Point(self.x + dist * math.sin(self.azi + g90), self.y + dist * math.cos(self.azi + g90)), self.npk, self.azi, self.p_type)
def __init__(self, point=None, npk=None, azi=None, p_type=None): """Return""" super(RoadPoint, self).__init__(point.x, point.y, point.z) self.x = point.x self.y = point.y self.z = point.z self.point2d = Point(self.x, self.y) self.npk = npk self.azi = azi self.p_type = p_type self.align = "" self.incli = 0 self.v_param = 0 self.v_type = "none" self.terr = 0 self.terr_type = "none" self.dist_displ = 0 self.acum_pk = 0 if self.z is None: self.z = 0
def _get_pts_clot_out(self, start, end, interv): """Return list of axis points of clothoid out, and set the rest to the end of the clothoid and accum length """ accum = self.pts_accum list_pts = [] start2 = self.length() - end end2 = self.length() - start while start2 <= end2: if end2 == 0: end2 = 0.0000001 rad_clo = self.a_clot**2 / end2 tau_clo = end2 / (2 * rad_clo) x_o, y_o = aprox_coord((end2), tau_clo) x_1, y_1 = self.cloth_global(x_o, y_o) azi1 = self.pnt_azimuth(tau_clo) list_pts.append( RoadPoint(Point(x_1, y_1, 0), accum, round(azi1, 6), "Clot_out")) accum += interv end2 = end2 - interv self.pts_rest = start2 + (end2 + interv) self.pts_accum = accum - interv return list_pts
def test_init_3d(self): """Test 3D Point(1, 2, 3)""" point = Point(1, 2, 3) self.assertEqual(point.x, 1) self.assertEqual(point.y, 2) self.assertEqual(point.z, 3) self.assertFalse(point.is2D)
def get_roadpnts(self, start, end, interv): """Return list of axis points of a straight :: >>> line = Straight(Point(0,0,0), Point(10,10,0), 0, 0) >>> line.get_roadpnts(3,7,1) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE [RoadPoint(2.12132034356, 2.12132034356, 0.0, 0, 0.785398163397, ... """ accum = self.pts_accum if end == -1: end = self.length() azi = self.azimuth() list_pts = [] while start <= end: pnt = Point( self.pstart.x + start * math.sin(azi), self.pstart.y + start * math.cos(azi), self.pstart.z, ) list_pts.append(RoadPoint(pnt, accum, round(azi, 6), "Line")) accum += interv start += interv self.pts_rest = end - (start - interv) self.pts_accum = accum - interv return list_pts
def test_contains_point(self): """Test contain_point method""" area = Area(v_id=1, c_mapinfo=self.c_mapinfo) p = Point(0.5, 0.5) bbox = Bbox(4.0, 0.0, 4.0, 0.0) self.assertTrue(area.contains_point(p, bbox)) self.assertTrue(area.contains_point(p))
def bisecc(r_pnt, align): """Return cutoff roadpoint of a straight, given by a roadpoint with its azimuth, and a road object defined by the funct function """ recta2 = r_pnt.normal(math.pi) len_a = -0.00001 len_b = align.length() eq_a = align.funct(len_a, recta2) eq_b = align.funct(len_b, recta2) if round(eq_a[0], 5) * round(eq_b[0], 5) > 0: return None len_c = (len_a + len_b) / 2.0 while (len_b - len_a) / 2.0 > 0.00001: eq_c = align.funct(len_c, recta2) eq_a = align.funct(len_a, recta2) if eq_c[0] == 0: continue elif eq_a[0] * eq_c[0] <= 0: len_b = len_c else: len_a = len_c len_c = (len_a + len_b) / 2.0 return RoadPoint(Point(eq_c[1], eq_c[2], 0), len_c, r_pnt.azi, "")
def test_empty_init(self): """Test Point()""" point = Point() self.assertEqual(point.gtype, libvect.GV_POINT) self.assertEqual(point.x, 0) self.assertEqual(point.y, 0) self.assertIsNone(point.z) self.assertTrue(point.is2D)
def _axis_y_marks(self): """Return""" mark_y = [] labels = [] lim_sup = self.alt_y if lim_sup % int(self.mark_y_dist * self.scale) != 0: lim_sup += int(self.mark_y_dist * self.scale) for i in range(self.zero_y, lim_sup, int(self.mark_y_dist * self.scale)): # labels.append(self.min_elev + i / self.scale) labels.append(self.min_elev + (i - self.zero_y) / self.scale) mark_y.append( Line([ Point(self.zero_x - self.mark_lon, i), Point(self.zero_x + self.mark_lon, i), ])) return mark_y, labels
def uppoints(self): """ Return """ name_map = self.name_map + '__Topo' topo = VectorTopo(name_map) topo.open('r', layer=1) pts_org = [] pts_chg = [] attrs = [] for i in range(1, len(topo) + 1): act = topo.read(i).attrs['action'] if act != '': cat = topo.read(i).attrs['cat'] pto_org = topo.cat(cat, 'points', 1)[0] pto_chg = Point(pto_org.x, pto_org.y, pto_org.z) if topo.read(i).attrs['x'] is not None: pto_chg.x = float(topo.read(i).attrs['x']) if topo.read(i).attrs['y'] is not None: pto_chg.y = float(topo.read(i).attrs['y']) if topo.read(i).attrs['z'] is not None: pto_chg.z = float(topo.read(i).attrs['z']) pts_org.append(pto_org) pts_chg.append(pto_chg) attrs.append([ cat, topo.read(i).attrs['pk'], topo.read(i).attrs['name'], topo.read(i).attrs['azi'], topo.read(i).attrs['p_type'], topo.read(i).attrs['align'], topo.read(i).attrs['vparam'], topo.read(i).attrs['v_type'], topo.read(i).attrs['terr'], topo.read(i).attrs['t_type'], topo.read(i).attrs['dist_d'], pto_chg.x, pto_chg.y, pto_chg.z, '' ]) topo.close() if pts_org != []: topo.open('rw', 1, with_z=True) for i, pto in enumerate(pts_org): topo.rewrite(pto, pts_chg[i], attrs[i][1:]) topo.table.conn.commit() topo.close()
def test_distance(self): """Test distance method""" point0 = Point(0, 0, 0) point1 = Point(1, 0) self.assertEqual(point0.distance(point1), 1.0) point1.z = 1 self.assertAlmostEqual(point0.distance(point1), np.sqrt(2.0))
def _terr(self, r_line): """Return """ line = [] for r_pnt in r_line: line.append( Point(r_pnt.npk + self.zero_x, (r_pnt.terr - self.min_elev) * self.scale + self.zero_y)) return Line(line)
def _ras(self, r_line): """Return(self.max_elev - self.min_elev) """ line = [] for r_pnt in r_line: line.append( Point(r_pnt.npk + self.zero_x, (r_pnt.z - self.min_elev) * self.scale + self.zero_y)) return Line(line)
def add_points(vname, vmapset="", *points): """ >>> add_points('new', (1, 2), (2, 3), (3, 4)) """ mapset = get_mapset_vector(vname, vmapset) mode = "rw" if mapset else "w" with VectorTopo(vname, mapset, mode=mode) as vct: for x, y in points: vct.write(Point(x, y))
def project(self, dist, azi, sig=1): """Return""" return RoadPoint( Point(self.x + sig * dist * math.sin(azi), self.y + sig * dist * math.cos(azi)), self.npk, azi, "", )
def _axis_x(self, r_line): """Return""" lines = [] for j in range(0, 4): lines.append( Line([ Point(self.zero_x, self.zero_y - j * self.dist_ejes_x), Point( self.zero_x + r_line[-1].npk, self.zero_y - j * self.dist_ejes_x, ), ])) return lines, [ ["Axis X", "", ""], ["Elevation", "", ""], ["Terrain", "", ""], ["Red elevation", "", ""], ]
def write_vector_map(self, drainage_network, map_name, linking_elem): """Write a vector map to GRASS GIS using drainage_network is a networkx object """ with VectorTopo(map_name, mode='w', overwrite=self.overwrite) as vect_map: # create db links and tables dblinks = self.create_db_links(vect_map, linking_elem) # set category manually cat_num = 1 # dict to keep DB infos to write DB after geometries db_info = {k: [] for k in linking_elem} # Points for node in drainage_network.nodes(): if node.coordinates: point = Point(*node.coordinates) # add values map_layer, dbtable = dblinks['node'] self.write_vector_geometry(vect_map, point, cat_num, map_layer) # Get DB attributes attrs = tuple([cat_num] + node.get_attrs()) db_info['node'].append(attrs) # bump cat cat_num += 1 # Lines for in_node, out_node, edge_data in drainage_network.edges_iter( data=True): link = edge_data['object'] # assemble geometry in_node_coor = in_node.coordinates out_node_coor = out_node.coordinates if in_node_coor and out_node_coor: line_object = Line([in_node_coor] + link.vertices + [out_node_coor]) # set category and layer link map_layer, dbtable = dblinks['link'] self.write_vector_geometry(vect_map, line_object, cat_num, map_layer) # keep DB info attrs = tuple([cat_num] + link.get_attrs()) db_info['link'].append(attrs) # bump cat cat_num += 1 # write DB for geom_type, attrs in db_info.iteritems(): map_layer, dbtable = dblinks[geom_type] for attr in attrs: dbtable.insert(attr) dbtable.conn.commit() return self
def charact_pnts(self, vert): """Return """ char_r_pnts, char_r_pnts_attrs = vert.get_charact_pnts() list_r_pnts = [] for r_pnt in char_r_pnts: list_r_pnts.append( Point(r_pnt.npk + self.zero_x, (r_pnt.z - self.min_elev) * self.scale + self.zero_y)) return list_r_pnts, char_r_pnts_attrs
def parallel(self, dist, radio): """Return a parallel straight with a given distance and side given by the sing of radio. >>> line = Straight(Point(0,0,0), Point(20,20,10), 0, 0) >>> line.parallel(10, -1) # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE Straight(-7.07106781187, 7.07106781187, 0.0, 28.2842712475, ... """ if radio > 0: g90 = math.pi / 2 else: g90 = -math.pi / 2 x_1 = self.pstart.x + dist * math.sin(self.azimuth() + g90) y_1 = self.pstart.y + dist * math.cos(self.azimuth() + g90) x_2 = self.pend.x + dist * math.sin(self.azimuth() + g90) y_2 = self.pend.y + dist * math.cos(self.azimuth() + g90) return Straight(Point(x_1, y_1), Point(x_2, y_2))