当前位置: 首页>>代码示例>>Python>>正文


Python ogr.OFTReal方法代码示例

本文整理汇总了Python中ogr.OFTReal方法的典型用法代码示例。如果您正苦于以下问题:Python ogr.OFTReal方法的具体用法?Python ogr.OFTReal怎么用?Python ogr.OFTReal使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ogr的用法示例。


在下文中一共展示了ogr.OFTReal方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Add_Field

# 需要导入模块: import ogr [as 别名]
# 或者: from ogr import OFTReal [as 别名]
def Add_Field(input_lyr, field_name, ogr_field_type):
    """
    Add a field to a layer using the following ogr field types:
    0 = ogr.OFTInteger
    1 = ogr.OFTIntegerList
    2 = ogr.OFTReal
    3 = ogr.OFTRealList
    4 = ogr.OFTString
    5 = ogr.OFTStringList
    6 = ogr.OFTWideString
    7 = ogr.OFTWideStringList
    8 = ogr.OFTBinary
    9 = ogr.OFTDate
    10 = ogr.OFTTime
    11 = ogr.OFTDateTime
    """

    # List fields
    fields_ls = List_Fields(input_lyr)

    # Check if field exist
    if field_name in fields_ls:
        raise Exception('Field: "{0}" already exists'.format(field_name))

    # Create field
    inp_field = ogr.FieldDefn(field_name, ogr_field_type)
    input_lyr.CreateField(inp_field)

    return inp_field 
开发者ID:gespinoza,项目名称:hants,代码行数:31,代码来源:functions.py

示例2: dbf_to_df

# 需要导入模块: import ogr [as 别名]
# 或者: from ogr import OFTReal [as 别名]
def dbf_to_df(path):
    """
    Return DataFrame from attributes stored in dBase/xBase format.

    Uses OGR's ESRI Shapefile driver to read records from the file.

    Parameters
    ----------
    path : str
        File path to the dBase/xBase file.

    Returns
    -------
    df : pandas.DataFrame

    """
    import ogr

    # Open the file and collect information on fields.
    dbf = ogr.Open(path)
    table = dbf.GetLayer()
    header = table.GetLayerDefn()
    ncolumns = header.GetFieldCount()
    column_names = [header.GetFieldDefn(i).GetName() for i in range(ncolumns)]
    column_types = [header.GetFieldDefn(i).GetType() for i in range(ncolumns)]

    def read(row, i):
        """Return i-th field of a record."""
        # For performance, use the appropriate field type function.
        fld_type = column_types[i]
        if fld_type == ogr.OFTInteger:
            return row.GetFieldAsInteger(i)
        elif fld_type == ogr.OFTReal:
            return row.GetFieldAsDouble(i)
        elif fld_type == ogr.OFTStringList:
            return row.GetFieldAsStringList(i)
        elif fld_type == ogr.OFTIntegerList:
            return row.GetFieldAsIntegerList(i)
        elif fld_type == ogr.OFTRealList:
            return row.GetFieldAsDoubleList(i)
        else:
            return row.GetFieldAsString(i)

    # Represent records with memory-efficient generators.
    values = lambda row: (read(row, i) for i in range(ncolumns))
    records = (values(row) for row in table)

    df = pd.DataFrame.from_records(records, columns=column_names,
                                   coerce_float=False)
    return df 
开发者ID:UDST,项目名称:spandex,代码行数:52,代码来源:io.py

示例3: pointWriting

