Use OGR to create Vector Data in DXF format

Source: Internet
Author: User

Using the OGR library to create data in the DXF format is basically the same as creating data in the SHP format. The code is identical except the registration driver.

Note that the OGR creation of the DXF format requires the head. DXF in the data folder. Therefore, you must first set the directory of gdal_data. Use the following code to set the environment variables. You can also set the environment variables directly, but it is troublesome. It is easier to set the environment variables using the code:


After using the preceding settings, you can create Vector Data in the DXF format. In addition, the DXF format does not support attribute tables. If you create an attribute table, gdal prompts information such as an error occurred while creating the Attribute Table. The following code creates a DXF file for a rectangle:

# Include "ogrsf_frmts.h" int main () {// set the gdal_data directory cplsetconfigoption ("gdal_data", "E: \ gdal \ data "); // define the driver const char * pszdrivername = "DXF" for DXF; // register the OGR driver ogrregisterall (); ogrsfdriver * podriver; podriver = ogrsfdriverregistrar: getregistrar () -> getdriverbyname (pszdrivername); If (podriver = NULL) {printf ("% s driver not available. \ n ", pszdrivername); exit (1);} ogrdatasource * pods; // create a file pods = podrive R-> createdatasource ("E: \ rect. DXF ", null); If (pods = NULL) {printf (" creation of output file failed. \ n "); exit (1) ;}ogrlayer * polayer; // create a layer. It is estimated that many layers can be created in the DXF, polayer = pods-> createlayer (" rect ", null, wkbpolygon, null); If (polayer = NULL) {printf ("layer creation failed. \ n "); exit (1) ;}// create an element. Because DXF does not support attribute tables, ogrfeature * pofeature is not created. pofeature = ogrfeature :: createfeature (polayer-> getlaye Rdefn (); // create a rectangular polygon ogrlinearring; ring. addpoint (0, 0); ring. addpoint (0,100); ring. addpoint (100,100); ring. addpoint (100,0); ring. closerings (); ogrpolygon polygon; polygon. addring (& RING); // Add polygon to the element pofeature-> setgeometry (& polygon); // write the element to the layer if (polayer-> createfeature (pofeature )! = Ogrerr_none) {printf ("failed to create feature in DXF. \ n "); exit (1);} // release element ogrfeature: destroyfeature (pofeature); // close the dataset ogrdatasource: destroydatasource (pods );}

After executing the above Code, a rect. DXF file will be generated on the E disk. The effect of opening it with qgis is as follows (AutoCAD is not installed on the computer and you do not know what it looks like to open it with CAD ):

In addition, there is a bug in opening the DXF file using gdal. I submitted it to the gdal official website, but it has not been resolved. In general, the DXF driver in OGR is still not perfect. Hope to improve as soon as possible, bug address:

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: 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.