Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
csaybar committed Jun 12, 2024
1 parent d4d2f04 commit de0e688
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

Unreleased changes here.

## [0.4.0] - 2024-06-12

- Bug in a regex expression fixed. `grid2lonlat` method now works with regions outside South America.
- New method added to the Equi7Grid class. The method is called `lonlat2grid_ids`. This method is used to convert geographic coordinates to Equi7Grid tile ids given a specific level.


## [0.3.0] - 2024-06-11

- A new attribute added self.levels to the Equi7Grid constructor. This attribute is a List of the available levels in the QuadTree structure.
Expand Down
55 changes: 48 additions & 7 deletions equi7grid_lite/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,14 +343,12 @@ def grid2lonlat(
if isinstance(grid_id, gpd.GeoDataFrame):
grid_id = grid_id.id.values[0]

## Extract the zone
zone = grid_id[:2]

## Extract the metadata from the grid_id
re_expr = re.compile(r"SA(\d+)_E(\d+)N(\d+)")
distance = int(re_expr.search(grid_id).group(1))
nxtile = int(re_expr.search(grid_id).group(2))
nytile = int(re_expr.search(grid_id).group(3))
re_expr = re.compile(r"\b([A-Z]+)(\d+)_E(\d+)N(\d+)")
zone = re_expr.search(grid_id).group(1)
distance = int(re_expr.search(grid_id).group(2))
nxtile = int(re_expr.search(grid_id).group(3))
nytile = int(re_expr.search(grid_id).group(4))

# From Grid to Equi7Grid coordinates
if centroid:
Expand Down Expand Up @@ -402,7 +400,50 @@ def align2grid(
centroid=centroid,
xy_coords=False
)

def lonlat2grid_ids(
self,
lon: float,
lat: float,
level: int
) -> list:
"""Obtain the grid ids from a given point.
Args:
lon (float): Longitude of the point.
lat (float): Latitude of the point.
level (int): Level of the grid.
Returns:
list: A list with the grid ids.
"""
# Obtain the boundaries of the grid
grid_information = self.lonlat2grid(lon=lon, lat=lat, level=level)

# Obtain the boundaries of the grid
minx, miny, maxx, maxy = grid_information.geometry.bounds.values[0]

# Obtain the region code and the distance
re_expr = re.compile(r"\b([A-Z]+)(\d+)_E(\d+)N(\d+)")
region_code = re_expr.match(grid_information.id.values[0]).group(1)
distance = int(re_expr.match(grid_information.id.values[0]).group(2))

# Calculate the number of steps in the x and y directions
x_steps = math.ceil((maxx - minx) / distance)
y_steps = math.ceil((maxy - miny) / distance)

grids = []
for i in range(x_steps):
for j in range(y_steps):

# Calculate the boundaries of each grid cell
cell_minx = str(int(minx / distance + i * distance))
cell_miny = str(int(miny / distance + j * distance))

# Cook the grid name
name = f"{region_code}{distance}_E{cell_minx}N{cell_miny}"
grids.append(name)
return grids

def __str__(self) -> str:
"""Display the Equi7Grid information.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "equi7grid_lite"
version = "0.3.0"
version = "0.4.0"
description = "A user-friendly Python interface to interact with the Equi7Grid grid system"
authors = ["Cesar Aybar <[email protected]>", "Jair Flores <[email protected]>"]
repository = "https://github.com/csaybar/equi7grid-lite"
Expand Down

0 comments on commit de0e688

Please sign in to comment.