コード例 #1
0
ファイル: wells.py プロジェクト: mjasher/aus-hydro-data
def wells_in_boundary(bore_file, boundary_file, bbox, nrow, ncol, delr, delc):
    driver = ogr.GetDriverByName('ESRI Shapefile')
    boundary_dataSource = driver.Open(boundary_file, 0) # 0 means read-only. 1 means writeable.
    boundary_layer = boundary_dataSource.GetLayer()
    assert boundary_layer.GetFeatureCount() == 1
    boundary_feature = boundary_layer.GetFeature(0)
    boundary_geom = boundary_feature.GetGeometryRef()
    
    # boundary_ref = osr.SpatialReference()
    # boundary_ref.ImportFromWkt(bbox["ref"])

    min_x, max_x, min_y, max_y = bbox["bbox"]
    pixelWidth = (max_x - min_x) / ncol
    pixelHeight = (max_y - min_y) / nrow
    assert pixelHeight == delc
    assert pixelWidth == delr

    bore_json = clip_wells(bore_file, bbox)


    def rasterize_bores(source_layer, clipping_poly, row, col):
        return len([feature for feature in source_layer 
                    if feature.GetGeometryRef().Clone().Intersects(clipping_poly)
                    and feature.GetGeometryRef().Within(boundary_geom)])


    bore_array = utils.ogr_to_raster(bore_json, nrow, ncol, bbox['bbox'], rasterize_bores, filter=False)

    return bore_array
コード例 #2
0
ファイル: streams.py プロジェクト: mjasher/aus-hydro-data
def make_riv(river_file, boundary_file, bbox, nrow, ncol, delr, delc):

    driver = ogr.GetDriverByName('ESRI Shapefile')
    boundary_dataSource = driver.Open(boundary_file, 0) # 0 means read-only. 1 means writeable.
    boundary_layer = boundary_dataSource.GetLayer()
    assert boundary_layer.GetFeatureCount() == 1
    boundary_feature = boundary_layer.GetFeature(0)
    boundary_geom = boundary_feature.GetGeometryRef()

    min_x, max_x, min_y, max_y = bbox["bbox"]
    pixelWidth = (max_x - min_x) / ncol
    pixelHeight = (max_y - min_y) / nrow
    assert pixelHeight == delc
    assert pixelWidth == delr

    layers = [
        # "AHGFNetworkConnectivityUp",
        # "AHGFNetworkConnectivityDown",
        # "AHGFNetworkStream_FS",
        "AHGFNetworkStream", # note not revealed by ogrinfo
        # "AHGFCatchment",
    ]

    jsons = gdb_to_geojson(river_file, bbox, layers)

    river_json = jsons[layers.index("AHGFNetworkStream")]

    """
    ['HydroID', 
    'AHGFFType', 'Name', 'Hierarchy', 'Perennial', 'AusHydroID', 'AusHydroEr', 'SegmentNo', 'DrainID',
    'From_Node', 'To_Node', 
    'NextDownID', 
    'Enabled', 'FlowDir', 'SrcFCName', 
    'SrcFType', 'SrcType', 'SourceID', 'FeatRel', 'FSource', 'AttrRel', 'AttrSource', 
    'PlanAcc', 'Symbol', 'TextNote', 'GeodesLen', 'UpstrGeoLn', 'UpstrDArea', 'Shape_Length']

    """
    reaches = []
    def rasterize_riv(source_layer, clipping_poly, row, col):
        properties = []
        for feature in source_layer:
            geom = feature.GetGeometryRef()
            if feature.GetField('Hierarchy') == "Major" and clipping_poly.Intersects(boundary_geom) and clipping_poly.Intersects(geom):
                
                clipped = clipping_poly.Clone().Intersection(geom)
                length = clipped.Length()

                reaches.append({
                    "length": length,
                    "row": row,
                    "col": col,
                    'HydroID': feature.GetField('HydroID'),
                    'NextDownID': feature.GetField('NextDownID'),
                    'Name': feature.GetField('Name'),
                    })

                properties.append(length)

        return sum(properties)


    raster =  utils.ogr_to_raster(
        river_json,
        nrow, ncol,
        bbox['bbox'],
        rasterize_riv) 


    reach_ids = [r['HydroID'] for r in reaches]
    next_ids = [r['NextDownID'] for r in reaches]
    tips = []
    heads = []

    for reach in reaches:
        reach['index_of_next'] = [i for i, r in enumerate(reaches) if r['HydroID'] == reach['NextDownID']]
        reach['index_of_previous'] = [i for i, r in enumerate(reaches) if r['NextDownID'] == reach['HydroID']]
        if len(reach['index_of_next']) == 0:
            tips.append(reach)
        if len(reach['index_of_previous']) == 0:
            heads.append(reach)


    return {
        "raster": raster,
        "reaches": reaches,
        "heads": heads,
        "tips": tips,
    }