How to use the ogr.wkbPolygon 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 developmentseed / landsat-util / landsat / third_party / ogr2ogr.py View on Github external
elif poOutputSRS is not None:
                    poDstGeometry.AssignSpatialReference(poOutputSRS)

                if poClipDst is not None:
                    poClipped = poDstGeometry.Intersection(poClipDst)
                    if poClipped is None or poClipped.IsEmpty():
                        continue

                    poDstFeature.SetGeometryDirectly(poClipped)
                    poDstGeometry = poClipped

                if bForceToPolygon:
                    poDstFeature.SetGeometryDirectly(ogr.ForceToPolygon(poDstGeometry))

                elif bForceToMultiPolygon or \
                        (bPromoteToMulti and wkbFlatten(poDstGeometry.GetGeometryType()) == ogr.wkbPolygon):
                    poDstFeature.SetGeometryDirectly(ogr.ForceToMultiPolygon(poDstGeometry))

                elif bForceToMultiLineString or \
                        (bPromoteToMulti and wkbFlatten(poDstGeometry.GetGeometryType()) == ogr.wkbLineString):
                    poDstFeature.SetGeometryDirectly(ogr.ForceToMultiLineString(poDstGeometry))

            gdal.ErrorReset()
            if poDstLayer.CreateFeature( poDstFeature ) != 0 and not bSkipFailures:
                if nGroupTransactions > 0:
                    poDstLayer.RollbackTransaction()

                return False

        #/* Report progress */
        nCount = nCount  + 1
        if pfnProgress is not None:
github qgiscloud / qgis-cloud-plugin / qgiscloud / pyogr / ogr2ogr.py View on Github external
poDS.Destroy()
        return None

    if pszWhere is not None:
        poLyr.SetAttributeFilter(pszWhere)

    poFeat = poLyr.GetNextFeature()
    while poFeat is not None:
        poSrcGeom = poFeat.GetGeometryRef()
        if poSrcGeom is not None:
            eType = wkbFlatten(poSrcGeom.GetGeometryType())

            if poGeom is None:
                poGeom = ogr.Geometry(ogr.wkbMultiPolygon)

            if eType == ogr.wkbPolygon:
                poGeom.AddGeometry(poSrcGeom)
            elif eType == ogr.wkbMultiPolygon:
                for iGeom in range(poSrcGeom.GetGeometryCount()):
                    poGeom.AddGeometry(poSrcGeom.GetGeometryRef(iGeom))

            else:
                print("ERROR: Geometry not of polygon type.")
                if pszSQL is not None:
                    poDS.ReleaseResultSet(poLyr)
                poDS.Destroy()
                return None

        poFeat = poLyr.GetNextFeature()

    if pszSQL is not None:
        poDS.ReleaseResultSet(poLyr)
github DigitalGlobe / mltools / mltools / simple_lulc / simple_lulc.py View on Github external
while h-y0 >= winY:
        x0 = 0
        while w-x0 >= winX:
            # Create geometry
            ring = ogr.Geometry(ogr.wkbLinearRing)
            xc,yc = tf_raster_to_proj(x0,y0,geoTransform)
            ring.AddPoint( xc,yc )
            xc,yc = tf_raster_to_proj(x0+winX,y0,geoTransform)
            ring.AddPoint( xc,yc )
            xc,yc = tf_raster_to_proj(x0+winX,y0+winY,geoTransform)
            ring.AddPoint( xc,yc )
            xc,yc = tf_raster_to_proj(x0,y0+winY,geoTransform)
            ring.AddPoint( xc,yc )            
            xc,yc = tf_raster_to_proj(x0,y0,geoTransform)
            ring.AddPoint( xc,yc )
            poly = ogr.Geometry(ogr.wkbPolygon)
            poly.AddGeometry(ring)

            # Transform to target SRS
            poly.Transform(coordTrans)

            # Read data            
            data = raster.ReadAsArray(x0, y0, winX, winY).astype(np.float)
            # Classify data. Now this depends on if there is one or many 
            # feature vectors being computed
            # handle those cases accordingly, maybe a majority decision, 
            # maybe count labels, etc
            for featureVector in simple_feature_extractor(data):
                labels = classifier.predict(featureVector)
                        
            features.append({"class_name":labels[0], "geometry": poly})
