In [2]:
#!/usr/bin/python
#coding=GB2312
by openthings@163.com, 2016-04-25.
Numeric:高速的数组处理,对栅格数据尤其重要
NumPy:下一代的Numeric
更强大的gis库 http://www.gispython.org/
In [47]:
from osgeo import gdal, gdalconst
from osgeo.gdalconst import *
import gdal, gdalconst
from osgeo import ogr
In [8]:
point = ogr.Geometry(ogr.wkbPoint)
point.AddPoint(10,20)
Out[8]:
In [11]:
#help(point)
str(point)
Out[11]:
In [14]:
line = ogr.Geometry(ogr.wkbLineString)
line.AddPoint(10,10)
line.AddPoint(20,20)
line.SetPoint(0,30,30) #(10,10) -> (30,30)
In [15]:
str(line)
Out[15]:
统计所有点的数目
In [16]:
print(line.GetPointCount())
读取0号点的x坐标和y坐标
In [17]:
print(line.GetX(0))
print(line.GetY(0))
In [18]:
ring = ogr.Geometry(ogr.wkbLinearRing)
ring.AddPoint(0,0)
ring.AddPoint(100,0)
ring.AddPoint(100,100)
ring.AddPoint(0,100)
结束的时候,用CloseRings关闭ring,或者将最后一个点的坐标设定为与第一个点相同。
In [19]:
ring.CloseRings()
#ring.AddPoint(0,0)
In [20]:
str(ring)
Out[20]:
In [22]:
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)
Out[22]:
In [23]:
str(polygon)
Out[23]:
最后三句话比较重要,就是先建立一个polygon对象,然后添加外层ring和内层ring。
下面这句话可以帮你数数,polygon能有几个ring。
In [24]:
print(polygon.GetGeometryCount())
从polygon中读取ring,index的顺序和创建polygon时添加ring的顺序相同。
In [25]:
outring = polygon.GetGeometryRef(0)
inring = polygon.GetGeometryRef(1)
In [28]:
print("OutRing: ",str(outring))
print("InRing: ",str(inring))
In [30]:
multipoint = ogr.Geometry(ogr.wkbMultiPoint)
point = ogr.Geometry(ogr.wkbPoint)
point.AddPoint(10,10)
multipoint.AddGeometry(point)
point.AddPoint(20,20)
multipoint.AddGeometry(point)
Out[30]:
In [31]:
str(multipoint)
Out[31]:
In [ ]:
spatialRef = layer.GetSpatialRef()
spatialRef = geom.GetSpatialReference()
投影信息一般存储在.prj文件中,如果没有这个文件,上述函数返回None。
首先导入osr库,之后使用osr.SpatialReference()创建SpatialReference对象。 用下列语句向SpatialReference对象导入投影信息。
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()
对一个几何形状Geometry进行投影变换,要先初始化两个Projection,然后创建一个CoordinateTransformation对象,用它来做变换。
In [46]:
from osgeo import osr
sourceSR = osr.SpatialReference()
sourceSR.ImportFromEPSG(32612) #UTM 12N WGS84
targetSR = osr.SpatialReference()
targetSR.ImportFromEPSG(4326) #Geo WGS84
coordTrans = osr.CoordinateTransformation(sourceSR, targetSR)
In [41]:
print("Projection transform:")
print("Before:", multipoint)
#geom.Transform(coordTrans)
multipoint.Transform(coordTrans)
print("After :", multipoint)
据说,在windows里面跑不通(http://n2.nabble.com/PROJ-4-EPSG-28992-td2033665.html)
我在linux(Ubuntu15.04)里面没问题。
In [45]:
targetSR.MorphToESRI()
sr_wkt = targetSR.ExportToWkt()
print(sr)
file = open('test.prj', 'w')
file.write(sr_wkt)
file.close()
In [ ]: