The Gdal library calculates the centroid method.

Source: Internet
Author: User

The Gdal library calculates the centroid method.

The method for calculating the centroid in the Gdal library is as follows:

int ogrgeometry::centroid (Ogrpoint *popoint ) const

In its function implementation, is called the Geosgetcentroid () method in the GeoS library, and eventually the bool in GEOS

The geometry::getcentroid (coordinate& ret) const function creates a Centroidarea object and adds the geometric object to its centroid. The code is as follows:

Centroidarea cent;

Cent.add (this);

if (! cent.getcentroid (c))

return false;

Visible, the key is this kind of centroidarea.

Returned centroid coordinates result:

ret = coordinate (cg3.x/3.0/areasum2, cg3.y/3.0/areasum2);

Calculation process:

The method of this class adds a Geometry object, which eventually takes the following steps (omit inner loop considerations):

void

Centroidarea::addshell (const coordinatesequence *pts)

{

BOOL ispositivearea=! CGALGORITHMS::ISCCW (pts);

std::size_t Const N=pts->getsize ()-1;

for (std::size_t i=0; i<n; ++i)

{

Addtriangle (basept, Pts->getat (i), Pts->getat (i+1), Ispositivearea);

}

Addlinearsegments (*pts);

}

basept in a function The first point that is initialized to a ring.

As the code describes, each point is added to the class in turn, calling the private method Addtriangle, whose code is as follows:

Centroidarea::addtriangle (const coordinate &P0, const coordinate &P1,

const coordinate &P2, BOOL Ispositivearea)

{

Double sign= (ispositivearea)? 1.0:-1.0;

CENTROID3 (P0,P1,P2,TRIANGLECENT3);

Double area2res=area2 (P0,P1,P2);

cg3.x+=sign*area2res*trianglecent3.x;

CG3.Y+=SIGN*AREA2RES*TRIANGLECENT3.Y;

Areasum2+=sign*area2res;

}

The member properties CG3 and Areasum2 get the value.

Where area2res and TRIANGLECENT3 are calculated as follows:

Double centroidarea::area2 (const coordinate &P1, const coordinate &P2, const coordinate &P3)

{

Return (p2.x-p1.x) * (P3.Y-P1.Y)-(p3.x-p1.x) * (P2.Y-P1.Y);

}

The calculation is twice times the area.

void centroidarea::centroid3 (const coordinate &P1, const coordinate &P2,

const coordinate &p3, coordinate &c)

{

c.x=p1.x+p2.x+p3.x;

C.Y=P1.Y+P2.Y+P3.Y;

}

The XY of C, divided by 3, is the mean value of P1,P2,P3.

There are other branches, which seem to be some alternatives, which are skipped here.

So it is quite a traversal of all the points, the 0 points of the ring and the current point and the current point of the next point of a triangle to make a cumulative calculation. For each step:

The polygon centroid calculation formula on the wiki:

The center of a non-self-closing polygon determined by n vertices (xi, Yi) can be calculated as follows: [4]

The notation ( XN, YN) is the same as the vertex ( x0, y0) . The area of the polygon is:

The center of the polygon is given by the following formula:

In the area of the center to ask:

The area center is very similar to the center of mass, and the area center depends only on the geometry of the shape. if the object is homogeneous, the center of mass will be located in the center of the area .

For a two-part graph, there will be the following equation:

is the distance from the area center of a specific section to the selected reference system. Is the area of a particular section.

When a complex geometry can be divided into known simple geometries, the area centers of each part are calculated first, and then the area center of the entire graph is calculated by the following general formula:

The distance from the y-axis to the center here is, the distance from the x-axis to the center is, the center coordinates are.

http://zh.wikipedia.org/wiki/%E5%87%A0%E4%BD%95%E4%B8%AD%E5%BF%83

The Gdal library calculates the centroid method.

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.