Gdal reads and writes vector files -- C #

Source: Internet
Author: User

When using OGR in C # To Read and Write Vector Data, you must reference "using osgeo. OGR ;". At the same time, in order to process the Chinese path and the Chinese field, you need to set the following two attributes at the beginning, the Code is as follows:

// To support the Chinese path, add the following code (not required in most cases) // osgeo. gdal. gdal. setconfigoption ("gdal_filename_is_utf8", "no"); // to make the Attribute Table field support Chinese characters, add the following osgeo statement. gdal. gdal. setconfigoption ("shape_encoding ","");

1. Read Vector

Read the vector Code as follows, as shown in result 1. The post-processing result 2 is filtered by attribute.

Static void readvectorfile () {// to support Chinese paths, add the following code osgeo. gdal. gdal. setconfigoption ("gdal_filename_is_utf8", "no"); // to make the Attribute Table field support Chinese characters, add the following osgeo statement. gdal. gdal. setconfigoption ("shape_encoding", ""); string strvectorfile = "E: \ datum \ gdalcstest \ debug \ Beijing. SHP "; // register all drivers. registerall (); // open the data datasource DS = OGR. open (strvectorfile, 0); If (DS = NULL) {console. writeline ("opening file [{0}] failed! ", Strvectorfile); return;} console. writeline (" opening file [{0}] successful! ", Strvectorfile); // obtain the number of layers in the data source. Generally, there is only one SHP data layer. If it is MDB or DXF, there will be multiple int ilayercount = Ds. getlayercount (); // obtain the first layer Layer olayer = Ds. getlayerbyindex (0); If (olayer = NULL) {console. writeline ("failed to get layer {0! \ N ", 0); return;} // initializes the layer. If a filtering operation is performed on the layer, all the previous filtering operations are cleared. resetreading (); // filter the elements in the layer using the SQL statement of the attribute table. For details, refer to the SQL query section olayer. setattributefilter ("\" name99 \ "like \" Beijing Municipal District \ ""); // filter the elements in the layer using the specified geometric object // olayer. setspatialfilter (); // filter the elements in the layer by the specified four to the range. // olayer. setspatialfilterrect (); // obtain the Attribute Table header in the layer and output the console. writeline ("attribute table structure information:"); featuredefn odefn = olayer. getlayerdefn (); int ifieldcount = Odefn. getfieldcount (); For (INT iattr = 0; iattr <ifieldcount; iattr ++) {fielddefn ofield = odefn. getfielddefn (iattr); console. writeline ("{0 }:{ 1} ({2 }. {3}) ", ofield. getnameref (), ofield. getfieldtypename (ofield. getfieldtype (), ofield. getwidth (), ofield. getprecision ();} // number of elements in the output layer console. writeline ("number of elements = {0}", olayer. getfeaturecount (0); feature ofeature = NULL; // the elements in the layer are traversed below while (Ofeature = olayer. getnextfeature ())! = NULL) {console. writeline ("currently processed {0}: \ n property value:", ofeature. getfid (); // get the Attribute Table content in the element for (INT ifield = 0; ifield <ifieldcount; ifield ++) {fielddefn ofielddefn = odefn. getfielddefn (ifield); fieldtype = ofielddefn. getfieldtype (); Switch (type) {casefieldtype. oftstring: console. writeline ("{0} \ t", ofeature. getfieldasstring (ifield); break; casefieldtype. oftreal: console. writeline ("{0} \ t", ofeature. get Fieldasdouble (ifield); break; casefieldtype. oftinteger: console. writeline ("{0} \ t", ofeature. getfieldasinteger (ifield); break; default: console. writeline ("{0} \ t", ofeature. getfieldasstring (ifield); break ;}// obtain the ry ogeometry = ofeature in the element. getgeometryref (); // for demonstration, only one element Information break is output;} console. writeline ("disabled dataset! ");}

Tu Figure 1 OGR library use C # Read vector Example 1

Figure 2 example of using C # to read a vector in the OGR library 2

2. Write Vector

When using C # To create a vector image, use a string in WKT format to create it. You can also create an instance in other ways. The Code is as follows. The written vector graphics and Attribute Table 3 are shown.

Static void writevectorfile () {// to support Chinese paths, add the following code osgeo. gdal. gdal. setconfigoption ("gdal_filename_is_utf8", "no"); // to make the Attribute Table field support Chinese characters, add the following osgeo statement. gdal. gdal. setconfigoption ("shape_encoding", ""); string strvectorfile = "E: \ datum \ gdalcstest \ debug \ testpolygon. SHP "; // register all drivers. registerall (); // create data. Here we use the SHP File Created for ESRI as an example string strdrivername = "esrishapefile"; driver odriver = OGR. getdriverbyname (strdr Ivername); If (odriver = NULL) {console. writeline ("% s driver unavailable! \ N ", strvectorfile); return;} // create the data source datasource ODS = odriver. createdatasource (strvectorfile, null); If (ODS = NULL) {console. writeline ("failed to create Vector file [% s! \ N ", strvectorfile); return;} // creates a polygon layer. No space reference is specified here. If necessary, specify layer olayer = ODS here. createlayer ("testpolygon", null, wkbgeometrytype. wkbpolygon, null); If (olayer = NULL) {console. writeline ("layer creation failed! \ N "); return;} // create an Attribute Table below // first create an integer attribute fielddefn ofieldid = newfielddefn (" fieldid ", fieldtype. oftinteger); olayer. createfield (ofieldid, 1); // create another struct attribute named featurename with a length of 50 fielddefn ofieldname = newfielddefn ("fieldname", fieldtype. oftstring); ofieldname. setwidth (100); olayer. createfield (ofieldname, 1); featuredefn odefn = olayer. getlayerdefn (); // create a triangle element feature ofeaturetriangle = newf Eature (odefn); ofeaturetriangle. setfield (0, 0); ofeaturetriangle. setfield (1, "Triangle"); geometry geomtriangle = geometry. createfromwkt ("polygon (0)"); ofeaturetriangle. setgeometry (geomtriangle); olayer. createfeature (ofeaturetriangle); // create a rectangle element feature ofeaturerectangle = newfeature (odefn); ofeaturerectangle. setfield (0, 1); ofeaturerectangle. setfield (1, "rectangle"); geometry geomrectangle = Geometry. createfromwkt ("polygon (30, 60, 60, 30, 30, 30, 0)"); ofeaturerectangle. setgeometry (geomrectangle); olayer. createfeature (ofeaturerectangle); // create a pentagram element feature ofeaturepentagon = newfeature (odefn); ofeaturepentagon. setfield (0, 2); ofeaturepentagon. setfield (1, "pentagram"); geometry geompentagon = geometry. createfromwkt ("polygon (70 15,700,)"); ofeaturepentagon. setgeom Etry (geompentagon); olayer. createfeature (ofeaturepentagon); console. writeline ("\ n dataset created! \ N ");}

Figure 3 vector image written

3. Vector Data Management
Static void vectordelete (string strvectorfile) {// register all drivers OGR. registerall (); driver odriver = NULL; {// open the vector datasource ODS = OGR. open (strvectorfile, 0); If (ODS = NULL) {file. delete (strvectorfile); return;} odriver = ODS. getdriver (); If (odriver = NULL) {file. delete (strvectorfile); return ;}} if (odriver. deletedatasource (strvectorfile) = OGR. ogrerr_none) return; else file. delete (strvectorfile);} staticvoid vectorrename (string stroldfile, string strnewfile) {// register all drivers OGR. registerall (); driver odriver = NULL; {// open the vector datasource ODS = OGR. open (stroldfile, 0); If (ODS = NULL) {file. move (stroldfile, strnewfile); return;} odriver = ODS. getdriver (); If (odriver = NULL) {file. move (stroldfile, strnewfile); return;} datasource odds = odriver. copydatasource (ODS, strnewfile, null); If (odds = NULL) {file. move (stroldfile, strnewfile); Return ;}} if (odriver. deletedatasource (stroldfile) = OGR. ogrerr_none) return; else file. move (stroldfile, strnewfile );}

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.