Skip to content

Commit

Permalink
code cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
pveigadecamargo committed Dec 16, 2023
1 parent 6f050ec commit 533b992
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 20 deletions.
6 changes: 1 addition & 5 deletions aequilibrae/project/basic_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
Expand All @@ -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))
8 changes: 5 additions & 3 deletions aequilibrae/project/network/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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)}
Expand Down
10 changes: 6 additions & 4 deletions aequilibrae/project/network/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)}
Expand All @@ -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)
Expand All @@ -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")
Expand Down Expand Up @@ -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

Expand Down
10 changes: 6 additions & 4 deletions aequilibrae/project/network/periods.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)}
Expand All @@ -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)
Expand All @@ -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")
Expand All @@ -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()

Expand Down
10 changes: 6 additions & 4 deletions aequilibrae/project/zoning.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 533b992

Please sign in to comment.