Skip to content
GitLab
Explore
Projects
Groups
Snippets
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Eric Duminil
Get Baden-Württemberg CityGML Opendata
Commits
47f08468
Commit
47f08468
authored
10 months ago
by
Eric Duminil
Browse files
Options
Download
Email Patches
Plain Diff
Addings types, and precision arg
parent
a6a1b4d0
master
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
get_coordinates_by_zipcode.py
+21
-15
get_coordinates_by_zipcode.py
test_zipcode.py
+4
-6
test_zipcode.py
with
25 additions
and
21 deletions
+25
-21
get_coordinates_by_zipcode.py
+
21
-
15
View file @
47f08468
...
@@ -5,20 +5,22 @@ e.g. for RegionChooser or download_files_from_LGL_BW.py.
...
@@ -5,20 +5,22 @@ e.g. for RegionChooser or download_files_from_LGL_BW.py.
Also accepts multiple Zipcodes, or Zipcode prefix.
Also accepts multiple Zipcodes, or Zipcode prefix.
python get_coordinates_by_zipcode.py --help
usage: get_coordinates_by_zipcode.py [-h] [-p PRECISION] PLZ [PLZ ...]
usage: get_coordinates_by_zipcode.py [-h] PLZ [PLZ ...]
Get WKT geometry for desired PLZs
Get WKT geometry for desired PLZs
positional arguments:
positional arguments:
PLZ desired PLZs
PLZ
desired PLZs
options:
options:
-h, --help show this help message and exit
-h, --help show this help message and exit
-p PRECISION, --precision PRECISION
precision of returned polygon [m]
> python get_coordinates_by_zipcode.py 70174
> python get_coordinates_by_zipcode.py 70174
> python get_coordinates_by_zipcode.py 70567 70569
> python get_coordinates_by_zipcode.py 70567 70569
> python get_coordinates_by_zipcode.py 70
> python get_coordinates_by_zipcode.py
-p1000
70
"""
"""
# TODO: Write tests
# TODO: Write tests
...
@@ -32,15 +34,16 @@ from shapely.geometry import shape
...
@@ -32,15 +34,16 @@ from shapely.geometry import shape
from
shapely.ops
import
unary_union
from
shapely.ops
import
unary_union
INPUT_FOLDER
=
Path
(
'plz'
)
INPUT_FOLDER
=
Path
(
'plz'
)
PLZ_FILENAME
=
'plz-5stellig.geojson'
PLZ_FILENAME
:
str
=
'plz-5stellig.geojson'
PLZ_SHAPE_FILE
=
INPUT_FOLDER
/
PLZ_FILENAME
PLZ_SHAPE_FILE
=
INPUT_FOLDER
/
PLZ_FILENAME
PRECISION
=
10
# [m]
PRECISION
:
float
=
10
# [m]
ONE_DEGREE
=
40e6
/
360
# [m]
ONE_DEGREE
:
float
=
40e6
/
360
# [m]
PLZ_SHAPES
=
None
PLZ_SHAPES
:
dict
CACHED
:
bool
=
False
def
_download_plz_shapes_if_needed
():
def
_download_plz_shapes_if_needed
()
->
None
:
if
not
PLZ_SHAPE_FILE
.
exists
():
if
not
PLZ_SHAPE_FILE
.
exists
():
from
tqdm
import
tqdm
from
tqdm
import
tqdm
import
requests
import
requests
...
@@ -54,9 +57,9 @@ def _download_plz_shapes_if_needed():
...
@@ -54,9 +57,9 @@ def _download_plz_shapes_if_needed():
print
(
' Done'
)
print
(
' Done'
)
def
_get_plz_shapes
():
def
_get_plz_shapes
()
->
dict
:
global
PLZ_SHAPES
global
PLZ_SHAPES
,
CACHED
if
PLZ_SHAPES
:
if
CACHED
:
return
PLZ_SHAPES
return
PLZ_SHAPES
_download_plz_shapes_if_needed
()
_download_plz_shapes_if_needed
()
...
@@ -65,13 +68,14 @@ def _get_plz_shapes():
...
@@ -65,13 +68,14 @@ def _get_plz_shapes():
with
open
(
PLZ_SHAPE_FILE
)
as
f
:
with
open
(
PLZ_SHAPE_FILE
)
as
f
:
print
(
' Done'
)
print
(
' Done'
)
PLZ_SHAPES
=
json
.
load
(
f
)
PLZ_SHAPES
=
json
.
load
(
f
)
CACHED
=
True
return
PLZ_SHAPES
return
PLZ_SHAPES
except
json
.
decoder
.
JSONDecodeError
:
except
json
.
decoder
.
JSONDecodeError
:
PLZ_SHAPE_FILE
.
unlink
()
PLZ_SHAPE_FILE
.
unlink
()
raise
AttributeError
(
f
"
{
PLZ_FILENAME
}
seems to be damaged. Removing it. Please try again!"
)
raise
AttributeError
(
f
"
{
PLZ_FILENAME
}
seems to be damaged. Removing it. Please try again!"
)
def
get_coordinates_by_zipcode
(
plz_patterns
,
precision
=
PRECISION
):
def
get_coordinates_by_zipcode
(
plz_patterns
:
list
[
str
]
,
precision
:
float
=
PRECISION
)
->
str
:
plz_shapes
=
_get_plz_shapes
()
plz_shapes
=
_get_plz_shapes
()
geometries
=
[]
geometries
=
[]
for
plz_pattern
in
plz_patterns
:
for
plz_pattern
in
plz_patterns
:
...
@@ -114,5 +118,7 @@ if __name__ == '__main__':
...
@@ -114,5 +118,7 @@ if __name__ == '__main__':
parser
=
argparse
.
ArgumentParser
(
description
=
'Get WKT geometry for desired PLZs'
)
parser
=
argparse
.
ArgumentParser
(
description
=
'Get WKT geometry for desired PLZs'
)
parser
.
add_argument
(
'plzs'
,
metavar
=
'PLZ'
,
type
=
str
,
nargs
=
'+'
,
parser
.
add_argument
(
'plzs'
,
metavar
=
'PLZ'
,
type
=
str
,
nargs
=
'+'
,
help
=
'desired PLZs'
)
help
=
'desired PLZs'
)
parser
.
add_argument
(
'-p'
,
'--precision'
,
default
=
PRECISION
,
type
=
int
,
help
=
'precision of returned polygon [m]'
)
args
=
parser
.
parse_args
()
args
=
parser
.
parse_args
()
get_coordinates_by_zipcode
(
args
.
plzs
)
get_coordinates_by_zipcode
(
args
.
plzs
,
args
.
precision
)
This diff is collapsed.
Click to expand it.
test_zipcode.py
+
4
-
6
View file @
47f08468
import
unittest
import
unittest
import
get_coordinates_by_zipcode
from
get_coordinates_by_zipcode
import
get_coordinates_by_zipcode
class
Test
Clas
s
(
unittest
.
TestCase
):
class
Test
GetCoordinate
s
(
unittest
.
TestCase
):
def
test_method
(
self
):
def
test_get_mohringen
(
self
):
self
.
assertEqual
(
'foo'
.
upper
(),
'FOO'
)
coords
=
get_coordinates_by_zipcode
([
'70567'
])
get_coordinates_by_zipcode
.
get_coordinates_by_zipcode
([
'70567'
])
get_coordinates_by_zipcode
.
get_coordinates_by_zipcode
([
'70567'
],
1_000
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Snippets