コード例 #1
0
ファイル: test_path_finders.py プロジェクト: SSTyT/tod
 def test_get_indicators_path(self):
     shp_name = "estaciones"
     subcategory = "buffers"
     exp_path = os.path.join(pf.get_project_dir(),
                             "indicadores/buffers/estaciones/estaciones")
     path = pf.get_indicators_shp_path(shp_name, subcategory)
     self.assertEqual(path, exp_path)
コード例 #2
0
ファイル: recalculate_indicators.py プロジェクト: SSTyT/tod
def main(buffers_dir=None, skip=None, recalculate=False, area_level="RADIO",
         subcategory=None, omit_fields=None, by_area=None):
    skip = skip or []
    by_area = by_area or []

    for buffer_dir in iter_buffer_dirs(buffers_dir):
        print("Calculating", os.path.basename(buffer_dir), "indicators")
        sys.stdout.flush()

        buffer_indic_path = get_indicators_shp_path(
            os.path.basename(buffer_dir), "buffers")
        # print(buffer_indic_path + ".shp")
        if not os.path.isfile(buffer_indic_path + ".shp") or recalculate:
            recalculate_indicators(buffer_dir, area_level, skip,
                                   subcategory=subcategory,
                                   omit_fields=omit_fields,
                                   by_area=by_area)
コード例 #3
0
ファイル: recalculate_indicators.py プロジェクト: SSTyT/tod
def recalculate_indicators(new_shp_dir, area_level, skip=None,
                           subcategory=None, omit_fields=None, by_area=None):
    skip = skip or []
    by_area = by_area or []

    # some fields are omitted always
    if omit_fields:
        if not type(omit_fields) == list:
            omit_fields = [omit_fields]
        omit_fields = OMIT_FIELDS
        omit_fields.extend(omit_fields)

    new_shp_path = find_shp_path(new_shp_dir)
    shp_name = os.path.basename(new_shp_path)

    sf = shapefile.Reader(new_shp_path)
    df_indicators = get_indicators(area_level)
    weights = get_weights(new_shp_path, area_level)

    w = shapefile.Writer(shapefile.POLYGON)

    indicators = _get_indicator_names(df_indicators)
    for field in sf.fields[1:]:
        w.field(*field)
    for indicator in indicators:
        field = [str(indicator), str("N"), 20, 18]
        # print(indicator)
        w.field(*field)
    w.field(str("area_km2"), str("N"), 20, 18)
    w.field(str("hab_km2"), str("N"), 20, 18)
    # print(w.fields)

    for record_shape in sf.iterShapeRecords():
        record = record_shape.record
        shape = record_shape.shape

        # print(record[0])
        if type(record[0]) == int:
            id_record = unicode(record[0])
        else:
            id_record = unicode(record[0].decode("utf-8"))

        if len(weights[id_record]) > 0:
            calculated_indicators = _calc_indicators(indicators,
                                                     df_indicators,
                                                     weights[id_record],
                                                     area_level,
                                                     skip, by_area)
            # print(calculated_indicators)
            record.extend(calculated_indicators)

            area = calculate_area(shape) / 1000000
            record.append(area)

            population = calculated_indicators[indicators.index(POPULATION)]
            pop_density = population / area
            record.append(pop_density)

            w.record(*record)

            w.poly(shapeType=shapefile.POLYGON, parts=[shape.points])

    path = get_indicators_shp_path(shp_name, subcategory)
    w.save(path)

    utils.copy_prj(new_shp_path.decode("utf-8"), path)