From 533b9927ed0e16ef190479d78b710761620f0628 Mon Sep 17 00:00:00 2001 From: pveigadecamargo Date: Sat, 16 Dec 2023 10:44:31 +1000 Subject: [PATCH] code cleaning --- aequilibrae/project/basic_table.py | 6 +----- aequilibrae/project/network/links.py | 8 +++++--- aequilibrae/project/network/nodes.py | 10 ++++++---- aequilibrae/project/network/periods.py | 10 ++++++---- aequilibrae/project/zoning.py | 10 ++++++---- 5 files changed, 24 insertions(+), 20 deletions(-) diff --git a/aequilibrae/project/basic_table.py b/aequilibrae/project/basic_table.py index 5a208a23e..3d2c4e83c 100644 --- a/aequilibrae/project/basic_table.py +++ b/aequilibrae/project/basic_table.py @@ -3,7 +3,6 @@ from aequilibrae.project.field_editor import FieldEditor from aequilibrae.utils.db_utils import commit_and_close -from aequilibrae.utils.spatialite_utils import connect_spatialite class BasicTable: @@ -16,7 +15,7 @@ def __init__(self, project): self.__table_type__ = "" def extent(self) -> Polygon: - """Queries the extent of thelayer included in the model + """Queries the extent of the layer included in the model Returns: *model extent* (:obj:`Polygon`): Shapely polygon with the bounding box of the layer. @@ -35,6 +34,3 @@ def __copy__(self): def __deepcopy__(self, memodict=None): raise Exception(f"{self.__table_type__} object cannot be copied") - - def conn(self): - return commit_and_close(connect_spatialite(self.project.path_to_file)) \ No newline at end of file diff --git a/aequilibrae/project/network/links.py b/aequilibrae/project/network/links.py index f64421d84..3cd532367 100644 --- a/aequilibrae/project/network/links.py +++ b/aequilibrae/project/network/links.py @@ -7,6 +7,8 @@ from aequilibrae.project.data_loader import DataLoader from aequilibrae.project.network.link import Link from aequilibrae.project.table_loader import TableLoader +from aequilibrae.utils.db_utils import commit_and_close +from aequilibrae.utils.spatialite_utils import connect_spatialite class Links(BasicTable): @@ -115,7 +117,7 @@ def delete(self, link_id: int) -> None: link = self.__items.pop(link_id) # type: Link link.delete() else: - with self.conn as conn: + with commit_and_close(connect_spatialite(self.project.path_to_file)) as conn: d = conn.execute("Delete from Links where link_id=?", [link_id]).rowcount if d: self.project.logger.warning(f"Link {link_id} was successfully removed from the project database") @@ -125,7 +127,7 @@ def delete(self, link_id: int) -> None: def refresh_fields(self) -> None: """After adding a field one needs to refresh all the fields recognized by the software""" tl = TableLoader() - with self.conn as conn: + with commit_and_close(connect_spatialite(self.project.path_to_file)) as conn: self.__max_id = conn.execute("select coalesce(max(link_id),0) from Links").fetchone()[0] tl.load_structure(conn, "links") self.sql = tl.sql @@ -158,7 +160,7 @@ def __existence_error(self, link_id): raise ValueError(f"Link {link_id} does not exist in the model") def __link_data(self, link_id: int) -> dict: - with self.conn as conn: + with commit_and_close(connect_spatialite(self.project.path_to_file)) as conn: data = conn.execute(f"{self.sql} where link_id=?", [link_id]).fetchone() if data: return {key: val for key, val in zip(self.__fields, data)} diff --git a/aequilibrae/project/network/nodes.py b/aequilibrae/project/network/nodes.py index 5e293303a..d09d515ec 100644 --- a/aequilibrae/project/network/nodes.py +++ b/aequilibrae/project/network/nodes.py @@ -6,6 +6,8 @@ from aequilibrae.project.data_loader import DataLoader from aequilibrae.project.network.node import Node from aequilibrae.project.table_loader import TableLoader +from aequilibrae.utils.db_utils import commit_and_close +from aequilibrae.utils.spatialite_utils import connect_spatialite class Nodes(BasicTable): @@ -61,7 +63,7 @@ def get(self, node_id: int) -> Node: else: self.__items[node.node_id] = self.__items.pop(node_id) - with self.conn as conn: + with commit_and_close(connect_spatialite(self.project.path_to_file)) as conn: data = conn.execute(f"{self.sql} where node_id=?", [node_id]).fetchone() if data: data = {key: val for key, val in zip(self.__fields, data)} @@ -74,7 +76,7 @@ def get(self, node_id: int) -> Node: def refresh_fields(self) -> None: """After adding a field one needs to refresh all the fields recognized by the software""" tl = TableLoader() - with self.conn as conn: + with commit_and_close(connect_spatialite(self.project.path_to_file)) as conn: tl.load_structure(conn, "nodes") self.sql = tl.sql self.__fields = deepcopy(tl.fields) @@ -92,7 +94,7 @@ def new_centroid(self, node_id: int) -> Node: **node_id** (:obj:`int`): Id of the centroid to be created """ - with self.conn as conn: + with commit_and_close(connect_spatialite(self.project.path_to_file)) as conn: ct = conn.execute("select count(*) from nodes where node_id=?", [node_id]).fetchone()[0] if ct > 0: raise Exception("Node_id already exists. Failed to create it") @@ -125,7 +127,7 @@ def lonlat(self) -> pd.DataFrame: :Returns: **table** (:obj:`DataFrame`): Pandas DataFrame with all the nodes, with geometry as lon/lat """ - with self.conn as conn: + with commit_and_close(connect_spatialite(self.project.path_to_file)) as conn: df = pd.read_sql("SELECT node_id, ST_X(geometry) AS lon, ST_Y(geometry) AS lat FROM nodes", conn) return df diff --git a/aequilibrae/project/network/periods.py b/aequilibrae/project/network/periods.py index f55af0ccc..5776d6a0f 100644 --- a/aequilibrae/project/network/periods.py +++ b/aequilibrae/project/network/periods.py @@ -6,6 +6,8 @@ from aequilibrae.project.data_loader import DataLoader from aequilibrae.project.network.period import Period from aequilibrae.project.table_loader import TableLoader +from aequilibrae.utils.db_utils import commit_and_close +from aequilibrae.utils.spatialite_utils import connect_spatialite class Periods(BasicTable): @@ -66,7 +68,7 @@ def get(self, period_id: int) -> Period: else: self.__items[period.period_id] = self.__items.pop(period_id) - with self.conn as conn: + with commit_and_close(connect_spatialite(self.project.path_to_file)) as conn: data = conn.execute(f"{self.sql} where period_id=?", [period_id]).fetchone() if data: data = {key: val for key, val in zip(self.__fields, data)} @@ -79,7 +81,7 @@ def get(self, period_id: int) -> Period: def refresh_fields(self) -> None: """After adding a field one needs to refresh all the fields recognized by the software""" tl = TableLoader() - with self.conn as conn: + with commit_and_close(connect_spatialite(self.project.path_to_file)) as conn: tl.load_structure(conn, "periods") self.sql = tl.sql self.__fields = deepcopy(tl.fields) @@ -100,7 +102,7 @@ def new_period(self, period_id: int, start: int, end: int, description: str = No **description** (:obj:`str`): Optional human readable description of the time period e.g. '1pm - 5pm' """ - with self.conn as conn: + with commit_and_close(connect_spatialite(self.project.path_to_file)) as conn: dt = conn.execute("SELECT COUNT(*) FROM periods WHERE period_id=?", [period_id]).fetchone()[0] if dt[0] > 0: raise Exception("period_id already exists. Failed to create it") @@ -125,7 +127,7 @@ def data(self) -> pd.DataFrame: :Returns: **table** (:obj:`DataFrame`): Pandas DataFrame with all the periods """ - with self.conn as conn: + with commit_and_close(connect_spatialite(self.project.path_to_file)) as conn: dl = DataLoader(conn, "periods") return dl.load_table() diff --git a/aequilibrae/project/zoning.py b/aequilibrae/project/zoning.py index 534983056..630cb2077 100644 --- a/aequilibrae/project/zoning.py +++ b/aequilibrae/project/zoning.py @@ -10,7 +10,9 @@ from aequilibrae.project.project_creation import run_queries_from_sql_file from aequilibrae.project.table_loader import TableLoader from aequilibrae.project.zone import Zone +from aequilibrae.utils.db_utils import commit_and_close from aequilibrae.utils.geo_index import GeoIndex +from aequilibrae.utils.spatialite_utils import connect_spatialite class Zoning(BasicTable): @@ -71,7 +73,7 @@ def create_zoning_layer(self): if not self.__has_zoning(): qry_file = join(realpath(__file__), "database_specification", "tables", "zones.sql") - with self.conn as conn: + with commit_and_close(connect_spatialite(self.project.path_to_file)) as conn: run_queries_from_sql_file(conn, self.project.logger, qry_file) self.__load() else: @@ -83,7 +85,7 @@ def coverage(self) -> Polygon: :Returns: **model coverage** (:obj:`Polygon`): Shapely (Multi)polygon of the zoning system. """ - with self.conn as conn: + with commit_and_close(connect_spatialite(self.project.path_to_file)) as conn: dt = conn.execute('Select ST_asBinary("geometry") from zones;').fetchall() polygons = [shapely.wkb.loads(x[0]) for x in dt] return unary_union(polygons) @@ -130,13 +132,13 @@ def refresh_geo_index(self): self.__geo_index.insert(feature_id=zone_id, geometry=zone.geometry) def __has_zoning(self): - with self.conn as conn: + with commit_and_close(connect_spatialite(self.project.path_to_file)) as conn: dt = conn.execute("SELECT name FROM sqlite_master WHERE type='table';").fetchall() return any(["zone" in x[0].lower() for x in dt]) def __load(self): tl = TableLoader() - with self.conn as conn: + with commit_and_close(connect_spatialite(self.project.path_to_file)) as conn: zones_list = tl.load_table(conn, "zones") self.__fields = deepcopy(tl.fields)