• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

DaveFoss / DAVE_data / 10884071777

16 Sep 2024 12:41PM UTC coverage: 92.844% (+6.4%) from 86.4%
10884071777

Pull #10

github

uvchik
Add get_layer function for all layers
Pull Request #10: Add basic functions

65 of 71 branches covered (91.55%)

Branch coverage included in aggregate %.

222 of 227 new or added lines in 11 files covered. (97.8%)

441 of 474 relevant lines covered (93.04%)

4.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.55
/src/dave_data/geometry/polygon.py
1
from geopandas import read_file
5✔
2

3
from dave_data.geometry.layers import get_federal_state_layer
5✔
4
from dave_data.geometry.layers import get_nuts_layer
5✔
5
from dave_data.geometry.layers import get_postcode_layer
5✔
6

7
dave_data_settings = {"crs_main": "EPSG:4326"}
5✔
8

9

10
def postalcode_to_polygon(postalcode):
5✔
11
    """
12
    Creating a polygon based on postalcodes
13

14
    Parameters
15
    ----------
16
    postalcode : int, str, list of str, list of int
17
          Postalcodes areas which define the polygon. Use ['ALL'] for all
18
          postalcode areas in germany
19

20
    Returns
21
    -------
22
    polygon : Shapely Polygon / MultiPolygon
23

24
    Examples
25
    --------
26
    >>> from dave_data.geometry.polygon import postalcode_to_polygon
27
    >>> from shapely.geometry import MultiPolygon, Polygon
28
    >>> polygon_postal = postalcode_to_polygon(postalcode=['34225', '34117'])
29
    >>> isinstance(polygon_postal, MultiPolygon)
30
    True
31
    >>> polygon_postal = postalcode_to_polygon(postalcode=[34225])
32
    >>> isinstance(polygon_postal, Polygon)
33
    True
34
    """
35
    # convert single values to list
36
    if isinstance(postalcode, (int, str)):
5✔
NEW
37
        postalcode = [str(postalcode).zfill(5)]
×
38

39
    # convert all values to str and check if string values are valid numbers
40
    postalcode = [str(int(p)).zfill(5) for p in postalcode]
5✔
41

42
    # get postcode map
43
    postal, meta_data = get_postcode_layer()
5✔
44

45
    # return one polygon containing all postcode polygons
46
    return postal.loc[postalcode].union_all()
5✔
47

48

49
def ags2polygon(ags):
5✔
50
    """
51
    Returns the polygon of the given region.
52

53
    Find the ags-code of any town in Germany:
54
    https://www.statistikportal.de/de/gemeindeverzeichnis
55

56
    Parameters
57
    ----------
58
    ags : str or int
59

60
    Returns
61
    -------
62
    shapely.polygon : Outline of the given region
63

64
    """
NEW
65
    raise NotImplementedError("ags2polygon is not implemented so far.")
×
66

67

68
def town_to_polygon(town):
5✔
69
    """
70
    Create a polygon based on town names
71

72
    Parameters
73
    ----------
74
    town : str
75
        Town areas which define the polygon. Use ['ALL'] for all town areas
76
        in germany
77

78
    Returns
79
    -------
80
    polygon : Shapely Polygon / MultiPolygon
81

82
    Examples
83
    --------
84
    >>> from shapely.geometry import MultiPolygon
85
    >>> from dave_data.geometry.polygon import town_to_polygon
86
    >>> polygon_town = town_to_polygon(town='Kassel')
87
    >>> isinstance(polygon_town, MultiPolygon)
88
    True
89
    """
90
    postal, meta_data = get_postcode_layer()
5✔
91

92
    town = postal.loc[postal["note"].str.lower().str.find(town.lower()) >= 0]
5✔
93

94
    return town.union_all()
5✔
95

96

97
def federal_state_to_polygon(federal_state):
5✔
98
    """
99
    Create a polygon based on federal states
100

101
    Parameters
102
    ----------
103
    federal_state : str
104
        Federal state areas which define the polygon. Use ['ALL'] for all
105
        Federal state areas in germany
106

107
    Returns
108
    -------
109
    polygon : Shapely Polygon / MultiPolygon
110

111
    Examples
112
    --------
113
    >>> from dave_data.geometry.polygon import federal_state_to_polygon
114
    >>> from shapely.geometry import MultiPolygon, Polygon
115
    >>> polygon_fed = federal_state_to_polygon(federal_state='Hessen')
116
    >>> isinstance(polygon_fed, (MultiPolygon, Polygon))
117
    True
118
    """
119
    states, meta_data = get_federal_state_layer()
5✔
120

121
    state = states.loc[
5✔
122
        states["name"].str.lower().str.find(federal_state.lower()) >= 0
123
    ]
124
    return state.union_all()
5✔
125

126

127
def nuts_to_polygon(nuts, year=2016):
5✔
128
    """
129
    Create a polygon based on nuts regions
130

131
    Parameters
132
    ----------
133
    nuts : List(Str)
134
          Nuts areas which define the polygon. Diffrent nuts levels can
135
          be combined. Use ['ALL'] for all Nuts 3 areas in germany
136

137
    year : scalar(INT), optional(default=2016)
138
          The year which forms the basis of the data set
139

140
    Returns
141
    -------
142
    polygon : Shapely Polygon / MultiPolygon
143

144
    Examples
145
    --------
146
    >>> from dave_data.geometry.polygon import nuts_to_polygon
147
    >>> from shapely.geometry import Polygon
148
    >>> polygon_nuts = nuts_to_polygon(nuts="DE11B", year=2013)
149
    >>> isinstance(polygon_nuts, Polygon)
150
    True
151
    """
152
    # read nuts-3 areas
153
    nuts_all, meta_data = get_nuts_layer(year=year)
5✔
154
    nuts_3 = nuts_all[nuts_all.LEVL_CODE == 3]
5✔
155

156
    nuts_region = nuts_3.loc[nuts_3["NUTS_ID"].str.lower() == nuts.lower()]
5✔
157

158
    return nuts_region.union_all()
5✔
159

160

161
def file_to_polygon(filepath, layer=None):
5✔
162
    """
163
    Create a polygon based on a file including geographical data
164

165
    Parameters
166
    ----------
167
    filepath : scalar(Str)
168
        Absolut Path to a file including geographical data. Possible datatypes
169
        are .shp, .gpkg and .geojson
170

171
    layer : scalar(Str), optional(default=None)
172
          The layer name of the data to be considered. Necessary for datatype
173
          .gpkg
174

175
    Returns
176
    -------
177
    polygon : Shapely Polygon / MultiPolygon
178

179
    """
180
    file_data = read_file(filepath, layer=layer)
5✔
181
    return file_data.union_all()
5✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc