Get length of each side in a polygon

7 messages Options
Embed this post
Permalink
vurfem

Get length of each side in a polygon

Reply Threaded More More options
Print post
Permalink
Hi list;

I have a shapefile having polygons. I want to get length of each side in polygon. There is getLength() method in JTS but it gives the perimeter of the whole polygon. Is there any method or any way to get length of each side of the polygon respectively?

It is urgent for my study.

Thanks for any help.

Vurfem.
mbedward

Re: Get length of each side in a polygon

Reply Threaded More More options
Print post
Permalink
Hi Vurfem,

Here's one way of doing it:

SimpleFeature feature = ...
Polygon poly = (Polygon) feature.getDefaultGeometry();

// We only want the polygon's boundary, ignoring any holes
LineString boundary = poly.getExteriorRing();

// We need to get rid of any redundant vertices e.g. collinear points
// that would artificially split sides. Any point that is closer than
// minDistance to the line between the points before and after it will
// be removed (choose a minDistance value for your own data)
final double minDistance = 0.01d;
Coordinate[] coords = DouglasPeuckerLineSimplifier.simplify(
        boundary.getCoordinates(), minDistance);

double[] sideLength = new double[coords.length - 1];
for (int i = 0; i < sideLength.length; i++) {
    sideLength[i] = Point2D.distance(coords[i].x, coords[i].y,
coords[i+1].x, coords[i+1].y);
}

Michael

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Geotools-gt2-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
Christian Müller-3

Re: Get length of each side in a polygon

Reply Threaded More More options
Print post
Permalink
In reply to this post by vurfem
A possibility would be

1) Get the exterior ring of the polygon
2) get the points of the exterior ring, I think the should be ordered
anti-clockwise, please check this in advance
3) use the good old pythagoras, a*a + b*b = c*c

vurfem writes:

>
> Hi list;
>
> I have a shapefile having polygons. I want to get length of each side in
> polygon. There is getLength() method in JTS but it gives the perimeter of
> the whole polygon. Is there any method or any way to get length of each side
> of the polygon respectively?  
>
> It is urgent for my study.  
>
> Thanks for any help.
>
> Vurfem.
> --
> View this message in context: http://n2.nabble.com/Get-length-of-each-side-in-a-polygon-tp3888696p3888696.html
> Sent from the geotools-gt2-users mailing list archive at Nabble.com.
>
> ------------------------------------------------------------------------------
> Come build with us! The BlackBerry(R) Developer Conference in SF, CA
> is the only developer event you need to attend this year. Jumpstart your
> developing skills, take BlackBerry mobile applications to market and stay
> ahead of the curve. Join us from November 9 - 12, 2009. Register now!
> http://p.sf.net/sfu/devconference
> _______________________________________________
> Geotools-gt2-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
 


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Geotools-gt2-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
aaime

Re: Get length of each side in a polygon

Reply Threaded More More options
Print post
Permalink
Christian Müller ha scritto:
> A possibility would be
>
> 1) Get the exterior ring of the polygon
> 2) get the points of the exterior ring, I think the should be ordered
> anti-clockwise, please check this in advance
> 3) use the good old pythagoras, a*a + b*b = c*c

Assuming the spatial reference system is not geodetic (e.g, WGS84).
If it is, you'll have to use the GeodeticCalculator to get discances
between the points. Pythagoras is a nice guy, but works fine only
when the reference surface is planar ;-)

Cheers
Andrea

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Geotools-gt2-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
mbedward

Re: Get length of each side in a polygon

Reply Threaded More More options
Print post
Permalink
2009/10/26 Andrea Aime wrote:
> Pythagoras is a nice guy, but works fine only
> when the reference surface is planar ;-)
>

Actually, if the legends are to be believed, Pythagoras was definitely
NOT a nice guy !

Vurfem, Adrea's comment applies to the code example that I posted
earlier, ie. it will only work for planar coordinates.

Michael

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Geotools-gt2-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
mbedward

Re: Get length of each side in a polygon

Reply Threaded More More options
Print post
Permalink
In reply to this post by mbedward
Hi Vurfem,

> Thank you for your reply. I try the code you have suggested. But when I run
> the code it gives me an error
>
> "com.vividsolutions.jts.geom.MultiPolygon cannot be cast to
> com.vividsolutions.jts.geom.Polygon

Sorry - my fault. I should have thought of that.  You can do this...

MultiPolygon multiPoly = (MultiPolygon) feature.getDefaultGeometry();
Polygon poly = (Polygon) multiPoly.getGeometryN( 0 );

Or if you expect any of your multi-polygons will contain more than one
polygon you can do this instead...

for (int i = 0; i < multiPoly.getNumGeometries(); i++) {
    Polygon poly = (Polygon) multiPoly.getGeometryN( i );
    ...

Hope that helps.

Michael

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Geotools-gt2-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users
Christian Müller-3

Re: Get length of each side in a polygon

Reply Threaded More More options
Print post
Permalink
In reply to this post by aaime
Yep, you are right here. But JTS assumes planar surfaces. If you want to
compare the calculated results with the result of the calculated JTS
perimiter, Pythagoras is your friend.

Assuming a geodetic reference system, the perimeter an the area too are
problematic.

 

Andrea Aime writes:

> Christian Müller ha scritto:
>> A possibility would be  
>>
>> 1) Get the exterior ring of the polygon
>> 2) get the points of the exterior ring, I think the should be ordered
>> anti-clockwise, please check this in advance
>> 3) use the good old pythagoras, a*a + b*b = c*c
>
> Assuming the spatial reference system is not geodetic (e.g, WGS84).
> If it is, you'll have to use the GeodeticCalculator to get discances
> between the points. Pythagoras is a nice guy, but works fine only
> when the reference surface is planar ;-)
>
> Cheers
> Andrea
 


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Geotools-gt2-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users