C # create and write a shape file using the gdal/OGR Library

Source: Internet
Author: User

Gdal/OGR is a well-known open source GIS library. gdal operates on raster data while OGR operates on vector data, which is equivalent to a common data access library. This library is even used in ESRI products.

Currently, gdal/OGR also provides support for. Net compilation. For details about the compilation and usage process, refer to Google. Some examples of C, C ++, and Python are available on the official website.Code. But there is no sample code for C #,ProgramSimilar to each other, the following is a sample code for creating a shape file in C # and adding a point to it.

// Register OGR Library
String Pszdrivername =   " ESRI shapefile " ;
Osgeo. OGR. OGR. registerall ();
 
// Call the driver interface for reading and writing Shape files
Osgeo. OGR. Driver podriver = Osgeo. OGR. OGR. getdriverbyname (pszdrivername );
If (Podriver =   Null )
MessageBox. Show ( " Driver Error " );
 
// Use this driver to create a shape File
Osgeo. OGR. datasource pods;
Pods = Podriver. createdatasource ( " Point_out.shp " , Null );
If (Pods =   Null )
MessageBox. Show ( " Datasource creation error " );

// Create Layer
Osgeo. OGR. layer polayer;
Polayer = Pods. createlayer ( " Point_out " , Null , Osgeo. OGR. wkbgeometrytype. wkbpoint, Null );
If (Polayer =   Null )
MessageBox. Show ( " Layer creation failed " );

// Create two attribute Columns
Osgeo. OGR. fielddefn ofield =   New Osgeo. OGR. fielddefn ( " Name " , Osgeo. OGR. fieldtype. oftstring );
Ofield. setwidth ( 16 );
Osgeo. OGR. fielddefn ofield2 =   New Osgeo. OGR. fielddefn ( " Height " , Osgeo. OGR. fieldtype. oftinteger );
Polayer. createfield (ofield, 1 );
Polayer. createfield (ofield2, 0 );

// Create a feature and a point
Osgeo. OGR. Feature pofeature =   New Feature (polayer. getlayerdefn ());
Osgeo. OGR. Geometry PT =   New Geometry (osgeo. OGR. wkbgeometrytype. wkbpoint );
// Write 20 points to the created shape,
For ( Int I =   100 ; I <   120 ; I ++ )
{
// Attribute 1 "name" value assignment
Pofeature. setfield ( 0 , " Point " + I. tostring ());
// Attribute 2 "height" value assignment
Pofeature. setfield ( 1 , I );
// Add coordinate point
PT. addpoint (I, I, 0 );
Pofeature. setgeometry (PT );
// Write the Feature Element points with coordinates and attributes to the layer.
Polayer. createfeature (pofeature );
}

//Disable file read/write
Pofeature. Dispose ();
Pods. Dispose ();

The definition of the projection coordinate system is not involved here, and will be improved later.

Reprinted from: http://www.cnblogs.com/webgis8/archive/2009/07/19/giswei.html

C # Read and Write shape projection information using the gdal/OGR Library(Reprinted)

 

 

today, I spent a little time reading and writing the projection information in shape in OGR in C.
for information about the read/write vectors and attributes, see the previous C # create and write a shape file using the gdal/OGR library, the test passes [original]
the specific process is
// projection experiment area
osgeo. OSR. spatialreference SS;
SS = orglayer. getspatialref (); // The orglayer is osgeo. OGR. layer instances
string PPSS;
SS. exporttowkt (Out PPSS); // output the shape projection information of the read file to the formatted string PPSS
MessageBox. show (PPSS);
very simple.
for example, the output encoding string is
geogcs ["gcs_north_american_1927", datum ["north_american_datum_1927", spheroid ["clarke_1866", 6378206.4, 294.9786982], primem ["Greenwich", 0], Unit ["degree", 0.0174532925199433]
This string is the standard WKT string in OGR

The process of writing projection information to the new shape is also relatively simple.
String strwkt = "geogcs [\" gcs_north_american_1927 \ ", datum [\" north_american_datum_1927 \ ", spheroid [\" clarke_1866 \ ", 6378206.4, 294.9786982], primem [\ "Greenwich \", 0], Unit [\ "degree \", 0.0174532925199433] ";
Note that the double quotation marks in the string are \"
Osgeo. OSR. spatialreference SRS = new osgeo. OSR. spatialreference (strwkt );
// In the layer creation function, the second parameter is // osgeo. OSR. spatialreference.
In this case
Polayer = pods. createlayer ("layername1", SRS, osgeo. OGR. wkbgeometrytype. wkbpoint, null );

 

Reprinted from: http://www.cnblogs.com/webgis8/archive/2009/08/05/1539305.html

 

 

Osgeo. OGR. OGR. registerall (); why can't I execute it? An exception occurs when the type of "osgeo. OGR. OGR" is set.

 

This problem is caused by incomplete DLL. In addition to the four DLL files to be referenced, there are also five DLL files to be placed in the DEBUG directory.
When compiling gdal under C #, a total of nine DLL files are generated. On the compiled local machine, the program finds several other DLL files through the Environment Variable path.
On a computer that has not compiled gdal, put the nine compiled DLL files under debug and everything is okay.

 

Download the C # version class library with gdal

Related Article

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.