Skip to content

Commit 4fcdd30

Browse files
authored
Merge pull request #8 from ni1o1/0.3.3
update 0.3.3
2 parents eaf0936 + 6b445f5 commit 4fcdd30

File tree

7 files changed

+112
-22
lines changed

7 files changed

+112
-22
lines changed

docs/source/analysis.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ precision : number
2828
padding : number
2929
padding time before and after sunrise and sunset
3030
accuracy : number
31-
size of grids.
31+
size of grids. Produce vector polygons if set as `vector`
3232

3333
**Return**
3434

docs/source/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
author = 'Qing Yu'
2323

2424
# The full version, including alpha/beta/rc tags
25-
release = '0.3.2'
26-
version = '0.3.2'
25+
release = '0.3.3'
26+
version = '0.3.3'
2727
html_logo = "_static/logo-wordmark-light.png"
2828
html_favicon = '_static/logo.ico'
2929
# -- General configuration ---------------------------------------------------

example/Example1-building_shadow_analysis.ipynb

Lines changed: 58 additions & 8 deletions
Large diffs are not rendered by default.

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="pybdshadow",
8-
version="0.3.2",
8+
version="0.3.3",
99
author="Qing Yu",
1010
author_email="[email protected]",
1111
description="Python package to generate building shadow geometry",

src/pybdshadow/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3333
"""
3434

35-
__version__ = '0.3.2'
35+
__version__ = '0.3.3'
3636
__author__ = 'Qing Yu <[email protected]>'
3737

3838
# module level doc-string

src/pybdshadow/analysis.py

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ def get_timeSeries(day, lon, lat, precision=3600, padding=1800):
2828
dates['date'] = dates['datetime'].apply(lambda r: str(r)[:19])
2929
return dates
3030

31-
3231
def cal_sunshine(buildings, day='2022-01-01', roof=False,grids = gpd.GeoDataFrame(), accuracy=1, precision=3600, padding=0):
3332
'''
3433
Calculate the sunshine time in given date.
@@ -48,7 +47,7 @@ def cal_sunshine(buildings, day='2022-01-01', roof=False,grids = gpd.GeoDataFra
4847
padding : number
4948
padding time before and after sunrise and sunset
5049
accuracy : number
51-
size of grids.
50+
size of grids. Produce vector polygons if set as `vector`
5251
5352
**Return**
5453
@@ -69,12 +68,35 @@ def cal_sunshine(buildings, day='2022-01-01', roof=False,grids = gpd.GeoDataFra
6968

7069
# Generate shadow every 1800 s
7170
shadows = cal_sunshadows(buildings, dates=[day], precision=precision, padding=padding)
72-
# Grid analysis of shadow cover duration(ground).
73-
grids = cal_shadowcoverage(
74-
shadows, buildings, grids = grids,roof=roof, precision=precision, accuracy=accuracy)
71+
if accuracy == 'vector':
72+
if roof:
73+
shadows = shadows[shadows['type'] == 'roof']
74+
shadows = bd_preprocess(shadows)
75+
shadows = shadows.groupby(['date','type'])['geometry'].apply(
76+
lambda df: MultiPolygon(list(df)).buffer(0)).reset_index()
77+
shadows = bd_preprocess(shadows)
78+
shadows = count_overlapping_features(shadows)
79+
else:
80+
shadows = shadows[shadows['type'] == 'ground']
81+
shadows = bd_preprocess(shadows)
82+
shadows = shadows.groupby(['date','type'])['geometry'].apply(
83+
lambda df: MultiPolygon(list(df)).buffer(0)).reset_index()
84+
shadows = bd_preprocess(shadows)
85+
shadows = count_overlapping_features(shadows)
86+
87+
shadows['time'] = shadows['count']*precision
88+
shadows['Hour'] = sunlighthour-shadows['time']/3600
89+
shadows.loc[shadows['Hour']<=0,'Hour']=0
90+
return shadows
91+
else:
92+
# Grid analysis of shadow cover duration(ground).
93+
grids = cal_shadowcoverage(
94+
shadows, buildings, grids = grids,roof=roof, precision=precision, accuracy=accuracy)
7595

76-
grids['Hour'] = sunlighthour-grids['time']/3600
77-
return grids
96+
grids['Hour'] = sunlighthour-grids['time']/3600
97+
return grids
98+
99+
78100

79101

80102
def cal_sunshadows(buildings, cityname='somecity', dates=['2022-01-01'], precision=3600, padding=0,
@@ -195,4 +217,17 @@ def cal_shadowcoverage(shadows_input, buildings, grids = gpd.GeoDataFrame(),roof
195217
grids = pd.merge(grids, gridcount, how='left')
196218
grids['time'] = grids['count'].fillna(0)*precision
197219

198-
return grids
220+
return grids
221+
222+
def count_overlapping_features(gdf):
223+
import shapely
224+
bounds = gdf.geometry.exterior.unary_union
225+
new_polys = list(shapely.ops.polygonize(bounds))
226+
new_gdf = gpd.GeoDataFrame(geometry=new_polys)
227+
new_gdf['id'] = range(len(new_gdf))
228+
new_gdf_centroid = new_gdf.copy()
229+
new_gdf_centroid['geometry'] = new_gdf.centroid
230+
overlapcount = gpd.sjoin(new_gdf_centroid,gdf)
231+
overlapcount = overlapcount.groupby(['id'])['index_right'].count().rename('count').reset_index()
232+
out_gdf = pd.merge(new_gdf,overlapcount)
233+
return out_gdf

src/pybdshadow/tests/test_analysis.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,9 @@ def test_analysis(self):
3434
assert len(bdgrids)==1185
3535

3636
grids = pybdshadow.cal_sunshine(buildings)
37-
assert len(grids)==1882
37+
assert len(grids)==1882
38+
39+
sunshine = pybdshadow.cal_sunshine(buildings,accuracy='vector')
40+
assert len(sunshine)==117
41+
sunshine = pybdshadow.cal_sunshine(buildings,accuracy='vector',roof = True)
42+
assert len(sunshine)==9

0 commit comments

Comments
 (0)