How can I look up a polygon that contains a point fast, given all polygons are a partition?









up vote
0
down vote

favorite












I have a function



get_polygon(polygon_collection, point):
for polygon in polygon_collection:
if polygon.intersects(point):
return polygon
return None


This approach works, but it is in O(n) * O(single polygon check). This can for sure be reduced to O(log(n)) * O(single polygon check) if one builds a tree data structure.



Does shapely support that directly?



Structure of polyogon collection



  • The collection contains n MultiPolygons - hence one object can consist of many polygons.

  • Each MultiPolygon can have enclaves / exclaves

  • Typically (> 95%), they are simple polygons without holes

  • Typically (> 50%), the shape is rather simple (e.g. squares)

Use case example



The list of polygons could be postal code areas in Germany. That would be several thousands. Then I have GPS positions of me and some friends, that would be several thousands as well. I want to say in which area where we got most data points.










share|improve this question























  • I don't think you're tackling the problem correctly (for your use case). Postal codes in Germany have a specific order, you should never check all postal codes there. Start with checking out the first digit so you can rule out ~9/10 already and never have to check those. Then narrow it down from there. Look at the postal codes as a tree. You would never iterate through all leaves if you can exclude a whole branch.
    – DonQuiKong
    yesterday










  • For general polygons, pre-compute a sorted list of bounding boxes for the gps-achses and then figure out which few polygons the gps coordinate can be in for example. If your x-coordinate (or whatever the gps equivalent is) is 5 and your polygons are distributed from 0 to 100, only check those where the bounding box starts left from 5 and ends right from 5. That's iterating partly through 2 sorted lists. Same for y.
    – DonQuiKong
    yesterday











  • @DonQuiKong The use case I described is way simpler than my actual use case. I already suggested that building a tree data structure is what I would like to do (in fact, I already wrote custom software for it). But I wonder if shapely has built-in support for building that tree and using it.
    – Martin Thoma
    yesterday










  • Oh, that's what you mean. Yeah, never mind.
    – DonQuiKong
    yesterday














up vote
0
down vote

favorite












I have a function



get_polygon(polygon_collection, point):
for polygon in polygon_collection:
if polygon.intersects(point):
return polygon
return None


This approach works, but it is in O(n) * O(single polygon check). This can for sure be reduced to O(log(n)) * O(single polygon check) if one builds a tree data structure.



Does shapely support that directly?



Structure of polyogon collection



  • The collection contains n MultiPolygons - hence one object can consist of many polygons.

  • Each MultiPolygon can have enclaves / exclaves

  • Typically (> 95%), they are simple polygons without holes

  • Typically (> 50%), the shape is rather simple (e.g. squares)

Use case example



The list of polygons could be postal code areas in Germany. That would be several thousands. Then I have GPS positions of me and some friends, that would be several thousands as well. I want to say in which area where we got most data points.










share|improve this question























  • I don't think you're tackling the problem correctly (for your use case). Postal codes in Germany have a specific order, you should never check all postal codes there. Start with checking out the first digit so you can rule out ~9/10 already and never have to check those. Then narrow it down from there. Look at the postal codes as a tree. You would never iterate through all leaves if you can exclude a whole branch.
    – DonQuiKong
    yesterday










  • For general polygons, pre-compute a sorted list of bounding boxes for the gps-achses and then figure out which few polygons the gps coordinate can be in for example. If your x-coordinate (or whatever the gps equivalent is) is 5 and your polygons are distributed from 0 to 100, only check those where the bounding box starts left from 5 and ends right from 5. That's iterating partly through 2 sorted lists. Same for y.
    – DonQuiKong
    yesterday











  • @DonQuiKong The use case I described is way simpler than my actual use case. I already suggested that building a tree data structure is what I would like to do (in fact, I already wrote custom software for it). But I wonder if shapely has built-in support for building that tree and using it.
    – Martin Thoma
    yesterday










  • Oh, that's what you mean. Yeah, never mind.
    – DonQuiKong
    yesterday












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a function



get_polygon(polygon_collection, point):
for polygon in polygon_collection:
if polygon.intersects(point):
return polygon
return None


This approach works, but it is in O(n) * O(single polygon check). This can for sure be reduced to O(log(n)) * O(single polygon check) if one builds a tree data structure.



Does shapely support that directly?



Structure of polyogon collection



  • The collection contains n MultiPolygons - hence one object can consist of many polygons.

  • Each MultiPolygon can have enclaves / exclaves

  • Typically (> 95%), they are simple polygons without holes

  • Typically (> 50%), the shape is rather simple (e.g. squares)

Use case example



The list of polygons could be postal code areas in Germany. That would be several thousands. Then I have GPS positions of me and some friends, that would be several thousands as well. I want to say in which area where we got most data points.










share|improve this question















I have a function



get_polygon(polygon_collection, point):
for polygon in polygon_collection:
if polygon.intersects(point):
return polygon
return None


This approach works, but it is in O(n) * O(single polygon check). This can for sure be reduced to O(log(n)) * O(single polygon check) if one builds a tree data structure.



Does shapely support that directly?



Structure of polyogon collection



  • The collection contains n MultiPolygons - hence one object can consist of many polygons.

  • Each MultiPolygon can have enclaves / exclaves

  • Typically (> 95%), they are simple polygons without holes

  • Typically (> 50%), the shape is rather simple (e.g. squares)

Use case example



The list of polygons could be postal code areas in Germany. That would be several thousands. Then I have GPS positions of me and some friends, that would be several thousands as well. I want to say in which area where we got most data points.







python shapely






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday

























asked yesterday









Martin Thoma

38.6k50278493




38.6k50278493











  • I don't think you're tackling the problem correctly (for your use case). Postal codes in Germany have a specific order, you should never check all postal codes there. Start with checking out the first digit so you can rule out ~9/10 already and never have to check those. Then narrow it down from there. Look at the postal codes as a tree. You would never iterate through all leaves if you can exclude a whole branch.
    – DonQuiKong
    yesterday










  • For general polygons, pre-compute a sorted list of bounding boxes for the gps-achses and then figure out which few polygons the gps coordinate can be in for example. If your x-coordinate (or whatever the gps equivalent is) is 5 and your polygons are distributed from 0 to 100, only check those where the bounding box starts left from 5 and ends right from 5. That's iterating partly through 2 sorted lists. Same for y.
    – DonQuiKong
    yesterday











  • @DonQuiKong The use case I described is way simpler than my actual use case. I already suggested that building a tree data structure is what I would like to do (in fact, I already wrote custom software for it). But I wonder if shapely has built-in support for building that tree and using it.
    – Martin Thoma
    yesterday










  • Oh, that's what you mean. Yeah, never mind.
    – DonQuiKong
    yesterday
















  • I don't think you're tackling the problem correctly (for your use case). Postal codes in Germany have a specific order, you should never check all postal codes there. Start with checking out the first digit so you can rule out ~9/10 already and never have to check those. Then narrow it down from there. Look at the postal codes as a tree. You would never iterate through all leaves if you can exclude a whole branch.
    – DonQuiKong
    yesterday










  • For general polygons, pre-compute a sorted list of bounding boxes for the gps-achses and then figure out which few polygons the gps coordinate can be in for example. If your x-coordinate (or whatever the gps equivalent is) is 5 and your polygons are distributed from 0 to 100, only check those where the bounding box starts left from 5 and ends right from 5. That's iterating partly through 2 sorted lists. Same for y.
    – DonQuiKong
    yesterday











  • @DonQuiKong The use case I described is way simpler than my actual use case. I already suggested that building a tree data structure is what I would like to do (in fact, I already wrote custom software for it). But I wonder if shapely has built-in support for building that tree and using it.
    – Martin Thoma
    yesterday










  • Oh, that's what you mean. Yeah, never mind.
    – DonQuiKong
    yesterday















I don't think you're tackling the problem correctly (for your use case). Postal codes in Germany have a specific order, you should never check all postal codes there. Start with checking out the first digit so you can rule out ~9/10 already and never have to check those. Then narrow it down from there. Look at the postal codes as a tree. You would never iterate through all leaves if you can exclude a whole branch.
– DonQuiKong
yesterday




I don't think you're tackling the problem correctly (for your use case). Postal codes in Germany have a specific order, you should never check all postal codes there. Start with checking out the first digit so you can rule out ~9/10 already and never have to check those. Then narrow it down from there. Look at the postal codes as a tree. You would never iterate through all leaves if you can exclude a whole branch.
– DonQuiKong
yesterday












For general polygons, pre-compute a sorted list of bounding boxes for the gps-achses and then figure out which few polygons the gps coordinate can be in for example. If your x-coordinate (or whatever the gps equivalent is) is 5 and your polygons are distributed from 0 to 100, only check those where the bounding box starts left from 5 and ends right from 5. That's iterating partly through 2 sorted lists. Same for y.
– DonQuiKong
yesterday





For general polygons, pre-compute a sorted list of bounding boxes for the gps-achses and then figure out which few polygons the gps coordinate can be in for example. If your x-coordinate (or whatever the gps equivalent is) is 5 and your polygons are distributed from 0 to 100, only check those where the bounding box starts left from 5 and ends right from 5. That's iterating partly through 2 sorted lists. Same for y.
– DonQuiKong
yesterday













@DonQuiKong The use case I described is way simpler than my actual use case. I already suggested that building a tree data structure is what I would like to do (in fact, I already wrote custom software for it). But I wonder if shapely has built-in support for building that tree and using it.
– Martin Thoma
yesterday




@DonQuiKong The use case I described is way simpler than my actual use case. I already suggested that building a tree data structure is what I would like to do (in fact, I already wrote custom software for it). But I wonder if shapely has built-in support for building that tree and using it.
– Martin Thoma
yesterday












Oh, that's what you mean. Yeah, never mind.
– DonQuiKong
yesterday




Oh, that's what you mean. Yeah, never mind.
– DonQuiKong
yesterday

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224490%2fhow-can-i-look-up-a-polygon-that-contains-a-point-fast-given-all-polygons-are-a%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53224490%2fhow-can-i-look-up-a-polygon-that-contains-a-point-fast-given-all-polygons-are-a%23new-answer', 'question_page');

);

Post as a guest














































































Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo