Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Dynamic Exposure
OpenBuildingMap
rabotnik-obm
Commits
af39dccf
Commit
af39dccf
authored
Aug 10, 2021
by
Marius Kriegerowski
Browse files
Refactored taxonomies
parent
d9443876
Changes
3
Hide whitespace changes
Inline
Side-by-side
rabotnikobm/
taxonomy/gem_taxonomy
.py
→
rabotnikobm/
occupancy/mapping
.py
View file @
af39dccf
...
...
@@ -20,46 +20,46 @@ import csv
from
collections
import
defaultdict
def
group_ta
xonomy
(
taxonomy
:
list
[
dict
[
str
,
str
]])
->
dict
[
str
,
dict
[
str
,
str
]]:
"""Extract groups of identical
key
s and group them in nested dicts."""
grouped_ta
xonomy
=
defaultdict
(
dict
)
def
group_ta
gs
(
occupancy_mapping
:
list
[
dict
[
str
,
str
]])
->
dict
[
str
,
dict
[
str
,
str
]]:
"""Extract groups of identical
tag
s and group them in nested dicts."""
grouped_ta
g_mapping
=
defaultdict
(
dict
)
for
row
in
taxonomy
:
for
row
in
occupancy_mapping
:
key
=
row
.
pop
(
"key"
)
value
=
row
.
pop
(
"value"
)
grouped_ta
xonomy
[
key
].
update
({
value
:
row
})
grouped_ta
g_mapping
[
key
].
update
({
value
:
row
})
return
grouped_ta
xonomy
return
grouped_ta
g_mapping
def
read_csv
(
fn
:
str
)
->
list
[
dict
[
str
,
str
]]:
"""Read content from csv into list of dicts."""
with
open
(
fn
)
as
csvfile
:
taxonomy_reader
=
list
(
csv
.
DictReader
(
csvfile
))
mapping
=
list
(
csv
.
DictReader
(
csvfile
))
return
taxonomy_reader
return
mapping
class
Taxonomy
OccupancyMapper
:
class
OccupancyMapper
:
"""Map building tags to `GEM_taxonomy_occupancy`"""
def
__init__
(
self
,
mapping
):
self
.
mapping
=
mapping
def
tags_to_
taxonomy
(
self
,
building
_tags
:
dict
[
dict
[
str
,
str
]])
->
list
[
str
]:
"""Map `
building
_tags` to lists of GEM_taxonomy_occupancy as defined in
def
tags_to_
occupancy
(
self
,
osm
_tags
:
dict
[
dict
[
str
,
str
]])
->
list
[
str
]:
"""Map `
osm
_tags` to lists of GEM_taxonomy_occupancy as defined in
building_and_POIs_tags.csv and landuse_tags.csv. Tags may be duplicated.
Args:
building
_tags: list of building tag strings
osm
_tags: list of
OSM
building tag strings
associated with a specific building
Returns:
list of `GEM_taxonomy_occupancy`s
"""
occupancies
=
[]
for
building_tag
in
building
_tags
:
for
key
,
value
in
building
_tag
.
items
():
for
osm_tag
in
osm
_tags
:
for
key
,
value
in
osm
_tag
.
items
():
occupancy
=
self
.
mapping
.
get
(
key
,
{}).
get
(
value
,
None
)
if
occupancy
is
not
None
:
occupancies
.
append
(
occupancy
[
"GEM_taxonomy_occupancy"
])
...
...
@@ -68,8 +68,8 @@ class TaxonomyOccupancyMapper:
@
classmethod
def
from_csv
(
cls
,
fn
:
str
):
"""Read a csv and initialize a `
Taxonomy
OccupancyMapper`."""
taxonomy
=
read_csv
(
fn
)
grouped_taxonomy
=
group_taxonomy
(
taxonomy
)
"""Read a csv and initialize a `OccupancyMapper`."""
occupancy_mapping
=
read_csv
(
fn
)
occupancy_mapping_grouped
=
group_tags
(
occupancy_mapping
)
return
cls
(
mapping
=
grouped_taxonomy
)
return
cls
(
mapping
=
occupancy_mapping_grouped
)
setup.py
View file @
af39dccf
...
...
@@ -18,7 +18,7 @@
from
setuptools
import
setup
,
find_packages
tests_require
=
[
"pytest"
]
tests_require
=
[
"pytest"
,
"pytest-asyncio"
]
linters_require
=
[
"pylint"
,
"pre-commit"
]
setup
(
...
...
tests/test_
gem_taxonomy
.py
→
tests/test_
occupancy_mapping
.py
View file @
af39dccf
...
...
@@ -16,39 +16,39 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
from
rabotnikobm.
taxonomy.gem_taxonomy
import
group_taxonomy
,
Taxonomy
OccupancyMapper
from
rabotnikobm.
occupancy.mapping
import
group_tags
,
OccupancyMapper
def
test_group_ta
xonomy
():
def
test_group_ta
gs
():
rows
=
[
{
"key"
:
"a"
,
"value"
:
0
},
{
"key"
:
"a"
,
"value"
:
1
},
{
"key"
:
"b"
,
"value"
:
2
},
]
grouped
=
group_ta
xonomy
(
rows
)
grouped
=
group_ta
gs
(
rows
)
assert
grouped
==
{
"a"
:
{
0
:
{},
1
:
{}},
"b"
:
{
2
:
{}}}
def
test_mapper_from_csv
(
pytestconfig
):
demo_file
=
pytestconfig
.
rootpath
/
"data/rules/occupancy/building_and_PoIs_tags.csv"
mapper
=
Taxonomy
OccupancyMapper
.
from_csv
(
demo_file
)
mapper
=
OccupancyMapper
.
from_csv
(
demo_file
)
assert
mapper
def
test_
taxonom
y_mapper_building_pois
(
pytestconfig
):
def
test_
occupanc
y_mapper_building_pois
(
pytestconfig
):
demo_file
=
pytestconfig
.
rootpath
/
"data/rules/occupancy/building_and_PoIs_tags.csv"
mapper
=
Taxonomy
OccupancyMapper
.
from_csv
(
demo_file
)
mapper
=
OccupancyMapper
.
from_csv
(
demo_file
)
sample_tags
=
[{
"amenity"
:
"community_centre"
},
{
"amenity"
:
"cafe"
},
{
"x"
:
"y"
}]
assert
mapper
.
tags_to_
taxonom
y
(
sample_tags
)
==
[
"ASS4"
,
"COM5"
]
assert
mapper
.
tags_to_
occupanc
y
(
sample_tags
)
==
[
"ASS4"
,
"COM5"
]
def
test_
taxonom
y_mapper_landuse
(
pytestconfig
):
def
test_
occupanc
y_mapper_landuse
(
pytestconfig
):
demo_file
=
pytestconfig
.
rootpath
/
"data/rules/occupancy/landuse_tags.csv"
mapper
=
Taxonomy
OccupancyMapper
.
from_csv
(
demo_file
)
mapper
=
OccupancyMapper
.
from_csv
(
demo_file
)
sample_tags
=
[{
"amenity"
:
"university"
,
"a"
:
"b"
},
{
"landuse"
:
"brownfield"
},
{
"x"
:
"y"
}]
assert
mapper
.
tags_to_
taxonom
y
(
sample_tags
)
==
[
"EDU3"
,
"UNDECIDABLE"
]
assert
mapper
.
tags_to_
occupanc
y
(
sample_tags
)
==
[
"EDU3"
,
"UNDECIDABLE"
]
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