Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

split regions #230

Open
mathause opened this issue Jun 28, 2021 · 1 comment
Open

split regions #230

mathause opened this issue Jun 28, 2021 · 1 comment
Labels

Comments

@mathause
Copy link
Member

Assume I want to split a country/ region into two regions - how can I achieve this? E.g. define an eastern and western US.

@mathause
Copy link
Member Author

I don't think I will ever include something like this but let's add this here for reference:

import geopandas as gp
import pandas as pd
import regionmask
import shapely

countries = regionmask.defined_regions.natural_earth.countries_110

# convert regions to a gp.GeoDataFrame
data = dict(
        numbers=countries.numbers,
        abbrevs=countries.abbrevs,
        names=countries.names,
        geometry=countries.polygons,
    )
df = gp.GeoDataFrame.from_dict(data)


# create two polygons encompassing the eastern and western half of the US
lon = -98.24
p1 = shapely.geometry.Polygon([[-180, 0], [lon, 0], [lon, 90], [-180, 90]])
p2 = shapely.geometry.Polygon([[0, 0], [lon, 0], [lon, 90], [0, 90]])
df_polys = gp.GeoDataFrame(geometry=[p1, p2])

# select US
sel = df.abbrevs == "US"

# split in half
US_split = gp.overlay(df.loc[sel], df_polys, how='intersection')

# update the names & abbrevs
US_split.abbrevs = US_split.abbrevs.str.cat(["w", "e"])
US_split.names = US_split.names.str.cat(["west", "east"], sep=" ")
US_split.numbers =  [177, 178]

df_full = pd.concat([df.loc[~sel], US_split])

# create new regions
countries_us2 = regionmask.Regions(
        df_full["geometry"],
        numbers=df_full.numbers,
        names=df_full.names,
        abbrevs=df_full.abbrevs
        )

and a plot:

countries_us2.plot()

Figure_1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant