Skip to content

Commit

Permalink
Dev pyheatdemand (#21)
Browse files Browse the repository at this point in the history
* ChangeNames

* EditFiles

* EditReadme

* EditFiles

* EditFiles

* EditFiles

* EditFiles
  • Loading branch information
AlexanderJuestel authored Nov 5, 2023
1 parent c186aa8 commit a6ff10e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 deletions.
18 changes: 10 additions & 8 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ Here is how to get started.
### Questions

For questions about PyHeatDemand (e.g. its applications, functionality, and usage),
please [search the existing issues for related questions](https://github.com/AlexanderJuestel/pyhheatdemand/issues).
If your question has not already been asked, then [make a new issue](https://github.com/AlexanderJuestel/pyhheatdemand/issues/new/choose).

please [search the existing issues for related questions](https://github.com/AlexanderJuestel/pyheatdemand/issues).
If your question has not already been asked, then [make a new issue](https://github.com/AlexanderJuestel/pyheatdemand/issues/new/choose).


### Reporting Bugs

Please report bugs on the [issue page using the bug report template](https://github.com/AlexanderJuestel/pyhheatdemand/issues/new?assignees=&labels=&template=bug_report.md&title=) and label the issue as a bug.
Please report bugs on the [issue page using the bug report template](https://github.com/AlexanderJuestel/pyheatdemand/issues/new?assignees=&labels=&template=bug_report.md&title=) and label the issue as a bug.
The template asks essential questions for you to answer so that we can to understand, reproduce, and fix the bug.
Be verbose!
Whenever possible, provide tracebacks and/or error messages, screenshots, and sample code or other files.
Expand All @@ -23,7 +25,7 @@ Whenever possible, provide tracebacks and/or error messages, screenshots, and sa

We encourage users to submit ideas for improvements to the PyHeatDemand project. For
this please create an issue on the
[issue page](https://github.com/AlexanderJuestel/pyhheatdemand/issues) with the *Feature
[issue page](https://github.com/AlexanderJuestel/pyheatdemand/issues) with the *Feature
Request* template and label. Please make sure to use a descriptive title and to
provide ample background information to help us implement that functionality
in the future.
Expand All @@ -39,7 +41,7 @@ or anything that you think should be in the repository.
Contributions should address an open issue (either a bug or a feature request).
If you have found a new bug
or have an idea or a new feature,
then please [open the issue](https://github.com/AlexanderJuestel/pyhheatdemand/issues/new/choose)
then please [open the issue](https://github.com/AlexanderJuestel/pyheatdemand/issues/new/choose)
for discussion and link to that issue in your pull request.

### Python code guidelines
Expand Down Expand Up @@ -78,7 +80,7 @@ For all comments, follow [PEP 8](https://www.python.org/dev/peps/pep-0008/).
### Licensing

All contributed code will be licensed under
[a LGPL-3 license](https://github.com/AlexanderJuestel/pyhheatdemand/blob/main/LICENSE).
[a LGPL-3 license](https://github.com/AlexanderJuestel/pyheatdemand/blob/main/LICENSE).
If you did not write the code yourself,
it is your responsibility to ensure that the existing license is compatible
and included in the contributed files.
Expand All @@ -91,7 +93,7 @@ You should be familiar with `pytest` before contributing.
Please run all tests locally before creating a pull request.
You can do this by running `pytest` via your terminal in your PyHeatDemand folder:
```bash
cd ./path/to/pyhheatdemand
cd ./path/to/pyheatdemand
pytest
```
All tests are located in the `test` folder and its subfolders.
Expand All @@ -100,4 +102,4 @@ All contributed code must include test code in the pull request.
### Pull Requests

All contributions are made via pull requests (PR's) to the main branch.
You must complete the checklist in the [PR template](https://github.com/AlexanderJuestel/pyhheatdemand/blob/main/PULL_REQUEST_TEMPLATE.md) before we will review the PR and consider merging your contribution.
You must complete the checklist in the [PR template](https://github.com/AlexanderJuestel/pyheatdemand/blob/main/PULL_REQUEST_TEMPLATE.md) before we will review the PR and consider merging your contribution.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ A CI is present to test the current code. It can be initiated using `pytest --co
running the tests, `coverage report -m` can be executed to get an report on the coverage and which lines are not covered
by the tests.

## API Reference
For creating the API reference, navigate to the `docs` folder and execute `sphinx-apidoc -o source/ ../pyheatdemand`.

<a name="ref"></a>
## References

Expand Down
2 changes: 1 addition & 1 deletion docs/source/about.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Resources
* `DGE Rollout Webviewer <https://data.geus.dk/egdi/?mapname=dgerolloutwebtool#baslay=baseMapGEUS&extent=39620,-1581250,8465360,8046630&layers=dge_heat_final>`_

Citing PyHeatDemand
-----------
-------------------
If you are using **PyHeatDemand** for your scientific research, please remember to cite our work.

.. code::
Expand Down
16 changes: 14 additions & 2 deletions pyheatdemand/processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,15 @@ def create_outline(gdf: gpd.GeoDataFrame) -> gpd.GeoDataFrame:


def calculate_hd(hd_gdf: gpd.GeoDataFrame,
mask_gdf: gpd.GeoDataFrame,
mask_gdf: Union[gpd.GeoDataFrame, Polygon],
hd_data_column: str = ''):
"""Calculate Heat Demand.
Parameters
__________
hd_gdf : gpd.GeoDataFrame
Heat demand data as GeoDataFrame.
mask_gdf : gpd.GeoDataFrame
mask_gdf : Union[gpd.GeoDataFrame, shapely.geometry.Polygon]
Mask for the output Heat Demand Data.
hd_data_column : str
Name of the column that contains the Heat Demand Data, e.g. ``hd_data_column='HD'``.
Expand Down Expand Up @@ -224,6 +224,11 @@ def calculate_hd(hd_gdf: gpd.GeoDataFrame,
"""

# Converting Shapely Polygon to GeoDataFrame
if isinstance(mask_gdf, Polygon):
mask_gdf = gpd.GeoDataFrame(geometry=[mask_gdf],
crs=hd_gdf.crs)

# Checking that the hd_gdf is of type GeoDataFrame
if not isinstance(hd_gdf, gpd.GeoDataFrame):
raise TypeError('The heat demand gdf must be provided as GeoDataFrame')
Expand All @@ -244,34 +249,41 @@ def calculate_hd(hd_gdf: gpd.GeoDataFrame,
if mask_gdf.crs != hd_gdf.crs:
hd_gdf = hd_gdf.to_crs(mask_gdf.crs)

# Exploding MultiPolygons
if any(shapely.get_type_id(hd_gdf.geometry) == 6):
hd_gdf = hd_gdf.explode(index_parts=True).reset_index(drop=True)

# Assigning area to Polygons
if all(shapely.get_type_id(hd_gdf.geometry) == 3):
# Assigning area of original geometries to GeoDataFrame
hd_gdf['area'] = hd_gdf.area

# Assigning lengths to LineStrings
elif all(shapely.get_type_id(hd_gdf.geometry) == 1):
# Assigning length of original geometries to GeoDataFrame
hd_gdf['length'] = hd_gdf.length

# Overlaying Heat Demand Data with Mask
overlay = gpd.overlay(df1=hd_gdf,
df2=mask_gdf)

# Calculate HD for Polygons
if all(shapely.get_type_id(hd_gdf.geometry) == 3):
# Assigning area of splitted geometries to GeoDataFrame
overlay['area_new'] = overlay.area

# Calculating the share of the original Heat Demand for each splitted geometry
overlay['HD'] = overlay[hd_data_column] * overlay['area_new'] / overlay['area']

# Calculate HD for LineStrings
elif all(shapely.get_type_id(hd_gdf.geometry) == 1):
# Assigning length of splitted geometries to GeoDataFrame
overlay['length_new'] = overlay.length

# Calculating the share of the original Heat Demand for each splitted geometry
overlay['HD'] = overlay[hd_data_column] * overlay['length_new'] / overlay['length']

# Calculate HD for Points
elif all(shapely.get_type_id(hd_gdf.geometry) == 0):
overlay['HD'] = overlay[hd_data_column]

Expand Down
7 changes: 7 additions & 0 deletions test/test_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ def test_calculate_hd(mask_gdf, hd_gdf):
assert isinstance(gdf_hd, gpd.GeoDataFrame)
assert gdf_hd.crs == hd_gdf.crs

gdf_hd = calculate_hd(hd_gdf=hd_gdf,
mask_gdf=mask_gdf.iloc[0].geometry,
hd_data_column='WOHNGEB_WB')

assert isinstance(gdf_hd, gpd.GeoDataFrame)
assert gdf_hd.crs == hd_gdf.crs


@pytest.mark.parametrize('mask_gdf',
[gpd.read_file('data/Interreg_NWE_mask_500m_EPSG3034.shp')])
Expand Down

0 comments on commit a6ff10e

Please sign in to comment.