QGis Creating a temporary layer and adding features to C + + development

Source: Internet
Author: User

Development environment: WIN10 + VS2010 + Qt 4.8.6 + QGis 2.14.4

In fact, the function of this article is similar to a simplified version of "Add text data Layer" in Qgis, this article does not involve the use of dialog box, do not create features by interacting with the user, but directly through the code to add point features, play a role.

(a) First, the whole process is probably combed down:

1, first create a temporary (memory) vector layer;

2. Add the created layer to the map canvas;

3, the creation of geometric elements;

4. Add geometric elements to the vector layer;

5. Update the layer extent and refresh the canvas.

(b) Program code:

1 //Test Code2 3 /*"point?crs=epsg:4326&field=id:integer&4 * field=name:string (&index=yes&)5 * memoryid={63152c31-9f38-4410-9983-fc9abe84973f} "6  */7QString layerproperties ="Point ?";//Geometry Type8Layerproperties.append (QString ("crs=epsg:4326&"));//Reference coordinate system9Layerproperties.append (QString ("field=id:integer&field=name:string (&)"));//Add FieldTenLayerproperties.append (QString ("index=yes&"));//Create an index OneLayerproperties.append (QString (//Temporary Encoding A "memoryid=%1"). Arg (Quuid::createuuid (). toString () )); -  -qgsvectorlayer* Newlayer =NewQgsvectorlayer ( theLayerproperties, QString ("Staging Point Layer"), QString ("Memory" ) ); -  - if(!newlayer->isValid ()) - { +     return false; - } +  A //Add to map atQgsmaplayerregistry::instance ()Addmaplayer (newlayer); -  -qgsvectordataprovider* DateProvider = newlayer->Dataprovider (); -  - //Create Point - qgsfeature myfeature; inMyfeature.setgeometry (Qgsgeometry::frompoint (Qgspoint (102.4443,32.2123)) ); -Myfeature.setattributes (Qgsattributes () << qvariant (1) << Qvariant ("Test")); to  + qgsfeature MyFeature1; -Myfeature1.setgeometry (Qgsgeometry::frompoint (Qgspoint (102.4643,32.2133)) ); theMyfeature1.setattributes (Qgsattributes () << qvariant (2) << Qvariant ("test1")); *  $ //Start editingPanax NotoginsengNewlayer->startediting (); -  the //Add features +Dateprovider->addfeatures (Qgsfeaturelist () << myfeature <<MyFeature1); A  the //Save +Newlayer->commitchanges (); -  $ //Update Scope $Newlayer->updateextents (); -Mmapcanvas->refresh (); - return true;

(c) Code Analysis:

Create a temporary layer

第3-11 line, this code may be I and other online tutorials in a different place, here by constructing a URL form of a string, through the above comments people should probably understand the meaning of this string, through this form is very simple and convenient to make the layer we will create a number of conditions, In particular, simplify adding to layer fields:

Line 7th QString layerproperties = "point?" defines the geometry type of the layer we create, which can be "point", "LineString", "Polygon", "MultiPoint", " Multilinestring "," Multipolygon "one of them;

Line 8th QString ("crs=epsg:4326&") is the reference coordinate system of the layer, it is a good habit to define a correct coordinate system, if you need some flexibility can be selected by the dialog box in the way of Qgis, or according to your own needs to implement , the only thing that needs to change is "epsg:4326" ;

Line 8th QString ("Field=id:integer&field=name:string (&)") is a defined layer field, which is also a convenient place for me, where multiple fields are connected with "&" , the complete form is field=name:type (length,precision) , from the parameter see not only can define length also can define its precision;

Line 10th QString ("index=yes&") is defined as a spatial index, which is useful for layers with large data volumes;

Line 11th QString ("memoryid=%1"). Arg (Quuid::createuuid (). toString ()) is interesting, and it creates a globally unique identifier (UUID) through Quuid. The interpretation in QT is primarily used for entity identities in distributed computing environments, and here is the unique identifier used when we create temporary layers multiple times;

The 第3-5 line is a full display of the URL form string, containing all of the above, and a string of numbers in the last {} is an identity created automatically by Quuid. Although I have not tried, but should be in addition to the geometry type must be defined, the others are optional, of course, if we adopt this approach is not only to define a layer of the geometry type.

There's a lot of work going on up there, and line 13th really creates a temporary layer:

New " Staging Point Layer " " Memory " ) );

The Qgsvectorlayer class has 3 parameters, the 1th parameter is the URL form of the string layerproperties, another very simple way is to directly define its layer geometry type can be, such as "point"; the 2nd parameter is the name of the layer The 3rd parameter is the type of the layer created, where the incoming "memory" represents the creation of a temporary layer.

add a layer to the map canvas

The layer 21st, 23 will be created is added to the map canvas, and a qgsvectordataprovider pointer is obtained, which is used later.

Creating geometric features

第25-32 Line created two point features to show the results, many examples on the web in order to make people more clear, is written separately, I this more convenient:

Myfeature.setgeometry (Qgsgeometry::frompoint (Qgspoint (102.444332.2123));

After using Setgeometry () to set the geometry of the feature, we continue to write the attribute with SetAttributes (), the property type, the order is consistent with what we created above, and the SetAttributes () parameter is the Qgsattributes object. And Qgsattributes is actually qvector<qvariant> The Qvariant object that is being added in the following code.

Myfeature.setattributes (Qgsattributes () << qvariant (1) << qvariant ("test "));

The two lines of code above succeeded in creating the geometric feature and setting its properties.

Adding geometric features to vector layers

第34-41 We use the Qgsvectordataprovider pointer obtained above to add geometric features to the vector layer, we first use startediting () to make the layer editable, and then use the CommitChanges () To submit the change results.

Refresh

Finally, update the layer extent and refresh the canvas to be OK.

Here to say, if you follow this method to run, in the map canvas does not display properly, check whether the canvas is not thawed, that is:Mapcanvas ()->freeze (false) , I was here to eat a loss, because only contact Qgis development soon, I froze the canvas in other parts of the program, causing the layer to not display properly, and I did it many times before I found it.

(iv):

Qgis development is just my hobby, try to own a little learning experience and share with you, such as in the above content has incorrect place, or there can be improved places welcome to correct.

QGis Creating a temporary layer and adding features to C + + development

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.