Python ogr.OFTReal() Examples
The following are 6
code examples of ogr.OFTReal().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
ogr
, or try the search function
.
Example #1
Source File: functions.py From hants with Apache License 2.0 | 5 votes |
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
Example #2
Source File: functions.py From wa with Apache License 2.0 | 5 votes |
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
Example #3
Source File: io.py From spandex with BSD 3-Clause "New" or "Revised" License | 4 votes |
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
Example #4
Source File: xa_gdal.py From python-urbanPlanning with MIT License | 4 votes |
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
Example #5
Source File: xa_gdal.py From python-urbanPlanning with MIT License | 4 votes |
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
Example #6
Source File: xa_gdal.py From python-urbanPlanning with MIT License | 4 votes |
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