def get_min_height(self, lat_ll, lon_ll, lat_ur, lon_ur): result = { 'location_min': [], 'h_min': self.NODATA, 'counter_min': 0, 'source': self.attribution_name, 'attributions': [self.attribution] } if not (-90 <= lat_ll <= 90 and -180 <= lon_ll <= 180 and -90 <= lat_ur <= 90 and -180 <= lon_ur <= 180): raise ValueError('invalid coordinates ({}, {}), ({}, {})'.format( lat_ll, lon_ll, lat_ur, lon_ur)) # consider only correctly defined rectangle: if lat_ll > lat_ur or lon_ll > lon_ur: return result try: osgr_ll = toOsgr(eV.LatLon(lat_ll, lon_ll)) if len(osgr_ll.toStr()) == 0: raise ValueError('not a valid OSGR coordinate') except ValueError: return result try: osgr_ur = toOsgr(eV.LatLon(lat_ur, lon_ur)) if len(osgr_ur.toStr()) == 0: raise ValueError('not a valid OSGR coordinate') except ValueError: return result file_list = {} try: self.create_filelist(osgr_ll, osgr_ur, file_list) except IOError: return result result.update(self.check_min_files(self.filter_min_list(file_list))) return result
def test_ll_to_osgr_to_ll(): # location on the Isles of Scilly lat = 49.926244 lon = -6.297934 ll_orig = eV.LatLon(lat, lon) osgr = toOsgr(ll_orig) ll_osgr = osgr.toLatLon(eV.LatLon) assert ll_orig.distanceTo(ll_osgr) < 1 parsed_osgr = parseOSGR(str(osgr)) ll_parsed_osgr = parsed_osgr.toLatLon(eV.LatLon) assert ll_orig.distanceTo(ll_parsed_osgr) < 1 osgr_new = Osgr(osgr.easting, osgr.northing) ll_osgr_new = osgr_new.toLatLon(eV.LatLon) assert ll_orig.distanceTo(ll_osgr) < 1
def get_height(self, lat, lon): if not (-90 <= lat <= 90 and -180 <= lon <= 180): raise ValueError('invalid coordinates ({}, {})'.format(lat, lon)) result = { 'altitude_m': self.NODATA, 'source': self.attribution_name, 'lat': lat, 'lon': lon, 'distance_m': 0, 'attributions': [self.attribution] } if lat < 49.7 or lat > 62 or lon < -10 or lon > 4: return result try: osgr = toOsgr(eV.LatLon(lat, lon)) if len(osgr.toStr()) == 0: raise ValueError('not a valid OSGR coordinate') except ValueError: return result # fit request to the grid osgr = osgr_to_grid(osgr) latlon = osgr.toLatLon(eV.LatLon) lat_found = latlon.lat lon_found = latlon.lon filename = get_filename(osgr) full_path = os.path.join(self.path, filename[:2].lower(), filename) if not os.path.isfile(full_path): return result x = get_x(osgr) y = get_y(osgr) with open(full_path, "rb") as f: # go to the right spot, f.seek((y * NCOLS + x) * 4) # read four bytes and convert them: buf = f.read(4) # ">f" is a four byte float val = struct.unpack('>f', buf)[0] result.update({ 'lat_found': round(lat_found, 6), 'lon_found': round(lon_found, 6), 'altitude_m': round(val, 2), 'distance_m': round(calculate_distance(lat, lon, lat_found, lon_found), 3) }) return result