def test(self): lons, lats, depths = geodetic.npoints_between(lon1=40.77, lat1=38.9, depth1=17.5, lon2=31.14, lat2=46.23, depth2=5.2, npoints=7) expected_lons = [ 40.77, 39.316149154562076, 37.8070559966114, 36.23892429550906, 34.60779411051164, 32.90956020775102, 31.14 ] expected_lats = [ 38.9, 40.174608368560094, 41.43033989236144, 42.66557829138413, 43.87856696738466, 45.067397797471415, 46.23 ] expected_depths = [17.5, 15.45, 13.4, 11.35, 9.3, 7.25, 5.2] self.assertTrue(numpy.allclose(lons, expected_lons)) self.assertTrue(numpy.allclose(lats, expected_lats)) self.assertTrue(numpy.allclose(depths, expected_depths)) # the last and the first points should be exactly the same as two # original corner points, so no "assertAlmostEqual" for them self.assertEqual(lons[0], 40.77) self.assertEqual(lats[0], 38.9) self.assertEqual(depths[0], 17.5) self.assertEqual(lons[-1], 31.14) self.assertEqual(lats[-1], 46.23) self.assertEqual(depths[-1], 5.2)
def test_same_points(self): lon, lat, depth = 1.2, 3.4, 5.6 lons, lats, depths = geodetic.npoints_between( lon, lat, depth, lon, lat, depth, npoints=7 ) expected_lons = [lon] * 7 expected_lats = [lat] * 7 expected_depths = [depth] * 7 self.assertTrue(numpy.allclose(lons, expected_lons)) self.assertTrue(numpy.allclose(lats, expected_lats)) self.assertTrue(numpy.allclose(depths, expected_depths))
def test_same_points(self): lon, lat, depth = 1.2, 3.4, 5.6 lons, lats, depths = geodetic.npoints_between(lon, lat, depth, lon, lat, depth, npoints=7) expected_lons = [lon] * 7 expected_lats = [lat] * 7 expected_depths = [depth] * 7 self.assertTrue(numpy.allclose(lons, expected_lons)) self.assertTrue(numpy.allclose(lats, expected_lats)) self.assertTrue(numpy.allclose(depths, expected_depths))
def get_resampled_coordinates(lons, lats): """ Resample polygon line segments and return the coordinates of the new vertices. This limits distortions when projecting a polygon onto a spherical surface. Parameters define longitudes and latitudes of a point collection in the form of lists or numpy arrays. :return: A tuple of two numpy arrays: longitudes and latitudes of resampled vertices. """ num_coords = len(lons) assert num_coords == len(lats) lons1 = numpy.array(lons) lats1 = numpy.array(lats) lons2 = numpy.concatenate((lons1[1:], lons1[0:1])) lats2 = numpy.concatenate((lats1[1:], lats1[0:1])) distances = geodetic.geodetic_distance(lons1, lats1, lons2, lats2) resampled_lons = [lons[0]] resampled_lats = [lats[0]] for i in xrange(num_coords): next_point = (i + 1) % num_coords lon1, lat1 = lons[i], lats[i] lon2, lat2 = lons[next_point], lats[next_point] distance = distances[i] num_points = int(distance / UPSAMPLING_STEP_KM) + 1 if num_points >= 2: # We need to increase the resolution of this arc by adding new # points. new_lons, new_lats, _ = geodetic.npoints_between( lon1, lat1, 0, lon2, lat2, 0, num_points ) resampled_lons.extend(new_lons[1:]) resampled_lats.extend(new_lats[1:]) else: resampled_lons.append(lon2) resampled_lats.append(lat2) # we cut off the last point because it repeats the first one. return numpy.array(resampled_lons[:-1]), numpy.array(resampled_lats[:-1])
def get_resampled_coordinates(lons, lats): """ Resample polygon line segments and return the coordinates of the new vertices. This limits distortions when projecting a polygon onto a spherical surface. Parameters define longitudes and latitudes of a point collection in the form of lists or numpy arrays. :return: A tuple of two numpy arrays: longitudes and latitudes of resampled vertices. """ num_coords = len(lons) assert num_coords == len(lats) lons1 = numpy.array(lons) lats1 = numpy.array(lats) lons2 = numpy.concatenate((lons1[1:], lons1[0:1])) lats2 = numpy.concatenate((lats1[1:], lats1[0:1])) distances = geodetic.geodetic_distance(lons1, lats1, lons2, lats2) resampled_lons = [lons[0]] resampled_lats = [lats[0]] for i in xrange(num_coords): next_point = (i + 1) % num_coords lon1, lat1 = lons[i], lats[i] lon2, lat2 = lons[next_point], lats[next_point] distance = distances[i] num_points = int(distance / UPSAMPLING_STEP_KM) + 1 if num_points >= 2: # We need to increase the resolution of this arc by adding new # points. new_lons, new_lats, _ = geodetic.npoints_between( lon1, lat1, 0, lon2, lat2, 0, num_points) resampled_lons.extend(new_lons[1:]) resampled_lats.extend(new_lats[1:]) else: resampled_lons.append(lon2) resampled_lats.append(lat2) # we cut off the last point because it repeats the first one. return numpy.array(resampled_lons[:-1]), numpy.array(resampled_lats[:-1])
def _define_bins(bins_data, mag_bin_width, dist_bin_width, coord_bin_width, truncation_level, n_epsilons): """ Define bin edges for disaggregation histograms. Given bins data as provided by :func:`_collect_bins_data`, this function finds edges of histograms, taking into account maximum and minimum values of magnitude, distance and coordinates as well as requested sizes/numbers of bins. """ mags, dists, lons, lats, _joint_probs, tect_reg_types, trt_bins = bins_data mag_bins = mag_bin_width * numpy.arange( int(numpy.floor(mags.min() / mag_bin_width)), int(numpy.ceil(mags.max() / mag_bin_width) + 1) ) dist_bins = dist_bin_width * numpy.arange( int(numpy.floor(dists.min() / dist_bin_width)), int(numpy.ceil(dists.max() / dist_bin_width) + 1) ) west, east, north, south = get_spherical_bounding_box(lons, lats) west = numpy.floor(west / coord_bin_width) * coord_bin_width east = numpy.ceil(east / coord_bin_width) * coord_bin_width lon_extent = get_longitudinal_extent(west, east) lon_bins, _, _ = npoints_between( west, 0, 0, east, 0, 0, numpy.round(lon_extent / coord_bin_width) + 1 ) lat_bins = coord_bin_width * numpy.arange( int(numpy.floor(south / coord_bin_width)), int(numpy.ceil(north / coord_bin_width) + 1) ) eps_bins = numpy.linspace(-truncation_level, truncation_level, n_epsilons + 1) return mag_bins, dist_bins, lon_bins, lat_bins, eps_bins, trt_bins
def test(self): lons, lats, depths = geodetic.npoints_between( lon1=40.77, lat1=38.9, depth1=17.5, lon2=31.14, lat2=46.23, depth2=5.2, npoints=7 ) expected_lons = [40.77, 39.316149154562076, 37.8070559966114, 36.23892429550906, 34.60779411051164, 32.90956020775102, 31.14] expected_lats = [38.9, 40.174608368560094, 41.43033989236144, 42.66557829138413, 43.87856696738466, 45.067397797471415, 46.23] expected_depths = [17.5, 15.45, 13.4, 11.35, 9.3, 7.25, 5.2] self.assertTrue(numpy.allclose(lons, expected_lons)) self.assertTrue(numpy.allclose(lats, expected_lats)) self.assertTrue(numpy.allclose(depths, expected_depths)) # the last and the first points should be exactly the same as two # original corner points, so no "assertAlmostEqual" for them self.assertEqual(lons[0], 40.77) self.assertEqual(lats[0], 38.9) self.assertEqual(depths[0], 17.5) self.assertEqual(lons[-1], 31.14) self.assertEqual(lats[-1], 46.23) self.assertEqual(depths[-1], 5.2)
def _define_bins(bins_data, mag_bin_width, dist_bin_width, coord_bin_width, truncation_level, n_epsilons): """ Define bin edges for disaggregation histograms. Given bins data as provided by :func:`_collect_bins_data`, this function finds edges of histograms, taking into account maximum and minimum values of magnitude, distance and coordinates as well as requested sizes/numbers of bins. """ mags, dists, lons, lats, _joint_probs, tect_reg_types, trt_bins = bins_data mag_bins = mag_bin_width * numpy.arange( int(numpy.floor(mags.min() / mag_bin_width)), int(numpy.ceil(mags.max() / mag_bin_width) + 1)) dist_bins = dist_bin_width * numpy.arange( int(numpy.floor(dists.min() / dist_bin_width)), int(numpy.ceil(dists.max() / dist_bin_width) + 1)) west, east, north, south = get_spherical_bounding_box(lons, lats) west = numpy.floor(west / coord_bin_width) * coord_bin_width east = numpy.ceil(east / coord_bin_width) * coord_bin_width lon_extent = get_longitudinal_extent(west, east) lon_bins, _, _ = npoints_between( west, 0, 0, east, 0, 0, numpy.round(lon_extent / coord_bin_width) + 1) lat_bins = coord_bin_width * numpy.arange( int(numpy.floor(south / coord_bin_width)), int(numpy.ceil(north / coord_bin_width) + 1)) eps_bins = numpy.linspace(-truncation_level, truncation_level, n_epsilons + 1) return mag_bins, dist_bins, lon_bins, lat_bins, eps_bins, trt_bins