1, GDAL (Geospatial Data Abstraction Library), the source grid space data conversion database. It uses an abstract data model to express the various file formats that are supported. It also has a series of command-line tools for data conversion and processing.
OGR provides read-write support for vector data formats, which support file formats including ESRI Shapefiles, S-57, Sdts, Postgis,oracle Spatial, Mapinfo mid/mif, and Mapinfo TAB.
OGR includes the following sections:
Geometry: Class Geometry (including Ogrgeometry) encapsulates the OpenGIS vector data model and provides some geometric operations, WKB (well knows Binary) and wkt (well known Text) Formats are converted to each other, as well as space reference systems (projections).
Spatial Reference: Class ogrspatialreference encapsulates the definition of projections and datum planes.
Feature: Class Ogrfeature encapsulates a complete Feature definition, and a complete Feature includes a series of attributes for geometry and geometry.
Feature Definition: Class Ogrfeaturedefn encapsulates Feature attributes, types, names, and their default space reference systems. A Ogrfeaturedefn object usually corresponds to a layer (layer).
Layer: Class Ogrlayer is an abstract base class that represents a layer of elements (Feature) inside a data source class Ogrdatasource.
Data Source: Class Ogrdatasource is an abstract base class that represents a file or a database that contains Ogrlayer objects.
2, Eclipse+python+gdal
(1), Python and Pydev installation
http://blog.csdn.net/cdl2008sky/article/details/8295176
(2), build gdal development environment.
(2.1), go to Python's GDAL home page http://pypi.python.org/pypi/GDAL/1.6.1 Download the GDAL python package--gdal-1.6.1.win32-py2.6.exe.
Installing the Gdal-1.6.1.win32-py2.6.exe file will automatically install to the Python installation directory ($:\python26\lib\site-packages).
(2.2), to Gdal official website http://www.gdal.org/http://download.osgeo.org/gdal/win32/1.6/Download Gdalwin32exe160.zip, decompression
Indent to any directory, the proposed use of English directory, such as we extract into the D-packing directory, such as D:\gdalwin32-1.6.
(2.3), according to Readme_exe. TXT file description, in "My Computer"-> "Advanced"-> "Environment variables". Add "D:\gdalwin32-1.6\bin" to the "Path" variable
In and add an environment variable gdal_data, the variable value is set to D:\gdalwin32-1.6\data.
(2.4), to http://pypi.python.org/pypi/numpy download Python's numpy module Numpy-1.7.1.win32-py2.6.exe, install this package (if your Python environment does not include this package, Be sure to install, because some of the features of Gdal depend on this package.
Test:
>>>from OSGeo Import Gdal
>>>from Osgeo.gdalconst Import *
>>>dataset=gdal. Open (' d:/test.jpg ', ga_readonly)
>>>dataset. Getdriver (). ShortName
>>> ' JPEG '
If there are no error prompts, the library is already installed.
Eg: Read and write Shap files
Read and write steps
1. Import Module
2. Register Driver
3. Open DataSource
4. Get Layer
5. Get feature
6...
7. Destroy feature
8. Destroy Layer
9. Destroy DataSource
#!/usr/bin/python #-*-CODING:GBK-*-' Created on 2013-8-27 @author: CHENLL ' "' Import Os,sys from OSGeo import gdal From OSGeo import ogr from OSGeo import OSR import numpy #读取shap文件 def readshap (): #为了支持中文路径, add the following code gdal. Setconfigoption ("Gdal_filename_is_utf8", "NO") #为了使属性表字段支持中文, please add the following sentence GDAL. Setconfigoption ("shape_encoding", "") #注册所有的驱动 ogr. RegisterAll () #数据格式的驱动 driver = ogr. Getdriverbyname (' ESRI Shapefile ') ds = driver.
Open (' e:\\arcgis\\data\\export_output.shp '); If DS is None:print ' could not open 1km Metro station Test 2.shp ' Sys.exit (1) #获取第0个图层 Layer0 = ds.
Getlayerbyindex (0); #投影 spatialref = Layer0.
Getspatialref (); # Print the number of elements in the output layer (' Number of elements =%d ', Layer0. Getfeaturecount (0)) Print (' property sheet structure information ') Defn = Layer0. Getlayerdefn () Ifieldcount = Defn. GetFieldCount () for index in range (ifieldcount): Ofield =defn. Getfielddefn (index) print ('%s:%s (%d.%d) '% (ofield.gEtnameref (), Ofield.getfieldtypename (Ofield.gettype ()), Ofield.getwidth (), Ofield.getprecision ()) feature = Layer0 . Getnextfeature () # below begins to traverse the elements in the layer while feature isn't None: # Gets the property sheet content in the element for index in rang E (ifieldcount): Ofield =defn.
Getfielddefn (index) line = "%s (%s) ="% (Ofield.getnameref (), Ofield.getfieldtypename (Ofield.gettype ())) If feature. Isfieldset (index): line = line+ "%s"% (feature. Getfieldasstring (index)) Else:line = line+ "(null)" Print (line) # Gets the geometry geometry =feature in the element. Getgeometryref () Print geometry # In order to demonstrate, output only one element information break feature. Destroy () ds. Destroy () #创建shap文件 def createshap (): #为了支持中文路径, add the following code gdal. Setconfigoption ("Gdal_filename_is_utf8", "NO") #为了使属性表字段支持中文, please add the following sentence GDAL. Setconfigoption ("shape_encoding", "") #注册所有的驱动 ogr. REgisterall () #数据格式的驱动 driver = ogr. Getdriverbyname (' ESRI Shapefile ') ds=driver. CreateDataSource ("E:\\arcgis\\point") shaplayer=ds.
Createlayer ("poi", geom_type=ogr.wkbpoint); #添加字段 fielddefn = ogr. FIELDDEFN (' id ', ogr.
oftstring) Fielddefn.setwidth (4) Shaplayer.createfield (FIELDDEFN); #创建feature defn = shaplayer.getlayerdefn () feature = Ogr.
Feature (DEFN); #添加属性 feature. SetField ("id", "Liu") #添加坐标 point = Ogr. Geometry (Ogr.wkbpoint) point. Addpoint (float (113.56647912), float (22.16128203)) feature.
Setgeometry (point); Shaplayer.createfeature (feature) feature. Destroy () #指定投影 sr = Osr.
Spatialreference (); Sr.
IMPORTFROMEPSG (32612);
Prjfile = open ("E:\\arcgis\\point\\poi.prj", ' W '); Sr.
Morphtoesri (); Prjfile.write (Sr.
Exporttowkt ());
Prjfile.close (); Ds.
Destroy () def main (): Readshap ();
Createshap ();
if __name__ = = "__main__": Main ();
Python API: http://gdal.org/python/
http://pcjericks.github.io/py-gdalogr-cookbook/