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
a6a1b4d0
Commit
a6a1b4d0
authored
10 months ago
by
Eric Duminil
Browse files
Options
Download
Email Patches
Plain Diff
Adding cache, and precision
parent
ab9091b7
master
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
get_coordinates_by_zipcode.py
+17
-11
get_coordinates_by_zipcode.py
requirements.txt
+1
-0
requirements.txt
test_zipcode.py
+1
-0
test_zipcode.py
with
19 additions
and
11 deletions
+19
-11
get_coordinates_by_zipcode.py
+
17
-
11
View file @
a6a1b4d0
...
...
@@ -22,7 +22,6 @@ Also accepts multiple Zipcodes, or Zipcode prefix.
"""
# TODO: Write tests
# TODO: Use caching for shapes
# TODO: Rename functions
import
argparse
...
...
@@ -35,37 +34,49 @@ from shapely.ops import unary_union
INPUT_FOLDER
=
Path
(
'plz'
)
PLZ_FILENAME
=
'plz-5stellig.geojson'
PLZ_SHAPE_FILE
=
INPUT_FOLDER
/
PLZ_FILENAME
PRECISION
=
10
# [m]
ONE_DEGREE
=
40e6
/
360
# [m]
PLZ_SHAPES
=
None
def
_download_plz_shapes_if_needed
():
if
not
PLZ_SHAPE_FILE
.
exists
():
from
tqdm
import
tqdm
import
requests
print
(
"Downloading %s..."
%
PLZ_FILENAME
)
URL
=
"https://downloads.suche-postleitzahl.org/v2/public/"
+
PLZ_FILENAME
response
=
requests
.
get
(
URL
,
stream
=
True
)
INPUT_FOLDER
.
mkdir
(
exist_ok
=
True
)
with
open
(
PLZ_SHAPE_FILE
,
"wb"
)
as
handle
:
for
data
in
tqdm
(
response
.
iter_content
(
chunk_size
=
1024
),
unit
=
'kB'
):
handle
.
write
(
data
)
print
(
' Done'
)
def
_get_plz_shapes
():
print
(
"Parsing %s..."
%
PLZ_FILENAME
)
global
PLZ_SHAPES
if
PLZ_SHAPES
:
return
PLZ_SHAPES
_download_plz_shapes_if_needed
()
try
:
print
(
"Parsing %s..."
%
PLZ_FILENAME
)
with
open
(
PLZ_SHAPE_FILE
)
as
f
:
print
(
' Done'
)
return
json
.
load
(
f
)
PLZ_SHAPES
=
json
.
load
(
f
)
return
PLZ_SHAPES
except
json
.
decoder
.
JSONDecodeError
:
PLZ_SHAPE_FILE
.
unlink
()
raise
AttributeError
(
f
"
{
PLZ_FILENAME
}
seems to be damaged. Removing it. Please try again!"
)
def
_get_coordinates
(
data
,
plz_patterns
):
def
get_coordinates_by_zipcode
(
plz_patterns
,
precision
=
PRECISION
):
plz_shapes
=
_get_plz_shapes
()
geometries
=
[]
for
plz_pattern
in
plz_patterns
:
found
=
False
for
plz_geojson
in
data
[
'features'
]:
for
plz_geojson
in
plz_shapes
[
'features'
]:
if
re
.
match
(
plz_pattern
,
plz_geojson
[
'properties'
][
'plz'
]):
found
=
True
...
...
@@ -85,7 +96,7 @@ def _get_coordinates(data, plz_patterns):
raise
AttributeError
(
f
"Sorry, no information could be found for PLZ=
{
plz_pattern
}
"
)
merged
=
unary_union
(
geometries
)
wkt_polygon
=
merged
.
simplify
(
0.0001
).
wkt
wkt_polygon
=
merged
.
simplify
(
precision
/
ONE_DEGREE
).
wkt
print
(
wkt_polygon
)
try
:
import
pyperclip
...
...
@@ -99,11 +110,6 @@ def _get_coordinates(data, plz_patterns):
return
wkt_polygon
def
get_coordinates_by_zipcode
(
plzs
):
plz_shapes
=
_get_plz_shapes
()
return
_get_coordinates
(
plz_shapes
,
plzs
)
if
__name__
==
'__main__'
:
parser
=
argparse
.
ArgumentParser
(
description
=
'Get WKT geometry for desired PLZs'
)
parser
.
add_argument
(
'plzs'
,
metavar
=
'PLZ'
,
type
=
str
,
nargs
=
'+'
,
...
...
This diff is collapsed.
Click to expand it.
requirements.txt
+
1
-
0
View file @
a6a1b4d0
pyproj
requests
pyperclip
tqdm
This diff is collapsed.
Click to expand it.
test_zipcode.py
+
1
-
0
View file @
a6a1b4d0
...
...
@@ -7,6 +7,7 @@ class TestClass(unittest.TestCase):
def
test_method
(
self
):
self
.
assertEqual
(
'foo'
.
upper
(),
'FOO'
)
get_coordinates_by_zipcode
.
get_coordinates_by_zipcode
([
'70567'
])
get_coordinates_by_zipcode
.
get_coordinates_by_zipcode
([
'70567'
],
1_000
)
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