# 需要导入模块: import ogr [as 别名]
# 或者: from ogr import OFTReal [as 别名]
def pointWriting(fn,pt_lyrName_w,ref_lyr=False):
    ds=ogr.Open(fn,1)
    
    '''参考层,用于空间坐标投影,字段属性等参照'''
    ref_lyr=ds.GetLayer(ref_lyr)
    ref_sr=ref_lyr.GetSpatialRef()
    print(ref_sr)
    ref_schema=ref_lyr.schema #查看属性表字段名和类型
    for field in ref_schema:
        print(field.name,field.GetTypeName())   
 
    '''建立新的datasource数据源'''
    sf_driver=ogr.GetDriverByName('ESRI Shapefile')
    sfDS=os.path.join(fn,r'sf')
    if os.path.exists(sfDS):
        sf_driver.DeleteDataSource(sfDS)
    pt_ds=sf_driver.CreateDataSource(sfDS)
    if pt_ds is None:
        sys.exit('Could not open{0}'.format(sfDS))
        
    '''建立新layer层'''    
    if pt_ds.GetLayer(pt_lyrName_w):
        pt_ds.DeleteLayer(pt_lyrName_w)    
    pt_lyr=pt_ds.CreateLayer(pt_lyrName_w,ref_sr,ogr.wkbPoint)
    
    '''配置字段,名称以及类型和相关参数'''
    pt_lyr.CreateFields(ref_schema)
    LatFd=ogr.FieldDefn("origiLat",ogr.OFTReal)
    LatFd.SetWidth(8)
    LatFd.SetPrecision(3)
    pt_lyr.CreateField(LatFd)
    LatFd.SetName("Lat")
    pt_lyr.CreateField(LatFd)
     
    '''建立feature空特征和设置geometry几何类型'''
    print(pt_lyr.GetLayerDefn())
    pt_feat=ogr.Feature(pt_lyr.GetLayerDefn())    
    
    for feat in ref_lyr:  #循环feature
        '''设置几何体'''
        pt_ref=feat.geometry().Clone()
        wkt="POINT(%f %f)" %  (float(pt_ref.GetX()+0.01) , float(pt_ref.GetY()+0.01))
        newPt=ogr.CreateGeometryFromWkt(wkt) #使用wkt的方法建立点
        pt_feat.SetGeometry(newPt)
        '''设置字段值'''
        for i_field in range(feat.GetFieldCount()):
            pt_feat.SetField(i_field,feat.GetField(i_field))
        pt_feat.SetField("origiLat",pt_ref.GetX())
        pt_feat.SetField("Lat",pt_ref.GetX()+0.01)
        '''根据设置的几何体和字段值,建立feature。循环建立多个feature特征'''
        pt_lyr.CreateFeature(pt_feat)    
    del ds 
开发者ID:richieBao,项目名称:python-urbanPlanning,代码行数:54,代码来源:xa_gdal.py

示例4: lineWriting

# 需要导入模块: import ogr [as 别名]
# 或者: from ogr import OFTReal [as 别名]
def lineWriting(fn,ln_lyrName_w,ref_lyr=False):
    ds=ogr.Open(fn,1)
    
    '''参考层,用于空间坐标投影,字段属性等参照'''
    ref_lyr=ds.GetLayer(ref_lyr)
    ref_sr=ref_lyr.GetSpatialRef()
    print(ref_sr)
    ref_schema=ref_lyr.schema #查看属性表字段名和类型
    for field in ref_schema:
        print(field.name,field.GetTypeName())      

    '''建立新layer层'''    
    if ds.GetLayer(ln_lyrName_w):
        ds.DeleteLayer(ln_lyrName_w)    
    ln_lyr=ds.CreateLayer(ln_lyrName_w,ref_sr,ogr.wkbMultiLineString)    

    '''配置字段,名称以及类型和相关参数'''
    Fd=ogr.FieldDefn("length",ogr.OFTReal)
    Fd.SetWidth(8)
    Fd.SetPrecision(3)
    ln_lyr.CreateField(Fd)
    Fd=ogr.FieldDefn("name",ogr.OFTString)
    ln_lyr.CreateField(Fd)    

    '''建立feature空特征和设置geometry几何类型'''
    print(ln_lyr.GetLayerDefn())
    ln_feat=ogr.Feature(ln_lyr.GetLayerDefn())

    for feat in ref_lyr:  #循环feature
        '''设置几何体'''
        ln_ref=feat.geometry().Clone()
        temp=""
        for j in range(ln_ref.GetPointCount()):
            if j==ln_ref.GetPointCount()-1:
                temp+="%f %f"%(float(ln_ref.GetX(j)+0.01) , float(ln_ref.GetY(j)+0.01))
            else:
                temp+="%f %f,"%(float(ln_ref.GetX(j)+0.01) , float(ln_ref.GetY(j)+0.01))
        wkt="LINESTRING(%s)" % temp  #使用wkt的方法建立直线
