Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geopandas #570

Merged
merged 5 commits into from
Sep 9, 2024
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
19 changes: 11 additions & 8 deletions aequilibrae/project/data_loader.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from os import PathLike
from typing import Union

import geopandas as gpd
import pandas as pd
import shapely.wkb

from aequilibrae.utils.db_utils import commit_and_close
from aequilibrae.utils.spatialite_utils import connect_spatialite
Expand All @@ -12,17 +13,19 @@ def __init__(self, path_to_file: PathLike, table_name: str):
self.__pth_file = path_to_file
self.table_name = table_name

def load_table(self) -> pd.DataFrame:
def load_table(self) -> Union[gpd.GeoDataFrame, pd.DataFrame]:
with commit_and_close(connect_spatialite(self.__pth_file)) as conn:
fields, _, geo_field = self.__find_table_fields()
fields = [f'"{x}"' for x in fields]
if geo_field is not None:
fields.append('ST_AsBinary("geometry") geometry')
keys = ",".join(fields)
df = pd.read_sql_query(f"select {keys} from '{self.table_name}'", conn)
if geo_field is not None:
df.geometry = df.geometry.apply(shapely.wkb.loads)
return df
if geo_field is not None:
keys += ', Hex(ST_AsBinary("geometry")) as geometry'

sql = f"select {keys} from '{self.table_name}'"
if geo_field is None:
return pd.read_sql_query(sql, conn)
else:
return gpd.GeoDataFrame.from_postgis(sql, conn, geom_col="geometry", crs="EPSG:4326")

def __find_table_fields(self):
with commit_and_close(connect_spatialite(self.__pth_file)) as conn:
Expand Down
6 changes: 3 additions & 3 deletions aequilibrae/project/network/links.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from copy import deepcopy

import pandas as pd
import geopandas as gpd
import shapely.wkb

from aequilibrae.project.basic_table import BasicTable
Expand Down Expand Up @@ -136,11 +136,11 @@ def refresh_fields(self) -> None:
self.__fields = deepcopy(tl.fields)

@property
def data(self) -> pd.DataFrame:
def data(self) -> gpd.GeoDataFrame:
"""Returns all links data as a Pandas DataFrame

:Returns:
**table** (:obj:`DataFrame`): Pandas dataframe with all the links, complete with Geometry
**table** (:obj:`GeoDataFrame`): GeoPandas GeoDataFrame with all the nodes
"""
dl = DataLoader(self.project.path_to_file, "links")
return dl.load_table()
Expand Down
5 changes: 3 additions & 2 deletions aequilibrae/project/network/nodes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from copy import deepcopy

import geopandas as gpd
import pandas as pd

from aequilibrae.project.basic_table import BasicTable
Expand Down Expand Up @@ -111,11 +112,11 @@ def save(self):
item.save()

@property
def data(self) -> pd.DataFrame:
def data(self) -> gpd.GeoDataFrame:
"""Returns all nodes data as a Pandas DataFrame

:Returns:
**table** (:obj:`DataFrame`): Pandas DataFrame with all the nodes, complete with Geometry
**table** (:obj:`GeoDataFrame`): GeoPandas GeoDataFrame with all the nodes
"""
dl = DataLoader(self.project.path_to_file, "nodes")
return dl.load_table()
Expand Down
14 changes: 0 additions & 14 deletions aequilibrae/project/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,6 @@ def close(self) -> None:
finally:
self.deactivate()

def load(self, project_path: str) -> None:
"""
Loads project from disk

.. deprecated:: 0.7.0
Use :func:`open` instead.

:Arguments:
**project_path** (:obj:`str`): Full path to the project data folder. If the project inside does
not exist, it will fail.
"""
warnings.warn(f"Function has been deprecated. Use my_project.open({project_path}) instead", DeprecationWarning)
self.open(project_path)

def connect(self):
return database_connection("network", self.project_base_path)

Expand Down
9 changes: 4 additions & 5 deletions aequilibrae/project/zoning.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
from os.path import join, realpath
from typing import Union, Dict

import pandas as pd

import geopandas as gpd
import shapely.wkb
from shapely.geometry import Point, Polygon, LineString, MultiLineString
from shapely.ops import unary_union

from aequilibrae.project.basic_table import BasicTable
from aequilibrae.project.project_creation import run_queries_from_sql_file
from aequilibrae.project.data_loader import DataLoader
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
Expand Down Expand Up @@ -166,11 +165,11 @@ def __create_return_zone(self, data):
return zone

@property
def data(self) -> pd.DataFrame:
def data(self) -> gpd.GeoDataFrame:
"""Returns all zones data as a Pandas DataFrame

:Returns:
**table** (:obj:`DataFrame`): Pandas DataFrame with all the zones, complete with Geometry
**table** (:obj:`GeoDataFrame`): GeoPandas GeoDataFrame with all the nodes
"""
dl = DataLoader(self.project.path_to_file, "zones")
return dl.load_table()
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
# 5: '',
# 6: '',
# 7: '',
11: 't',
11: "t",
# 12: "R",
}
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ extend-exclude = '''docs/*'''


[build-system]
requires = ["setuptools", "numpy<1.99", "cython", "pyarrow==16.1.0", "wheel"]
requires = ["setuptools", "numpy<1.99", "cython", "pyarrow==17.0.0", "wheel"]


[tool.ruff]
Expand Down Expand Up @@ -93,7 +93,7 @@ manylinux-pypy_aarch64-image = "manylinux_2_28"
archs = ["auto", "aarch64"]
build = ["cp310-*", "cp311-*", "cp312-*"]
repair-wheel-command = [
"auditwheel repair -w {dest_dir} {wheel} --exclude libarrow.so.1601 --exclude libarrow_python.so"
"auditwheel repair -w {dest_dir} {wheel} --exclude libarrow.so.1700 --exclude libarrow_python.so"
]

[tool.cibuildwheel.macos]
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ pandas
pyproj
rtree
openmatrix
pyarrow==16.1.0
pyarrow==17.0.0
geopandas
tqdm