コード例 #1
0
def main(area_level="RADIO",
         limit=10000,
         field_name="reach_area",
         transport_shp_name="recorridos_de_colectivos"):
    df, old_index, new_index = get_indicators_df(area_level)
    already_done = list(df[pd.notnull(df[field_name])].index)

    # get bus lines
    path_bus = get_transport_shp_path(transport_shp_name)
    sf_lines = shapefile.Reader(path_bus)
    lines = get_shapely_shapes(sf_lines)

    # get division shapes
    path_divisions = get_division_path(AREA_LEVEL_SHP_NAME[area_level])
    sf_polys = shapefile.Reader(path_divisions)
    polys = get_shapely_shapes(sf_polys)
    ids = [record[0] for record in sf_polys.records()]
    area_level_shapes = {
        id_p: polygon
        for id_p, polygon in zip(ids, polys) if id_p not in already_done
    }

    # iterate division shapes calculating the indicator
    start = time.time()
    total_shapes = len(area_level_shapes)
    progress_status = "Nothing done."
    for i, (id_shape, shape) in enumerate(area_level_shapes.iteritems(), 1):
        # only add new value if the shape is in the index
        if id_shape in new_index:
            try:
                lines_intersect = intersecting_lines(shape, lines)
                surface = calc_reachable_surface_and_people(
                    shape, lines_intersect)
                add_value(id_shape, surface, df)

                elapsed = (time.time() - start) / 60.0
                average = elapsed / i
                prediction = (total_shapes - i) * average

                progress_status = """
                {} {}/{} in {:.2f} mins. Average: {:.2f} Time to end: {:.2f}
                """.format(id_shape.ljust(10), i, total_shapes, elapsed,
                           average, prediction).strip()
                print(progress_status, end="\r" * len(progress_status))

                if i >= limit:
                    print(progress_status)
                    print("Limit of", limit, "shapes reached.")
                    break
            except KeyboardInterrupt:
                print(progress_status)
                print("Interrupted!")
                break

    print("Saving values...", end=" ")
    save_indicators_df(df, old_index)
    print("Done.")
コード例 #2
0
ファイル: reachability.py プロジェクト: SSTyT/tod
def main(area_level="RADIO", limit=10000, field_name="reach_area",
         transport_shp_name="recorridos_de_colectivos"):
    df, old_index, new_index = get_indicators_df(area_level)
    already_done = list(df[pd.notnull(df[field_name])].index)

    # get bus lines
    path_bus = get_transport_shp_path(transport_shp_name)
    sf_lines = shapefile.Reader(path_bus)
    lines = get_shapely_shapes(sf_lines)

    # get division shapes
    path_divisions = get_division_path(AREA_LEVEL_SHP_NAME[area_level])
    sf_polys = shapefile.Reader(path_divisions)
    polys = get_shapely_shapes(sf_polys)
    ids = [record[0] for record in sf_polys.records()]
    area_level_shapes = {id_p: polygon for id_p, polygon in zip(ids, polys) if
                         id_p not in already_done}

    # iterate division shapes calculating the indicator
    start = time.time()
    total_shapes = len(area_level_shapes)
    progress_status = "Nothing done."
    for i, (id_shape, shape) in enumerate(area_level_shapes.iteritems(), 1):
        # only add new value if the shape is in the index
        if id_shape in new_index:
            try:
                lines_intersect = intersecting_lines(shape, lines)
                surface = calc_reachable_surface_and_people(shape,
                                                            lines_intersect)
                add_value(id_shape, surface, df)

                elapsed = (time.time() - start) / 60.0
                average = elapsed / i
                prediction = (total_shapes - i) * average

                progress_status = """
                {} {}/{} in {:.2f} mins. Average: {:.2f} Time to end: {:.2f}
                """.format(id_shape.ljust(10), i, total_shapes, elapsed,
                           average, prediction).strip()
                print(progress_status, end="\r" * len(progress_status))

                if i >= limit:
                    print(progress_status)
                    print("Limit of", limit, "shapes reached.")
                    break
            except KeyboardInterrupt:
                print(progress_status)
                print("Interrupted!")
                break

    print("Saving values...", end=" ")
    save_indicators_df(df, old_index)
    print("Done.")
コード例 #3
0
ファイル: profiling.py プロジェクト: yghlc/tod
def _prepare_test_case():
    path_colectivos = get_transport_shp_path("recorrido-colectivos")
    path_radios = get_division_path("radios_censo_2010")

    sf_lines = shapefile.Reader(path_colectivos)
    lines = get_shapely_shapes(sf_lines)

    sf_polys = shapefile.Reader(path_radios)
    polys = get_shapely_shapes(sf_polys)
    ids = [record[0] for record in sf_polys.records()]
    radios = {id: polygon for id, polygon in zip(ids, polys)}
    radio = radios["14_1_8"]

    return radio, lines
コード例 #4
0
ファイル: create_buffers.py プロジェクト: SSTyT/tod
def create_buffered_shp(directory, distance, buffer_dir=BUFFER_DIR,
                        resolution=16, recalculate=False,
                        context_shp_or_polygon=None):
    context_polygon = create_context_polygon(context_shp_or_polygon)

    shp_path = pf.find_shp_path(directory)
    shp_name = _create_shp_name(shp_path, distance)
    # print("\n".join([directory, shp_name, buffer_dir]))
    buffer_shp_path = _create_shp_path(directory, shp_name, buffer_dir)
    # print(buffer_shp_path)

    if not os.path.isfile(buffer_shp_path + ".shp") or recalculate:

        # read shapefile with pyshp
        sf_est = shapefile.Reader(shp_path)

        # create buffers from shapely shapes
        buffer_shapes = []
        for shape in geo_utils.get_shapely_shapes(sf_est):
            if not context_polygon or context_polygon.contains(shape):
                buffer_shapes.append(create_buffer(shape, distance,
                                                   resolution,
                                                   context_polygon))

        write_shapefile(sf_est, buffer_shapes, buffer_shp_path)
        utils.copy_prj(shp_path, buffer_shp_path)
コード例 #5
0
ファイル: create_buffers.py プロジェクト: yghlc/tod
def create_buffered_shp(directory,
                        distance,
                        buffer_dir=BUFFER_DIR,
                        resolution=16,
                        recalculate=False,
                        context_shp_or_polygon=None):
    context_polygon = create_context_polygon(context_shp_or_polygon)

    shp_path = pf.find_shp_path(directory)
    shp_name = _create_shp_name(shp_path, distance)
    # print("\n".join([directory, shp_name, buffer_dir]))
    buffer_shp_path = _create_shp_path(directory, shp_name, buffer_dir)
    # print(buffer_shp_path)

    if not os.path.isfile(buffer_shp_path + ".shp") or recalculate:

        # read shapefile with pyshp
        sf_est = shapefile.Reader(shp_path)

        # create buffers from shapely shapes
        buffer_shapes = []
        for shape in geo_utils.get_shapely_shapes(sf_est):
            if not context_polygon or context_polygon.contains(shape):
                buffer_shapes.append(
                    create_buffer(shape, distance, resolution,
                                  context_polygon))

        write_shapefile(sf_est, buffer_shapes, buffer_shp_path)
        utils.copy_prj(shp_path, buffer_shp_path)