#        print(wkt)
        newLn=ogr.CreateGeometryFromWkt(wkt)        
        ln_feat.SetGeometry(newLn)
        '''设置字段值'''
        ln_feat.SetField("name",feat.GetField("name"))
        ln_feat.SetField("length",newLn.Length())
        '''根据设置的几何体和字段值,建立feature。循环建立多个feature特征'''
        ln_lyr.CreateFeature(ln_feat)    
    del ds 
开发者ID:richieBao,项目名称:python-urbanPlanning,代码行数:49,代码来源:xa_gdal.py

示例5: polygonWriting

# 需要导入模块: import ogr [as 别名]
# 或者: from ogr import OFTReal [as 别名]
def polygonWriting(fn,pg_lyrName_w,ref_lyr=False):
    ds=ogr.Open(fn,1)
    
    '''参考层,用于空间坐标投影,字段属性等参照'''
    ref_lyr=ds.GetLayer(ref_lyr)
    ref_sr=ref_lyr.GetSpatialRef()
#    print(ref_sr)
    ref_schema=ref_lyr.schema #查看属性表字段名和类型
    for field in ref_schema:
        print(field.name,field.GetTypeName())      

    '''建立新layer层'''    
    if ds.GetLayer(pg_lyrName_w):
        ds.DeleteLayer(pg_lyrName_w)    
    pg_lyr=ds.CreateLayer(pg_lyrName_w,ref_sr,ogr.wkbMultiPolygon)    

    '''配置字段,名称以及类型和相关参数'''
    Fd=ogr.FieldDefn("area",ogr.OFTReal)
    Fd.SetWidth(8)
    Fd.SetPrecision(8)
    pg_lyr.CreateField(Fd)
    Fd=ogr.FieldDefn("name",ogr.OFTString)
    pg_lyr.CreateField(Fd)    

    '''建立feature空特征和设置geometry几何类型'''
#    print(pg_lyr.GetLayerDefn())
    pg_feat=ogr.Feature(pg_lyr.GetLayerDefn())
    
    for feat in ref_lyr:  #循环feature
        '''设置几何体'''
        pg_ref=feat.geometry().Clone()
        tempSub=""
        for j in range(pg_ref.GetGeometryCount()):
            ring=pg_ref.GetGeometryRef(j)
            vertexes=ring.GetPoints()
#            print(len(vertexes))
            temp=""
            for i in range(len(vertexes)):                
                if i==len(vertexes)-1:
                    temp+="%f %f"%(float(vertexes[i][0]+0.01) , float(vertexes[i][1]+0.01))
                else:
                    temp+="%f %f,"%(float(vertexes[i][0]+0.01) , float(vertexes[i][1]+0.01))
            if j==pg_ref.GetGeometryCount()-1:
                tempSub+="(%s)"%temp   
            else:
                tempSub+="(%s),"%temp
#        print(tempSub)    
        wkt="POLYGON(%s)" % tempSub  #使用wkt的方法建立直线
#        print(wkt)
        newPg=ogr.CreateGeometryFromWkt(wkt)        
        pg_feat.SetGeometry(newPg)
        
        '''设置字段值'''
        pg_feat.SetField("name",feat.GetField("NAME"))
        pg_feat.SetField("area",newPg.GetArea())
        '''根据设置的几何体和字段值,建立feature。循环建立多个feature特征'''
        pg_lyr.CreateFeature(pg_feat)    
    del ds 
开发者ID:richieBao,项目名称:python-urbanPlanning,代码行数:60,代码来源:xa_gdal.py


注:本文中的ogr.OFTReal方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。