Use OGR to create an arc chart

Source: Internet
Author: User

In many cases, we need to record some arc images in the Vector file. Because currently, OGR does not support Saving Vector images by means of center radius, therefore, the coordinate points on the arc must be calculated to store these vector images. It is really troublesome to create these points according to the ARC algorithm, OGR provides a function called approximatearcangles to calculate the coordinates of arc points. It is very convenient to use. The following is a simple description of this function. Function prototype:

Create an elliptical arc segment. Specify the coordinates of the origin, the length of the long half axis of the elliptic, the length of the short half axis, the rotation angle, the starting angle, and the ending angle to generate a section of the arc segment. The parameter dfmaxanglestepsizedegrees is used to specify the maximum step size. You can use the configuration option ogr_arc_stepsize to set the default value.

As shown in Arc Segment 1, the two white lines on the left and the lower sides are the X and Y axes, the intersection is the coordinate origin, and the center coordinate of the green elliptic is (dfcenterx, dfcentery, DFZ ), the two blue line segments in the length of the long half axis and the short half axis are displayed. the purple line segments in the start angle and end angle direction rays are shown. The red arc segments in the figure are generated.


Figure 1 function parameter description

OGRGeometry * OGRGeometryFactory::approximateArcAngles(double dfCenterX, double dfCenterY, double dfZ, double dfPrimaryRadius, double dfSecondaryRadius, double dfRotation, double dfStartAngle, double dfEndAngle, double dfMaxAngleStepSizeDegrees )

Parameter description:

Dfcenterx

Elliptical origin X coordinate

Dfcentery

Y coordinate of the elliptical Origin

DFZ

Zcoordinate of the elliptical Origin

Dfprimaryradius

Elliptical long half-axis length

Dfsecondaryradius

Length of the shorter half-axis of an ellipse

Dfrotation

Clockwise rotation angle of an elliptic

Dfstartangle

Arc Start Point angle (clockwise angle from the X axis)

Dfendangle

Angle of the arc end point (clockwise angle from the X axis)

Dfmaxanglestepsizedegrees

The angle corresponding to the longest step of the arc segment. If it is set to 0, it is calculated by default.

Through the above function parameter description and graphics should be clear, the following uses a piece of code to create several graphics for testing. The test code is as follows:

