GDAL python Tutorial (1)--read and write vector data with OGR

Source: Internet
Author: User

The handout and source code for this tutorial are all OpenGIS lessons from Utah State University

Related materials, including handouts, source code, sample data, please download http://www.gis.usu.edu/~chrisg/python/from here

I just do some translation, writing and learning experience, copyright belongs to the original author.

Welcome to reprint, but don't forget the above paragraph.

==================================================

Why use open source?

Advantages

    1. Free, suitable for individuals and small businesses
    2. Powerful development tools that make it easier to find bugs
    3. Across platforms, both Windows and Linux can be used
    4. Shavers

Disadvantages

    1. No embedded geoprocessing processor
    2. Less people to use

Open Source Rs/gis Module

    1. OGR Vector Library: Simple vector data read and write, is part of the Gdal
    2. Gdal geo-Spatial data Abstraction Library:

A) Read and write raster data

b) ArcGIS is also based on the Gdal development

c) C + + library, but can be called with Python

Related modules

    1. Numeric: High-speed array processing, which is especially important for raster data
    2. NumPy: The next generation of numeric
    3. More powerful GIS library http://www.gispython.org/

Import Library:

Import Ogr

Or:

From OSGeo import ogr

The universal approach is:

Try

From OSGeo import ogr

Except

Import Ogr

To read a type of data, you must first load the data driver, which is to initialize an object so that it "knows" a data structure.

Import Ogr

Driver = ogr. Getdriverbyname (' ESRI shapefiles ')

The open () method of the data-driven driver returns a data source object

Open (<filename>, <update>)

Where update 0 is read-only and 1 is writable

For example:

From OSGeo import ogr

Driver = ogr. Getdriverbyname (' ESRI shapefiles ')

filename = ' c:/users/gongwei/documents/my ebooks/python_and_sage/gdal python/test/ospy_data1/sites.shp '

DataSource = driver. Open (filename,0)

If DataSource is None:

print ' could not open '

Sys.exit (1)

print ' done! '

Note that filename must write the absolute path!

Because absolute paths must be used, in order to simplify the code, it is often used to os.chdir ()

Reading the data layer

Layer = Datasource.getlayer (0)

In general, ESRI's shapefiles are filled with 0, and if not, the default is 0.

How many points are there in this data layer?

n = layer. Getfeaturecount ()

print ' Feature count: ', n

Read the upper and lower left and right borders

extent = layer. GetExtent ()

Print ' extent: ', extent

PRINT ' UL: ', extent[0], extent[3]

print ' LR: ', extent[1], extent[2]

Read a feature feature (finally cut to the chase), here is a point to read

feat = layer. Getfeature (41)

FID = feat. GetField (' id ')

Print FID

feat = layer. Getfeature (0)

FID = feat. GetField (' id ') #should be a different ID

Print FID

There are also sequential reads of feature, looping through all the feature

feat = layer. Getnextfeature () #读取下一个

While feat:

feat = layer. Getnextfeature ()

Later. Resetreading () #复位

Extracting the geometric shape of a feature

Geom = feat. Getgeometryref ()

Geom. GetX ()

Geom. GetY ()

Print Geom.

Freeing memory

Feature. Destroy ()

Turn off the data source, which is equivalent to closing files in file system operations

Datasource.destroy ()

After reading it, how to write it.

Create a new file

Driver. CreateDataSource (<filename>)

But the file can't already exist, or it will go wrong.

Create a new layer

Datasource.createlayer (<name>,createlayer (<name>, Geom_type=<ogrwkbgeometrytype>, [SRS])

As an example:

DS2 = driver. CreateDataSource (' test.shp ')

Layer2 = ds2. Createlayer (' Test ', geom_type=ogr.wkbpoint)

To delete a shp file

Driver. Deletedatasource (' test.shp ')

To add a new field, it can only be added to the layer, and there is no data

Add a field if it is a string and set the width

Fielddefn = ogr. FIELDDEFN (' id ', ogr. oftstring)

Fielddefn.setwidth (4)

Layer. CreateField (FIELDDEFN)

To add a new feature, you first have to complete the previous step and add the field fields to the

Then read the corresponding feature type from the layer and create the feature

Featuredefn = layer. Getlayerdefn ()

Feature = Ogr. Feature (FEATUREDEFN)

Setting geometric shapes

Feature. Setgeometry (Point)

Set a value for a field

Feature. SetField (' id ', 23)

Write feature to Layer

Layer. Createfeature (Feature)

GDAL python Tutorial (1)--read and write vector data with OGR

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.