# -*- coding: utf-8 -*-

print(u"python与开源QGis课题研究组")
#print("汉字")

#+++++++++++++++++
#创建矢量数据文件
#+++++++++++++++++

try:
    from osgeo import ogr
except:
    import ogr
    
driver = ogr.GetDriverByName("ESRI Shapefile")

import os

'''
ds = driver.CreateDataSource("xx_tesp.shp")
layer = ds.CreateLayer('test',geom_type = ogr.wkbPoint)
fieldDefn = ogr.FieldDefn('id',ogr.OFTString)
fieldDefn.SetWidth(4)
layer.CreateField(fieldDefn)
featureDefn = layer.GetLayerDefn()
print(featureDefn)
feature = ogr.Feature(featureDefn)
#设定几何形状
point = ogr.Geometry(ogr.wkbPoint)
point.SetPoint(0,123,123)
#设定字段数值
feature.SetField('id',23)
#将要素写入到图层
layer.CreateFeature(feature)
ds.Destroy()

import os
out_shp = "xx_tesp.shp"
dir(out_shp)
if os.path.exists(out_shp):
    driver.DeleteDataSource(out_shp)
    
dir(out_shp)

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

line = ogr.Geometry(ogr.wkbLineString)
print(line)
line.AddPoint(10,10)
print(line)
line.AddPoint(20,20)
print(line)
line.SetPoint(2,30,30)
print(line)
print(line.GetPointCount())
print(line.GetX(0))
print(line.GetX(1))
print(line.GetX(3))

ring = ogr.Geometry(ogr.wkbLinearRing)
ring.AddPoint(0,0)
ring.AddPoint(100,0)
ring.AddPoint(100,100)
ring.AddPoint(0,100)
ring.CloseRings()
print(ring)
print(type(ring))

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.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())

#outring2 = polygon.GetGeometryRef(0)
#inring2 = polygon.GetGeometryRef(1)
#print(outring2)
#print(inring2)

for ringx in range(polygon.GetGeometryCount()):
    print(polygon.GetGeometryRef(ringx))

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

point.AddPoint(20,20)
multipoint.AddGeometry(point)
print(multipoint)

mp = multipoint.GetGeometryCount()
print(mp)

for mpx in range(multipoint.GetGeometryCount()):
    print(multipoint.GetGeometryRef(mpx))

extfile = 'xx_data_pt.shp'
if os.access(extfile,os.F_OK):
    driver.DeleteDataSource(extfile)
#1、创建数据源
newds = driver.CreateDataSource(extfile)
#print(dir(newds))
#print(dir(newds.CreateLayer))
#2、创建数据源图层
lyrn = newds.CreateLayer('point',None,ogr.wkbPoint)

#3、定义图层字段,添加图层字段
fieldcnstr = ogr.FieldDefn("idx",ogr.OFTInteger)
lyrn.CreateField(fieldcnstr)

fieldf = ogr.FieldDefn("namex",ogr.OFTString)
lyrn.CreateField(fieldf)

point_coors_arr = [[1,0],[2,0],[3,0],[4,0]]
for idxx,point_coors in enumerate(point_coors_arr):
    #print(type(idxx))
    #print(type(point_coors))
    wkt = 'POINT (%f %f)' % (point_coors[0],point_coors[1])
    #print(wkt)
    geom = ogr.CreateGeometryFromWkt(wkt)
    
    feat = ogr.Feature(lyrn.GetLayerDefn())
    
    feat.SetField('idx',idxx)
    feat.SetField('namex','ID{0}'.format(idxx))
    
    feat.SetGeometry(geom)
    lyrn.CreateFeature(feat)
    
    #print(lyrn.GetLayerDefn())
    #print('idx:%i' % (idxx))
    #print("namex:%s" % 'ID{0}'.format(idxx))
    #x = 'ID{0}'.format(idxx)
    #print(x)
    """
    wkt = 'POINT (%f %f)' % (point_coors[0],point_coors[1])
    geom = ogr.CreateGeometryFromWkt(wkt)
    feat = ogr.Feature(lyrn.GetLayerDefn())
    feat.SetField('idx',idxx)
    feat.SetField('namex','ID{0}'.format(idxx))
    feat.SetGeometry(geom)
    lyrn.CreateFeature(feat)
    """
newds.Destroy()

extfile = 'xx_data_line.shp'
driver = ogr.GetDriverByName("ESRI Shapefile")
if os.access(extfile,os.F_OK):
    driver.DeleteDataSource(extfile)

newds = driver.CreateDataSource(extfile)
lyrn = newds.CreateLayer('line',None,ogr.wkbLineString)

#字段定义创建
fieldcnstr = ogr.FieldDefn("id",ogr.OFTInteger)
fieldf = ogr.FieldDefn("name",ogr.OFTString)
lyrn.CreateField(fieldcnstr)
lyrn.CreateField(fieldf)

point_coors_arr = [[0,0,1,2,3,-2,6,0]]
#print(point_coors_arr)
#print(type(point_coors_arr))
#print(len(point_coors_arr))

for idx,point_coors in enumerate(point_coors_arr):
    #wkt = 'LINESTRING(%f %f,%f %f,%f %f,%f %f)' % (point_coors[len(point_coors_arr) - len(point_coors_arr)],point_coors[len(point_coors_arr) - len(point_coors_arr) + 1],)
    wkt = 'LINESTRING(%f %f,%f %f,%f %f,%f %f)' % (point_coors[0],point_coors[1],point_coors[2],point_coors[3],point_coors[4],point_coors[5]
    ,point_coors[6],point_coors[7])
    print(wkt)
    geom = ogr.CreateGeometryFromWkt(wkt)
    feat = ogr.Feature(lyrn.GetLayerDefn())
    feat.SetField('id',idx)
    feat.SetField('name','line_one')
    feat.SetGeometry(geom)
    lyrn.CreateFeature(feat)
    
newds.Destroy()

extfile = 'xx_data_polygon.shp'
driver = ogr.GetDriverByName('ESRI Shapefile')
if os.access(extfile,os.F_OK):
    driver.DeleteDataSource(extfile)
#创建数据源文件
newds = driver.CreateDataSource(extfile)
#创建几何形状图层
lyrn = newds.CreateLayer('polygon',None,ogr.wkbPolygon)
#定义字段
fieldcnstr = ogr.FieldDefn('id',ogr.OFTInteger)
fieldf = ogr.FieldDefn('name',ogr.OFTString)
#图层添加字段
lyrn.CreateField(fieldcnstr)
lyrn.CreateField(fieldf)

wkt_polygon_1 = 'POLYGON((2 1,12 1,12 4,2 4,1 2))'
wkt_polygon_2 = 'POLYGON((4 1,8 1,8 3,4 3,3 1))'
wkt_polygon_3 = 'POLYGON((8 4,10 4, 10 5,8 5,6 4))'
#print(type(wkt_polygon_1))
point_coors_arr = [wkt_polygon_1,wkt_polygon_2,wkt_polygon_3]
#print(type(point_coors_arr))
for idx,point_coors in enumerate(point_coors_arr):
    #print(idx)
    #print(point_coors)
    wkt = point_coors
    #使用wkt创建几何图形
    geom = ogr.CreateGeometryFromWkt(wkt)
    #获取图层要素  ogr.GetLayerDefn , ogr.Feature()
    feat = ogr.Feature(lyrn.GetLayerDefn())
    feat.SetField('id',idx)
    print('poly_{idx}'.format(idx = idx))
    #feat.SetField('name','poly_{idx}'.format(idx = idx))
    feat.SetField('name','poly_{idx}'.format(idx = idx))
    feat.SetGeometry(geom)
    lyrn.CreateFeature(feat)
    
newds.Destroy()

from osgeo import ogr
import os,math
inshp = "xx_data_polygon.shp"
ds = ogr.Open(inshp)  #打开shp源文件
driver = ogr.GetDriverByName("ESRI Shapefile")

outputfile = "xx_data_polygon_copy.shp"   #输出shp
if os.access(outputfile,os.F_OK):
    driver.DeleteDataSource(outputfile)

pt_cp = driver.CopyDataSource(ds,outputfile)
pt_cp.Release()

from osgeo import ogr
import os,math
inshp = 'xx_data_polygon.shp'
ds = ogr.Open(inshp)
driver = ogr.GetDriverByName("ESRI Shapefile")
outputfile = 'cp_polygon.shp'
if os.access(outputfile,os.F_OK):
    driver.DeleteDataSource(outputfile)
    
pt_cp = driver.CopyDataSource(ds,outputfile)
pt_cp.Release()

outputfile = 'cp2.shp'
if os.access(outputfile,os.F_OK):
    driver.DeleteDataSource(outputfile)
newds = driver.CreateDataSource(outputfile)
layer = ds.GetLayer()
pt_layer = newds.CopyLayer(layer,'xxx')
#newds.Destroy()

outputfile = 'cp3.shp'
if os.access(outputfile,os.F_OK):
    driver.DeleteDataSource(outputfile)

newds = driver.CreateDataSource(outputfile)
layernew = newds.CreateLayer('worldcopy',None,ogr.wkbLineString)
layer = ds.GetLayer()
feature = layer.GetNextFeature()
if feature is not None:
    layernew.CreateFeature(feature)
    feature = layer.GetNextFeature()
    
newds.Destroy()

driver = ogr.GetDriverByName("ESRI Shapefile")

inshp = 'xx_data_polygon.shp'
ds = ogr.Open(inshp)

outf = 'cxp.shp'
if os.access(outf,os.F_OK):
    driver.DeleteDataSource(outf)
    print("exists")
newds = driver.CreateDataSource(outf)
layernew = newds.CreateLayer('worldcopy',None,ogr.wkbLineString)

layer = ds.GetLayer()

#print(layer)
feature = layer.GetNextFeature()
print(feature)
while feature is not None:
    layernew.CreateFeature(feature)
    feature = layer.GetNextFeature()
newds.Destroy()
'''

from osgeo import ogr
ds = ogr.Open('convChk.shp')
layer = ds.GetLayer(0)
spatialRef = layer.GetSpatialRef()
print(spatialRef)

05-19 12:05