DSHydro / planetsca

https://dshydro.github.io/planetsca/
MIT License
0 stars 2 forks source link

API key errors in api_search #7

Closed spestana closed 4 months ago

spestana commented 5 months ago

Currently using data_gathering.api_search with a bad API key does not alert the user that the Planet API has returned an error message (a 401 response).

Instead, a user would not encounter an error until trying to extract geometry features from the returned json:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[10], line 6
      4 print(domain_geometry)
      5 result = dg.api_search(item_type, apiKey)
----> 6 geojson_data, gdf = dg.downloadable_PlanetIDs(result, domain_geometry)
      7 id_list = dg.id_gemoetry_lists(geojson_data, gdf)
      8 order_urls = dg.submit_orders(id_list, item_type, bundle_type, apiKey)

File ~/git/PyPlanetSCA-Python-Library/src/planetsca/data_gathering.py:175, in downloadable_PlanetIDs(result, domain_geometry)
    173 def downloadable_PlanetIDs(result, domain_geometry):
    174     geojson_data = result.json()
--> 175     gdf = gpd.GeoDataFrame.from_features(geojson_data["features"])
    177     # Add a new column to 'gdf' with the intersection area
    178     gdf["intersection_area"] = gdf["geometry"].intersection(domain_geometry).area

KeyError: 'features'

We should add a feature to the api_search function so that it will alert the user if it returns anything other than a 200 response.

spestana commented 5 months ago

For example, this would print out the json response (and any error messages from the planet API) if the response code isn't OK:

if result.status_code != 200:
    print("Planet API response not OK: ", result.json())
IanChiu-333 commented 5 months ago

Make two error messages to catch no API and incorrect API

IanChiu-333 commented 4 months ago

Just finished this and merged into main. There's two catches before the main code is run for if there is no API or the API is not a string and using an invalid API.