def load_cpa_gdf(filepath, target_crs, slope_filter=None, layer=None):

    if layer is not None:
        cpa_gdf = gpd.read_file(filepath, layer=layer)
    else:
        cpa_gdf = gpd.read_file(filepath)

    if slope_filter:
        cpa_gdf = cpa_gdf.loc[cpa_gdf["m_slope"] <= slope_filter, :]
        cpa_gdf = cpa_gdf.reset_index(drop=True)

    cpa_gdf = cpa_gdf.to_crs(target_crs)
    centroid = find_centroid(cpa_gdf)
    cpa_gdf["Latitude"] = centroid.y
    cpa_gdf["Longitude"] = centroid.x
    cpa_gdf["cpa_id"] = cpa_gdf.index

    cpa_gdf["prefSite"] = cpa_gdf["prefSite"].fillna(0)

    dist_cols = [
        "d_coast_sub_161kVplus", "d_coast", "d_sub_load_metro_750k_center"
    ]
    for col in dist_cols:
        mile_col = f"{col}_miles"
        cpa_gdf[mile_col] = cpa_gdf[col] * 1.60934

    return cpa_gdf
def load_cpa_gdf(filepath, target_crs, slope_filter=None, layer=None):

    if layer is not None:
        cpa_gdf = gpd.read_file(filepath, layer=layer)
    else:
        cpa_gdf = gpd.read_file(filepath)

    if slope_filter:
        cpa_gdf = cpa_gdf.loc[cpa_gdf["m_slope"] <= slope_filter, :]
        cpa_gdf = cpa_gdf.reset_index(drop=True)

    cpa_gdf = cpa_gdf.to_crs(target_crs)
    centroid = find_centroid(cpa_gdf)
    cpa_gdf["Latitude"] = centroid.y
    cpa_gdf["Longitude"] = centroid.x
    cpa_gdf["cpa_id"] = cpa_gdf.index

    return cpa_gdf
def load_metro_areas_shapefile():
    shpfile_path = (
        CWD / "USA_Core_Based_Statistical_Area" / "USA_Core_Based_Statistical_Area.shp"
    )
    metro_areas = gpd.read_file(shpfile_path)
    metro_areas = metro_areas.to_crs(epsg=4326)

    metro_areas["center"] = find_centroid(metro_areas)

    keep_cols = ["CBSA_ID", "NAME", "CBSA_TYPE", "POPULATION", "center", "geometry"]
    # metro_areas["geometry"] = metro_areas["center"]
    metro_areas = metro_areas.loc[:, keep_cols]
    metro_areas["metro_id"] = metro_areas["CBSA_ID"]
    metro_areas.columns = metro_areas.columns.str.lower()
    metro_areas["state"] = metro_areas["name"].str.split(", ").str[-1]
    metro_areas = metro_areas.loc[~metro_areas.state.isin(["AK", "HI", "PR"]), :]

    return metro_areas
Esempio n. 4
0
def transmission_line_distance(trans_constraints_df,
                               ipm_shapefile,
                               settings,
                               units="mile"):
    logger.info("Calculating transmission line distance")
    ipm_shapefile["geometry"] = ipm_shapefile.buffer(0.01)
    model_polygons = ipm_shapefile.dissolve(by="model_region")
    model_polygons = model_polygons.to_crs(epsg=4326)
    region_centroids = find_centroid(model_polygons)

    distances = [
        single_line_distance(line_name, region_centroids, units=units)
        for line_name in trans_constraints_df["Transmission Path Name"]
    ]
    trans_constraints_df[f"distance_{units}"] = distances
    trans_constraints_df[f"distance_{units}"] = trans_constraints_df[
        f"distance_{units}"]

    return trans_constraints_df