Void writevectorfile1 () {// to support the Chinese SHP path, add the following code cplsetconfigoption ("gdal_filename_is_utf8", "no "); # ifdef creatshp // to make the Attribute Table field support Chinese characters, add the following sentence, create shpcplsetconfigoption ("shape_encoding", ""); const char * pszvectorfile = "E: \ testarc. SHP "; const char * pszdrivername =" ESRI shapefile "; # else // create dxfcplsetconfigoption (" gdal_data "," C: \ warmerda \ gdal110 \ data "); const char * pszvectorfile = "E: \ testarc. DXF "; Co NST char * pszdrivername = "DXF"; # endif // register all drivers ogrregisterall (); // create ogrsfdriver * podriver = ogrsfdriverregistrar: getregistrar () -> getdriverbyname (pszdrivername); If (podriver = NULL) {printf ("% s driver unavailable! \ N ", pszdrivername); return;} // create the data source ogrdatasource * pods = podriver-> createdatasource (pszvectorfile, null); If (pods = NULL) {printf ("failed to create Vector file [% s! \ N ", pszvectorfile); return;} // creates a layer and a line layer. No space reference is specified here. If necessary, you need to specify ogrlayer * polayer = pods-> createlayer ("testpolygon", null, wkblinestring, null) here; if (polayer = NULL) {printf ("layer creation failed! \ N "); ogrdatasource: destroydatasource (pods); return;} // The property table is not supported due to DXF, therefore, the following OGR statements will prompt an error message // create an Attribute Table // first create an integer property named fieldid ogrfielddefn ofieldid ("fieldid", oftinteger ); polayer-> createfield (& ofieldid); // create another struct attribute named featurename. The character length is ogrfielddefn ofieldname ("fieldname", oftstring); ofieldname. setwidth (100); polayer-> createfield (& ofieldname); ogrfeaturedefn * podefn = polayer-> getlayerdefn (); // create two coordinate axes X and yogrfea Ture * pofeaturexaxis = ogrfeature: createfeature (podefn); pofeaturexaxis-> setfield (0, 0); pofeaturexaxis-> setfield (1, "X axis "); ogrlinestring * pgoxaxis = (ogrlinestring *) ogrgeometryfactory: creategeometry (wkblinestring); pgoxaxis-> setpoint (0,-100, 0); pgoxaxis-> setpoint (1,100, 0 ); pofeaturexaxis-> setgeometry (pgoxaxis); polayer-> createfeature (pofeaturexaxis); ogrfeature: destroyfeature (pofeaturexaxi S); // create the Y axis ogrfeature * pofeatureyaxis = ogrfeature: createfeature (podefn); pofeatureyaxis-> setfield (0, 1); pofeatureyaxis-> setfield (1, "Y axis"); ogrlinestring * pgeoyaxis = (ogrlinestring *) ogrgeometryfactory: creategeometry (wkblinestring); pgeoyaxis-> setpoint (0, 0,-100 ); pgeoyaxis-> setpoint (1, 0,100); pofeatureyaxis-> setgeometry (pgeoyaxis); polayer-> createfeature (pofeatureyaxis); ogrfeature: destroyfeat Ure (circle); // create a circular element ogrfeature * pofeaturecircle = ogrfeature: createfeature (podefn); pofeaturecircle-> setfield (0, 2); pofeaturecircle-> setfield (1, "circle"); ogrgeometry * pcircleline = ogrgeometryfactory: approximatearcangles (50.0,-10.0, 0.0, 10.0, 10.0, 0.0, 0.0, 360.0, 1.0 ); pofeaturecircle-> setgeometry (pcircleline); polayer-> createfeature (pofeaturecircle); ogrfeature: destroyfeature (pofea Turecircle); // create a circular element ogrfeature * pofeatureeclipse = ogrfeature: createfeature (podefn); pofeatureeclipse-> setfield (0, 3); pofeatureeclipse-> setfield (1, "elliptical"); ogrgeometry * pw.seline = ogrgeometryfactory: approximatearcangles (50.0, 50.0, 0.0, 10.0, 20.0, 45.0, 0.0, 360.0, 1.0 ); pofeatureeclipse-> setgeometry (peclipseline); polayer-> createfeature (pofeatureeclipse); ogrfeature: destroyfeature (pofeat Ureeclipse); // create a semi-circular element ogrfeature * pofeaturearc = ogrfeature: createfeature (podefn); pofeaturearc-> setfield (0, 4); pofeaturearc-> setfield (1, "semi-circular"); ogrgeometry * parcline = ogrgeometryfactory: approximatearcangles (-50.0, 50.0, 0.0, 20.0, 20.0, 0.0, 45.0, 215.0, 1.0 ); pofeaturearc-> setgeometry (parcline); polayer-> createfeature (pofeaturearc); ogrfeature: destroyfeature (pofeaturearc); ogrdatasource: de Stroydatasource (pods); printf ("\ n dataset created! \ N ");}

The above is a linear layer. If you want to create a surface image, you need to construct a line element in it as a surface. The figure 2 and Figure 3 created by the above Code are shown. The two axes X and Y are perpendicular to each other to facilitate positioning of several arc segments. There is an elliptic, the center of the elliptic is (50, 50), and the length and half axes are 20 and 10, respectively, at the same time, 45 degrees are rotated, And the generated Arc Range is the whole elliptical arc (0 ~ 360), the sampling interval is 1 degree, that is, the whole Arc Section consists of 360 points. There is also a circle, the center of the circle is (50,-10), the radius is 10, other and elliptical. There is also a semi-arc, the center of the semi-arc is (-50, 50), the circle radius is 20, the starting point is 45 degrees, the end point is 215 degrees, a total of 180 degrees, a semi-arc.

Figure 2 created SHP graph open with ArcMap mark attribute value Figure 3 created DXF graph open with AutoCAD

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.