Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compute takes ages to produce the result. #314

Open
emaildipen opened this issue Oct 23, 2024 · 3 comments
Open

compute takes ages to produce the result. #314

emaildipen opened this issue Oct 23, 2024 · 3 comments

Comments

@emaildipen
Copy link

I have a Dask GeoDataFrame, from which I extracted the geometry and performed infill using Shapely. I used geometry.interiors to set an area threshold and fill the holes. After that, I created a new geometry DataFrame. However, I don’t understand why it takes so long when I try to convert the Dask GeoSeries into a GeoSeries. Whenever I use the .compute() command, it takes ages—more than 12 hours. I thought something might be wrong with my approach.

@martinfleis
Copy link
Member

Please post the code you have used, not only its description.

@emaildipen
Copy link
Author

emaildipen commented Oct 23, 2024

def fill_holes(geometry, min_hole_size):
    """
    Fill holes in a geometry (Polygon or MultiPolygon) if they are smaller than min_hole_size.
    """
    if geometry.geom_type == 'Polygon':
        if geometry.interiors:
            new_interiors = [interior for interior in geometry.interiors if Polygon(interior).area >= min_hole_size]
            return Polygon(geometry.exterior, new_interiors)
        else:
            return geometry
    elif geometry.geom_type == 'MultiPolygon':
        return unary_union([fill_holes(poly, min_hole_size) for poly in geometry])
    else:
        return geometry
    # Apply fill_holes function in parallel
filled = ddf.map_partitions(lambda ddf: ddf.geometry.apply(lambda geom: fill_holes(geom, min_hole_size)))

filled_ser=filled.compute()

@phofl
Copy link

phofl commented Jan 2, 2025

The reason is probably Gil contention, could you try creating an explicit cluster for Dask? i.e.

from distributed import Client

def fill_holes(geometry, min_hole_size):
    """
    Fill holes in a geometry (Polygon or MultiPolygon) if they are smaller than min_hole_size.
    """
    if geometry.geom_type == 'Polygon':
        if geometry.interiors:
            new_interiors = [interior for interior in geometry.interiors if Polygon(interior).area >= min_hole_size]
            return Polygon(geometry.exterior, new_interiors)
        else:
            return geometry
    elif geometry.geom_type == 'MultiPolygon':
        return unary_union([fill_holes(poly, min_hole_size) for poly in geometry])
    else:
        return geometry
    # Apply fill_holes function in parallel

client = Client(n_workers=your_number_of_cores)

filled = ddf.map_partitions(lambda ddf: ddf.geometry.apply(lambda geom: fill_holes(geom, min_hole_size)))
print(client.dashboard_link)
filled_ser=filled.compute()

That will use all cores in parallel and you can use the printed link to observe what's going on

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants