How to use the ogr.FieldDefn function in ogr

To help you get started, we’ve selected a few ogr examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github pism / pypismtools / scripts / contour2shp.py View on Github external
# Get driver
driver = ogr.GetDriverByName("ESRI Shapefile")
# Create shapeData
shp_filename = validateShapePath(shp_filename)
if os.path.exists(shp_filename):
    os.remove(shp_filename)
shapeData = driver.CreateDataSource(shp_filename)
# Create spatialReference, EPSG 4326 (lonlat)
spatialReference = osr.SpatialReference()
spatialReference.ImportFromEPSG(4326)
layerName = os.path.splitext(os.path.split(shp_filename)[1])[0]
layer = shapeData.CreateLayer(layerName, spatialReference, ogr.wkbPolygon)
layerDefinition = layer.GetLayerDefn()
field_defn = ogr.FieldDefn("level", ogr.OFTReal)
layer.CreateField(field_defn)
field_defn = ogr.FieldDefn("year", ogr.OFTReal)
layer.CreateField(field_defn)
field_defn = ogr.FieldDefn("timestamp", ogr.OFTDateTime)
layer.CreateField(field_defn)

if tdim:
    time = nc.variables["time"]
    time_units = time.units
    time_calendar = time.calendar
    if time[0] < 0:
        is_paleo = True
    else:
        is_paleo = False
    cdftime = utime(time_units, time_calendar)
    for k, t in enumerate(time):
        if is_paleo:
            timestamp = "1-1-1"
github CivicSpleen / ambry / ambry / old / geo / util.py View on Github external
# copied geometry object is being released before being used.
    geos = []
    for i in range(bound_lyr.GetFeatureCount()):
        f = bound_lyr.GetFeature(i)
        g = f.geometry()
        geos.append(g.Clone())

    # Combine hot spots that have intersecting bounding boxes, to get larger
    # areas that cover all of the adjacent intersecting smaller areas.
    geos = dg.combine_envelopes(geos, use_bb=use_bb, use_distance=use_distance)

    # Write out the combined bounds areas.
    lyr = ogr_ds.CreateLayer('combined_bounds', aa.srs)
    lyr.CreateField(ogr.FieldDefn('id', ogr.OFTInteger))
    lyr.CreateField(ogr.FieldDefn('area', ogr.OFTReal))
    lyr.CreateField(ogr.FieldDefn('name', ogr.OFTString))
    lyr.CreateField(ogr.FieldDefn('code', ogr.OFTString))

    envelopes = []
    id = 1
    for env in geos:
        f = ogr.Feature(lyr.GetLayerDefn())
        bb = dg.create_bb(env.GetEnvelope(), env.GetSpatialReference())
        f.SetGeometry(bb)
        f.SetField(0, id)
        f.SetField(1, bb.Area())
        f.SetField(2, None)
        f.SetField(3, None)
        id += 1
        lyr.CreateFeature(f)
        envelopes.append(
            {'id': id, 'env': bb.GetEnvelope(), 'area': bb.Area()})
github rustychris / stompy / stompy / spatial / wkb2shp.py View on Github external
geom_type = int(types.max())
        # print "Chose geometry type to be %s"%ogr2text[geom_type]

    new_layer = new_ds.CreateLayer(layer_name,
                                   srs=srs,
                                   geom_type=geom_type)

    # setup the feature definition:

    # create fields based on the field key/value pairs
    # return for the first wkb file
    casters = []
    for field_i,key in enumerate(field_names):
        val = field_values[0][field_i]
        if type(val) == int or isinstance(val,np.integer):
            field_def = ogr.FieldDefn(key,ogr.OFTInteger)
            casters.append( int )
        elif isinstance(val,np.floating):
            # above: use np.float, as it seems to be more compatible with
            # 32-bit and 64-bit floats.

            # This is an old bug - seems to work without this in the modern
            # era, and in turn, asscalar does *not* work with list of lists
            # # a numpy array of float32 ends up with
            # # a type here of ,
            # # which doesn't match isinstance(...,float)
            # # asscalar helps out with that
            # 2018-07: np.floating is maybe the proper solution.
            print( "float valued key is %s"%key)
            field_def = ogr.FieldDefn(key,ogr.OFTReal)
            field_def.SetWidth(64)
            field_def.SetPrecision(10)
