Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

occurrence search returns observation outside specified polygon provided in geometry parameter #46

Closed
aubreymoore opened this issue Jan 4, 2019 · 5 comments
Milestone

Comments

@aubreymoore
Copy link

When I search for Oryctes rhinoceros within a bounding box for Guam, I get an occurrence record for the Philippines. Using python 2.7/pygbif 0.2.0.

from pygbif import occurrences as occ
occ.search(scientificName='Oryctes rhinoceros',
geometry='POLYGON((144.61 13.22, 144.61 13.66, 144.96 13.66, 144.96 13.22, 144.61 13.22))',
limit=1)

Returns:

{u'count': 3,
u'endOfRecords': False,
u'facets': [],
u'limit': 1,
u'offset': 0,
u'results': [{u'acceptedScientificName': u'Oryctes rhinoceros (Linnaeus, 1758)',
u'acceptedTaxonKey': 4995642,
u'basisOfRecord': u'HUMAN_OBSERVATION',
u'catalogNumber': u'4864787',
u'class': u'Insecta',
u'classKey': 216,
u'collectionCode': u'Observations',
u'coordinateUncertaintyInMeters': 88056.0,
u'country': u'Philippines',
u'countryCode': u'PH',
u'crawlId': 141,
u'datasetKey': u'50c9509d-22c7-4a22-a47d-8c48425ef4a7',
u'datasetName': u'iNaturalist research-grade observations',
u'dateIdentified': u'2016-12-30T15:01:30',
u'day': 23,
u'decimalLatitude': 13.4756,
u'decimalLongitude': 120.851388,
u'eventDate': u'2016-12-23T04:00:00',
...

_

@sckott
Copy link
Collaborator

sckott commented Jan 4, 2019

thanks for the report @aubreymoore having a look

@sckott
Copy link
Collaborator

sckott commented Jan 4, 2019

looksl like your WKT has the wrong winding direction.

GBIF docs have for the geometry parameter https://www.gbif.org/developer/occurrence#p_geometry

I'll update the docs on this.

If you reverse the winding order of your WKT it works as expected I think:

geom_rev="POLYGON((144.61 13.22,144.96 13.22,144.96 13.66,144.61 13.66,144.61 13.22))"
res2 = occ.search(scientificName='Oryctes rhinoceros', geometry=geom_rev, limit=10)
[ z['country'] for z in res2['results'] ]

['Guam',
 'Guam',
 'Guam',
 'Guam',
 'Guam',
 'Guam',
 'Guam',
 'Guam',
 'Guam',
 'Guam']
[ [z['decimalLatitude'], z['decimalLongitude']] for z in res2['results'] ]

[[13.491572, 144.845642],
 [13.430427, 144.801009],
 [13.474637, 144.783201],
 [13.430502, 144.800539],
 [13.511599, 144.803081],
 [13.427608, 144.788581],
 [13.521747, 144.879896],
 [13.588958, 144.867862],
 [13.49438, 144.776794],
 [13.493889, 144.77417]]

I haven't found an easy Python lib for reversing winding order, but in R, you could

install.packages("wicket")
x = 'POLYGON((144.61 13.22, 144.61 13.66, 144.96 13.66, 144.96 13.22, 144.61 13.22))'
wicket::wkt_reverse(x)

@sckott sckott added this to the v0.3 milestone Jan 4, 2019
@aubreymoore
Copy link
Author

Thanks for the explanation. And thanks for maintaining this useful package.

@sckott
Copy link
Collaborator

sckott commented Jan 5, 2019

okay, this should do it:

from geojson_rewind import rewind
from geomet import wkt
import decimal
import statistics

# from https://stackoverflow.com/a/12472564/1091766
def flatten(S):
    if S == []:
        return S
    if isinstance(S[0], list):
        return flatten(S[0]) + flatten(S[1:])
    return S[:1] + flatten(S[1:])

def wkt_rewind(x):
    '''
    change WKT winding order

    :param x: WKT string

    Usage::

        x = 'POLYGON((144.61 13.22, 144.61 13.66, 144.96 13.66, 144.96 13.22, 144.61 13.22))'
        wkt_rewind(x)
    '''
    z = wkt.loads(x)
    coords = z['coordinates']
    nums = flatten(coords)
    dec_n = [ decimal.Decimal(str(w)).as_tuple().exponent for w in nums ]
    digits = abs(statistics.mean(dec_n))
    wounded = rewind(z)
    back_to_wkt = wkt.dumps(wounded, decimals = digits)
    return back_to_wkt

@sckott
Copy link
Collaborator

sckott commented Jan 5, 2019

the function is now in the library if you reinstall

@sckott sckott closed this as completed Jan 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants