4

I have a list of Shapely polygons. From that list I want to extract only unique polygons removing the duplicates.

How to do it in a faster way? (My list contains thousands of polygons)

from shapely.geometry import Polygon

lists = [[(1,1),(2,2),(3,3),(4,4)], [(6,6),(7,7),(8,8),(9,9)], [(1,1),(2,2),(3,3),(4,4)]]
polys = [Polygon(item) for item in lists] ##This is given condition

for poly in polys:

    test = [p.intersects(poly) for p in polys] ##Return true or false
    print test


[True, False, True]
[False, True, False]
[True, False, True]

The expected result is:

[[(1,1),(2,2),(3,3),(4,4)], [(6,6),(7,7),(8,8),(9,9)]]
Georgy
  • 12,464
  • 7
  • 65
  • 73
Borys
  • 1,323
  • 6
  • 16
  • 40

1 Answers1

11

Note that you should not use intersects(), as that will identify any polygons that overlap at all as duplicates. Use equals() instead.

You can create a list that stores the unique polygons, and then for each polygon in your list, loop over the polygons stored in the outer list, and if none of them are equal to the new one, add this to the list, you can use any() function for that.

Example:

from shapely.geometry import Polygon

lists = [[(1,1),(2,2),(3,3),(4,4)], [(6,6),(7,7),(8,8),(9,9)], [(1,1),(2,2),(3,3),(4,4)]]
polys = [Polygon(item) for item in lists] ##This is given condition
uniqpolies = []
for poly in polys:

    if not any(p.equals(poly) for p in uniqpolies):
        uniqpolies.append(poly)

From documentation on any():

Return True if any element of the iterable is true. If the iterable is empty, return False.

Georgy
  • 12,464
  • 7
  • 65
  • 73
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • Is it also possible to return the results directly with out collecting in the list? – Borys Jul 25 '15 at 03:53
  • Without the other list, you would have to loop through them yourself. And its correct - `for p in uniqpoies` . – Anand S Kumar Jul 25 '15 at 03:59
  • 1
    Just a note that you should not use intersects(), as that will identify any polygons that overlap at all as duplicates. As @Mike T above suggested, use equals(). – Jon Sep 17 '18 at 22:00