-
Notifications
You must be signed in to change notification settings - Fork 28
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
feat: add script to translate urban and rural shapefiles to USA tamu loadzones #319
Open
merrielle
wants to merge
10
commits into
develop
Choose a base branch
from
merrielle-ur-to-tamu
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4063456
add missing init files and set urban index to county name
372ae06
Add area name to urban-rural index
8307711
feat: add script to translate urban and rural shapefiles to USA tamu …
38fe4ad
cleanup, start docs
abb10db
docs: add docstrings
cc066c2
test: add tests for urban-rural translation
e8ff704
isort
85e80a4
flake8
d25cd53
fix old urban-rural test
7723e22
remove unused import
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import os | ||
|
||
import geopandas as gpd | ||
import pandas as pd | ||
|
||
from prereise.gather.const import abv2state | ||
from prereise.gather.const import lower_48_states_abv | ||
from prereise.utility.shapefile import download_shapefiles | ||
|
||
|
||
|
@@ -16,22 +17,22 @@ def append_rural_areas_to_urban(states, urban_areas): | |
:return: (*geopandas.geodataframe.GeoDataFrame*) -- new gdf of urban and rural areas | ||
includes a column indicating whether the area is urban or not | ||
""" | ||
lower_48_states_abv = list(abv2state.keys()) | ||
lower_48_states_abv.remove("AK") | ||
lower_48_states_abv.remove("HI") | ||
|
||
states = states.rename(columns={"STUSPS": "state"}) | ||
states = states.loc[states["state"].isin(lower_48_states_abv)] | ||
|
||
urban_areas["state"] = urban_areas["NAME10"].str[-2:] | ||
# this drops 66 out of 3601 rows | ||
urban_areas = urban_areas.loc[urban_areas["state"].isin(lower_48_states_abv)] | ||
|
||
urban_areas = urban_areas.rename(columns={"NAME10": "name"}) | ||
urban_areas = urban_areas.set_index("name", verify_integrity=True) | ||
urban_areas["urban"] = True | ||
|
||
rural_areas = states.overlay(urban_areas, how="difference") | ||
rural_areas.index = rural_areas["state"] + "_rural" | ||
rural_areas["urban"] = False | ||
|
||
return urban_areas.append(rural_areas, ignore_index=True)[ | ||
return pd.concat([urban_areas, rural_areas], verify_integrity=True)[ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hadn't seen the |
||
["state", "geometry", "urban"] | ||
] | ||
|
||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import geopandas as gpd | ||
import pandas as pd | ||
from powersimdata.input.grid import Grid | ||
from shapely.geometry import Point, Polygon | ||
|
||
from prereise.utility.translate_urban_rural_to_usa_tamu import ( | ||
create_substation_gdf, | ||
create_usa_tamu_convex_hull_shapefile, | ||
fix_lz_border, | ||
format_states_gdf, | ||
) | ||
|
||
WA_lz_polygon = Polygon([(1, 1), (0, 5), (5, 6), (4, 1)]) | ||
CA_lz_polygon = Polygon([(0, 10), (0, 15), (5, 15), (5, 10)]) | ||
WA_border_polygon = Polygon([(0, 0), (0, 5), (5, 5), (5, 0)]) | ||
CA_border_polygon = Polygon([(0, 10), (0, 20), (3, 20), (3, 10)]) | ||
HI_border_polygon = Polygon([(100, 100), (100, 105), (105, 105)]) | ||
|
||
# Mock US State borders | ||
states = gpd.GeoDataFrame( | ||
{ | ||
"geometry": [WA_border_polygon, CA_border_polygon, HI_border_polygon], | ||
"STUSPS": ["WA", "CA", "HI"], | ||
} | ||
) | ||
formatted_states = gpd.GeoDataFrame( | ||
{ | ||
"geometry": {"WA": WA_border_polygon, "CA": CA_border_polygon}, | ||
"state": {"WA": "WA", "CA": "CA"}, | ||
} | ||
) | ||
sub_gdf = gpd.GeoDataFrame( | ||
{ | ||
"zone": {122: "Bay Area", 6134: "Washington"}, | ||
"geometry": {122: Point(2, 3), 6134: Point(14, 4)}, | ||
} | ||
) | ||
|
||
|
||
def test_format_states_gdf(): | ||
results = format_states_gdf(states) | ||
assert formatted_states.to_dict() == results.to_dict() | ||
|
||
|
||
class MockGrid: | ||
def __init__(self): | ||
"""Constructor""" | ||
self.bus2sub = pd.DataFrame({"sub_id": {0: 122, 1: 122, 2: 6134}}) | ||
self.bus = pd.DataFrame({"zone_id": {0: 204, 1: 204, 2: 201}}) | ||
self.id2zone = {204: "Bay Area", 201: "Washington"} | ||
self.sub = pd.DataFrame({"lon": {122: 2, 6134: 14}, "lat": {122: 3, 6134: 4}}) | ||
|
||
@property | ||
def __class__(self): | ||
"""If anyone asks, I'm a Grid object!""" | ||
return Grid | ||
|
||
|
||
def test_create_substation_gdf(): | ||
mock_grid = MockGrid() | ||
results = create_substation_gdf(mock_grid) | ||
assert sub_gdf.to_dict() == results.to_dict() | ||
|
||
|
||
def test_fix_lz_border_single_lz(): | ||
results = fix_lz_border(formatted_states, "WA", WA_lz_polygon) | ||
expected = WA_border_polygon | ||
assert expected == results | ||
|
||
|
||
def test_fix_lz_border_multi_lz(): | ||
results = fix_lz_border(formatted_states, "CA", CA_lz_polygon) | ||
expected = Polygon([(0, 15), (3, 15), (3, 10), (0, 10)]) | ||
assert expected == results | ||
|
||
|
||
def test_create_usa_tamu_convex_hull_shapefile(): | ||
# gdf where CA substations match CA_lz_polygon | ||
sub_gdf_2 = gpd.GeoDataFrame( | ||
{ | ||
"zone": { | ||
122: "Bay Area", | ||
123: "Bay Area", | ||
124: "Bay Area", | ||
125: "Bay Area", | ||
6134: "Washington", | ||
}, | ||
"geometry": { | ||
122: Point(0, 10), | ||
123: Point(0, 15), | ||
124: Point(5, 15), | ||
125: Point(5, 10), | ||
6134: Point(14, 4), | ||
}, | ||
} | ||
) | ||
results = create_usa_tamu_convex_hull_shapefile(sub_gdf_2, formatted_states) | ||
expected = { | ||
"geometry": { | ||
"Bay Area": Polygon([(0, 15), (3, 15), (3, 10), (0, 10)]), | ||
"Washington": WA_border_polygon, | ||
}, | ||
"state": {"Bay Area": "CA", "Washington": "WA"}, | ||
} | ||
assert expected == results.to_dict() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could reuse the same lookup from powersimdata, e.g.