dwyerk / boundaries

Python code behind the boundaries blog post @ https://web.archive.org/web/20201013181320/http://blog.thehumangeo.com/2014/05/12/drawing-boundaries-in-python/
38 stars 10 forks source link

Question on code #1

Open standarddev opened 4 years ago

standarddev commented 4 years ago

Hi! I'm in the process of learning python and using your code to do so!

I was able to run your code on the files that you provided. I decided to create my own file in qgis and try it on that but I'm running into an error with the concave_hull, edge_points = alpha_shape(points, alpha=1.87) line.

Traceback (most recent call last):

File "", line 63, in concave_hull, edge_points = alpha_shape(points, alpha=1.87)

File "", line 35, in alpha_shape for ia, ib, ic in tri.vertices:

ValueError: too many values to unpack (expected 3)

I'm not sure how to fix it. Any advice? I've attached the point file I'm trying to do it on. Test.zip

dwyerk commented 4 years ago

There's nothing obviously wrong with your shapefile. I ran the Delaunay triangulation on it using qgis and there were no errors, so I think it's good input.

It's possible that it's tripping up the scipy Delaunay implementation. I would recommend that you run the code in a debugger (PyCharm, etc) and set a breakpoint at line 35 and inspect the value of tri.vertices.

standarddev commented 4 years ago

Thanks!

I got that working. Only part now I can’t figure out is when I run

Triangles = list(polygonize(m)), I get tuple object is not callable.

I did just polygonize(m) and that works fine. Just can’t get the list to work around it.

Any ideas?

dwyerk commented 4 years ago

I'd make the same recommendation for setting a breakpoint at that point in the code and inspecting the values of each variable. list() should work just fine with whatever is returned from polygonize, so it's likely that something else has changed here.

standarddev commented 4 years ago

Thanks! It looks like the polygonize(m) function is working fine. It results in a generator which I expected. What is the point of the list() around that? What is it trying to accomplish?

Without it, the cascaded union doesn’t work but I’m not sure why.

Thanks!

dwyerk commented 4 years ago

cascaded_union expects a list as input, not a generator, so list() unrolls the generator and returns a list. Consider this generator:

def gen():
    for i in range(10):
        yield i

g = gen()
lst = list(gen())

print(lst[0]) # <-- prints 0
print(g[0])   # <-- raises TypeError