eodms-sgdot / py-eodms-rapi

EODMS RAPI Client is a Python3 package used to access the REST API service provided by the Earth Observation Data Management System (EODMS) from Natural Resources Canada.
Other
13 stars 4 forks source link

Geometry filtering query not interpreting spatial predicate #32

Closed peanutbutter-memory closed 1 year ago

peanutbutter-memory commented 1 year ago

Attempting to replicate EODMS GUI search via Python API.

Can query collections and available fields and choices.

Running into issue when attempting to use geometry filter (was not able to run without a geometry filter). The spatial predicate within or intersects seems to not be parsed correctly; tracebacks refer to the second letter of each predicate I have tried. Attempted to contain predicate in a list but that failed too.

Windows 11 running WSL 2 Ubuntu 22.04 with Python 3.11. Latest install of py-eodms-rapi.

import os

from dotenv import load_dotenv
from eodms_rapi import EODMSRAPI

load_dotenv()

username=os.getenv("EODMS_EMAIL")
password=os.getenv("EODMS_PASSWORD")

rapi = EODMSRAPI(username, password)

#%%
desired_product = 'WorldView-3'

# List all products available to user
#print(rapi.get_collections(as_list=True))

dates = [{"start": "20230501_000000", "end": "20230823_000000"}]
filter = {"Sensor Mode": ["PSH"]}

geographic_area = ("intersects", {"type": "Polygon",
        "coordinates": [
          [
            [
              -80.98348190613918,
              70.55936098953137
            ],
            [
              -128.08040511581584,
              73.66999943625751
            ],
            [
              -138.38572103373428,
              68.87269436214862
            ],
            [
              -124.16519435207523,
              67.7870395850222
            ],
            [
              -114.69037016788643,
              66.69986515771265
            ],
            [
              -99.25854956956725,
              66.11648758438096
            ],
            [
              -83.04517551683422,
              65.48014739072471
            ],
            [
              -75.59319320583784,
              67.65281907013141
            ],
            [
              -80.98348190613918,
              70.55936098953137
            ]
          ]
        ]

      }
)

# List all fields and choices
#print(rapi.print_results(rapi.get_available_fields(desired_product)))
#print(rapi.print_results(rapi.get_field_choices(desired_product)))

# Submit a search
rapi.search(desired_product, filters=filter, features=geographic_area, dates=dates)
results = rapi.get_results()
# Pretty print of results for human reading:)
rapi.print_results(results)
peanutbutter-memory commented 1 year ago

Traceback

NameError                                 Traceback (most recent call last)
Cell In[39], line 59
      9 geographic_area = ("intersects", {"type": "Polygon",
     10         "coordinates": [
     11           [
   (...)
     51       }
     52 )
     54 # List all fields and choices
     55 #print(rapi.print_results(rapi.get_available_fields(desired_product)))
     56 #print(rapi.print_results(rapi.get_field_choices(desired_product)))
     57 
     58 # Submit a search
---> 59 rapi.search(desired_product, filters=filter, features=geographic_area, dates=dates)
     60 results = rapi.get_results()
     61 # Pretty print of results for human reading:)

File ~/.virtualenvs/tc-nast/lib/python3.11/site-packages/eodms_rapi/eodms.py:3091, in EODMSRAPI.search(self, collection, filters, features, dates, result_fields, max_results)
   3088 params = {'collection': self.collection}
   3090 if filters is not None or features is not None or dates is not None:
-> 3091     params['query'] = self._parse_query(filters, features, dates)
   3092     if self.err_occurred:
   3093         return None

File ~/.virtualenvs/tc-nast/lib/python3.11/site-packages/eodms_rapi/eodms.py:1120, in EODMSRAPI._parse_query(self, filters, feats, dates)
   1117 op = f[0].upper()
   1118 src = f[1]
-> 1120 self.geoms = self.geo.add_geom(src)
   1122 if self.geoms is None or isinstance(self.geoms, SyntaxError):
   1123     self.msg = f"Geometry feature #{str(idx + 1)} could " \
   1124             f"not be determined. Excluding it from search."

File ~/.virtualenvs/tc-nast/lib/python3.11/site-packages/eodms_rapi/geo.py:316, in EODMSGeo.add_geom(self, in_src)
    314 if not isinstance(in_src, list):
    315     try:
--> 316         eval(in_src)
    317     except SyntaxError as err:
    318         self.logger.warning(f"{err}")

File <string>:1

NameError: name 'n' is not defined
keballantyne commented 1 year ago

Fixed in release v1.5.6 (https://github.com/eodms-sgdot/py-eodms-rapi/commit/175a11239af061c7b83e00a20a497eb39ebe6955)