github DigitalGlobe / mltools / algos / vanilla_detector / vanilla_detector.py View on Github external
features = []

    # simple sliding detection window
    y0 = 0
    while h-y0 >= winY:
        x0 = 0
        while w-x0 >= winX:
            # Create geometry
            ring = ogr.Geometry(ogr.wkbLinearRing)
            xc,yc = tfRasterToProj(x0,y0,geoTransform); ring.AddPoint( xc,yc )
            xc,yc = tfRasterToProj(x0+winX,y0,geoTransform); ring.AddPoint( xc,yc )
            xc,yc = tfRasterToProj(x0+winX,y0+winY,geoTransform); ring.AddPoint( xc,yc )
            xc,yc = tfRasterToProj(x0,y0+winY,geoTransform); ring.AddPoint( xc,yc )            
            xc,yc = tfRasterToProj(x0,y0,geoTransform); ring.AddPoint( xc,yc )
            poly = ogr.Geometry(ogr.wkbPolygon)
            poly.AddGeometry(ring)
            # Transform to target SRS
            poly.Transform(coordTrans)

            # Read data            
            data = raster.ReadAsArray(x0, y0, winX, winY).astype(np.float)
            # Classify data. Now this depends on if there is one or many feature vectors being computed
            # handle those cases accordingly, maybe a majority decision, maybe count labels, etc
            for featureVector in computeFeatureVectors(data):
                labels = classifier.predict(featureVector)
            
            if labels[0] != 'clutter':            
                features.append({"Class":labels[0], "geom": poly})
            
            x0 += stepX
        y0 += stepY
github Kitware / Danesfield / tools / align_buildings.py View on Github external
def shift_vector(inputFeatures, outputVectorFile, outputLayerName, outProjection, offsetGeo):
    outDriver = ogr.GetDriverByName("ESRI Shapefile")
    print("Shifting vector -> {}".format(os.path.basename(outputVectorFile)))
    outVector = outDriver.CreateDataSource(outputVectorFile)
    outSrs = osr.SpatialReference(outProjection)
    # create layer
    outLayer = outVector.CreateLayer(os.path.basename(outputLayerName),
                                     srs=outSrs, geom_type=ogr.wkbPolygon)
    outFeatureDef = outLayer.GetLayerDefn()
    # create rings from input rings by shifting points
    for feature in inputFeatures:
        # create the poly
        outPoly = ogr.Geometry(ogr.wkbPolygon)
        poly = feature.GetGeometryRef()
        for ring_idx in range(poly.GetGeometryCount()):
            ring = poly.GetGeometryRef(ring_idx)
            # create the ring
            outRing = ogr.Geometry(ogr.wkbLinearRing)
            for i in range(0, ring.GetPointCount()):
                pt = ring.GetPoint(i)
                outRing.AddPoint(pt[0] + offsetGeo[0], pt[1] + offsetGeo[1])
            outPoly.AddGeometry(outRing)
        # create feature
        outFeature = ogr.Feature(outFeatureDef)
        outFeature.SetGeometry(outPoly)
        outLayer.CreateFeature(outFeature)
github mapmint / mapmint / mapmint-services / vector-tools-src / cgi-env / vectSql.py View on Github external
def DumpReadableGeometry( poGeometry, pszPrefix, options ):

    if pszPrefix == None:
        pszPrefix = ""

    if 'DISPLAY_GEOMETRY' in options and EQUAL(options['DISPLAY_GEOMETRY'], 'SUMMARY'):

        line = ("%s%s : " % (pszPrefix, poGeometry.GetGeometryName() ))
        eType = poGeometry.GetGeometryType()
        if eType == ogr.wkbLineString or eType == ogr.wkbLineString25D:
            line = line + ("%d points" % poGeometry.GetPointCount())
            print(line)
        elif eType == ogr.wkbPolygon or eType == ogr.wkbPolygon25D:
            nRings = poGeometry.GetGeometryCount()
            if nRings == 0:
                line = line + "empty"
            else:
                poRing = poGeometry.GetGeometryRef(0)
                line = line + ("%d points" % poRing.GetPointCount())
                if nRings > 1:
                    line = line + (", %d inner rings (" % (nRings - 1))
                    for ir in range(0,nRings-1):
                        if ir > 0:
                            line = line + ", "
                        poRing = poGeometry.GetGeometryRef(ir+1)
                        line = line + ("%d points" % poRing.GetPointCount())
                    line = line + ")"
            print(line)
