Skip to content
Snippets Groups Projects
Commit 05912aa5 authored by Laurens Oostwegel's avatar Laurens Oostwegel Committed by Laurens Oostwegel
Browse files

Add the obm_buildings rule

parent 8cbbc9c9
No related branches found
No related tags found
No related merge requests found
Pipeline #65055 passed
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
# along with this program. If not, see http://www.gnu.org/licenses/. # along with this program. If not, see http://www.gnu.org/licenses/.
class ObmBuildingInformation: class ObmBuildingsInformation:
def __call__(self, database, key, *args, **kwargs): def __call__(self, database, key, *args, **kwargs):
""" """
Get the information of one building, including the attributes of the building, the Get the information of one building, including the attributes of the building, the
......
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<rule name="ObmBuildingInformation" category="building"> <rule name="ObmBuildingsInformation" category="building">
<input> <input>
<param type="PostGISDatabase">database</param> <param type="PostGISDatabase">database</param>
<param type="int">key</param> <param type="int">key</param>
</input> </input>
<function filepath="osm_replication.py"/> <function filepath="obm_buildings_information.py"/>
<output> <output>
<param type="dict">building_information</param> <param type="dict">building_information</param>
</output> </output>
......
class ObmBuildingsUpsert:
def __call__(
self,
database,
geometry,
osm_id,
floorspace=None,
occupancy=None,
storeys=None,
relation_id=None,
quadkey=None,
*args,
**kwargs,
):
"""
Insert or update a building in the `obm_buildings` table.
Args:
database (PostGISDatabase):
Exposure database.
geometry (str):
WKT formatted geometry of the building, wrapped in the `ST_GeomFromText()`
function.
osm_id (int):
OSM ID of the building.
floorspace (float):
The calculated floorspace of the building.
occupancy (str):
The occupancy type formatted to the GEM occupancy types.
storeys (int):
Number of stories in the building.
relation_id (int):
ID of the OSM relation feature that this building is part of.
quadkey (str):
Quadkey of the tile that the building is located in.
"""
sql_statement = f"""
INSERT INTO obm_buildings
(osm_id, geometry, floorspace, occupancy, storeys, relation_id, quadkey,
last_update)
VALUES (
{osm_id},
{geometry},
{self.format_value(floorspace)},
{self.format_value(occupancy)},
{self.format_value(storeys)},
{self.format_value(relation_id)},
{self.format_value(quadkey)},
NOW()
)
ON CONFLICT (osm_id) DO UPDATE
SET geometry = excluded.geometry,
floorspace = excluded.floorspace,
occupancy = excluded.occupancy,
storeys = excluded.storeys,
relation_id = excluded.relation_id,
quadkey = excluded.quadkey,
last_update = excluded.last_update
"""
database.cursor.execute(sql_statement)
database.connection.commit()
@staticmethod
def format_value(value):
"""
Format values correctly to be used in an SQL statement.
Args:
value (object):
The value that needs to be formatted.
Returns:
Correctly formatted value.
"""
# None values should be `NULL` in PostgreSQL
if value is None:
return "NULL"
# If a value is a string, it should be in quotation marks
elif isinstance(value, str):
return f"'{value}'"
# Else return a string representation of the value
else:
return str(value)
<?xml version="1.0" encoding="UTF-8" ?>
<rule name="ObmBuildingsUpsert" category="building">
<input>
<param type="PostGISDatabase">database</param>
<param type="str">geometry</param>
<param type="int">osm_id</param>
<param type="float">floorspace</param>
<param type="str">occupancy</param>
<param type="int">storeys</param>
<param type="int">relation_id</param>
<param type="str">quadkey</param>
</input>
<function filepath="obm_buildings_upsert.py"/>
<output />
</rule>
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
### Select rules ### Select rules
* **osm_replication** * **ObmBuildingsInformation**
Get the information of one building, including the attributes of the building, the attributes of Get the information of one building, including the attributes of the building, the attributes of
all relations that the building is member of, the attributes of all POIs inside the building and all relations that the building is member of, the attributes of all POIs inside the building and
...@@ -14,4 +14,6 @@ all land-use objects that intersect the building. ...@@ -14,4 +14,6 @@ all land-use objects that intersect the building.
### Upsert rules ### Upsert rules
*Not implemented* * **ObmBuildingsUpsert**
Insert or update a building in the `obm_buildings` table.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment