Oleksandr,
Good point! I assumed they were aligned.

Yuta, if the rectangle's coordinates are just the corners, then compute the
distance between each of the points.

def distance(c0, c1):
    x0, y0 = c0
    x1, y1 = c1
    return math.hypot(x1-x0, y1-y0)

c0, c1, c2 = poly.exterior.coords[:3]
width = distance(c0, c1)
height = distance(c1, c2)
diag = distance(c0, c2)  # same as math.hypot(width, height)

If there are multiple points along the edges of the rectangle's exterior,
then compute the rotation angle, rotate the polygon to align with the
coordinate axes (using shapely.affinity.rotate
http://toblerity.org/shapely/manual.html#shapely.affinity.rotate), and
compute the values as in my previous email. This should work:

# polyr is a rectangular polygon at some arbitrary angle
x0, y0 = polyr.exterior.coords[0]
x1, y1 = polyr.exterior.coords[1]
angle = math.atan2(y1-y0, x1-x0)
# It doesn't matter if polyr.exterior.coords[1] is a corner or not as the
angle is the same.

poly = shapely.affinity.rotate(polyr, -angle, [x0, y0], use_radians=True)
# poly is now aligned with the coordinate axes

minx, miny, maxx, maxy = poly.bounds

width = maxx - minx
height = maxy - miny
diagonal = math.hypot(width, height)


-Jake


On Tue, Feb 17, 2015 at 9:43 AM, Oleksandr Huziy <guziy.sa...@gmail.com>
wrote:

> Hi Jake:
>
> will your method work when the polygon sides are not parallel to the axes?
>
> Cheers
>
> 2015-02-17 9:22 GMT-05:00 Jake Wasserman <jwasser...@gmail.com>:
>
> Hi Yuta,
>> You can use the `bounds` property on Shapely geometry objects to help (
>> http://toblerity.org/shapely/manual.html#object.bounds).
>> This should work:
>>
>> poly = shapely.geometry.Polygon([(0,0), (1,0), (1,3), (0, 3)])
>> minx, miny, maxx, maxy = poly.bounds
>>
>> width = maxx - minx
>> height = maxy - miny
>> diagonal = math.hypot(width, height)
>>
>> You can always wrap that into a function if you find yourself reusing it
>> a lot.
>>
>> -Jake
>>
>>
>>
>> On Tue, Feb 17, 2015 at 9:04 AM, Yuta Sato <yutaxs...@gmail.com> wrote:
>>
>>> Dear Shapely Developers and Users:
>>>
>>> I am a regular user of shapely.
>>>
>>> I want to see more smarter shapely.
>>>
>>> Meanwhile, could you add an ability to calculate length of height,
>>> width, and diagonal of a rectangular polygon?
>>>
>>> It would be great help if someone could write a function in Python using
>>> existing shapely for that.
>>>
>>> Thanks.
>>>
>>> Yuta
>>>
>>> _______________________________________________
>>> Community mailing list
>>> Community@lists.gispython.org
>>> http://lists.gispython.org/mailman/listinfo/community
>>>
>>>
>>
>> _______________________________________________
>> Community mailing list
>> Community@lists.gispython.org
>> http://lists.gispython.org/mailman/listinfo/community
>>
>>
>
>
> --
> Sasha
>
> _______________________________________________
> Community mailing list
> Community@lists.gispython.org
> http://lists.gispython.org/mailman/listinfo/community
>
>
_______________________________________________
Community mailing list
Community@lists.gispython.org
http://lists.gispython.org/mailman/listinfo/community

Reply via email to