github beeoda / scripts / bin / sample_map.py View on Github external
"""
    """
    # Corners of pixel in pixel coordinates
    corners = [(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)]

    # Raster geo-transform
    gt = map_ds.GetGeoTransform()
    # Get OSR spatial reference from raster to give to OGR dataset
    map_sr = osr.SpatialReference()
    map_sr.ImportFromWkt(map_ds.GetProjectionRef())

    # Get OGR driver
    driver = ogr.GetDriverByName(ogr_frmt)
    # Create OGR dataset and layer
    sample_ds = driver.CreateDataSource(output)
    layer = sample_ds.CreateLayer('sample', map_sr, geom_type=ogr.wkbPolygon)

    # Add fields for layer
    # Sample ID field
    layer.CreateField(ogr.FieldDefn('ID', ogr.OFTInteger))
    # Row/Col fields
    layer.CreateField(ogr.FieldDefn('ROW', ogr.OFTInteger))
    layer.CreateField(ogr.FieldDefn('COL', ogr.OFTInteger))
    # Strata field
    layer.CreateField(ogr.FieldDefn('STRATUM', ogr.OFTInteger))

    # Loop through samples adding to layer
    for i, (stratum, col, row) in enumerate(zip(strata, cols, rows)):
        # Feature
        feature = ogr.Feature(layer.GetLayerDefn())
        feature.SetField('ID', int(i))
        feature.SetField('ROW', int(row))
github developmentseed / landsat-util / landsat / third_party / ogr2ogr.py View on Github external
pszNewLayerName = args[iArg]

        elif EQUAL(args[iArg],"-nlt") and iArg < nArgc-1:

            if EQUAL(args[iArg+1],"NONE"):
                eGType = ogr.wkbNone
            elif EQUAL(args[iArg+1],"GEOMETRY"):
                eGType = ogr.wkbUnknown
            elif EQUAL(args[iArg+1],"PROMOTE_TO_MULTI"):
                bPromoteToMulti = True
            elif EQUAL(args[iArg+1],"POINT"):
                eGType = ogr.wkbPoint
            elif EQUAL(args[iArg+1],"LINESTRING"):
                eGType = ogr.wkbLineString
            elif EQUAL(args[iArg+1],"POLYGON"):
                eGType = ogr.wkbPolygon
            elif EQUAL(args[iArg+1],"GEOMETRYCOLLECTION"):
                eGType = ogr.wkbGeometryCollection
            elif EQUAL(args[iArg+1],"MULTIPOINT"):
                eGType = ogr.wkbMultiPoint
            elif EQUAL(args[iArg+1],"MULTILINESTRING"):
                eGType = ogr.wkbMultiLineString
            elif EQUAL(args[iArg+1],"MULTIPOLYGON"):
                eGType = ogr.wkbMultiPolygon
            elif EQUAL(args[iArg+1],"GEOMETRY25D"):
                eGType = ogr.wkbUnknown | ogr.wkb25DBit
            elif EQUAL(args[iArg+1],"POINT25D"):
                eGType = ogr.wkbPoint25D
            elif EQUAL(args[iArg+1],"LINESTRING25D"):
                eGType = ogr.wkbLineString25D
            elif EQUAL(args[iArg+1],"POLYGON25D"):
                eGType = ogr.wkbPolygon25D
github iocast / featureserver / FeatureServer / VectorFormats / Formats / OGR.py View on Github external
gring = ogr.Geometry(type=ogr.wkbLineString)
                for coordinate in ring:
                    gring.AddPoint_2D(coordinate[0], coordinate[1])
                geometry.AddGeometry(gring)
    
        
        elif type == 'Polygon':
            for ring in coordinates:
                gring = ogr.Geometry(type=ogr.wkbLinearRing)
                for coordinate in ring:
                    gring.AddPoint_2D(coordinate[0], coordinate[1])
                geometry.AddGeometry(gring)
        
        elif type == 'MultiPolygon':
            for poly in coordinates:
                gpoly = ogr.Geometry(type=ogr.wkbPolygon)
                for ring in poly:
                    gring = ogr.Geometry(type=ogr.wkbLinearRing)
                    for coordinate in ring:
                        gring.AddPoint_2D(coordinate[0], coordinate[1])
                    gpoly.AddGeometry(gring)
                geometry.AddGeometry(gpoly)
        
        return geometry
github ofringer / suntanspy / GIS / maptools.py View on Github external
def get_shape_type(obj):
        if isinstance(obj,geometry.Polygon):
            return ogr.wkbPolygon
        elif isinstance(obj,geometry.LineString):
            return ogr.wkbLineString
        elif isinstance(obj,geometry.Point):
            return ogr.wkbPoint
        else:
            raise Exeption, 'incompatible type: %s'%(type(obj))