gdsbook / book

This book serves as an introduction to a whole new way of thinking systematically about geographic data, using geographical analysis and computation to unlock new insights hidden within data.
https://geographicdata.science
Other
338 stars 104 forks source link

Replace `pygeos` imports #315

Open darribas opened 11 months ago

darribas commented 11 months ago

Chapter 8 (points) includes imports from pygeos:

from pygeos import minimum_rotated_rectangle, from_shapely, to_shapely

My sense is, since shapely 2.0, general recommendation is to rely on shapely rather than pygeos directly. Should we update this? One probably more for @ljwolf

ljwolf commented 11 months ago

Yes. Should be a straight swap to shapely!

dfolch commented 10 months ago

One fewer dependency for the book would be great. Here is a proposed update for this.

Original

For the minimum rotated rectangle, we will use the minimum_rotated_rectangle function from the pygeos module, which constructs the minimum rotated rectangle for an input multi-point object. This means that we will need to collect our points together into a single multi-point object and then compute the rotated rectangle for that object.

from pygeos import minimum_rotated_rectangle, from_shapely, to_shapely

point_array = geopandas.points_from_xy(x=user.x, y=user.y)

min_rot_rect = minimum_rotated_rectangle(
    from_shapely(
        point_array.unary_union()
    )
)
min_rot_rect = to_shapely(min_rot_rect)

Update

For the minimum rotated rectangle, we will use the minimum_rotated_rectangle property from GeoPandas, which constructs the minimum rotated rectangle for an input multi-point object. This means that we will need to collect our points together into a single multi-point object and then compute the rotated rectangle for that object.

point_array = geopandas.points_from_xy(x=user.x, y=user.y)

min_rot_rect = point_array.unary_union().minimum_rotated_rectangle