pytroll / pytroll/pyresample

Operation to check if two polygons overlap is inefficient

Open
#372 0 comments 0 reactions 1 assignee View on GitHub

@adybbroe is already working on this.

Since Jul 20, 2021.

Dominant language
Python
Stars
385
Forks
102
Avg merge
4d 2h
Merged PRs (30d)
9

Description

Code Sample, a minimal, complete, and verifiable piece of code

I did a profiling of the below code. The input was a list of trollsched.satpass.Pass satellite passes with only two passes overlapping each other over the areas of interest.

The _bool_opermethod of SphPolygon class was called three times, each taking almost 5 seconds. In tot al the code ran for 19 seconds.

# Your code here
def derive_combined_coverage(passes, area_def):
    """From a sequence of satellite overpasses derived the total coverage of an area."""

    npasses = len(passes)
    if npasses == 0:
        print("No passes in time window!")
        return 0

    area_boundary = AreaDefBoundary(area_def, frequency=100)
    area_boundary = area_boundary.contour_poly

    list_of_polygons = []
    for mypass in passes:
        list_of_polygons.append(mypass.boundary.contour_poly)

    non_overlaps = GetNonOverlapUnions(list_of_polygons)
    non_overlaps.merge()

    polygons = non_overlaps.get_polygons()
    pass_ids = non_overlaps.get_ids()

    coverage = 0
    for polygon in polygons:
        isect = polygon.intersection(area_boundary)
        if isect:
            coverage = coverage + isect.area()

    area_cov = coverage / area_boundary.area()
    print("Area coverage = {0}".format(area_cov))

Problem description

In the newly merged PR #347 new functionality is added in order to for instance support the problem of deriving the total coverage of several (possibly overlapping) satellite overpasses over an area of interest. However, when checking if two polygons overlap in the function check_if_two_polygons_overlap a union is performed, and when performing this union operation with the _bool_oper method of the SphPolygon class a lot of (in this case) unnecessary calculations finding the new polygon is carried out. This takes time when we only need to know if the two polygons intersects.

In addition to solving this it would be beneficial to refactor the SphPolygon class a bit, so that the methods are smaller and do preferably only one thing.

Expected Output
Actual Result, Traceback if applicable
Versions of Python, package at hand and relevant dependencies

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.