# 3. GDAL python教程（2）——几何形状geometry与投影projection¶

## 3.1. 建立新的几何形状¶

```point = ogr.Geometry(ogr.wkbPoint)
point.AddPoint(10,20)
```

### 3.1.1. 新建line¶

```line = ogr.Geometry(ogr.wkbLineString)
line.AddPoint(10,10)
line.AddPoint(20,20)
line.SetPoint(0,30,30)   #(10,10) -> (30,30)
```

```print line.GetPointCount()
```

```print line.GetX(0)
print line.GetY(0)
```

```ring = ogr.Geometry(ogr.wkbLinearRing)
ring.AddPoint(0,0)
ring.AddPoint(100,0)
ring.AddPoint(100,100)
ring.AddPoint(0,100)
```

```ring.CloseRings()
ring.AddPoint(0,0)
```

```outring = ogr.Geometry(ogr.wkbLinearRing)
outring.AddPoint(0,0)
outring.AddPoint(100,0)
outring.AddPoint(100,100)
outring.AddPoint(0,100)
outring.AddPoint(0,0)

inring = ogr.Geometry(ogr.wkbLinearRing)inring = ogr.Geometry(ogr.wkbLinearRing)
inring.AddPoint(25,25)
inring.AddPoint(75,25)
inring.AddPoint(75,75)
inring.AddPoint(25,75)
inring.CloseRings()

polygon = ogr.Geometry(ogr.wkbPolygon)
polygon.AddGeometry(outring)
polygon.AddGeometry(inring)
```

```print polygon.GetGeometryCount()
```

```outring = polygon.GetGeometryRef(0)
inring = polygon.GetGeometryRef(1)
```

## 3.2. 创建复合几何形状multi geometry¶

```multipoint = ogr.Geometry(ogr.wkbMultiPoint)
point = ogr.Geometry(ogr.wkbPoint)point = ogr.Geometry(ogr.wkbPoint)
point.AddPoint(10,10)
multipoint.AddGeometry(point)
point.AddPoint(20,20)
multipoint.AddGeometry(point)
```

```spatialRef = layer.GetSpatialRef()
spatialRef = geom.GetSpatialReference()
```

• ImportFromWkt(<wkt>)
• ImportFromEPSG(<epsg>)
• ImportFromProj4(<proj4>)
• ImportFromESRI(<proj_lines>)
• ImportFromPCI(<proj>, <units>, <parms>)
• ImportFromUSGS(<proj_code>, <zone>)
• ImportFromXML(<xml>)

• ExportToWkt()
• ExportToPrettyWkt()
• ExportToProj4()
• ExportToPCI()
• ExportToUSGS()
• ExportToXML()

```sourceSR = osr.SpatialReference()
sourceSR.ImportFromEPSG(32612) #UTM 12N WGS84
targetSR = osr.SpatialReference()
targetSR.ImportFromEPSG(4326) #Geo WGS84
coordTrans = osr.CoordinateTransformation(sourceSR, targetSR)
geom.Transform(coordTrans)
```

http://n2.nabble.com/PROJ-4-EPSG-28992-td2033665.html

```targetSR.MorphToESRI()
file = open('test.prj', 'w')
file.write(targetSR.ExportToWkt())
ffile.close()
```