def cities_on_map(A, distance_limit=100): """Put selected cities on map. Ensure that cities shown are at least dis_lim km apart. Input A: Selected cities sorted by intensity and population. distance_limit: Minimum distance [km] between cities shown on map (default is 100 km) Output Generates text file for use by GMT to plot cities on map """ # Always take the first city (which is the one with the highest intensity) index = [0] # Indices of all remaining cities T = range(1, len(A)) # Loop through remaining cities and determine which to plot T2 = [] b = 0 while True: # Find cities more than distance_limit km away from city b (anchor city) for i in range(len(T)): k = T[i] # Index of other city start = Point(latitude=A['lat'][b], longitude=A['lon'][b]) # Anchor city end = Point(latitude=A['lat'][k], longitude=A['lon'][k]) # Other city r = start.distance_to(end)/1000 # Calculate distance and convert to km if r >= distance_limit: # Select city i because it is sufficiently far away from anchor city T2 += [(k)] # Determine whether to use more cities or not if len(T2) > 1: # If more than one candidate exist pick the first of the selected cities as new anchor city b = T2[0] index += [(b)] # Replace T with what now remains and reset T2 T = T2[1:] T2 = [] elif len(T2) == 1: # If only one city exists add it and exit loop index += [(T2[0])] break else: # If no cities were found exit loop break # Make sure there is no old file hanging around cmd = '/bin/rm -rf city.txt' os.system(cmd) # Record selected cities in GMT file city_filename = 'city.txt' for i in index: cmd = 'cat << END >> %s' % city_filename + '\n'+''+str(A['lon'][i])+' '+str(A['lat'][i])+' 15 0 0 BR '+A['name'][i]+'\n'+'END' os.system(cmd)
def setUp(self): self.eps = 0.001 # Accept 0.1 % relative error self.RSISE = Point(-35.27456, 149.12065) self.Home = Point(-35.25629, 149.12494) # 28 Scrivener Street, ACT self.Syd = Point(-33.93479, 151.16794) # Sydney Airport self.Nadi = Point(-17.75330, 177.45148) # Nadi Airport self.Kobenhavn = Point(55.70248, 12.58364) # Kobenhavn, Denmark self.Muncar = Point(-8.43, 114.33) # Muncar, Indonesia
def testBearingNorth(self): """Bearing due north (0 deg) correct within double precision """ eps = 1.0e-12 p1 = Point(0.0, 0.0) p2 = Point(1.0, 0.0) b = p1.bearing_to(p2) msg = 'Computed northward bearing: %d, Should have been: %d' % (b, 0) assert numpy.allclose(b, 0, rtol=eps, atol=eps), msg
def testBearingWest(self): """Bearing due west (270 deg) is correct within double precision """ eps = 1.0e-12 B = 270 # True bearing p1 = Point(0.0, 0.0) p3 = Point(0.0, 1.0) b = p3.bearing_to(p1) msg = 'Computed southward bearing %d. Expected %d' % (b, B) assert numpy.allclose(b, B, rtol=eps, atol=eps), msg
def test_equator_example(self): """Distance and bearing of real example (near equator) are correct """ # Test data from http://www.movable-type.co.uk/scripts/latlong.html D = 11448.0959593 # True Distance [m] p1 = Point(latitude=-0.59, longitude=117.10) p2 = Point(latitude=-0.50, longitude=117.15) d = p1.distance_to(p2) msg = 'Dist to point failed %f. Expected %f' % (d, D) assert numpy.allclose(d, D, rtol=1.0e-3), msg
def calculate_location_info(event_info, city_info): """Calculate distance from earthquake to most affected city Input event_info: Information about earthquake city_info: List of cities sorted by intensity Note all information is passed in to allow future modifications such as options for calculating * distance to nearest city * distance to most affected city * distance to biggest city * etc """ # Get location of earthquake FIXME: Should store these as floats earthquake_location = Point(float(event_info['lat']), float(event_info['lon'])) # Select city (in this case, take the most affected) city = city_info[0] # Get location and name of selected city city_name = city[0] city_location = Point(city[4], city[3]) # Compute distance [m] and bearing [deg from city] d = city_location.distance_to(earthquake_location) b = city_location.bearing_to(earthquake_location) # Create string and update event_info if 5 <= b < 85: direction = 'Timur Laut' # North East elif 85 <= b < 95: direction = 'Timur' # East elif 95 <= b < 175: direction = 'Tenggara' # South East elif 175 <= b < 185: direction = 'Selatan' # South elif 185 <= b < 265: direction = 'Barat Daya' # South West elif 265 <= b < 275: direction = 'Barat' # West elif 275 <= b < 355: direction = 'Barat Laut' # North West else: direction = 'Utara' # North s = 'Berjarak %i km, %i$^\circ$ Arah %s %s' % (d/1000, b, direction, city_name) event_info['location_string'] = s
def testEarthquake2Muncar(self): """Distance and bearing of real example (quake -> Muncar) are correct """ # Test data from http://www.movable-type.co.uk/scripts/latlong.html D = 151318 # True Distance [m] B = 26 # 26 19 42 / 26 13 57 # Bearing to between points (start, end) p1 = Point(latitude=-9.65, longitude=113.72) d = p1.distance_to(self.Muncar) msg = 'Dist to Muncar failed %f. Expected %f' % (d, D) assert numpy.allclose(d, D), msg b = p1.bearing_to(self.Muncar) msg = 'Bearing to Muncar %i. Expected %i' % (b, B) assert b == B, msg
def test_generate_circle(self): """A circle with a given radius can be generated correctly """ # Generate a circle around Sydney airport with radius 3km radius = 3000 C = self.Syd.generate_circle(radius) # Check distance around the circle # Note that not every point will be exactly 3000m # because the circle in defined in geographic coordinates for c in C: p = Point(c[1], c[0]) d = self.Syd.distance_to(p) msg = ('Radius %f not with in expected tolerance. Expected %d' % (d, radius)) assert numpy.allclose(d, radius, rtol=2.0e-1), msg
class TestCase(unittest.TestCase): def setUp(self): self.eps = 0.001 # Accept 0.1 % relative error self.RSISE = Point(-35.27456, 149.12065) self.Home = Point(-35.25629, 149.12494) # 28 Scrivener Street, ACT self.Syd = Point(-33.93479, 151.16794) # Sydney Airport self.Nadi = Point(-17.75330, 177.45148) # Nadi Airport self.Kobenhavn = Point(55.70248, 12.58364) # Kobenhavn, Denmark self.Muncar = Point(-8.43, 114.33) # Muncar, Indonesia def testBearingNorth(self): """Bearing due north (0 deg) correct within double precision """ eps = 1.0e-12 p1 = Point(0.0, 0.0) p2 = Point(1.0, 0.0) b = p1.bearing_to(p2) msg = 'Computed northward bearing: %d, Should have been: %d' % (b, 0) assert numpy.allclose(b, 0, rtol=eps, atol=eps), msg def testBearingSouth(self): """Bearing due south (180 deg) is correct within double precision """ eps = 1.0e-12 B = 180 # True bearing p1 = Point(0.0, 0.0) p2 = Point(1.0, 0.0) b = p2.bearing_to(p1) msg = 'Computed southward bearing %d. Expected %d' % (b, B) assert numpy.allclose(b, B, rtol=eps, atol=eps), msg def testBearingEast(self): """Bearing due west (270 deg) is correct within double precision """ eps = 1.0e-12 B = 90 # True bearing p1 = Point(0.0, 0.0) p3 = Point(0.0, 1.0) b = p1.bearing_to(p3) msg = 'Computed southward bearing %d. Expected %d' % (b, B) assert numpy.allclose(b, B, rtol=eps, atol=eps), msg def testBearingWest(self): """Bearing due west (270 deg) is correct within double precision """ eps = 1.0e-12 B = 270 # True bearing p1 = Point(0.0, 0.0) p3 = Point(0.0, 1.0) b = p3.bearing_to(p1) msg = 'Computed southward bearing %d. Expected %d' % (b, B) assert numpy.allclose(b, B, rtol=eps, atol=eps), msg def testRSISE2Home(self): """Distance and bearing of real example (RSISE -> Home) are correct """ D = 2068.855 # True Distance to Home B = 11 # True Bearing to Home d = self.RSISE.distance_to(self.Home) msg = 'Dist from RSISE to Home %f. Expected %f' % (d, D) assert numpy.allclose(d, D, rtol=1.0e-6), msg b = self.RSISE.bearing_to(self.Home) msg = 'Bearing from RSISE to Home %i. Expected %i' % (b, B) assert b == B, msg def testRSISE2Sydney(self): """Distance and bearing of real example (RSISE -> Syd) are correct """ D = 239407.67 # True Distance to Sydney Airport B = 52 # True Bearing to Sydney Airport d = self.RSISE.distance_to(self.Syd) msg = 'Dist from RSISE to Sydney airport %f. Expected %f' % (d, D) assert numpy.allclose(d, D, rtol=1.0e-6), msg b = self.RSISE.bearing_to(self.Syd) msg = 'Bearing from RSISE to Sydney airport %i. Expected %i' % (b, B) assert b == B, msg def testRSISE2Nadi(self): """Distance and bearing of real example (RSISE -> Nadi) are correct """ D = 3406100 # True Distance to Nadi Airport B = 63 # True Bearing to Nadi Airport d = self.RSISE.distance_to(self.Nadi) msg = 'Dist from RSISE to Nadi airport %f. Expected %f' % (d, D) assert numpy.allclose(d, D, rtol=1.0e-4), msg b = self.RSISE.bearing_to(self.Nadi) msg = 'Bearing from RSISE to Nadi airport %i. Expected %i' % (b, B) assert b == B, msg def testRSISE2Kobenhavn(self): """Distance and bearing of real example (RSISE -> Kbh) are correct """ D = 16025 * 1000 # True Distance to Kobenhavn B = 319 # True Bearing to Kobenhavn d = self.RSISE.distance_to(self.Kobenhavn) msg = 'Dist from RSISE to Kobenhavn %f. Expected %f' % (d, D) assert numpy.allclose(d, D, rtol=1.0e-3), msg b = self.RSISE.bearing_to(self.Kobenhavn) msg = 'Bearing from RSISE to Nadi airport %i. Expected %i' % (b, B) assert b == B, msg def testEarthquake2Muncar(self): """Distance and bearing of real example (quake -> Muncar) are correct """ # Test data from http://www.movable-type.co.uk/scripts/latlong.html D = 151318 # True Distance [m] B = 26 # 26 19 42 / 26 13 57 # Bearing to between points (start, end) p1 = Point(latitude=-9.65, longitude=113.72) d = p1.distance_to(self.Muncar) msg = 'Dist to Muncar failed %f. Expected %f' % (d, D) assert numpy.allclose(d, D), msg b = p1.bearing_to(self.Muncar) msg = 'Bearing to Muncar %i. Expected %i' % (b, B) assert b == B, msg def test_equator_example(self): """Distance and bearing of real example (near equator) are correct """ # Test data from http://www.movable-type.co.uk/scripts/latlong.html D = 11448.0959593 # True Distance [m] p1 = Point(latitude=-0.59, longitude=117.10) p2 = Point(latitude=-0.50, longitude=117.15) d = p1.distance_to(p2) msg = 'Dist to point failed %f. Expected %f' % (d, D) assert numpy.allclose(d, D, rtol=1.0e-3), msg
class TestCase(unittest.TestCase): def setUp(self): self.eps = 0.001 # Accept 0.1 % relative error self.RSISE = Point(-35.27456, 149.12065) self.Home = Point(-35.25629, 149.12494) # 28 Scrivener Street, ACT self.Syd = Point(-33.93479, 151.16794) # Sydney Airport self.Nadi = Point(-17.75330, 177.45148) # Nadi Airport self.Kobenhavn = Point(55.70248, 12.58364) # Kobenhavn, Denmark self.Muncar = Point(-8.43, 114.33) # Muncar, Indonesia def testBearingNorth(self): """Bearing due north (0 deg) correct within double precision """ eps = 1.0e-12 p1 = Point(0.0, 0.0) p2 = Point(1.0, 0.0) b = p1.bearing_to(p2) msg = 'Computed northward bearing: %d, Should have been: %d' % (b, 0) assert numpy.allclose(b, 0, rtol=eps, atol=eps), msg def testBearingSouth(self): """Bearing due south (180 deg) is correct within double precision """ eps = 1.0e-12 B = 180 # True bearing p1 = Point(0.0, 0.0) p2 = Point(1.0, 0.0) b = p2.bearing_to(p1) msg = 'Computed southward bearing %d. Expected %d' % (b, B) assert numpy.allclose(b, B, rtol=eps, atol=eps), msg def testBearingEast(self): """Bearing due west (270 deg) is correct within double precision """ eps = 1.0e-12 B = 90 # True bearing p1 = Point(0.0, 0.0) p3 = Point(0.0, 1.0) b = p1.bearing_to(p3) msg = 'Computed southward bearing %d. Expected %d' % (b, B) assert numpy.allclose(b, B, rtol=eps, atol=eps), msg def testBearingWest(self): """Bearing due west (270 deg) is correct within double precision """ eps = 1.0e-12 B = 270 # True bearing p1 = Point(0.0, 0.0) p3 = Point(0.0, 1.0) b = p3.bearing_to(p1) msg = 'Computed southward bearing %d. Expected %d' % (b, B) assert numpy.allclose(b, B, rtol=eps, atol=eps), msg def testRSISE2Home(self): """Distance and bearing of real example (RSISE -> Home) are correct """ D = 2068.855 # True Distance to Home B = 11 # True Bearing to Home d = self.RSISE.distance_to(self.Home) msg = 'Dist from RSISE to Home %f. Expected %f' % (d, D) assert numpy.allclose(d, D, rtol=1.0e-6), msg b = self.RSISE.bearing_to(self.Home) msg = 'Bearing from RSISE to Home %i. Expected %i' % (b, B) assert b == B, msg def testRSISE2Sydney(self): """Distance and bearing of real example (RSISE -> Syd) are correct """ D = 239407.67 # True Distance to Sydney Airport B = 52 # True Bearing to Sydney Airport d = self.RSISE.distance_to(self.Syd) msg = 'Dist from RSISE to Sydney airport %f. Expected %f' % (d, D) assert numpy.allclose(d, D, rtol=1.0e-6), msg b = self.RSISE.bearing_to(self.Syd) msg = 'Bearing from RSISE to Sydney airport %i. Expected %i' % (b, B) assert b == B, msg def testRSISE2Nadi(self): """Distance and bearing of real example (RSISE -> Nadi) are correct """ D = 3406100 # True Distance to Nadi Airport B = 63 # True Bearing to Nadi Airport d = self.RSISE.distance_to(self.Nadi) msg = 'Dist from RSISE to Nadi airport %f. Expected %f' % (d, D) assert numpy.allclose(d, D, rtol=1.0e-4), msg b = self.RSISE.bearing_to(self.Nadi) msg = 'Bearing from RSISE to Nadi airport %i. Expected %i' % (b, B) assert b == B, msg def testRSISE2Kobenhavn(self): """Distance and bearing of real example (RSISE -> Kbh) are correct """ D = 16025 * 1000 # True Distance to Kobenhavn B = 319 # True Bearing to Kobenhavn d = self.RSISE.distance_to(self.Kobenhavn) msg = 'Dist from RSISE to Kobenhavn %f. Expected %f' % (d, D) assert numpy.allclose(d, D, rtol=1.0e-3), msg b = self.RSISE.bearing_to(self.Kobenhavn) msg = 'Bearing from RSISE to Nadi airport %i. Expected %i' % (b, B) assert b == B, msg def testEarthquake2Muncar(self): """Distance and bearing of real example (quake -> Muncar) are correct """ # Test data from http://www.movable-type.co.uk/scripts/latlong.html D = 151318 # True Distance [m] B = 26 # 26 19 42 / 26 13 57 # Bearing to between points (start, end) p1 = Point(latitude=-9.65, longitude=113.72) d = p1.distance_to(self.Muncar) msg = 'Dist to Muncar failed %f. Expected %f' % (d, D) assert numpy.allclose(d, D), msg b = p1.bearing_to(self.Muncar) msg = 'Bearing to Muncar %i. Expected %i' % (b, B) assert b == B, msg def test_equator_example(self): """Distance and bearing of real example (near equator) are correct """ # Test data from http://www.movable-type.co.uk/scripts/latlong.html D = 11448.0959593 # True Distance [m] p1 = Point(latitude=-0.59, longitude=117.10) p2 = Point(latitude=-0.50, longitude=117.15) d = p1.distance_to(p2) msg = 'Dist to point failed %f. Expected %f' % (d, D) assert numpy.allclose(d, D, rtol=1.0e-3), msg def test_generate_circle(self): """A circle with a given radius can be generated correctly """ # Generate a circle around Sydney airport with radius 3km radius = 3000 C = self.Syd.generate_circle(radius) # Check distance around the circle # Note that not every point will be exactly 3000m # because the circle in defined in geographic coordinates for c in C: p = Point(c[1], c[0]) d = self.Syd.distance_to(p) msg = ('Radius %f not with in expected tolerance. Expected %d' % (d, radius)) assert numpy.allclose(d, radius, rtol=2.0e-1), msg
def city_info(R, B, path, eve_info): """List cities exposed sorted by MMI experienced Input R: Bounding box of interest B: MMI point values from shakemap path: Location of library data eve_info: Event info (in this case get epicentre) """ # Load raw city information path = path + "/cities/Indonesia.txt" A = np.loadtxt(path, dtype={"names": ("lon", "lat", "pop", "name"), "formats": (float, float, float, "S10")}) # Mask of those cities that are inside region of interest (1D boolean array) mask = (A["lon"] > R[0]) & (A["lon"] < R[1]) & (A["lat"] > R[2]) & (A["lat"] < R[3]) # Get indices of cities in region of interest (1D array of integers) index = np.nonzero(mask)[0] # Get single array from tuple output if len(index) == 0: # No cities were found in region of interest. # Search entire database for the nearest city eve_lon = float(eve_info["lon"]) eve_lat = float(eve_info["lat"]) min_dist = sys.maxint for i in range(len(A)): start = Point(latitude=A["lat"][i], longitude=A["lon"][i]) end = Point(latitude=eve_lat, longitude=eve_lon) dis = start.distance_to(end) / 1000 if dis < min_dist: min_dist = dis index = i # Assign MMI 0 to nearest city outside region of interest A = A[index] city = [(A["name"], A["pop"], 1, A["lon"], A["lat"])] else: # One or more cities were found in region of interest A = A[index] # Create grid of MMI values (FIXME: More explanation in here please) points = B[:, 0:2] values = B[:, 4] I = make_grid(points, values, (A["lon"], A["lat"])) # Create city list with entries: Name, population, MMI, lon, lat intensity = I.tolist() name = A["name"].tolist() pop = A["pop"].tolist() lon = A["lon"].tolist() lat = A["lat"].tolist() city = zip(name, pop, intensity, lon, lat) # Convert to array and sort by intensity # (and population if intensity is the same) city = np.array(city, dtype=city_dtype) city = np.sort(city, order=["intensity", "population"]) city = np.flipud(city) return city