Why use open source?
Advantages
1. Free, suitable for individuals and small businesses
2. Powerful development tools, easy to find bugs
3. Cross-platform, Windows and Linux are available
4. Pull the Wind!
Disadvantages
1. No embedded geoprocessing processor
2. Fewer 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, especially important for raster data
2. NumPy: The numeric of the next generation
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)
The above is the Python gdal tutorial: Read and write vector data with ogr content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!