Skip to content

Commit

Permalink
TIL: Add instruction for reading the generated geoparquet file.
Browse files Browse the repository at this point in the history
  • Loading branch information
Taher Chegini committed Apr 23, 2024
1 parent 57d8d31 commit 0a33fec
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
My Today I Learned snippets. Inspired by [simonw/til](https://github.com/simonw/til).

<!-- index starts -->

## Python

- [Install and setup `miniforge`](https://cheginit.github.io/til/python/miniforge.html) - 2021-02-20
Expand Down Expand Up @@ -45,4 +46,5 @@ My Today I Learned snippets. Inspired by [simonw/til](https://github.com/simonw/
## Github Actions

- [Speed up Github Actions with `mamba`](https://cheginit.github.io/til/github_actions/mamba.html) - 2021-02-26

<!-- index ends -->
16 changes: 16 additions & 0 deletions python/buildings.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,19 @@ def overture_buildings(
bbox_example = (-74.02169, 40.696423, -73.891338, 40.831263)
overture_buildings(bbox_example, "building", "nyc_buildings_subset.parquet")
```

You can read the GeoParquet file with GeoPandas using the following code snippet:

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


manhatten = pd.read_parquet("nyc_buildings_subset.parquet")
manhatten = gpd.GeoDataFrame(
manhatten.drop(columns="geometry"),
geometry=shapely.wkb.loads(manhatten["geometry"]),
crs=4326,
)
```
10 changes: 7 additions & 3 deletions python/frechet.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ FloatArray = npt.NDArray[np.float64]


@njit("f8(f8[:, ::1], f8[:, ::1])", fastmath=True, parallel=True)
def frechet_distance(true_coords: FloatArray, pred_coords: FloatArray)-> float:
def frechet_distance(true_coords: FloatArray, pred_coords: FloatArray) -> float:
"""Compute the discrete Fréchet distance between two lines"""
n, m = len(true_coords), len(pred_coords)
# Compute pairwise euclidean distance
p = 2
dist = np.zeros((n, m))
for i in prange(n):
for j in prange(m):
dist[i, j] = np.sum(np.abs(true_coords[i] - pred_coords[j]) ** p) ** (1.0 / p)
dist[i, j] = np.sum(np.abs(true_coords[i] - pred_coords[j]) ** p) ** (
1.0 / p
)

cost = np.full((n, m), np.inf)
cost[0, 0] = dist[0, 0]
Expand All @@ -34,6 +36,8 @@ def frechet_distance(true_coords: FloatArray, pred_coords: FloatArray)-> float:
cost[0, j] = max(cost[0, j - 1], dist[0, j])
for i in range(1, n):
for j in range(1, m):
cost[i, j] = max(min(cost[i - 1, j], cost[i, j - 1], cost[i - 1, j - 1]), dist[i, j])
cost[i, j] = max(
min(cost[i - 1, j], cost[i, j - 1], cost[i - 1, j - 1]), dist[i, j]
)
return cost[-1, -1]
```

0 comments on commit 0a33fec

Please sign in to comment.