Skip to content

Instantly share code, notes, and snippets.

@klashxx
Created April 7, 2017 20:34
Show Gist options
  • Select an option

  • Save klashxx/927ef1f742b264c3c278c57bd67aef78 to your computer and use it in GitHub Desktop.

Select an option

Save klashxx/927ef1f742b264c3c278c57bd67aef78 to your computer and use it in GitHub Desktop.
import itertools
import pandas as pd
from shapely.geometry import Polygon
from shapely.geometry.multipolygon import MultiPolygon
umbral = 0.8
list_poligonos = []
df = pd.read_csv('polygons15.csv', names=['x1', 'y1', 'x2', 'y2', 'x3', 'y3', 'x4', 'y4', 'umbral', 'd'] )
print(df.loc[df['umbral'] >= umbral])
for row in df.loc[df['umbral'] >= umbral].itertuples():
list_poligonos.append(Polygon([(row.x1, row.y1), (row.x2, row.y2), (row.x3, row.y3), (row.x4, row.y4)]))
poligonos = MultiPolygon(list_poligonos)
print(len(poligonos))
print(poligonos.bounds)
for A, B in itertools.combinations(list_poligonos, 2):
if A.within(B):
print('Poligono ', A, ' contenido en ', B)
@klashxx
Copy link
Author

klashxx commented Apr 7, 2017

>>> import itertools
>>> import pandas as pd
>>> from shapely.geometry import Polygon
>>> from shapely.geometry.multipolygon import MultiPolygon
>>> 
>>> umbral = 0.8
>>> list_poligonos = []
>>> 
>>> df = pd.read_csv('polygons15.csv', names=['x1', 'y1', 'x2', 'y2', 'x3', 'y3', 'x4', 'y4', 'umbral', 'd'] )
>>> 
>>> print(df.loc[df['umbral'] >= umbral])
        x1    y1    x2    y2    x3    y3    x4    y4   umbral  d
54     270    38   270   166   399   166   399    38  0.86329  1
55     309    38   309   166   438   166   438    38  0.95889  1
56     348    38   348   166   477   166   477    38  0.81368  1
176   1354   115  1354   243  1483   243  1483   115  0.82351  1
177   1393   115  1393   243  1522   243  1522   115  0.85561  1
236     38   192    38   320   167   320   167   192  0.85603  1
311   1122   230  1122   358  1251   358  1251   230  0.80762  1
358   1122   268  1122   396  1251   396  1251   268  0.89122  1
380    154   307   154   435   283   435   283   307  0.84945  1
426    116   345   116   473   245   473   245   345  0.82650  1
427    154   345   154   473   283   473   283   345  0.97266  1
443    774   345   774   473   903   473   903   345  0.86724  1
473    116   383   116   511   245   511   245   383  0.85303  1
474    154   383   154   511   283   511   283   383  0.97375  1
475    193   383   193   511   322   511   322   383  0.80560  1
491    812   383   812   511   941   511   941   383  0.90332  1
520    116   422   116   550   245   550   245   422  0.82553  1
521    154   422   154   550   283   550   283   422  0.95945  1
522    193   422   193   550   322   550   322   422  0.81361  1
523    232   422   232   550   361   550   361   422  0.86752  1
525    309   422   309   550   438   550   438   422  0.90582  1
537    774   422   774   550   903   550   903   422  0.88730  1
538    812   422   812   550   941   550   941   422  0.91365  1
671    503   537   503   665   632   665   632   537  0.87184  1
718    503   575   503   703   632   703   632   575  0.96167  1
724    735   575   735   703   864   703   864   575  0.80465  1
738   1277   575  1277   703  1406   703  1406   575  0.81445  1
785   1277   614  1277   742  1406   742  1406   614  0.90783  1
831   1238   652  1238   780  1367   780  1367   652  0.80979  1
832   1277   652  1277   780  1406   780  1406   652  0.85519  1
879   1277   691  1277   819  1406   819  1406   691  0.86217  1
1080  1780   844  1780   972  1909   972  1909   844  0.82519  1
1127  1780   883  1780  1011  1909  1011  1909   883  0.81276  1
1281   464  1036   464  1164   593  1164   593  1036  0.86746  1
1289   774  1036   774  1164   903  1164   903  1036  0.87789  1
1444  1315  1152  1315  1280  1444  1280  1444  1152  0.83701  1
1490  1277  1190  1277  1318  1406  1318  1406  1190  0.91252  1
1491  1315  1190  1315  1318  1444  1318  1444  1190  0.88715  1
1507   116  1228   116  1356   245  1356   245  1228  0.91908  1
1538  1315  1228  1315  1356  1444  1356  1444  1228  0.87749  1
1539  1354  1228  1354  1356  1483  1356  1483  1228  0.80069  1
1857   928  1497   928  1625  1057  1625  1057  1497  0.91276  1
1907  1044  1536  1044  1664  1173  1664  1173  1536  0.87867  1
2054  1277  1651  1277  1779  1406  1779  1406  1651  0.87247  1
>>>  
... for row in df.loc[df['umbral'] >= umbral].itertuples():
...     list_poligonos.append(Polygon([(row.x1, row.y1), (row.x2, row.y2), (row.x3, row.y3), (row.x4, row.y4)]))
... 
>>> poligonos = MultiPolygon(list_poligonos)
>>> 
>>> print(len(poligonos))
44
>>> print(poligonos.bounds)
(38.0, 38.0, 1909.0, 1779.0)
>>> 
>>> for A, B in itertools.combinations(list_poligonos, 2):
...    if A.within(B):
...       print('Poligono ', A, ' contenido en ', B)
... 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment