def test_input_coordinates(): """Make sure code handles lat or lon falling outside U.S. bounds.""" ident, good_lat, good_lon = 2, 42.7284, -84.5026 bad_lat = 17.4 bad_lon = -63.9 good_buffers = [0, 10, 2000] for buffer in good_buffers: # Good buffer, good lat, lon should have status=1 us_bounds_test = nhd_hr.HighResPoint(ident, good_lat, good_lon, buffer_m=buffer) assert us_bounds_test.status == 1 and us_bounds_test.message == '' m = f'Coordinates for id: {ident} are outside of the bounding box of the United States.' us_bounds_test = nhd_hr.HighResPoint(ident, bad_lat, good_lon, buffer_m=buffer) assert us_bounds_test.status == 0 and us_bounds_test.message == m # bad lon should set status to 0 (fail) us_bounds_test = nhd_hr.HighResPoint(ident, good_lat, bad_lon, buffer_m=buffer) assert us_bounds_test.status == 0 and us_bounds_test.message == m
def test_input_buffer(): """Tests to ensure buffer logic is working properly.""" good_buffers = [0, 10, 2000] for buffer in good_buffers: # Good buffer, good lat, lon should have status=1 buffer_test = nhd_hr.HighResPoint(ident, good_lat, good_lon, buffer_m=buffer) assert buffer_test.status == 1 and buffer_test.message == '' # buffer greater than 2000 should set status to 0 (fail) bad_buffers = [2001, 3000] for buffer in bad_buffers: buffer_test = nhd_hr.HighResPoint(ident, good_lat, good_lon, buffer_m=buffer) m = 'Maximum buffer is 2000 meters, reduce buffer.' assert buffer_test.status == 0 and buffer_test.message == m
def handle_data(input_file, latitude_field, longitude_field, stream_name_field, identifier_field, crs, buffer, method, nhd_version, hydro_type): """Hydrolink point data to the nhd high resolution. First set appropriate Python environment. Run HydroLinker. Examples included below. python hydrolinker.py >>> runs with defaults python hydrolinker.py --input_file=file_name.csv >>> runs with specified file name and default other values Returns >>> writes hydrolinked (hydro addressed information to csv file) """ click.echo('Thank you, processing now...') in_data = {"file": input_file, "lat": latitude_field, "lon": longitude_field, "stream_name": stream_name_field, "id": identifier_field} if in_data['file'].endswith('.csv'): click.echo('reading csv file') try: df = pd.read_csv(in_data['file'], encoding='iso-8859-1') except KeyError: click.echo('csv file did not properly import, verify file name and rerun') # If input file is not a CSV check to see if it is a shapefile elif in_data['file'].endswith('.shp'): click.echo('reading shapefile') try: df = gpd.GeoDataFrame.from_file(in_data['file']) except KeyError: click.echo('shapefile did not properly import, verify file name and rerun') # If input file is not a CSV or shapefile tell the user that the file type is not excepted else: click.echo('File type not currently accepted. Please try .csv or .shp') if in_data['lat'] in df and in_data['lon'] in df and in_data['id'] in df and in_data['stream_name'] in df: df = df.rename(columns={in_data['id']: 'id', in_data['lat']: 'lat', in_data['lon']: 'lon', in_data['stream_name']: 'stream' }) df['crs'] = int(crs) else: click.echo('Verify field names and rerun') for row in df.itertuples(): if nhd_version == 'nhdhr': hydrolink = nhd_hr.HighResPoint(row.id, float(row.lat), float(row.lon), input_crs=int(row.crs), water_name=str(row.stream), buffer_m=buffer) elif nhd_version == 'nhdplusv2': hydrolink = nhd_mr.HighResPoint(row.id, float(row.lat), float(row.lon), input_crs=int(row.crs), water_name=str(row.stream), buffer_m=buffer) hydrolink.hydrolink_method(method=method, hydro_type=hydro_type)
def handle_data(input_file, latitude_field, longitude_field, stream_name_field, identifier_field, crs, buffer, method, nhd_version, hydro_type): """Hydrolink point data to the nhd high resolution. HydroLinker accepts a CSV file of multiple points of interest, HydroLinks each to the specified version of NHD and writes HydroLink data (addresses to NHD) along with measures of certainty to a csv file. """ click.echo('Thank you, processing now...') in_data = { "file": input_file, "lat": latitude_field, "lon": longitude_field, "stream_name": stream_name_field, "id": identifier_field } if in_data['file'].endswith('.csv'): click.echo('reading csv file') try: df = pd.read_csv(in_data['file'], encoding='iso-8859-1') except KeyError: click.echo( 'csv file did not properly import, verify file name and rerun') # If input file is not a CSV check to see if it is a shapefile elif in_data['file'].endswith('.shp'): click.echo('reading shapefile') try: df = gpd.GeoDataFrame.from_file(in_data['file']) except KeyError: click.echo( 'shapefile did not properly import, verify file name and rerun' ) # If input file is not a CSV or shapefile tell the user that the file type is not excepted else: click.echo('File type not currently accepted. Please try .csv or .shp') if in_data['lat'] in df and in_data['lon'] in df and in_data[ 'id'] in df and in_data['stream_name'] in df: df = df.rename( columns={ in_data['id']: 'id', in_data['lat']: 'lat', in_data['lon']: 'lon', in_data['stream_name']: 'stream' }) df['crs'] = int(crs) else: click.echo('Verify field names and rerun') for row in df.itertuples(): if nhd_version == 'nhdhr': hydrolink = nhd_hr.HighResPoint(row.id, float(row.lat), float(row.lon), input_crs=int(row.crs), water_name=str(row.stream), buffer_m=buffer) elif nhd_version == 'nhdplusv2': hydrolink = nhd_mr.HighResPoint(row.id, float(row.lat), float(row.lon), input_crs=int(row.crs), water_name=str(row.stream), buffer_m=buffer) hydrolink.hydrolink_method(method=method, hydro_type=hydro_type)
# !/usr/bin/env python """Tests for `utils` package.""" import pytest from hydrolink import nhd_hr import requests import validators # Create test point that can be used for tests ident, good_lat, good_lon = 2, 42.7284, -84.5026 test_point = nhd_hr.HighResPoint(ident, good_lat, good_lon) def test_build_nhd_query(): """Validate structure of url.""" # test hem_flowline and hem_waterbody test_point.build_nhd_query(query=['hem_flowline', 'hem_waterbody']) assert validators.url(test_point.flowline_query) assert validators.url(test_point.waterbody_query) # test hem_waterbody_flowline test_point.hydrolink_waterbody = {} test_point.hydrolink_waterbody['nhdhr waterbody permanent identifier'] = '88894713' test_point.build_nhd_query(query=['hem_waterbody_flowline']) assert validators.url(test_point.flowline_query) def test_input_buffer(): """Tests to ensure buffer logic is working properly.""" good_buffers = [0, 10, 2000]