ArcGIS API for flex extension-obtain the center point coordinate of polygon

Source: Internet
Author: User
ArcGIS API for Flex is mainly built on ArcGIS Server rest API. The client has limited functions and many advanced functions are implemented through geometry service and geoprocessing service. However, some simple functions, such as getting the center point of polygon, bring up a custom window at this point. If services are used for interaction, the user experience may deteriorate. Therefore, you can write some code to implement these functions on the client.

Here is the code I wrote to get the coordinates of the center point from a polygon:
/**
* Author: Wu Yongfeng
* MSN/Email: warrenwyf@gmail.com
*/
Package wuyf
{
Import com. ESRI. AGS. Geometry. extent;
Import com. ESRI. AGS. Geometry. mappoint;
Import com. ESRI. AGS. Geometry. polygon;

Public class geometryutil
{
/**
* Obtain the center of gravity of a polygon.
* @ Param polygon Polygon
* @ Return center of gravity
*
*/
Static public function getgravitycenter (polygon): mappoint
{
VaR Ext: extent = polygon. extent;
VaR P0: mappoint = new mappoint (ext. xmin, ext. ymin );

VaR momentx: Number = 0;
VaR momenty: Number = 0;
VaR weight: Number = 0;
For (var I: Int = 0; I {
VaR PTS: array = polygon. RingsAs array;
For (var j: Int = 0; j {
VaR P1: mappoint = polygon. getpoint (I, j );
VaR P2: mappoint;
If (j = pts. Length-1)
{
P2 = polygon. getpoint (I, 0 );
}
Else
{
P2 = polygon. getpoint (I, j + 1 );
}

VaR dweight: Number = (p1.x-0000x) * (p2.y-p1.y)
-(P1.x-Snapshot X) * (p0.y-p1.y)/2
-(P2.x-Snapshot X) * (p2.y-Snapshot y)/2
-(P1.x-p2.x) * (p2.y-p1.y)/2;
Weight + = dweight;

VaR pTMP: mappoint = new mappoint (p1.x + p2.x)/2, (p1.y + p2.y)/2 );
VaR gravityx: Number = percentile x + (pTMP. x-p0.x) * 2/3;
VaR gravityy: Number = 127y + (pTMP. y-p0.y) * 2/3;
Momentx + = gravityx * dweight;
Momenty + = gravityy * dweight;
}
}

Return new mappoint (momentx/weight, momenty/weight );
}

}
}

See the results:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.