1

I have about 1000 coordinates and 3000 shapes. My goal is to see if a coordinate falls within each shape:

import pandas as pd
import shapefile
from shapely.geometry import Point, shape

coords = pd.read_csv("coords.csv")
areas = shapefile.Reader("./areas")
shapes = areas.shapes()

for a, b in zip(coords["a"], coords["b"]):
    for i in shapes:
        if (Point((a, b)).within(shape(i))):
            print("in shape")

Test prints show that there are heaps of calculations going on but the loop just won't end. For such a small amount of coordinates and shapes I believe it is to do with my code.

compsciman
  • 349
  • 3
  • 17
  • The maths involved in checking if a point is wiithin a shape can be quite costly which is why bounding boxes are usually used within games – Sayse Jun 03 '19 at 13:56
  • Also, "forever" isn't a measurable time – Sayse Jun 03 '19 at 13:57
  • 1
    @unutbu Wouldn't it be only 1000 coords * 3000 shapes? Where does the additional 1000 come from? – John Gordon Jun 03 '19 at 14:07
  • John Gordon is right, only 3.000.000 call of within(). Can you measure the time for one dataset? Just to give us some hints how costly theese operations are? – Frieder Jun 03 '19 at 14:10
  • I changed it to try every coordinate against just one shape. The time: `0.04166412353515625` The reason for asking is that this is part of a larger piece of an assignment so the fact it's taking more than a couple seconds means I've taken the wrong approach. – compsciman Jun 03 '19 at 14:20
  • Related: [Faster way of polygon intersection with shapely](https://stackoverflow.com/questions/14697442/faster-way-of-polygon-intersection-with-shapely) – Sayse Jun 03 '19 at 15:23
  • @JohnGordon: Oops, you are correct. – unutbu Jun 03 '19 at 15:28

0 Answers0