Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion apperception/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,12 @@ def _commit(self, commit=True):
def _execute_query(self, query: str) -> List[tuple]:
try:
self.cursor.execute(query)
return self.cursor.fetchall()
for notice in self.cursor.connection.notices:
print(notice)
if self.cursor.pgresult_ptr is not None:
return self.cursor.fetchall()
else:
return []
except psycopg2.errors.DatabaseError as error:
self.connection.rollback()
raise error
Expand Down
157 changes: 157 additions & 0 deletions nb-scripts/add_attributes.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%cd .."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from apperception.database import database"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"############ ROAD DIRECTION ############"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"### Add Road Direction Attributes to Tables ###\n",
"query = \"\"\"ALTER TABLE Item_General_Trajectory\n",
" ADD roadDirections tfloat;\n",
"\n",
" ALTER TABLE Cameras\n",
" ADD roadDirection numeric;\n",
" \"\"\"\n",
"database._execute_query(query=query)\n",
"database._commit(True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"### Update Road Direction For Camera ###\n",
"query = \"\"\"UPDATE Cameras\n",
" SET roadDirection = roadDirection(egoTranslation, egoHeading)\n",
" WHERE roadDirection IS NULL;\n",
" \"\"\"\n",
"database._execute_query(query=query)\n",
"database._commit(True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"### Create Function For roadDirection With Traj ###\n",
"query = \"\"\" \n",
" DROP FUNCTION IF EXISTS _roadDirectionAttr(trajectory tgeompoint, headings tfloat);\n",
" CREATE OR REPLACE FUNCTION _roadDirectionAttr(trajectory tgeompoint, headings tfloat) RETURNS tfloat AS\n",
" $BODY$\n",
" declare trajString text;\n",
" declare trajPointString text;\n",
" declare trajPoint geometry;\n",
" declare trajTime timestamptz;\n",
" declare result tfloat;\n",
" declare currentRoadDir float;\n",
" declare currentRoadDirTime tfloat;\n",
" BEGIN\n",
" trajString := asText(trajectory);\n",
" trajString := substr(trajString, 3, length(trajString) - 3);\n",
" FOREACH trajPointString IN ARRAY STRING_TO_ARRAY(trajString, ',')\n",
" LOOP\n",
" trajPoint := ST_GeomFromText((STRING_TO_ARRAY(trajPointString, '@'))[1]);\n",
" trajTime := (STRING_TO_ARRAY(trajPointString, '@'))[2];\n",
"\n",
" currentRoadDir := roadDirection(trajPoint, CAST(valueAtTimestamp(headings, trajTime) AS numeric));\n",
" currentRoadDirTime := CAST(currentRoadDir AS text) || CAST('@' AS text) || trajTime;\n",
"\n",
" IF result IS NULL THEN\n",
" result = currentRoadDirTime;\n",
" ELSE\n",
" result := appendInstant(result, currentRoadDirTime);\n",
" END IF;\n",
" END LOOP;\n",
" RETURN result;\n",
" END\n",
" $BODY$\n",
" LANGUAGE 'plpgsql' ;\n",
" \"\"\"\n",
"database._execute_query(query=query)\n",
"database._commit(True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"### Update Road Direction For General Trajectory Table ###\n",
"query = \"\"\"UPDATE Item_General_Trajectory\n",
" SET roadDirections = _roadDirectionAttr(trajCentroids, itemHeadings)\n",
" WHERE roadDirections IS NULL AND objectType LIKE 'vehicle%';\n",
" \"\"\"\n",
"database._execute_query(query=query)\n",
"database._commit(True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"############ ROAD SEGMENT TYPES ############"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.8.8 ('base')",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
},
"vscode": {
"interpreter": {
"hash": "5c9f2372a2bfaf539cf701a38e7f23ab828911ee177c2e7bc9c32aa1f4b546df"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}