Example #1
0
File: bf.py Project: zonakre/gasp
def dic_buffer_array_to_shp(arrayBf, outShp, epsg, fields=None):
    """
    Array with dict with buffer proprieties to Feature Class
    """

    import os
    from osgeo import ogr
    from gasp.prop.ff import drv_name
    from gasp.prop.prj import get_sref_from_epsg

    # Get SRS for output
    srs = get_sref_from_epsg(epsg)

    # Create output DataSource and Layer
    outData = ogr.GetDriverByName(drv_name(outShp)).CreateDataSource(outShp)

    lyr = outData.CreateLayer(os.path.splitext(os.path.basename(outShp))[0],
                              srs,
                              geom_type=ogr.wkbPolygon)

    # Create fields
    if fields:
        from gasp.mng.fld import add_fields

        add_fields(lyr, fields)

    lyrDefn = lyr.GetLayerDefn()
    for _buffer in arrayBf:
        newFeat = ogr.Feature(lyrDefn)

        geom = coord_to_buffer(_buffer["X"], _buffer["Y"], _buffer["RADIUS"])

        newFeat.SetGeometry(geom)

        for field in fields:
            if field in _buffer.keys():
                newFeat.SetField(field, _buffer[field])

        lyr.CreateFeature(newFeat)

        newFeat.Destroy()

    del lyrDefn
    outData.Destroy()

    return outShp
Example #2
0
def polylines_from_points(points,
                          polylines,
                          POLYLINE_COLUMN,
                          ORDER_FIELD=None,
                          epsg=None):
    """
    Create a Polyline Table from a Point Table
    
    A given Point Table:
    FID | POLYLINE_ID | ORDER_FIELD
     0  |    P1       |      1
     1  |    P1       |      2
     2  |    P1       |      3
     3  |    P1       |      4
     4  |    P2       |      1
     5  |    P2       |      2
     6  |    P2       |      3
     7  |    P2       |      4
     
    Will be converted into a new Polyline Table:
    FID | POLYLINE_ID
     0  |    P1
     1  |    P2
     
    In the Point Table, the POLYLINE_ID field identifies the Polyline of that point,
    the ORDER FIELD specifies the position (first point, second point, etc.)
    of the point in the polyline.
    
    If no ORDER field is specified, the points will be assigned to polylines
    by reading order.
    """

    import os
    from osgeo import ogr
    from gasp.prop.ff import drv_name
    from gasp.mng.prj import ogr_def_proj
    from gasp.mng.fld import ogr_list_fields_defn
    from gasp.mng.fld import add_fields

    # TODO: check if geometry is correct

    # List all points
    pntSrc = ogr.GetDriverByName(drv_name(points)).Open(points)
    pntLyr = pntSrc.GetLayer()

    lPnt = {}
    cnt = 0
    for feat in pntLyr:
        # Get Point Geom
        geom = feat.GetGeometryRef()
        # Polyline identification
        polyline = feat.GetField(POLYLINE_COLUMN)
        # Get position in the polyline
        order = feat.GetField(ORDER_FIELD) if ORDER_FIELD else cnt

        # Store data
        if polyline not in lPnt.keys():
            lPnt[polyline] = {order: (geom.GetX(), geom.GetY())}

        else:
            lPnt[polyline][order] = (geom.GetX(), geom.GetY())

        cnt += 1

    # Write output
    lineSrc = ogr.GetDriverByName(
        drv_name(polylines)).CreateDataSource(polylines)

    if not epsg:
        from gasp.prop.prj import get_shp_sref
        srs = get_shp_sref(points)

    else:
        from gasp.prop.prj import get_sref_from_epsg
        srs = get_sref_from_epsg(epsg)

    lineLyr = lineSrc.CreateLayer(os.path.splitext(
        os.path.basename(polylines))[0],
                                  srs,
                                  geom_type=ogr.wkbLineString)

    # Create polyline id field
    fields_types = ogr_list_fields_defn(pntLyr)
    add_fields(lineLyr,
               {POLYLINE_COLUMN: fields_types[POLYLINE_COLUMN].keys()[0]})

    polLnhDefns = lineLyr.GetLayerDefn()
    # Write lines
    for polyline in lPnt:
        new_feature = ogr.Feature(polLnhDefns)

        lnh = ogr.Geometry(ogr.wkbLineString)

        pnt_order = lPnt[polyline].keys()
        pnt_order.sort()

        for p in pnt_order:
            lnh.AddPoint(lPnt[polyline][p][0], lPnt[polyline][p][1])

        new_feature.SetField(POLYLINE_COLUMN, polyline)
        new_feature.SetGeometry(lnh)

        lineLyr.CreateFeature(new_feature)

        new_feature = None

    pntSrc.Destroy()
    lineSrc.Destroy()

    return polylines
