def test_rotations(self): for lat in num.linspace(-90., 90., 20): for lon in num.linspace(-180., 180., 20): point = num.array([lat, lon], dtype=num.float) xyz = orthodrome.latlon_to_xyz(point) rot = orthodrome.rot_to_00(point[0], point[1]) p2 = num.dot(rot, xyz) num.testing.assert_allclose(p2, [1., 0., 0.], atol=1.0e-7)
def test_rotation2(self): eps = 1.0e-7 lats = num.linspace(50., 60., 20) lons = num.linspace(170., 180., 20) lats2 = num.repeat(lats, lons.size) lons2 = num.tile(lons, lats.size) points = num.vstack((lats2, lons2)).T xyz = orthodrome.latlon_to_xyz(points) rot = orthodrome.rot_to_00(lats[0], lons[0]) xyz2 = num.dot(rot, xyz.T).T points2 = orthodrome.xyz_to_latlon(xyz2) assert num.all(points2[:, 1] > -eps)
def split_types(self, groups=None): xyz = od.latlon_to_xyz(self.points) xyzmid = (xyz[1:] + xyz[:-1, :]) * 0.5 cxyz = od.latlon_to_xyz(self.cpoints) d = od.distances3d(xyzmid[num.newaxis, :, :], cxyz[:, num.newaxis, :]) idmin = num.argmin(d, axis=0) itypes = self.itypes[idmin] if groups is None: groupmap = num.arange(len(self._index_to_type)) else: d = {} for igroup, group in enumerate(groups): for name in group: d[name] = igroup groupmap = num.array([d[name] for name in self._index_to_type], dtype=num.int) iswitch = num.concatenate( ([0], num.where(groupmap[itypes[1:]] != groupmap[itypes[:-1]])[0] + 1, [itypes.size])) results = [] for ii in range(iswitch.size - 1): if groups is not None: tt = [ self._index_to_type[ityp] for ityp in num.unique(itypes[iswitch[ii]:iswitch[ii + 1]]) ] else: tt = self._index_to_type[itypes[iswitch[ii]]] results.append((tt, self.points[iswitch[ii]:iswitch[ii + 1] + 1])) return results
def split_types(self, groups=None): xyz = od.latlon_to_xyz(self.points) xyzmid = (xyz[1:] + xyz[:-1, :]) * 0.5 cxyz = od.latlon_to_xyz(self.cpoints) d = od.distances3d(xyzmid[num.newaxis, :, :], cxyz[:, num.newaxis, :]) idmin = num.argmin(d, axis=0) itypes = self.itypes[idmin] if groups is None: groupmap = num.arange(len(self._index_to_type)) else: d = {} for igroup, group in enumerate(groups): for name in group: d[name] = igroup groupmap = num.array( [d[name] for name in self._index_to_type], dtype=num.int) iswitch = num.concatenate( ([0], num.where(groupmap[itypes[1:]] != groupmap[itypes[:-1]])[0]+1, [itypes.size])) results = [] for ii in range(iswitch.size-1): if groups is not None: tt = [self._index_to_type[ityp] for ityp in num.unique( itypes[iswitch[ii]:iswitch[ii+1]])] else: tt = self._index_to_type[itypes[iswitch[ii]]] results.append((tt, self.points[iswitch[ii]:iswitch[ii+1]+1])) return results
def test_point_in_polygon(self): if plot: from pyrocko.plot import mpl_graph_color import matplotlib.pyplot as plt from matplotlib.patches import Polygon axes = plt.gca() nip = 100 for i in range(1): np = 3 points = num.zeros((np, 2)) points[:, 0] = random_lat(size=3) points[:, 1] = random_lon(size=3) points_ip = num.zeros((nip*points.shape[0], 2)) for ip in range(points.shape[0]): n, e = orthodrome.latlon_to_ne_numpy( points[ip % np, 0], points[ip % np, 1], points[(ip+1) % np, 0], points[(ip+1) % np, 1]) ns = num.arange(nip) * n / nip es = num.arange(nip) * e / nip lats, lons = orthodrome.ne_to_latlon( points[ip % np, 0], points[ip % np, 1], ns, es) points_ip[ip*nip:(ip+1)*nip, 0] = lats points_ip[ip*nip:(ip+1)*nip, 1] = lons if plot: color = mpl_graph_color(i) axes.add_patch( Polygon( num.fliplr(points_ip), facecolor=light(color), edgecolor=color, alpha=0.5)) points_xyz = orthodrome.latlon_to_xyz(points_ip.T) center_xyz = num.mean(points_xyz, axis=0) assert num.all( orthodrome.distances3d( points_xyz, center_xyz[num.newaxis, :]) < 1.0) lat, lon = orthodrome.xyz_to_latlon(center_xyz) rot = orthodrome.rot_to_00(lat, lon) points_rot_xyz = num.dot(rot, points_xyz.T).T points_rot_pro = orthodrome.stereographic(points_rot_xyz) # noqa poly_xyz = orthodrome.latlon_to_xyz(points_ip) poly_rot_xyz = num.dot(rot, poly_xyz.T).T groups = orthodrome.spoly_cut([poly_rot_xyz], axis=0) num.zeros(points.shape[0], dtype=num.int) if plot: for group in groups: for poly_rot_group_xyz in group: axes.set_xlim(-180., 180.) axes.set_ylim(-90., 90.) plt.show()
def max_interpoint_distance(self): p = od.latlon_to_xyz(self.points) return math.sqrt( num.max( num.sum((p[num.newaxis, :, :] - p[:, num.newaxis, :])**2, axis=2)))
def max_interpoint_distance(self): p = od.latlon_to_xyz(self.points) return math.sqrt(num.max(num.sum( (p[num.newaxis, :, :] - p[:, num.newaxis, :])**2, axis=2)))