def main(
        fn_prefix: str = "",
        additional_metros: Optional[List[str]] = typer.Option(None),
):
    logger.info("Loading states, voronoi, and CPAs")
    us_states = load_us_states_gdf()

    # metro_voronoi_gdf = gpd.read_file("large_metro_voronoi.geojson")
    cpa_gdf = load_cpa_gdf(
        "OffshoreWind_CPA_BLUA_20210125",
        target_crs=us_states.crs,
    )
    # Specify fixed (OTRG3) and floating (OTRG10), with cutoff at 50m
    cpa_gdf["TRG"] = "OTRG13"
    cpa_gdf.loc[cpa_gdf["m_seafloorDepth"] >= -50, "TRG"] = "OTRG3"

    ipm_gdf = load_ipm_shapefile()

    metro_gdf = load_metro_areas_shapefile()
    largest_metros = find_largest_cities(
        metro_areas_gdf=metro_gdf,
        ipm_gdf=ipm_gdf,
        min_population=750000,
        additional_metros=additional_metros,
    )

    logger.info("Finding nearest MSA to assign IPM Region and cbsa_id")
    # cpa_metro = ckdnearest(
    #     cpa_gdf.reset_index(drop=True), largest_metros.reset_index(drop=True)
    # )
    # cpa_metro = cpa_metro.drop(columns=["lat2", "lon2"])

    # Only 3 CPAs get assigned to PJM_SMAC, and it causes errors when clustering.
    # smac_filter = cpa_metro.loc[cpa_metro.IPM_Region == "PJM_SMAC", :].index
    # cpa_metro.loc[smac_filter, "IPM_Region"] = "PJM_EMAC"
    # cpa_metro.loc[smac_filter, "metro_id"] = "37980"

    logger.info("Matching CPAs with VCE sites")
    site_locations = load_site_locations()
    site_locations = site_locations.rename(columns={
        "Latitude": "latitude",
        "Longitude": "longitude"
    })
    cpa_vce_site = ckdnearest(cpa_gdf.copy(), site_locations.copy())
    cpa_vce_site = cpa_vce_site.drop(columns=["lat2", "lon2"])

    # Load substations and join with states so we know the state location. Use this
    # for offshore wind landing in NJ so it doesn't delivery to NY_Z_J
    substation_gdf = load_substations()
    substation_gdf = gpd.sjoin(substation_gdf, us_states, how="left")
    landfall_points = gpd.read_file("landfall/landfall.gdb")
    landfall_points = landfall_points.to_crs(crs="EPSG:4326")
    landfall_points["center"] = find_centroid(landfall_points)
    landfall_points["latitude"] = landfall_points.center.y
    landfall_points["longitude"] = landfall_points.center.x
    landfall_points = gpd.sjoin(
        landfall_points,
        us_states.set_geometry(us_states.buffer(0.03)),
        how="left").drop_duplicates(subset=["ORIG_FID"])
    cpa_vce_interconnect_distances = calc_interconnect_distances(
        cpa_gdf=cpa_vce_site,
        landfall_gdf=landfall_points,
        substation_gdf=substation_gdf,
        metro_gdf=largest_metros,
    )

    cpa_vce_lcoe = calc_interconnect_costs_lcoe(cpa_vce_interconnect_distances)

    metro_ipm_map = (largest_metros[[
        "metro_id", "IPM_Region"
    ]].drop_duplicates().set_index("metro_id"))

    cpa_vce_lcoe["ipm_region"] = cpa_vce_lcoe["delivery_metro"].map(
        metro_ipm_map["IPM_Region"])
    cpa_vce_lcoe["metro_id"] = cpa_vce_lcoe["delivery_metro"]

    logger.info("Writing results to file")

    # cpa_vce_lcoe.drop(columns=["geometry"]).to_csv("base_offshorewind_lcoe.csv", index=False, float_format='%.5f')

    geodata_cols = [
        "cpa_id",
        # "state",
        "Site",
        "metro_id",
        "IPM_Region",
        "interconnect_annuity",
        "lcoe",
        "geometry",
    ]

    keep_cols = [
        "Area",
        "Latitude",
        "Longitude",
        "cpa_id",
        "Site",
        "ORIG_FID",
        "substation_id",
        "d_coast_miles",
        "d_coast_sub_161kVplus_miles",
        "d_sub_load_metro_750k_center_miles",
        "site_shore_miles",
        "shore_substation_miles",
        "substation_metro_miles",
        "land_substation_capex",
        "substation_metro_capex",
        "offshore_spur_capex",
        "interconnect_capex",
        "interconnect_annuity",
        "TRG",
        "ipm_region",
        "interconnect_capex",
        "offshore_wind_cf",
        "lcoe",
        "metro_id",
        "prefSite",
        "turbineType",
        "STATE_NAME",
    ]
    cpa_vce_lcoe[keep_cols].sort_values("cpa_id").drop_duplicates().to_csv(
        f"{fn_prefix}base_offshorewind_lcoe.csv", index=False)
def load_metro_areas_shapefile():
    shpfile_path = (
        CWD /
        "USA_Core_Based_Statistical_Area"  # / "USA_Core_Based_Statistical_Area.shp"
    )
    metro_areas = gpd.read_file(shpfile_path)
    metro_areas = metro_areas.to_crs(epsg=4326)

    corrected_metro_centroids = pd.read_csv(CWD.parent / "bin" /
                                            "msa_urban_centroids.csv")
    corrected_metro_centroids["CBSA_ID"] = corrected_metro_centroids[
        "CBSA_ID"].astype("str")
    corrected_metro_centroids = corrected_metro_centroids.set_index("CBSA_ID")
    corrected_metro_centroids = gpd.GeoDataFrame(
        corrected_metro_centroids,
        geometry=points_from_xy(
            corrected_metro_centroids["msa_longitude"],
            corrected_metro_centroids["msa_latitude"],
        ),
        crs="EPSG:4326",
    )

    metro_areas["center"] = find_centroid(metro_areas)
    metro_areas["corrected_center"] = metro_areas["CBSA_ID"].map(
        corrected_metro_centroids["geometry"])
    metro_areas["msa_center"] = metro_areas["center"]
    metro_areas.loc[~metro_areas["corrected_center"].isna(),
                    "center"] = metro_areas["corrected_center"]

    keep_cols = [
        "CBSA_ID",
        "NAME",
        "CBSA_TYPE",
        "POPULATION",
        "center",
        "msa_center",
        "geometry",
    ]

    # metro_areas["geometry"] = metro_areas["center"]
    metro_areas = metro_areas.loc[:, keep_cols]
    metro_areas["metro_id"] = metro_areas["CBSA_ID"]
    metro_areas.columns = metro_areas.columns.str.lower()
    metro_areas["state"] = metro_areas["name"].str.split(", ").str[-1]
    metro_areas = metro_areas.loc[
        ~metro_areas.state.isin(["AK", "HI", "PR"]), :]

    NY_Z_J_lon_lat = (-73.930488, 40.695448)
    NY_Z_K_lon_lat = (-73.008906, 40.840391)

    extra_metros = pd.DataFrame([["NY_Z_J", 1e6], ["NY_Z_K", 1e6]],
                                columns=["metro_id", "population"])
    extra_metros = gpd.GeoDataFrame(
        extra_metros,
        geometry=points_from_xy(*zip(NY_Z_J_lon_lat, NY_Z_K_lon_lat)),
        crs="EPSG:4326",
    )
    extra_metros["center"] = extra_metros["geometry"]

    metro_areas = pd.concat([metro_areas, extra_metros],
                            ignore_index=True,
                            sort=False)

    return metro_areas