Example #3
0
def gdl_mean_time_wByPop(unities,
                         unities_groups,
                         population_field,
                         destinations,
                         output,
                         workspace=None,
                         unities_epsg=4326,
                         destinations_epsg=4326):
    """
    Tempo medio ponderado pela populacao residente a infra-estrutura mais 
    proxima
    
    # TODO: Migrate to Pandas
    """

    import os
    from osgeo import ogr
    from gasp.prop.ff import drv_name
    from gasp.fm import points_to_list
    from gasp.mng.feat import feat_to_pnt
    from gasp.mng.prj import project_geom
    from gasp.mng.fld import add_fields
    from gasp.mng.genze import dissolve
    from gasp.web.glg.direct import get_time_pnt_destinations

    workspace = workspace if workspace else \
        os.path.dirname(output)

    # Unities to centroid
    pnt_unities = feat_to_pnt(
        unities, os.path.join(workspace, 'pnt_' + os.path.basename(unities)))

    # List destinations
    lst_destinies = points_to_list(destinations,
                                   listVal="dict",
                                   inEpsg=destinations_epsg,
                                   outEpsg=4326)

    # Calculate indicator
    polyUnit = ogr.GetDriverByName(drv_name(unities)).Open(unities, 1)

    polyLyr = polyUnit.GetLayer()

    polyLyr = add_fields(polyLyr, {'meantime': ogr.OFTReal})

    pntUnit = ogr.GetDriverByName(drv_name(pnt_unities)).Open(pnt_unities, 0)

    pntLyr = pntUnit.GetLayer()

    polyFeat = polyLyr.GetNextFeature()
    distUnities = {}
    groups = {}
    for pntFeat in pntLyr:
        geom = pntFeat.GetGeometryRef()

        if unities_epsg == 4326:
            originGeom = geom
        else:
            originGeom = project_geom(geom, unities_epsg, 4326, api='ogr')

        _id, duration, distance = get_time_pnt_destinations(
            originGeom, lst_destinies)

        __min = duration['value'] / 60.0
        pop = polyFeat.GetField(population_field)
        group = polyFeat.GetField(unities_groups)

        distUnities[polyFeat.GetFID()] = (__min, __min * pop)

        if group not in groups:
            groups[group] = __min * pop
        else:
            groups[group] += __min * pop

        polyFeat = polyLyr.GetNextFeature()

    del polyLyr
    polyUnit.Destroy()

    polyUnit = ogr.GetDriverByName(drv_name(unities)).Open(unities, 1)

    polyLyr = polyUnit.GetLayer()

    for feat in polyLyr:
        unitId = feat.GetFID()
        groupId = feat.GetField(unities_groups)

        indicator = (distUnities[unitId][1] /
                     groups[groupId]) * distUnities[unitId][0]

        feat.SetField('meantime', indicator)

        polyLyr.SetFeature(feat)

    del polyLyr, pntLyr
    polyUnit.Destroy()
    pntUnit.Destroy()

    dissolve(unities,
             output,
             unities_groups,
             statistics={'meantime': 'SUM'},
             api='ogr')