pelias / polygon-lookup

Fast point-in-polygon intersection for large numbers of polygons.
72 stars 17 forks source link

method search returns undefined #47

Closed jfoclpf closed 3 years ago

jfoclpf commented 3 years ago

Describe the bug

method search is returning undefined

Steps to Reproduce

const lookupDistritos = new PolygonLookup(geojson.distritos)
const poly = lookupDistritos.search(40.233412, -8.135783) // some place in Portugal
console.log(poly) // undefined

I cannot put here all the FeatureCollection geojson.distritos but it's well defined

{
  type: 'FeatureCollection',
  features: [
    { type: 'Feature', properties: [Object], geometry: [Object] },
    { type: 'Feature', properties: [Object], geometry: [Object] },
    { type: 'Feature', properties: [Object], geometry: [Object] },
    { type: 'Feature', properties: [Object], geometry: [Object] },
    { type: 'Feature', properties: [Object], geometry: [Object] },
    { type: 'Feature', properties: [Object], geometry: [Object] },
    { type: 'Feature', properties: [Object], geometry: [Object] },
    { type: 'Feature', properties: [Object], geometry: [Object] },
    { type: 'Feature', properties: [Object], geometry: [Object] },
    { type: 'Feature', properties: [Object], geometry: [Object] },
    { type: 'Feature', properties: [Object], geometry: [Object] },
    { type: 'Feature', properties: [Object], geometry: [Object] },
    { type: 'Feature', properties: [Object], geometry: [Object] },
    { type: 'Feature', properties: [Object], geometry: [Object] },
    { type: 'Feature', properties: [Object], geometry: [Object] },
    { type: 'Feature', properties: [Object], geometry: [Object] },
    { type: 'Feature', properties: [Object], geometry: [Object] },
    { type: 'Feature', properties: [Object], geometry: [Object] },
    { type: 'Feature', properties: [Object], geometry: [Object] },
    { type: 'Feature', properties: [Object], geometry: [Object] }
  ],
  bbox: [
    -31.26818656921381,
    30.03017616271984,
    -6.189142227172852,
    42.15431594848633
  ]
}

I should mention that each polygon has a very large number of sides, here a console.log of geojson.distritos.features[0].geometry.coordinates:

[
  [
    [ -8.14205360412592, 39.015289306640625 ],
    [ -8.141647338867188, 39.014663696289176 ],
    [ -8.141536712646428, 39.01450729370123 ],
    [ -8.141264915466309, 39.01403427124018 ],
    [ -8.140393257141056, 39.01247787475597 ],
    [ -8.139412879943848, 39.01243209838873 ],
    [ -8.13868522644043, 39.012397766113395 ],
    [ -8.136157989501953, 39.01227188110363 ],
    [ -8.134222030639592, 39.01218032836914 ],
    [ -8.132843017578068, 39.01210403442394 ],
    [ -8.132230758666935, 39.012130737304744 ],
    [ -8.128972053527775, 39.012218475341854 ],
    [ -8.128927230834961, 39.01230621337896 ],
    [ -8.129116058349553, 39.01317214965826 ],
    [ -8.12927055358881, 39.013931274414176 ],
    [ -8.129427909851074, 39.014736175537166 ],
    [ -8.129475593566895, 39.014923095703125 ],
    [ -8.129505157470646, 39.015029907226676 ],
    [ -8.129521369934025, 39.015182495117244 ],
    [ -8.129620552062931, 39.015636444091854 ],
    [ -8.12977027893055, 39.016265869140625 ],
    [ -8.129656791687012, 39.017257690429744 ],
    [ -8.129491806030273, 39.01791381835949 ],
    [ -8.12945365905756, 39.017986297607536 ],
    [ -8.129377365112248, 39.01810073852539 ],
    [ -8.129329681396428, 39.0181884765625 ],
    [ -8.129301071166992, 39.01828384399414 ],
    [ -8.129295349121094, 39.0183677673341 ],
    [ -8.129305839538517, 39.018436431884766 ],
    [ -8.129344940185547, 39.01854324340826 ],
    [ -8.129406929016113, 39.018760681152344 ],
    [ -8.129494667053223, 39.01899719238287 ],
    [ -8.129594802856445, 39.019218444824276 ],
    [ -8.12964153289795, 39.019321441650504 ],
    [ -8.129682540893555, 39.01945114135748 ],
    [ -8.129723548889103, 39.01960372924805 ],
    [ -8.129754066467285, 39.01969909667969 ],
    [ -8.129792213439941, 39.0197372436524 ],
    [ -8.129854202270451, 39.019767761230526 ],
    [ -8.129933357238713, 39.01981353759777 ],
    [ -8.129988670349121, 39.01987838745123 ],
    [ -8.130009651184025, 39.019943237304744 ],
    [ -8.130050659179688, 39.02008056640625 ],
    [ -8.130094528198242, 39.020221710205135 ],
    [ -8.130142211914062, 39.0203208923341 ],
    [ -8.13020133972168, 39.0203742980957 ],
    [ -8.130248069763127, 39.0204238891601 ],
    [ -8.130318641662484, 39.02052307128912 ],
    [ -8.130387306213322, 39.02063369750982 ],
    [ -8.13042163848877, 39.0207138061524 ],
    [ -8.130428314208984, 39.02077484130871 ],
    [ -8.130418777465763, 39.020839691162166 ],
    [ -8.130390167236214, 39.0209007263183 ],
    [ -8.130345344543457, 39.02098846435558 ],
    [ -8.130345344543457, 39.02104187011719 ],
    [ -8.130356788635254, 39.02107620239269 ],
    [ -8.130448341369629, 39.021202087402344 ],
    [ -8.130536079406738, 39.02130889892584 ],
    [ -8.130598068237248, 39.0214195251466 ],
    [ -8.130610466003418, 39.02151489257824 ],
    [ -8.130636215209961, 39.02163314819347 ],
    [ -8.130719184875488, 39.02168655395508 ],
    [ -8.130887031555119, 39.02177429199219 ],
    [ -8.130925178527832, 39.021831512451286 ],
    [ -8.130966186523438, 39.021930694580135 ],
    [ -8.131002426147404, 39.02203369140631 ],
    [ -8.13101577758789, 39.02220535278332 ],
    [ -8.131012916564941, 39.022365570068416 ],
    [ -8.131010055541879, 39.02266693115246 ],
    [ -8.130971908569336, 39.022994995117244 ],
    [ -8.130330085754395, 39.02309417724604 ],
    [ -8.128132820129395, 39.02361297607433 ],
    [ -8.126084327697754, 39.02368927001953 ],
    [ -8.125396728515568, 39.02375411987316 ],
    [ -8.124922752380314, 39.02383041381836 ],
    [ -8.12420654296875, 39.023891448974666 ],
    [ -8.122885704040527, 39.023788452148494 ],
    [ -8.122533798217773, 39.02375793457037 ],
    [ -8.122452735900879, 39.02368545532232 ],
    [ -8.121947288513127, 39.0234260559082 ],
    [ -8.121493339538517, 39.02330017089844 ],
    [ -8.121050834655762, 39.023067474365234 ],
    [ -8.120573043823242, 39.02286529541027 ],
    [ -8.120137214660645, 39.02263259887695 ],
    [ -8.119890213012695, 39.02257156372082 ],
    [ -8.119201660156193, 39.02259826660162 ],
    [ -8.118721008300724, 39.02257919311535 ],
    [ -8.117910385131779, 39.022552490234375 ],
    [ -8.117260932922363, 39.02243804931646 ],
    [ -8.116511344909611, 39.02239990234375 ],
    [ -8.116023063659668, 39.02224731445318 ],
    [ -8.115469932556096, 39.022098541259766 ],
    [ -8.114905357360783, 39.0220565795899 ],
    [ -8.114327430725098, 39.022010803222656 ],
    [ -8.113201141357365, 39.02175903320307 ],
    [ -8.112980842590332, 39.02170181274414 ],
    [ -8.112521171569767, 39.021583557128906 ],
    [ -8.111663818359318, 39.02172088623058 ],
    [ -8.110865592956543, 39.021724700927734 ],
    [ -8.110321998596191, 39.021724700927734 ],
    ... 18584 more items
  ]
]

Expected behavior

I wanted the polygons in which the point lies in

Environment:

$ node -v
v14.16.1
$ npm -v
7.9.0
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.5 LTS
Release:    18.04
Codename:   bionic
jfoclpf commented 3 years ago

The feature collection had swapped lat and long and thus the result was empty, maybe just a tip, if the result is undefined it could be more verbose just letting the user to know that console.log('no polygon found for that point')