Exemple #1
0
def mergeCountry(old: gp.geodataframe.GeoDataFrame, new: gp.geodataframe.GeoDataFrame, mini: str, big: str) -> gp.GeoDataFrame:
    bigName = big
    mini = old.loc[old["NAME"] == mini]["geometry"].tolist()[0]
    big = old.loc[old["NAME"] == big]["geometry"].tolist()[0]

    try:
        final = MultiPolygon([*list(mini), *list(big)])
    except:
        try:
            final = MultiPolygon([mini, *list(big)])
        except:
            try:
                final = MultiPolygon([*list(mini), big])
            except:
                try:
                    final = MultiPolygon([mini, big])
                except:
                    print("wtf???")

    #new.loc[new["name"] == "China", "geometry"] = final
    new = new.drop(new.loc[new["name"] == bigName].index.tolist()[0])

    new = new.append({"name": old.loc[old["NAME"] == bigName]["NAME"].tolist()[0],
                      "ISO2": old.loc[old["NAME"] == bigName]["ISO2"].tolist()[0],
                      "ISO3": old.loc[old["NAME"] == bigName]["ISO3"].tolist()[0],
                      "area": old.loc[old["NAME"] == bigName]["AREA"].tolist()[0],
                      "pop": old.loc[old["NAME"] == bigName]["POP2005"].tolist()[0],
                      "neighbors": old.loc[old["NAME"] == bigName]["NEIGHBORS"].tolist()[0],
                      "geometry": final}, ignore_index=True)


    return new
Exemple #2
0
def clip_by_extent(gdf: gpd.geodataframe.GeoDataFrame,
                   bbox: List[Union[int, float]],
                   inplace: bool = False) -> gpd.geodataframe.GeoDataFrame:
    """
    Clipping vector data by extent
    Args:
        gdf: GeoDataFrame to be clipped
        bbox: list of bounds for the gdf to be clipped
        inplace: - bool - default False -> copy of the current gdf is created
    Return:
        gdf: GeoDataFrame with the clipped values
    """

    # Checking if the gdf is of type GeoDataFrame
    if not isinstance(gdf, gpd.geodataframe.GeoDataFrame):
        raise TypeError('gdf must be of type GeoDataFrame')

    # Checking that the bbox is of type list
    if not isinstance(bbox, list):
        raise TypeError('Extent must be of type list')

    # Checking that all values are either ints or floats
    if not all(isinstance(n, (int, float)) for n in bbox):
        raise TypeError('Bounds values must be of type int or float')

    # Checking if inplace is of type bool
    if not isinstance(inplace, bool):
        raise TypeError('Inplace must be of type bool')

    # Creating the bounds from the bbox
    if len(bbox) == 6:
        minx, maxx, miny, maxy = bbox[0:4]
    else:
        minx, maxx, miny, maxy = bbox

    # Create deep copy of gdf
    if not inplace:
        gdf = gdf.copy(deep=True)

    # Adding XY values to gdf if they are not present yet
    if np.logical_not(pd.Series(['X', 'Y']).isin(gdf.columns).all()):
        gdf = extract_xy(gdf)

    # Clipping the GeoDataFrame
    gdf = gdf[(gdf.X >= minx) & (gdf.X <= maxx) & (gdf.Y >= miny) &
              (gdf.Y <= maxy)]

    # Drop geometry column
    gdf = gdf.drop('geometry', axis=1)

    # Create new geometry column
    gdf = gpd.GeoDataFrame(gdf,
                           geometry=gpd.points_from_xy(gdf.X, gdf.Y),
                           crs='EPSG:' + str(gdf.crs.to_epsg()))

    # Drop Duplicates
    gdf = gdf.drop_duplicates()

    return gdf