gbif / pygbif

GBIF Python client
https://pygbif.readthedocs.io/en/latest/
MIT License
110 stars 29 forks source link

Download with geometry parameter #81

Closed nmtarr closed 3 years ago

nmtarr commented 3 years ago

Hello,

I can't seem to figure out how to run occurrences.download() with a geometry parameter successfully. I've tried enough tweaks to get to the point of wondering if there's a bug. Anyone got any advice?

I'm using Python 3.7.10 and pygbif 0.5.0. Here is my code; it works without the geometry parameter:

------------------------------------------------

from pygbif import occurrences

username = 'xxxxxx' password = 'xxxxxx' email = 'xxxxxx'

download_filters = ["taxonKey = 2496287", "geometry within 'POLYGON ((-82.7 36.9, -85.0 35.6, -81.0 33.5, -79.4 36.3, -79.4 36.3, -82.7 36.9))'"]

download = occurrences.download(download_filters, pred_type='and', user = username, pwd = password, email = email)

-------------------------------------------------

And here is the error message:

Exception: error: JSON parse error: Cannot construct instance of org.gbif.api.model.occurrence.predicate.WithinPredicate, problem: may not be null; nested exception is com.fasterxml.jackson.databind.exc.ValueInstantiationException: Cannot construct instance of org.gbif.api.model.occurrence.predicate.WithinPredicate, problem: may not be null at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: org.gbif.api.model.occurrence.predicate.ConjunctionPredicate["predicates"]->java.util.ArrayList[1]), with error status code 400check your number of active downloads.

Regards,

Nathan

sckott commented 3 years ago

thanks, on leave until may 3, will have a look then

sckott commented 3 years ago

Wasn't quite sure, had to look this one up, see https://github.com/sckott/pygbif/blob/master/test/test-occurrences-download_request.py#L83

Seems a little bit different from other queries, have to use GbifDownload to construct a class first, then add_geometry

e.g.,

req = GbifDownload("name", "email")
req.add_geometry(
    "POLYGON((-14.06 42.55, 9.84 38.27, -7.03 26.43, -14.06 42.55))"
)
req.post_download()
nmtarr commented 3 years ago

Scott,

Thank you for looking into this. This is my first time submitting an issue that required edits on the part of a contributor. How should I proceed? Download and test the development version and confirm that the error was fixed for my application?

Nathan

On Thu, May 6, 2021 at 7:52 PM Scott Chamberlain @.***> wrote:

Wasn't quite sure, had to look this one up, see https://github.com/sckott/pygbif/blob/master/test/test-occurrences-download_request.py#L83

Seems a little bit different from other queries, have to use GbifDownload to construct a class first, then add_geometry

e.g.,

req = GbifDownload("name", "email")req.add_geometry( "POLYGON((-14.06 42.55, 9.84 38.27, -7.03 26.43, -14.06 42.55))" )req.post_download()

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/sckott/pygbif/issues/81#issuecomment-833952948, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEMYMOGPKHJED7VTBEEYTSLTMMTSHANCNFSM43NL7V7Q .

-- Nathan Tarr Research Scholar NC Cooperative Fish and Wildlife Research Unit | Department of Applied Ecology NC State University website https://nmtarr.wordpress.ncsu.edu/

All electronic mail messages in connection with State business which are sent to or received by this account are subject to the NC Public Records Law and may be disclosed to third parties.

sckott commented 3 years ago

@nmtarr Yes, download dev version from github pip install git+git://github.com/sckott/pygbif.git#egg=pygbif

nmtarr commented 3 years ago

@sckott I downloaded the dev version, but I'm still getting the error as described above. Can someone please confirm that my geometry parameter should indeed be formatted like this:

"geometry within 'POLYGON ((-82.7 36.9, -85.0 35.6, -81.0 33.5, -79.4 36.3, -79.4 36.3, -82.7 36.9))'"

I'm not confident that I've got it right.

Thanks for the help.

sckott commented 3 years ago

The string you supply to add_geometry should just be the WKT itself, without the geometry within part - the geometry within gets added internally. Does that make sense?

nmtarr commented 3 years ago

@sckott Thanks. That makes sense for use of add_geometry. I would really like to be able to include a geometry parameter with occurrences.download. The documentation for that function states that geometry can be included. Is that not actually the case?

sckott commented 3 years ago

Got it now, thanks for clarifying.

Okay, just pushed a change on branch geometry-predicate. The download predicate logic was splitting strings on spaces, which doesn't work for geometry predicate since you can't remove spaces from a WKT string. Should work now after reinstalling from github. Let me know if works.

nmtarr commented 3 years ago

It is working for me now. Thanks for addressing the issue!

sckott commented 3 years ago

Awesome, glad it works now.

i'll PR the change to the main branch and seek a bit of feedback first from other contribs