Discussion:
[Community] Feature Request: Shapely Rectangle Diagonal
Yuta Sato
2015-02-17 14:04:28 UTC
Permalink
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
Jake Wasserman
2015-02-17 14:22:28 UTC
Permalink
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
Post by Yuta Sato
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
http://lists.gispython.org/mailman/listinfo/community
Yuta Sato
2015-02-17 14:38:39 UTC
Permalink
Thank you Jake for your quick support.
Post by Jake Wasserman
Hi Yuta,
You can use the `bounds` property on Shapely geometry objects to help (
http://toblerity.org/shapely/manual.html#object.bounds).
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
Post by Yuta Sato
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
http://lists.gispython.org/mailman/listinfo/community
_______________________________________________
Community mailing list
http://lists.gispython.org/mailman/listinfo/community
Oleksandr Huziy
2015-02-17 14:43:19 UTC
Permalink
Hi Jake:

will your method work when the polygon sides are not parallel to the axes?

Cheers
Post by Jake Wasserman
Hi Yuta,
You can use the `bounds` property on Shapely geometry objects to help (
http://toblerity.org/shapely/manual.html#object.bounds).
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
Post by Yuta Sato
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
http://lists.gispython.org/mailman/listinfo/community
_______________________________________________
Community mailing list
http://lists.gispython.org/mailman/listinfo/community
--
Sasha
Jake Wasserman
2015-02-17 15:59:41 UTC
Permalink
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
Post by Oleksandr Huziy
will your method work when the polygon sides are not parallel to the axes?
Cheers
Hi Yuta,
Post by Jake Wasserman
You can use the `bounds` property on Shapely geometry objects to help (
http://toblerity.org/shapely/manual.html#object.bounds).
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
Post by Yuta Sato
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
http://lists.gispython.org/mailman/listinfo/community
_______________________________________________
Community mailing list
http://lists.gispython.org/mailman/listinfo/community
--
Sasha
_______________________________________________
Community mailing list
http://lists.gispython.org/mailman/listinfo/community
Kristian Thy
2015-02-17 16:09:46 UTC
Permalink
Hi Oleksandr,

Yes, if by "width" and "height" one means the geometric width in the
direction of the axes of the coordinate system.

If you want to measure the _mean_ width of the object, you need to
calculate a convex hull and inscribe it in a circle.

https://en.wikipedia.org/wiki/Mean_width

Cheers,
Kristian
Post by Oleksandr Huziy
will your method work when the polygon sides are not parallel to the axes?
Cheers
Post by Jake Wasserman
Hi Yuta,
You can use the `bounds` property on Shapely geometry objects to help (
http://toblerity.org/shapely/manual.html#object.bounds).
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
Post by Yuta Sato
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
Yuta Sato
2015-02-17 18:47:24 UTC
Permalink
I have a question: Does not "minx, miny, maxx, maxy" calculated by
poly.bounds in a geographic coordinate system provide the rectangle sides
parallel to the axes?
Post by Kristian Thy
Hi Oleksandr,
Yes, if by "width" and "height" one means the geometric width in the
direction of the axes of the coordinate system.
If you want to measure the _mean_ width of the object, you need to
calculate a convex hull and inscribe it in a circle.
https://en.wikipedia.org/wiki/Mean_width
Cheers,
Kristian
Post by Oleksandr Huziy
will your method work when the polygon sides are not parallel to the
axes?
Post by Oleksandr Huziy
Cheers
Post by Jake Wasserman
Hi Yuta,
You can use the `bounds` property on Shapely geometry objects to help (
http://toblerity.org/shapely/manual.html#object.bounds).
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
Post by Oleksandr Huziy
Post by Jake Wasserman
lot.
-Jake
Post by Yuta Sato
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,
Post by Oleksandr Huziy
Post by Jake Wasserman
Post by Yuta Sato
and diagonal of a rectangular polygon?
It would be great help if someone could write a function in Python
using
Post by Oleksandr Huziy
Post by Jake Wasserman
Post by Yuta Sato
existing shapely for that.
Thanks.
Yuta
_______________________________________________
Community mailing list
http://lists.gispython.org/mailman/listinfo/community
Harasty, Daniel J
2015-02-17 19:33:49 UTC
Permalink
A few comments for Yuta:

Note the bounds() method returns four VALUES -- not four points. Thus, they are not properly a rectangle, or even a geometry of any sort. They are a list of four values: the minimum- and maximum- x and y values.

If you are thinking geographically and you are using longitude as your x dimension and latitude as your x, then bound() gives you the “westmost, southmost, eastmost, and northmost” values. Note it does NOT guarantee that the original geometry actually had a vertex at any of the corners of the box implied by those values.

Note that you made reference to a “geographic coordinate system”. Shapely does its calculations on pure, abstract geometries. It is up to the user (you) to decide on a geographic system, and if Shapely geometric calculations are fit for your purpose. (For example, if you are using latitude and longitude as your “x and y” values in Shapely, the measures of things like Shapely’s “length” and “area” won’t actually give you measures of geographic length and area, owing to the curvature of the earth and that latitude and longitude do not constitute a pure, 2-dimensional planar coordinate system.)

Lastly, to your original question (and desire) that you’d like Shapely to give you useful things like measures of rectangular width, height, and diagonal, note that “RECTANGLE” is not a primitive Shapley shape – nor is it generally a primitive in most geometric processing APIs. A rectangle is an instance of one kind of polygon. Measures like width, height, and diagonal are not necessarily meaningful on arbitrary polygons. Thus it doesn’t seem to me they SHOULD be defined in Shapely, at least not on any of the shapes it currently defines.

However you could – if you see fit – subclass shapely.geometry.Polygon, and add these calculations yourself, then still use all other Shapely operations on Polygons
 because your Rectangle would be a Shapely polygon. You would have to decide in the design of this object if “rectangles” are parallel to the coordinate system, or could be “skew”
. and build constructors and accessors that enforce the constraints as you see fit.

Regards,
Dan Harasty


From: community-***@lists.gispython.org [mailto:community-***@lists.gispython.org] On Behalf Of Yuta Sato
Sent: Tuesday, February 17, 2015 1:47 PM
To: gispython.org community projects
Subject: Re: [Community] Feature Request: Shapely Rectangle Diagonal

I have a question: Does not "minx, miny, maxx, maxy" calculated by poly.bounds in a geographic coordinate system provide the rectangle sides parallel to the axes?



On Wed, Feb 18, 2015 at 1:09 AM, Kristian Thy <***@42.dk<mailto:***@42.dk>> wrote:
Hi Oleksandr,

Yes, if by "width" and "height" one means the geometric width in the
direction of the axes of the coordinate system.

If you want to measure the _mean_ width of the object, you need to
calculate a convex hull and inscribe it in a circle.

https://en.wikipedia.org/wiki/Mean_width

Cheers,
Kristian
Post by Oleksandr Huziy
will your method work when the polygon sides are not parallel to the axes?
Cheers
Post by Jake Wasserman
Hi Yuta,
You can use the `bounds` property on Shapely geometry objects to help (
http://toblerity.org/shapely/manual.html#object.bounds).
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
Post by Yuta Sato
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
***@lists.gispython.org<mailto:***@lists.gispython.org>
http://lists.gispython.org/mailman/listinfo/community
Harasty, Daniel J
2015-02-17 19:36:29 UTC
Permalink
Apologies to all for the typos.

The first line of the second paragraphs should have read:

If you are thinking geographically and you are using longitude as your x dimension and latitude as your y dimension, then bounds() gives you the “westmost, southmost, eastmost, and northmost” values.



From: community-***@lists.gispython.org [mailto:community-***@lists.gispython.org] On Behalf Of Harasty, Daniel J
Sent: Tuesday, February 17, 2015 2:34 PM
To: gispython.org community projects
Subject: Re: [Community] Feature Request: Shapely Rectangle Diagonal

A few comments for Yuta:

Note the bounds() method returns four VALUES -- not four points. Thus, they are not properly a rectangle, or even a geometry of any sort. They are a list of four values: the minimum- and maximum- x and y values.

If you are thinking geographically and you are using longitude as your x dimension and latitude as your x, then bound() gives you the “westmost, southmost, eastmost, and northmost” values. Note it does NOT guarantee that the original geometry actually had a vertex at any of the corners of the box implied by those values.

Note that you made reference to a “geographic coordinate system”. Shapely does its calculations on pure, abstract geometries. It is up to the user (you) to decide on a geographic system, and if Shapely geometric calculations are fit for your purpose. (For example, if you are using latitude and longitude as your “x and y” values in Shapely, the measures of things like Shapely’s “length” and “area” won’t actually give you measures of geographic length and area, owing to the curvature of the earth and that latitude and longitude do not constitute a pure, 2-dimensional planar coordinate system.)

Lastly, to your original question (and desire) that you’d like Shapely to give you useful things like measures of rectangular width, height, and diagonal, note that “RECTANGLE” is not a primitive Shapley shape – nor is it generally a primitive in most geometric processing APIs. A rectangle is an instance of one kind of polygon. Measures like width, height, and diagonal are not necessarily meaningful on arbitrary polygons. Thus it doesn’t seem to me they SHOULD be defined in Shapely, at least not on any of the shapes it currently defines.

However you could – if you see fit – subclass shapely.geometry.Polygon, and add these calculations yourself, then still use all other Shapely operations on Polygons
 because your Rectangle would be a Shapely polygon. You would have to decide in the design of this object if “rectangles” are parallel to the coordinate system, or could be “skew”
. and build constructors and accessors that enforce the constraints as you see fit.

Regards,
Dan Harasty


From: community-***@lists.gispython.org<mailto:community-***@lists.gispython.org> [mailto:community-***@lists.gispython.org] On Behalf Of Yuta Sato
Sent: Tuesday, February 17, 2015 1:47 PM
To: gispython.org community projects
Subject: Re: [Community] Feature Request: Shapely Rectangle Diagonal

I have a question: Does not "minx, miny, maxx, maxy" calculated by poly.bounds in a geographic coordinate system provide the rectangle sides parallel to the axes?



On Wed, Feb 18, 2015 at 1:09 AM, Kristian Thy <***@42.dk<mailto:***@42.dk>> wrote:
Hi Oleksandr,

Yes, if by "width" and "height" one means the geometric width in the
direction of the axes of the coordinate system.

If you want to measure the _mean_ width of the object, you need to
calculate a convex hull and inscribe it in a circle.

https://en.wikipedia.org/wiki/Mean_width

Cheers,
Kristian
Post by Oleksandr Huziy
will your method work when the polygon sides are not parallel to the axes?
Cheers
Post by Jake Wasserman
Hi Yuta,
You can use the `bounds` property on Shapely geometry objects to help (
http://toblerity.org/shapely/manual.html#object.bounds).
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
Post by Yuta Sato
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
***@lists.gispython.org<mailto:***@lists.gispython.org>
http://lists.gispython.org/mailman/listinfo/community

Loading...