Skip to content

Commit

Permalink
add methods to convert results to geodataframe
Browse files Browse the repository at this point in the history
  • Loading branch information
nreinicke committed Nov 8, 2024
1 parent 142d810 commit fbda5d3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 26 deletions.
40 changes: 20 additions & 20 deletions .github/workflows/lint_test.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: Lint & Test
name: Lint & Test

on:
push:
branches: [ main ]
branches: [main]
pull_request:

jobs:
Expand All @@ -11,22 +11,22 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install .[tests]
- name: Run black
run: black . --check
- name: Run ruff
run: ruff check .
- name: Run mypy
run: mypy .
- name: Run Tests
run: pytest tests
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install .[tests]
- name: Run black
run: black . --check
- name: Run ruff
run: ruff check .
- name: Run mypy
run: mypy .
- name: Run Tests
run: pytest tests
6 changes: 0 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ repos:
hooks:
- id: black
args: [--config, pyproject.toml]
- repo: https://github.com/econchick/interrogate
rev: 1.5.0
hooks:
- id: interrogate
args: [-c, pyproject.toml]
pass_filenames: false
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: v0.0.188
hooks:
Expand Down
43 changes: 43 additions & 0 deletions mappymatch/matchers/match_result.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from dataclasses import dataclass
from typing import List, Optional

import geopandas as gpd
import numpy as np
import pandas as pd

Expand All @@ -13,6 +14,30 @@ class MatchResult:
matches: List[Match]
path: Optional[List[Road]] = None

@property
def crs(self):
first_crs = self.matches[0].coordinate.crs
if not all([first_crs.equals(m.coordinate.crs) for m in self.matches]):
raise ValueError(
"Found that there were different CRS within the matches. "
"These must all be equal to use this function"
)
return first_crs

def matches_to_geodataframe(self) -> gpd.GeoDataFrame:
"""
Returns a geodataframe with all the coordinates and their resulting match (or NA if no match) in each row
"""
df = self.matches_to_dataframe()
gdf = gpd.GeoDataFrame(df, geometry="geom")

if len(self.matches) == 0:
return gdf

gdf = gdf.set_crs(self.crs)

return gdf

def matches_to_dataframe(self) -> pd.DataFrame:
"""
Returns a dataframe with all the coordinates and their resulting match (or NA if no match) in each row.
Expand Down Expand Up @@ -40,3 +65,21 @@ def path_to_dataframe(self) -> pd.DataFrame:
df = df.fillna(np.nan)

return df

def path_to_geodataframe(self) -> gpd.GeoDataFrame:
"""
Returns a geodataframe with the resulting estimated trace path through the road network.
The geodataframe is empty if there was no path.
Returns:
A geopandas geodataframe
"""
if self.path is None:
return gpd.GeoDataFrame()

df = self.path_to_dataframe()
gdf = gpd.GeoDataFrame(df, geometry="geom")

gdf = gdf.set_crs(self.crs)

return gdf

0 comments on commit fbda5d3

Please sign in to comment.