github OSGeo / gdal / pymod / samples / get_soundg.py View on Github external
shp_ds = shp_driver.CreateDataSource( shpfilename )

shp_layer = shp_ds.CreateLayer( 'out', geom_type = ogr.wkbPoint25D )

src_defn = src_soundg.GetLayerDefn()
field_count = src_defn.GetFieldCount()

#############################################################################-
# Copy the SOUNDG schema, and add an ELEV field.

out_mapping = []
for fld_index in range(field_count):
    src_fd = src_defn.GetFieldDefn( fld_index )
    
    fd = ogr.FieldDefn( src_fd.GetName(), src_fd.GetType() )
    fd.SetWidth( src_fd.GetWidth() )
    fd.SetPrecision( src_fd.GetPrecision() )
    if shp_layer.CreateField( fd ) != 0:
        out_mapping.append( -1 )
    else:
        out_mapping.append( shp_layer.GetLayerDefn().GetFieldCount() - 1 )

fd = ogr.FieldDefn( 'ELEV', ogr.OFTReal )
fd.SetWidth( 12 )
fd.SetPrecision( 4 )
shp_layer.CreateField( fd )
    
#############################################################################
# Process all SOUNDG features.

feat = src_soundg.GetNextFeature()
github georgebv / coastlib / coastlib / gis / enc_s57.py View on Github external
# Copy the SOUNDG schema, duplicating the current fields, and create the LAT,
#	LONG and DEPTH fields for the extracted Point and calculated data

out_mapping = []
for fld_index in range(field_count):

    src_fd = src_defn.GetFieldDefn(fld_index)
    fd = ogr.FieldDefn(src_fd.GetName(), src_fd.GetType())
    fd.SetWidth(src_fd.GetWidth())
    fd.SetPrecision(src_fd.GetPrecision())
    if kml_layer.CreateField(fd) != 0:
        out_mapping.append(-1)
    else:
        out_mapping.append(kml_layer.GetLayerDefn().GetFieldCount() - 1)

fd = ogr.FieldDefn('LAT', ogr.OFTReal)
fd.SetWidth(12)
fd.SetPrecision(7)
kml_layer.CreateField(fd)

fd = ogr.FieldDefn('LATD', ogr.OFTReal)
fd.SetWidth(12)
fd.SetPrecision(7)
kml_layer.CreateField(fd)

fd = ogr.FieldDefn('LATM', ogr.OFTReal)
fd.SetWidth(12)
fd.SetPrecision(7)
kml_layer.CreateField(fd)

fd = ogr.FieldDefn('LATS', ogr.OFTReal)
fd.SetWidth(12)
github gvSIGAssociation / gvsig-online / plugin_trip_planner / gvsigol_plugin_trip_planner / import-pois-osm / import-pois-osm.py View on Github external
field_name = ogr.FieldDefn("Name", ogr.OFTString)
        field_name.SetWidth(24)
        layer.CreateField(field_name)
        field_2 = ogr.FieldDefn("NameAndA", ogr.OFTString)
        field_2.SetWidth(240)
        layer.CreateField(field_2)
        field_3 = ogr.FieldDefn("Category", ogr.OFTString)
        field_3.SetWidth(50)
        layer.CreateField(field_3)
        field_4 = ogr.FieldDefn("Subcat", ogr.OFTString)
        field_4.SetWidth(50)
        layer.CreateField(field_4)
        field_5 = ogr.FieldDefn("Address", ogr.OFTString)
        field_5.SetWidth(140)
        layer.CreateField(field_5)
        field_6 = ogr.FieldDefn("Municipio", ogr.OFTString)
        field_6.SetWidth(80)
        layer.CreateField(field_6)


        # Process the text file and add the attributes and features to the shapefile
        for row in data_list:
                # create the feature
                feature = ogr.Feature(layer.GetLayerDefn())
                # Set the attributes using the values from the delimited text file
                
                strUTF8 = row[0].encode('utf-8')
                # print(strUTF8)
                feature.SetField("Name", strUTF8)
                # feature.SetField("Name", row[0])
                strUnicode = row[1]
                strUTF8 = strUnicode.encode('utf-8')
github ManishSahu53 / Vector-Map-Generation-from-Aerial-Imagery-using-Deep-Learning-GeoSpatial-UNET / src / postprocess.py View on Github external
# from Layer
    spatialRef = src_lyr.GetSpatialRef()
    # from Geometry
    feature = src_lyr.GetNextFeature()
    geom = feature.GetGeometryRef()
    spatialRef = geom.GetSpatialReference()
    src_srs.ImportFromWkt(spatialRef.ExportToWkt())

    # Creating destination shp
    dst_ds = drv.CreateDataSource(path_output)
    dst_layer = dst_ds.GetLayerByName(path_output)
    dst_layer = dst_ds.CreateLayer(
        path_output, geom_type=ogr.wkbPolygon, srs=src_srs)

    # Add an ID and area field
    idField = ogr.FieldDefn('id', ogr.OFTInteger)
    areaField = ogr.FieldDefn('area', ogr.OFTReal)
    dst_layer.CreateField(idField)
    dst_layer.CreateField(areaField)

    # Simplification of polygons
    multipoly2poly(src_lyr, para, dst_layer)
github developmentseed / landsat-util / landsat / third_party / ogr2ogr.py View on Github external
panMap = [ -1 for i in range(nSrcFieldCount) ]

    poDstFDefn = poDstLayer.GetLayerDefn()

    if papszSelFields is not None and not bAppend:

        nDstFieldCount = 0
        if poDstFDefn is not None:
            nDstFieldCount = poDstFDefn.GetFieldCount()

        for iField in range(len(papszSelFields)):

            iSrcField = poSrcFDefn.GetFieldIndex(papszSelFields[iField])
            if iSrcField >= 0:
                poSrcFieldDefn = poSrcFDefn.GetFieldDefn(iSrcField)
                oFieldDefn = ogr.FieldDefn( poSrcFieldDefn.GetNameRef(),
                                            poSrcFieldDefn.GetType() )
                oFieldDefn.SetWidth( poSrcFieldDefn.GetWidth() )
                oFieldDefn.SetPrecision( poSrcFieldDefn.GetPrecision() )

                if papszFieldTypesToString is not None and \
                    (CSLFindString(papszFieldTypesToString, "All") != -1 or \
                    CSLFindString(papszFieldTypesToString, \
                                ogr.GetFieldTypeName(poSrcFieldDefn.GetType())) != -1):

                    oFieldDefn.SetType(ogr.OFTString)

                # The field may have been already created at layer creation
                iDstField = -1
                if poDstFDefn is not None:
                    iDstField = poDstFDefn.GetFieldIndex(oFieldDefn.GetNameRef())
                if iDstField >= 0:
github nkarasiak / dzetsaka / scripts / function_vector.py View on Github external
# create the data source
    if os.path.exists(outShapeFile):
        outDriver.DeleteDataSource(outShapeFile)
    # Remove output shapefile if it already exists

    # options = ['SPATIALITE=YES'])
    ds = outDriver.CreateDataSource(outShapeFile)

    # create the spatial reference, WGS84

    lyrout = ds.CreateLayer('randomSubset', srs, ogr.wkbPoint)
    fields = [array[1].GetFieldDefnRef(i).GetName()
              for i in range(array[1].GetFieldCount())]

    for i, f in enumerate(fields):
        field_name = ogr.FieldDefn(f, array[1].GetFieldDefnRef(i).GetType())
        field_name.SetWidth(array[1].GetFieldDefnRef(i).GetWidth())
        lyrout.CreateField(field_name)

    for k in array:
        lyrout.CreateFeature(k)

    # Save and close the data source
    ds = None