A Preliminary Exploration of Space Superposition Function union in ogr1.10

Source: Internet
Author: User

Recently, it was found that the gdal/OGR Library released version 1.10. The new version adds the space overlay analysis function for vector layers, added functions such as Union, intersection, clip, erase, and identify to the ogrlayer class. After a simple attempt at the Union function, it is still very good and easier to use than ArcGIS. Based on your understanding and the help provided by OGR, we will briefly introduce the usage of the Union function. The usage of other functions should be similar to that of the Union function. Sorry.

The implementation of these functions requires the support of the geos library, that is, the switch of the geos library should be enabled when the gdal library is compiled. There are many methods on the Internet for the combined compilation of the gdal/geos library, for details, refer to the link http://blog.csdn.net/wangqinghao/article/details/8279672. After compilation, you can call the union function in the ogrlayer object. Let's take a look at the prototype of the Union function: Union
(Ogrlayer * playermethod, ogrlayer * playerresult, char ** papszoptions = NULL, gdalprogressfunc pfnprogress = null, void * pprogressarg = NULL), according to the explanation provided by OGR, the first three parameters are important:

Playermethod: The layer to be combined with the current layer. If I want to perform Union operations on layer1 and Layer2 and call the union function through the layer1 object, Layer2 is playermethod and cannot be empty.

Playerresult: Obviously, the layer storing the Union result cannot be blank. Here, it can be a layer created with no elements, or a new layer created with OGR in the program. My personal tendency is to create one with code in the program, simple code can be implemented.

Papszoptions: the two-dimensional pointer actually includes four parameters -- skip_failures = YES/NO, promote_to_multi = YES/NO, input_prefix = string, and method_prefix = string. When skip_failures = Yes, if an error occurs in merging a certain element, the merging of subsequent elements will be skipped. When promote_to_multi = Yes, You can convert a single-space object to a multi-space object. For example, you can convert polygons to multipolygons and linestrings to multilinestrings. Input_prefix = string: Enter the pre-mark of the attribute field of the layer in the result layer. Method_prefix = string, the attribute field of the operation layer is marked in the result layer. Take layer1 and Layer2 for example. The result layers after layer1 and Layer2 merge contain all attribute fields of layer1 and Layer2. If we set input_prefix = 1, method_prefix = 2, in the result layer field, all fields from layer1 are prefixed with "1" before the field name, and the fields from Layer2 are prefixed with "2 ".

The following two parameters are not attempted.

The following shows a specific sample code.

Char * filepath = "D: \ ceshi_data \ ceshi_new"; char * layername1 = "layer1"; char * layername2 = "Layer2"; ogrlayer * player1 = NULL; ogrlayer * player2 = NULL; ogrdatasource * pods = NULL; ogrregisterall (); pods = ogrsfdriverregistrar: open (filepath, true ); // read the two layers player1 = pods-> getlayerbyname (layername1); player2 = pods-> getlayerbyname (layername2) to be union ); // create a result layer ogrlayer * presultlayer = NULL; presultlayer = pods-> createlayer ("result", player2-> getspatialref (), wkbmultipolygon, null ); // configure the third parameter char ** P = new char * [4] in the Union function; P [0] = "skip_failures = yes "; P [1] = "promote_to_multi = yes"; P [2] = "input_prefix = 1"; P [3] = "method_prefix = 2"; player2-> Union (player1, presultlayer, P, null, null); // write the edits to the presultlayer to a file. If this sentence is not added, the result file will not record presultlayer-> synctodisk (); ogrdatasource :: destroydatasource (pods );

For example:

Layer1

Layer2:

Result layer result:

If the attribute fields in layer1 and Layer2 are the same, in the example, you want to save an attribute field in the result layer, when creating a result layer, you can create an attribute field set that is the same as that of layer1 for the result layer. Make the following changes in the Code:

// Create a result layer ogrlayer * presultlayer = NULL; presultlayer = pods-> createlayer ("result", player2-> getspatialref (), wkbmultipolygon, null ); // create the ogrfeaturedefn * pogrfeaturedefn = player1-> getlayerdefn (); For (INT I = 0; I <pogrfeaturedefn-> getfieldcount (); I ++) presultlayer-> createfield (pogrfeaturedefn-> getfielddefn (I); player2-> Union (player1, presultlayer, P, null, null );

The property table of the generated result layer is as follows:

The above is some simple usage of union, and several other functions are basically the same. If not, please criticize and correct it!

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.